2009-03-04 11:31:44 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2009-03-06 06:34:35 +08:00
|
|
|
// #define LOG_NDEBUG 0
|
2009-03-04 11:31:44 +08:00
|
|
|
#define LOG_TAG "libutils.threads"
|
|
|
|
|
2014-05-01 02:10:46 +08:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <memory.h>
|
2009-03-04 11:31:44 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-01-27 11:48:54 +08:00
|
|
|
#if !defined(_WIN32)
|
2009-03-04 11:31:44 +08:00
|
|
|
# include <pthread.h>
|
|
|
|
# include <sched.h>
|
|
|
|
# include <sys/resource.h>
|
2015-01-27 11:48:54 +08:00
|
|
|
#else
|
2009-03-04 11:31:44 +08:00
|
|
|
# include <windows.h>
|
|
|
|
# include <stdint.h>
|
|
|
|
# include <process.h>
|
|
|
|
# define HAVE_CREATETHREAD // Cygwin, vs. HAVE__BEGINTHREADEX for MinGW
|
|
|
|
#endif
|
|
|
|
|
2014-12-16 04:52:53 +08:00
|
|
|
#if defined(__linux__)
|
2009-03-04 11:31:44 +08:00
|
|
|
#include <sys/prctl.h>
|
|
|
|
#endif
|
|
|
|
|
2014-05-01 02:10:46 +08:00
|
|
|
#include <utils/threads.h>
|
|
|
|
#include <utils/Log.h>
|
|
|
|
|
|
|
|
#include <cutils/sched_policy.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_ANDROID_OS
|
|
|
|
# define __android_unused
|
|
|
|
#else
|
|
|
|
# define __android_unused __attribute__((__unused__))
|
|
|
|
#endif
|
|
|
|
|
2009-03-04 11:31:44 +08:00
|
|
|
/*
|
|
|
|
* ===========================================================================
|
|
|
|
* Thread wrappers
|
|
|
|
* ===========================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
using namespace android;
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2015-01-27 11:48:54 +08:00
|
|
|
#if !defined(_WIN32)
|
2009-03-04 11:31:44 +08:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/*
|
2010-09-04 08:07:07 +08:00
|
|
|
* Create and run a new thread.
|
2009-03-04 11:31:44 +08:00
|
|
|
*
|
|
|
|
* We create it "detached", so it cleans up after itself.
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef void* (*android_pthread_entry)(void*);
|
|
|
|
|
|
|
|
struct thread_data_t {
|
|
|
|
thread_func_t entryFunction;
|
|
|
|
void* userData;
|
|
|
|
int priority;
|
|
|
|
char * threadName;
|
|
|
|
|
|
|
|
// we use this trampoline when we need to set the priority with
|
2011-07-12 06:59:22 +08:00
|
|
|
// nice/setpriority, and name with prctl.
|
2009-03-04 11:31:44 +08:00
|
|
|
static int trampoline(const thread_data_t* t) {
|
|
|
|
thread_func_t f = t->entryFunction;
|
|
|
|
void* u = t->userData;
|
|
|
|
int prio = t->priority;
|
|
|
|
char * name = t->threadName;
|
|
|
|
delete t;
|
|
|
|
setpriority(PRIO_PROCESS, 0, prio);
|
2012-05-01 07:03:30 +08:00
|
|
|
if (prio >= ANDROID_PRIORITY_BACKGROUND) {
|
|
|
|
set_sched_policy(0, SP_BACKGROUND);
|
|
|
|
} else {
|
|
|
|
set_sched_policy(0, SP_FOREGROUND);
|
2010-09-10 06:50:18 +08:00
|
|
|
}
|
2015-01-27 11:48:54 +08:00
|
|
|
|
2009-03-04 11:31:44 +08:00
|
|
|
if (name) {
|
2013-03-08 07:34:28 +08:00
|
|
|
androidSetThreadName(name);
|
2009-03-04 11:31:44 +08:00
|
|
|
free(name);
|
|
|
|
}
|
|
|
|
return f(u);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-03-08 07:34:28 +08:00
|
|
|
void androidSetThreadName(const char* name) {
|
2014-12-16 04:52:53 +08:00
|
|
|
#if defined(__linux__)
|
2013-03-08 07:34:28 +08:00
|
|
|
// Mac OS doesn't have this, and we build libutil for the host too
|
|
|
|
int hasAt = 0;
|
|
|
|
int hasDot = 0;
|
|
|
|
const char *s = name;
|
|
|
|
while (*s) {
|
|
|
|
if (*s == '.') hasDot = 1;
|
|
|
|
else if (*s == '@') hasAt = 1;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
int len = s - name;
|
|
|
|
if (len < 15 || hasAt || !hasDot) {
|
|
|
|
s = name;
|
|
|
|
} else {
|
|
|
|
s = name + len - 15;
|
|
|
|
}
|
|
|
|
prctl(PR_SET_NAME, (unsigned long) s, 0, 0, 0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-03-04 11:31:44 +08:00
|
|
|
int androidCreateRawThreadEtc(android_thread_func_t entryFunction,
|
|
|
|
void *userData,
|
2014-05-01 02:10:46 +08:00
|
|
|
const char* threadName __android_unused,
|
2009-03-04 11:31:44 +08:00
|
|
|
int32_t threadPriority,
|
|
|
|
size_t threadStackSize,
|
|
|
|
android_thread_id_t *threadId)
|
|
|
|
{
|
2015-01-27 11:48:54 +08:00
|
|
|
pthread_attr_t attr;
|
2009-03-04 11:31:44 +08:00
|
|
|
pthread_attr_init(&attr);
|
|
|
|
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
|
|
|
|
|
|
|
#ifdef HAVE_ANDROID_OS /* valgrind is rejecting RT-priority create reqs */
|
|
|
|
if (threadPriority != PRIORITY_DEFAULT || threadName != NULL) {
|
2011-07-12 06:59:22 +08:00
|
|
|
// Now that the pthread_t has a method to find the associated
|
|
|
|
// android_thread_id_t (pid) from pthread_t, it would be possible to avoid
|
|
|
|
// this trampoline in some cases as the parent could set the properties
|
|
|
|
// for the child. However, there would be a race condition because the
|
|
|
|
// child becomes ready immediately, and it doesn't work for the name.
|
|
|
|
// prctl(PR_SET_NAME) only works for self; prctl(PR_SET_THREAD_NAME) was
|
|
|
|
// proposed but not yet accepted.
|
2009-03-04 11:31:44 +08:00
|
|
|
thread_data_t* t = new thread_data_t;
|
|
|
|
t->priority = threadPriority;
|
|
|
|
t->threadName = threadName ? strdup(threadName) : NULL;
|
|
|
|
t->entryFunction = entryFunction;
|
|
|
|
t->userData = userData;
|
|
|
|
entryFunction = (android_thread_func_t)&thread_data_t::trampoline;
|
2015-01-27 11:48:54 +08:00
|
|
|
userData = t;
|
2009-03-04 11:31:44 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (threadStackSize) {
|
|
|
|
pthread_attr_setstacksize(&attr, threadStackSize);
|
|
|
|
}
|
2015-01-27 11:48:54 +08:00
|
|
|
|
2009-03-04 11:31:44 +08:00
|
|
|
errno = 0;
|
|
|
|
pthread_t thread;
|
|
|
|
int result = pthread_create(&thread, &attr,
|
|
|
|
(android_pthread_entry)entryFunction, userData);
|
2011-07-15 05:27:18 +08:00
|
|
|
pthread_attr_destroy(&attr);
|
2009-03-04 11:31:44 +08:00
|
|
|
if (result != 0) {
|
2012-01-07 03:20:56 +08:00
|
|
|
ALOGE("androidCreateRawThreadEtc failed (entry=%p, res=%d, errno=%d)\n"
|
2009-03-04 11:31:44 +08:00
|
|
|
"(android threadPriority=%d)",
|
|
|
|
entryFunction, result, errno, threadPriority);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-06-02 23:59:28 +08:00
|
|
|
// Note that *threadID is directly available to the parent only, as it is
|
|
|
|
// assigned after the child starts. Use memory barrier / lock if the child
|
|
|
|
// or other threads also need access.
|
2009-03-04 11:31:44 +08:00
|
|
|
if (threadId != NULL) {
|
|
|
|
*threadId = (android_thread_id_t)thread; // XXX: this is not portable
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-07-12 06:59:22 +08:00
|
|
|
#ifdef HAVE_ANDROID_OS
|
|
|
|
static pthread_t android_thread_id_t_to_pthread(android_thread_id_t thread)
|
|
|
|
{
|
|
|
|
return (pthread_t) thread;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-03-04 11:31:44 +08:00
|
|
|
android_thread_id_t androidGetThreadId()
|
|
|
|
{
|
|
|
|
return (android_thread_id_t)pthread_self();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2015-01-27 11:48:54 +08:00
|
|
|
#else // !defined(_WIN32)
|
2009-03-04 11:31:44 +08:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Trampoline to make us __stdcall-compliant.
|
|
|
|
*
|
|
|
|
* We're expected to delete "vDetails" when we're done.
|
|
|
|
*/
|
|
|
|
struct threadDetails {
|
|
|
|
int (*func)(void*);
|
|
|
|
void* arg;
|
|
|
|
};
|
|
|
|
static __stdcall unsigned int threadIntermediary(void* vDetails)
|
|
|
|
{
|
|
|
|
struct threadDetails* pDetails = (struct threadDetails*) vDetails;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
result = (*(pDetails->func))(pDetails->arg);
|
|
|
|
|
|
|
|
delete pDetails;
|
|
|
|
|
2011-10-13 00:27:03 +08:00
|
|
|
ALOG(LOG_VERBOSE, "thread", "thread exiting\n");
|
2009-03-04 11:31:44 +08:00
|
|
|
return (unsigned int) result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create and run a new thread.
|
|
|
|
*/
|
|
|
|
static bool doCreateThread(android_thread_func_t fn, void* arg, android_thread_id_t *id)
|
|
|
|
{
|
|
|
|
HANDLE hThread;
|
|
|
|
struct threadDetails* pDetails = new threadDetails; // must be on heap
|
|
|
|
unsigned int thrdaddr;
|
|
|
|
|
|
|
|
pDetails->func = fn;
|
|
|
|
pDetails->arg = arg;
|
|
|
|
|
|
|
|
#if defined(HAVE__BEGINTHREADEX)
|
|
|
|
hThread = (HANDLE) _beginthreadex(NULL, 0, threadIntermediary, pDetails, 0,
|
|
|
|
&thrdaddr);
|
|
|
|
if (hThread == 0)
|
|
|
|
#elif defined(HAVE_CREATETHREAD)
|
|
|
|
hThread = CreateThread(NULL, 0,
|
|
|
|
(LPTHREAD_START_ROUTINE) threadIntermediary,
|
|
|
|
(void*) pDetails, 0, (DWORD*) &thrdaddr);
|
|
|
|
if (hThread == NULL)
|
|
|
|
#endif
|
|
|
|
{
|
2011-10-13 00:27:03 +08:00
|
|
|
ALOG(LOG_WARN, "thread", "WARNING: thread create failed\n");
|
2009-03-04 11:31:44 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(HAVE_CREATETHREAD)
|
|
|
|
/* close the management handle */
|
|
|
|
CloseHandle(hThread);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (id != NULL) {
|
|
|
|
*id = (android_thread_id_t)thrdaddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int androidCreateRawThreadEtc(android_thread_func_t fn,
|
|
|
|
void *userData,
|
2014-05-01 02:10:46 +08:00
|
|
|
const char* /*threadName*/,
|
|
|
|
int32_t /*threadPriority*/,
|
|
|
|
size_t /*threadStackSize*/,
|
2009-03-04 11:31:44 +08:00
|
|
|
android_thread_id_t *threadId)
|
|
|
|
{
|
|
|
|
return doCreateThread( fn, userData, threadId);
|
|
|
|
}
|
|
|
|
|
|
|
|
android_thread_id_t androidGetThreadId()
|
|
|
|
{
|
|
|
|
return (android_thread_id_t)GetCurrentThreadId();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2015-01-27 11:48:54 +08:00
|
|
|
#endif // !defined(_WIN32)
|
2009-03-04 11:31:44 +08:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
int androidCreateThread(android_thread_func_t fn, void* arg)
|
|
|
|
{
|
|
|
|
return createThreadEtc(fn, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
int androidCreateThreadGetID(android_thread_func_t fn, void *arg, android_thread_id_t *id)
|
|
|
|
{
|
|
|
|
return createThreadEtc(fn, arg, "android:unnamed_thread",
|
|
|
|
PRIORITY_DEFAULT, 0, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static android_create_thread_fn gCreateThreadFn = androidCreateRawThreadEtc;
|
|
|
|
|
|
|
|
int androidCreateThreadEtc(android_thread_func_t entryFunction,
|
|
|
|
void *userData,
|
|
|
|
const char* threadName,
|
|
|
|
int32_t threadPriority,
|
|
|
|
size_t threadStackSize,
|
|
|
|
android_thread_id_t *threadId)
|
|
|
|
{
|
|
|
|
return gCreateThreadFn(entryFunction, userData, threadName,
|
|
|
|
threadPriority, threadStackSize, threadId);
|
|
|
|
}
|
|
|
|
|
|
|
|
void androidSetCreateThreadFunc(android_create_thread_fn func)
|
|
|
|
{
|
|
|
|
gCreateThreadFn = func;
|
|
|
|
}
|
|
|
|
|
2012-03-17 13:18:39 +08:00
|
|
|
#ifdef HAVE_ANDROID_OS
|
2009-12-08 09:59:37 +08:00
|
|
|
int androidSetThreadPriority(pid_t tid, int pri)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
2015-01-27 11:48:54 +08:00
|
|
|
|
|
|
|
#if !defined(_WIN32)
|
2009-12-08 09:59:37 +08:00
|
|
|
int lasterr = 0;
|
|
|
|
|
2012-05-01 07:03:30 +08:00
|
|
|
if (pri >= ANDROID_PRIORITY_BACKGROUND) {
|
|
|
|
rc = set_sched_policy(tid, SP_BACKGROUND);
|
|
|
|
} else if (getpriority(PRIO_PROCESS, tid) >= ANDROID_PRIORITY_BACKGROUND) {
|
|
|
|
rc = set_sched_policy(tid, SP_FOREGROUND);
|
2009-12-08 09:59:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rc) {
|
|
|
|
lasterr = errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setpriority(PRIO_PROCESS, tid, pri) < 0) {
|
|
|
|
rc = INVALID_OPERATION;
|
|
|
|
} else {
|
|
|
|
errno = lasterr;
|
|
|
|
}
|
2009-12-09 08:38:01 +08:00
|
|
|
#endif
|
2015-01-27 11:48:54 +08:00
|
|
|
|
2009-12-08 09:59:37 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2011-09-16 03:21:40 +08:00
|
|
|
int androidGetThreadPriority(pid_t tid) {
|
2015-01-27 11:48:54 +08:00
|
|
|
#if !defined(_WIN32)
|
2011-09-16 03:21:40 +08:00
|
|
|
return getpriority(PRIO_PROCESS, tid);
|
2011-09-17 02:47:13 +08:00
|
|
|
#else
|
|
|
|
return ANDROID_PRIORITY_NORMAL;
|
|
|
|
#endif
|
2011-09-16 03:21:40 +08:00
|
|
|
}
|
|
|
|
|
2012-03-17 13:18:39 +08:00
|
|
|
#endif
|
2011-06-23 07:20:37 +08:00
|
|
|
|
2009-03-04 11:31:44 +08:00
|
|
|
namespace android {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ===========================================================================
|
|
|
|
* Mutex class
|
|
|
|
* ===========================================================================
|
|
|
|
*/
|
|
|
|
|
2015-01-27 11:48:54 +08:00
|
|
|
#if !defined(_WIN32)
|
2009-07-13 14:11:20 +08:00
|
|
|
// implemented as inlines in threads.h
|
2015-01-27 11:48:54 +08:00
|
|
|
#else
|
2009-03-04 11:31:44 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2009-08-01 06:20:17 +08:00
|
|
|
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));
|
|
|
|
|
2009-03-04 11:31:44 +08:00
|
|
|
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))
|
2011-10-13 00:27:03 +08:00
|
|
|
ALOG(LOG_WARN, "thread", "WARNING: bad result from unlocking mutex\n");
|
2009-03-04 11:31:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
status_t Mutex::tryLock()
|
|
|
|
{
|
|
|
|
DWORD dwWaitResult;
|
|
|
|
|
|
|
|
dwWaitResult = WaitForSingleObject((HANDLE) mState, 0);
|
|
|
|
if (dwWaitResult != WAIT_OBJECT_0 && dwWaitResult != WAIT_TIMEOUT)
|
2011-10-13 00:27:03 +08:00
|
|
|
ALOG(LOG_WARN, "thread", "WARNING: bad result from try-locking mutex\n");
|
2009-03-04 11:31:44 +08:00
|
|
|
return (dwWaitResult == WAIT_OBJECT_0) ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
2015-01-27 11:48:54 +08:00
|
|
|
#endif // !defined(_WIN32)
|
2009-03-04 11:31:44 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ===========================================================================
|
|
|
|
* Condition class
|
|
|
|
* ===========================================================================
|
|
|
|
*/
|
|
|
|
|
2015-01-27 11:48:54 +08:00
|
|
|
#if !defined(_WIN32)
|
2009-07-13 14:11:20 +08:00
|
|
|
// implemented as inlines in threads.h
|
2015-01-27 11:48:54 +08:00
|
|
|
#else
|
2009-03-04 11:31:44 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Windows doesn't have a condition variable solution. It's possible
|
|
|
|
* to create one, but it's easy to get it wrong. For a discussion, and
|
|
|
|
* the origin of this implementation, see:
|
|
|
|
*
|
|
|
|
* http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
|
|
|
|
*
|
|
|
|
* The implementation shown on the page does NOT follow POSIX semantics.
|
|
|
|
* As an optimization they require acquiring the external mutex before
|
|
|
|
* calling signal() and broadcast(), whereas POSIX only requires grabbing
|
|
|
|
* it before calling wait(). The implementation here has been un-optimized
|
|
|
|
* to have the correct behavior.
|
|
|
|
*/
|
|
|
|
typedef struct WinCondition {
|
|
|
|
// Number of waiting threads.
|
|
|
|
int waitersCount;
|
|
|
|
|
|
|
|
// Serialize access to waitersCount.
|
|
|
|
CRITICAL_SECTION waitersCountLock;
|
|
|
|
|
|
|
|
// Semaphore used to queue up threads waiting for the condition to
|
|
|
|
// become signaled.
|
|
|
|
HANDLE sema;
|
|
|
|
|
|
|
|
// An auto-reset event used by the broadcast/signal thread to wait
|
|
|
|
// for all the waiting thread(s) to wake up and be released from
|
|
|
|
// the semaphore.
|
|
|
|
HANDLE waitersDone;
|
|
|
|
|
|
|
|
// This mutex wouldn't be necessary if we required that the caller
|
|
|
|
// lock the external mutex before calling signal() and broadcast().
|
|
|
|
// I'm trying to mimic pthread semantics though.
|
|
|
|
HANDLE internalMutex;
|
|
|
|
|
|
|
|
// Keeps track of whether we were broadcasting or signaling. This
|
|
|
|
// allows us to optimize the code if we're just signaling.
|
|
|
|
bool wasBroadcast;
|
|
|
|
|
|
|
|
status_t wait(WinCondition* condState, HANDLE hMutex, nsecs_t* abstime)
|
|
|
|
{
|
|
|
|
// Increment the wait count, avoiding race conditions.
|
|
|
|
EnterCriticalSection(&condState->waitersCountLock);
|
|
|
|
condState->waitersCount++;
|
|
|
|
//printf("+++ wait: incr waitersCount to %d (tid=%ld)\n",
|
|
|
|
// condState->waitersCount, getThreadId());
|
|
|
|
LeaveCriticalSection(&condState->waitersCountLock);
|
2015-01-27 11:48:54 +08:00
|
|
|
|
2009-03-04 11:31:44 +08:00
|
|
|
DWORD timeout = INFINITE;
|
|
|
|
if (abstime) {
|
|
|
|
nsecs_t reltime = *abstime - systemTime();
|
|
|
|
if (reltime < 0)
|
|
|
|
reltime = 0;
|
|
|
|
timeout = reltime/1000000;
|
|
|
|
}
|
2015-01-27 11:48:54 +08:00
|
|
|
|
2009-03-04 11:31:44 +08:00
|
|
|
// Atomically release the external mutex and wait on the semaphore.
|
|
|
|
DWORD res =
|
|
|
|
SignalObjectAndWait(hMutex, condState->sema, timeout, FALSE);
|
2015-01-27 11:48:54 +08:00
|
|
|
|
2009-03-04 11:31:44 +08:00
|
|
|
//printf("+++ wait: awake (tid=%ld)\n", getThreadId());
|
2015-01-27 11:48:54 +08:00
|
|
|
|
2009-03-04 11:31:44 +08:00
|
|
|
// Reacquire lock to avoid race conditions.
|
|
|
|
EnterCriticalSection(&condState->waitersCountLock);
|
2015-01-27 11:48:54 +08:00
|
|
|
|
2009-03-04 11:31:44 +08:00
|
|
|
// No longer waiting.
|
|
|
|
condState->waitersCount--;
|
2015-01-27 11:48:54 +08:00
|
|
|
|
2009-03-04 11:31:44 +08:00
|
|
|
// Check to see if we're the last waiter after a broadcast.
|
|
|
|
bool lastWaiter = (condState->wasBroadcast && condState->waitersCount == 0);
|
2015-01-27 11:48:54 +08:00
|
|
|
|
2009-03-04 11:31:44 +08:00
|
|
|
//printf("+++ wait: lastWaiter=%d (wasBc=%d wc=%d)\n",
|
|
|
|
// lastWaiter, condState->wasBroadcast, condState->waitersCount);
|
2015-01-27 11:48:54 +08:00
|
|
|
|
2009-03-04 11:31:44 +08:00
|
|
|
LeaveCriticalSection(&condState->waitersCountLock);
|
2015-01-27 11:48:54 +08:00
|
|
|
|
2009-03-04 11:31:44 +08:00
|
|
|
// If we're the last waiter thread during this particular broadcast
|
|
|
|
// then signal broadcast() that we're all awake. It'll drop the
|
|
|
|
// internal mutex.
|
|
|
|
if (lastWaiter) {
|
|
|
|
// Atomically signal the "waitersDone" event and wait until we
|
|
|
|
// can acquire the internal mutex. We want to do this in one step
|
|
|
|
// because it ensures that everybody is in the mutex FIFO before
|
|
|
|
// any thread has a chance to run. Without it, another thread
|
|
|
|
// could wake up, do work, and hop back in ahead of us.
|
|
|
|
SignalObjectAndWait(condState->waitersDone, condState->internalMutex,
|
|
|
|
INFINITE, FALSE);
|
|
|
|
} else {
|
|
|
|
// Grab the internal mutex.
|
|
|
|
WaitForSingleObject(condState->internalMutex, INFINITE);
|
|
|
|
}
|
2015-01-27 11:48:54 +08:00
|
|
|
|
2009-03-04 11:31:44 +08:00
|
|
|
// Release the internal and grab the external.
|
|
|
|
ReleaseMutex(condState->internalMutex);
|
|
|
|
WaitForSingleObject(hMutex, INFINITE);
|
2015-01-27 11:48:54 +08:00
|
|
|
|
2009-03-04 11:31:44 +08:00
|
|
|
return res == WAIT_OBJECT_0 ? NO_ERROR : -1;
|
|
|
|
}
|
|
|
|
} WinCondition;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Constructor. Set up the WinCondition stuff.
|
|
|
|
*/
|
|
|
|
Condition::Condition()
|
|
|
|
{
|
|
|
|
WinCondition* condState = new WinCondition;
|
|
|
|
|
|
|
|
condState->waitersCount = 0;
|
|
|
|
condState->wasBroadcast = false;
|
|
|
|
// semaphore: no security, initial value of 0
|
|
|
|
condState->sema = CreateSemaphore(NULL, 0, 0x7fffffff, NULL);
|
|
|
|
InitializeCriticalSection(&condState->waitersCountLock);
|
|
|
|
// auto-reset event, not signaled initially
|
|
|
|
condState->waitersDone = CreateEvent(NULL, FALSE, FALSE, NULL);
|
|
|
|
// used so we don't have to lock external mutex on signal/broadcast
|
|
|
|
condState->internalMutex = CreateMutex(NULL, FALSE, NULL);
|
|
|
|
|
|
|
|
mState = condState;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Destructor. Free Windows resources as well as our allocated storage.
|
|
|
|
*/
|
|
|
|
Condition::~Condition()
|
|
|
|
{
|
|
|
|
WinCondition* condState = (WinCondition*) mState;
|
|
|
|
if (condState != NULL) {
|
|
|
|
CloseHandle(condState->sema);
|
|
|
|
CloseHandle(condState->waitersDone);
|
|
|
|
delete condState;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t Condition::wait(Mutex& mutex)
|
|
|
|
{
|
|
|
|
WinCondition* condState = (WinCondition*) mState;
|
|
|
|
HANDLE hMutex = (HANDLE) mutex.mState;
|
2015-01-27 11:48:54 +08:00
|
|
|
|
2009-03-04 11:31:44 +08:00
|
|
|
return ((WinCondition*)mState)->wait(condState, hMutex, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t Condition::waitRelative(Mutex& mutex, nsecs_t reltime)
|
|
|
|
{
|
2009-08-01 06:20:17 +08:00
|
|
|
WinCondition* condState = (WinCondition*) mState;
|
|
|
|
HANDLE hMutex = (HANDLE) mutex.mState;
|
|
|
|
nsecs_t absTime = systemTime()+reltime;
|
|
|
|
|
|
|
|
return ((WinCondition*)mState)->wait(condState, hMutex, &absTime);
|
2009-03-04 11:31:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Signal the condition variable, allowing one thread to continue.
|
|
|
|
*/
|
|
|
|
void Condition::signal()
|
|
|
|
{
|
|
|
|
WinCondition* condState = (WinCondition*) mState;
|
|
|
|
|
|
|
|
// Lock the internal mutex. This ensures that we don't clash with
|
|
|
|
// broadcast().
|
|
|
|
WaitForSingleObject(condState->internalMutex, INFINITE);
|
|
|
|
|
|
|
|
EnterCriticalSection(&condState->waitersCountLock);
|
|
|
|
bool haveWaiters = (condState->waitersCount > 0);
|
|
|
|
LeaveCriticalSection(&condState->waitersCountLock);
|
|
|
|
|
|
|
|
// If no waiters, then this is a no-op. Otherwise, knock the semaphore
|
|
|
|
// down a notch.
|
|
|
|
if (haveWaiters)
|
|
|
|
ReleaseSemaphore(condState->sema, 1, 0);
|
|
|
|
|
|
|
|
// Release internal mutex.
|
|
|
|
ReleaseMutex(condState->internalMutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Signal the condition variable, allowing all threads to continue.
|
|
|
|
*
|
|
|
|
* First we have to wake up all threads waiting on the semaphore, then
|
|
|
|
* we wait until all of the threads have actually been woken before
|
|
|
|
* releasing the internal mutex. This ensures that all threads are woken.
|
|
|
|
*/
|
|
|
|
void Condition::broadcast()
|
|
|
|
{
|
|
|
|
WinCondition* condState = (WinCondition*) mState;
|
|
|
|
|
|
|
|
// Lock the internal mutex. This keeps the guys we're waking up
|
|
|
|
// from getting too far.
|
|
|
|
WaitForSingleObject(condState->internalMutex, INFINITE);
|
|
|
|
|
|
|
|
EnterCriticalSection(&condState->waitersCountLock);
|
|
|
|
bool haveWaiters = false;
|
|
|
|
|
|
|
|
if (condState->waitersCount > 0) {
|
|
|
|
haveWaiters = true;
|
|
|
|
condState->wasBroadcast = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (haveWaiters) {
|
|
|
|
// Wake up all the waiters.
|
|
|
|
ReleaseSemaphore(condState->sema, condState->waitersCount, 0);
|
|
|
|
|
|
|
|
LeaveCriticalSection(&condState->waitersCountLock);
|
|
|
|
|
|
|
|
// Wait for all awakened threads to acquire the counting semaphore.
|
|
|
|
// The last guy who was waiting sets this.
|
|
|
|
WaitForSingleObject(condState->waitersDone, INFINITE);
|
|
|
|
|
|
|
|
// Reset wasBroadcast. (No crit section needed because nobody
|
|
|
|
// else can wake up to poke at it.)
|
|
|
|
condState->wasBroadcast = 0;
|
|
|
|
} else {
|
|
|
|
// nothing to do
|
|
|
|
LeaveCriticalSection(&condState->waitersCountLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release internal mutex.
|
|
|
|
ReleaseMutex(condState->internalMutex);
|
|
|
|
}
|
|
|
|
|
2015-01-27 11:48:54 +08:00
|
|
|
#endif // !defined(_WIN32)
|
2009-03-04 11:31:44 +08:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is our thread object!
|
|
|
|
*/
|
|
|
|
|
|
|
|
Thread::Thread(bool canCallJava)
|
|
|
|
: mCanCallJava(canCallJava),
|
|
|
|
mThread(thread_id_t(-1)),
|
|
|
|
mLock("Thread::mLock"),
|
|
|
|
mStatus(NO_ERROR),
|
|
|
|
mExitPending(false), mRunning(false)
|
2011-02-02 03:32:29 +08:00
|
|
|
#ifdef HAVE_ANDROID_OS
|
|
|
|
, mTid(-1)
|
|
|
|
#endif
|
2009-03-04 11:31:44 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Thread::~Thread()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t Thread::readyToRun()
|
|
|
|
{
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t Thread::run(const char* name, int32_t priority, size_t stack)
|
|
|
|
{
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
|
|
|
|
if (mRunning) {
|
|
|
|
// thread already started
|
|
|
|
return INVALID_OPERATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
// reset status and exitPending to their default value, so we can
|
|
|
|
// try again after an error happened (either below, or in readyToRun())
|
|
|
|
mStatus = NO_ERROR;
|
|
|
|
mExitPending = false;
|
|
|
|
mThread = thread_id_t(-1);
|
2015-01-27 11:48:54 +08:00
|
|
|
|
2009-03-04 11:31:44 +08:00
|
|
|
// hold a strong reference on ourself
|
|
|
|
mHoldSelf = this;
|
|
|
|
|
2009-03-06 06:34:35 +08:00
|
|
|
mRunning = true;
|
|
|
|
|
2009-03-04 11:31:44 +08:00
|
|
|
bool res;
|
|
|
|
if (mCanCallJava) {
|
|
|
|
res = createThreadEtc(_threadLoop,
|
|
|
|
this, name, priority, stack, &mThread);
|
|
|
|
} else {
|
|
|
|
res = androidCreateRawThreadEtc(_threadLoop,
|
|
|
|
this, name, priority, stack, &mThread);
|
|
|
|
}
|
2015-01-27 11:48:54 +08:00
|
|
|
|
2009-03-04 11:31:44 +08:00
|
|
|
if (res == false) {
|
|
|
|
mStatus = UNKNOWN_ERROR; // something happened!
|
|
|
|
mRunning = false;
|
|
|
|
mThread = thread_id_t(-1);
|
2009-03-06 06:34:35 +08:00
|
|
|
mHoldSelf.clear(); // "this" may have gone away after this.
|
|
|
|
|
|
|
|
return UNKNOWN_ERROR;
|
2009-03-04 11:31:44 +08:00
|
|
|
}
|
2015-01-27 11:48:54 +08:00
|
|
|
|
2009-03-06 06:34:35 +08:00
|
|
|
// Do not refer to mStatus here: The thread is already running (may, in fact
|
|
|
|
// already have exited with a valid mStatus result). The NO_ERROR indication
|
|
|
|
// here merely indicates successfully starting the thread and does not
|
|
|
|
// imply successful termination/execution.
|
|
|
|
return NO_ERROR;
|
2011-02-02 03:32:29 +08:00
|
|
|
|
|
|
|
// Exiting scope of mLock is a memory barrier and allows new thread to run
|
2009-03-04 11:31:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int Thread::_threadLoop(void* user)
|
|
|
|
{
|
|
|
|
Thread* const self = static_cast<Thread*>(user);
|
2011-02-02 03:32:29 +08:00
|
|
|
|
2009-03-04 11:31:44 +08:00
|
|
|
sp<Thread> strong(self->mHoldSelf);
|
|
|
|
wp<Thread> weak(strong);
|
|
|
|
self->mHoldSelf.clear();
|
|
|
|
|
2011-02-17 02:13:53 +08:00
|
|
|
#ifdef HAVE_ANDROID_OS
|
2009-09-09 17:38:13 +08:00
|
|
|
// this is very useful for debugging with gdb
|
|
|
|
self->mTid = gettid();
|
|
|
|
#endif
|
|
|
|
|
2009-03-06 06:34:35 +08:00
|
|
|
bool first = true;
|
2009-03-04 11:31:44 +08:00
|
|
|
|
|
|
|
do {
|
2009-03-06 06:34:35 +08:00
|
|
|
bool result;
|
|
|
|
if (first) {
|
|
|
|
first = false;
|
|
|
|
self->mStatus = self->readyToRun();
|
|
|
|
result = (self->mStatus == NO_ERROR);
|
|
|
|
|
2011-02-02 03:32:29 +08:00
|
|
|
if (result && !self->exitPending()) {
|
2009-03-06 06:34:35 +08:00
|
|
|
// Binder threads (and maybe others) rely on threadLoop
|
|
|
|
// running at least once after a successful ::readyToRun()
|
|
|
|
// (unless, of course, the thread has already been asked to exit
|
|
|
|
// at that point).
|
|
|
|
// This is because threads are essentially used like this:
|
|
|
|
// (new ThreadSubclass())->run();
|
|
|
|
// The caller therefore does not retain a strong reference to
|
|
|
|
// the thread and the thread would simply disappear after the
|
|
|
|
// successful ::readyToRun() call instead of entering the
|
|
|
|
// threadLoop at least once.
|
|
|
|
result = self->threadLoop();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result = self->threadLoop();
|
|
|
|
}
|
|
|
|
|
2011-02-02 03:32:29 +08:00
|
|
|
// establish a scope for mLock
|
|
|
|
{
|
|
|
|
Mutex::Autolock _l(self->mLock);
|
2009-03-04 11:31:44 +08:00
|
|
|
if (result == false || self->mExitPending) {
|
|
|
|
self->mExitPending = true;
|
|
|
|
self->mRunning = false;
|
2011-01-05 03:58:04 +08:00
|
|
|
// clear thread ID so that requestExitAndWait() does not exit if
|
|
|
|
// called by a new thread using the same thread ID as this one.
|
|
|
|
self->mThread = thread_id_t(-1);
|
2011-02-02 03:32:29 +08:00
|
|
|
// note that interested observers blocked in requestExitAndWait are
|
|
|
|
// awoken by broadcast, but blocked on mLock until break exits scope
|
2009-09-09 17:38:13 +08:00
|
|
|
self->mThreadExitedCondition.broadcast();
|
2009-03-04 11:31:44 +08:00
|
|
|
break;
|
|
|
|
}
|
2011-02-02 03:32:29 +08:00
|
|
|
}
|
2015-01-27 11:48:54 +08:00
|
|
|
|
2009-03-04 11:31:44 +08:00
|
|
|
// Release our strong reference, to let a chance to the thread
|
|
|
|
// to die a peaceful death.
|
|
|
|
strong.clear();
|
2009-09-09 17:38:13 +08:00
|
|
|
// And immediately, re-acquire a strong reference for the next loop
|
2009-03-04 11:31:44 +08:00
|
|
|
strong = weak.promote();
|
|
|
|
} while(strong != 0);
|
2015-01-27 11:48:54 +08:00
|
|
|
|
2009-03-04 11:31:44 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Thread::requestExit()
|
|
|
|
{
|
2011-02-02 03:32:29 +08:00
|
|
|
Mutex::Autolock _l(mLock);
|
2009-03-04 11:31:44 +08:00
|
|
|
mExitPending = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t Thread::requestExitAndWait()
|
|
|
|
{
|
2011-06-02 23:59:28 +08:00
|
|
|
Mutex::Autolock _l(mLock);
|
2009-03-06 06:34:35 +08:00
|
|
|
if (mThread == getThreadId()) {
|
2012-01-06 07:22:43 +08:00
|
|
|
ALOGW(
|
2009-03-06 06:34:35 +08:00
|
|
|
"Thread (this=%p): don't call waitForExit() from this "
|
|
|
|
"Thread object's thread. It's a guaranteed deadlock!",
|
|
|
|
this);
|
2009-03-04 11:31:44 +08:00
|
|
|
|
2009-03-06 06:34:35 +08:00
|
|
|
return WOULD_BLOCK;
|
2009-03-04 11:31:44 +08:00
|
|
|
}
|
2015-01-27 11:48:54 +08:00
|
|
|
|
2011-06-02 23:59:28 +08:00
|
|
|
mExitPending = true;
|
2009-03-06 06:34:35 +08:00
|
|
|
|
|
|
|
while (mRunning == true) {
|
|
|
|
mThreadExitedCondition.wait(mLock);
|
|
|
|
}
|
2011-02-02 03:32:29 +08:00
|
|
|
// This next line is probably not needed any more, but is being left for
|
|
|
|
// historical reference. Note that each interested party will clear flag.
|
2009-03-06 06:34:35 +08:00
|
|
|
mExitPending = false;
|
|
|
|
|
2009-03-04 11:31:44 +08:00
|
|
|
return mStatus;
|
|
|
|
}
|
|
|
|
|
2011-06-24 03:55:29 +08:00
|
|
|
status_t Thread::join()
|
|
|
|
{
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
if (mThread == getThreadId()) {
|
2012-01-06 07:22:43 +08:00
|
|
|
ALOGW(
|
2011-06-24 03:55:29 +08:00
|
|
|
"Thread (this=%p): don't call join() from this "
|
|
|
|
"Thread object's thread. It's a guaranteed deadlock!",
|
|
|
|
this);
|
|
|
|
|
|
|
|
return WOULD_BLOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (mRunning == true) {
|
|
|
|
mThreadExitedCondition.wait(mLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mStatus;
|
|
|
|
}
|
|
|
|
|
2013-03-12 05:34:56 +08:00
|
|
|
bool Thread::isRunning() const {
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
return mRunning;
|
|
|
|
}
|
|
|
|
|
2011-07-12 06:59:22 +08:00
|
|
|
#ifdef HAVE_ANDROID_OS
|
|
|
|
pid_t Thread::getTid() const
|
|
|
|
{
|
|
|
|
// mTid is not defined until the child initializes it, and the caller may need it earlier
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
pid_t tid;
|
|
|
|
if (mRunning) {
|
|
|
|
pthread_t pthread = android_thread_id_t_to_pthread(mThread);
|
2014-09-13 01:19:08 +08:00
|
|
|
tid = pthread_gettid_np(pthread);
|
2011-07-12 06:59:22 +08:00
|
|
|
} else {
|
|
|
|
ALOGW("Thread (this=%p): getTid() is undefined before run()", this);
|
|
|
|
tid = -1;
|
|
|
|
}
|
|
|
|
return tid;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-03-04 11:31:44 +08:00
|
|
|
bool Thread::exitPending() const
|
|
|
|
{
|
2011-02-02 03:32:29 +08:00
|
|
|
Mutex::Autolock _l(mLock);
|
2009-03-04 11:31:44 +08:00
|
|
|
return mExitPending;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}; // namespace android
|