也就是说如果定义了HAVE_PTHREADS宏,就有Mutex的相关方法实现,如果没有定义这个宏,Mutex的方法又是如何实现的,下面看看frameworks/base/libs/utils/Threads.cpp中的代码:
- #if defined(HAVE_PTHREADS)
- // implemented as inlines in threads.h
- #elif defined(HAVE_WIN32_THREADS)
- Mutex::Mutex()
- {
- HANDLE hMutex;
- assert(sizeof(hMutex) == sizeof(mState));
- hMutex = CreateMutex(NULL, FALSE, NULL);
- mState = (void*) hMutex;
- }
- Mutex::Mutex(const char* name)
- {
- // XXX: name not used for now
- HANDLE hMutex;
- assert(sizeof(hMutex) == sizeof(mState));
- hMutex = CreateMutex(NULL, FALSE, NULL);
- mState = (void*) hMutex;
- }
- Mutex::Mutex(int type, const char* name)
- {
- // XXX: type and name not used for now
- HANDLE hMutex;
- assert(sizeof(hMutex) == sizeof(mState));
- hMutex = CreateMutex(NULL, FALSE, NULL);
- mState = (void*) hMutex;
- }
- Mutex::~Mutex()
- {
- CloseHandle((HANDLE) mState);
- }
- status_t Mutex::lock()
- {
- DWORD dwWaitResult;
- dwWaitResult = WaitForSingleObject((HANDLE) mState, INFINITE);
- return dwWaitResult != WAIT_OBJECT_0 ? -1 : NO_ERROR;
- }
- void Mutex::unlock()
- {
- if (!ReleaseMutex((HANDLE) mState))
- LOG(LOG_WARN, "thread", "WARNING: bad result from unlocking mutex\n");
- }
- status_t Mutex::tryLock()
- {
- DWORD dwWaitResult;
- dwWaitResult = WaitForSingleObject((HANDLE) mState, 0);
- if (dwWaitResult != WAIT_OBJECT_0 && dwWaitResult != WAIT_TIMEOUT)
- LOG(LOG_WARN, "thread", "WARNING: bad result from try-locking mutex\n");
- return (dwWaitResult == WAIT_OBJECT_0) ? 0 : -1;
- }
- #else
- #error "Somebody forgot to implement threads for this platform."
- #endif
LOCAL_CXXFLAGS := -DHAVE_PTHREADS
4. jni函数名带下划线,结果出错,将函数名的下划线去掉,解决问题。
