summaryrefslogtreecommitdiffstats
path: root/libutils/Threads.cpp
diff options
context:
space:
mode:
authorYabin Cui <yabinc@google.com>2015-01-26 19:48:54 -0800
committerYabin Cui <yabinc@google.com>2015-01-27 14:23:22 -0800
commit4a6e5a3b641dd99b658c4c336490371a3a5ae180 (patch)
tree3048f441498f5d5aed8bb218ecaca5e9a80cbf40 /libutils/Threads.cpp
parentfb538fb13d930a7398695d1044ceaaf854949018 (diff)
downloadsystem_core-4a6e5a3b641dd99b658c4c336490371a3a5ae180.zip
system_core-4a6e5a3b641dd99b658c4c336490371a3a5ae180.tar.gz
system_core-4a6e5a3b641dd99b658c4c336490371a3a5ae180.tar.bz2
Kill HAVE_PTHREADS.
Bug: 19083585 Change-Id: Ic09eb3dd250bc5c5b63cac7998f99f5fa007f407
Diffstat (limited to 'libutils/Threads.cpp')
-rw-r--r--libutils/Threads.cpp80
1 files changed, 37 insertions, 43 deletions
diff --git a/libutils/Threads.cpp b/libutils/Threads.cpp
index b2c6903..1e014c6 100644
--- a/libutils/Threads.cpp
+++ b/libutils/Threads.cpp
@@ -24,11 +24,11 @@
#include <stdlib.h>
#include <unistd.h>
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
# include <pthread.h>
# include <sched.h>
# include <sys/resource.h>
-#elif defined(HAVE_WIN32_THREADS)
+#else
# include <windows.h>
# include <stdint.h>
# include <process.h>
@@ -59,7 +59,7 @@
using namespace android;
// ----------------------------------------------------------------------------
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
// ----------------------------------------------------------------------------
/*
@@ -90,7 +90,7 @@ struct thread_data_t {
} else {
set_sched_policy(0, SP_FOREGROUND);
}
-
+
if (name) {
androidSetThreadName(name);
free(name);
@@ -127,7 +127,7 @@ int androidCreateRawThreadEtc(android_thread_func_t entryFunction,
size_t threadStackSize,
android_thread_id_t *threadId)
{
- pthread_attr_t attr;
+ pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
@@ -146,14 +146,14 @@ int androidCreateRawThreadEtc(android_thread_func_t entryFunction,
t->entryFunction = entryFunction;
t->userData = userData;
entryFunction = (android_thread_func_t)&thread_data_t::trampoline;
- userData = t;
+ userData = t;
}
#endif
if (threadStackSize) {
pthread_attr_setstacksize(&attr, threadStackSize);
}
-
+
errno = 0;
pthread_t thread;
int result = pthread_create(&thread, &attr,
@@ -188,7 +188,7 @@ android_thread_id_t androidGetThreadId()
}
// ----------------------------------------------------------------------------
-#elif defined(HAVE_WIN32_THREADS)
+#else // !defined(_WIN32)
// ----------------------------------------------------------------------------
/*
@@ -268,9 +268,7 @@ android_thread_id_t androidGetThreadId()
}
// ----------------------------------------------------------------------------
-#else
-#error "Threads not supported"
-#endif
+#endif // !defined(_WIN32)
// ----------------------------------------------------------------------------
@@ -307,8 +305,8 @@ void androidSetCreateThreadFunc(android_create_thread_fn func)
int androidSetThreadPriority(pid_t tid, int pri)
{
int rc = 0;
-
-#if defined(HAVE_PTHREADS)
+
+#if !defined(_WIN32)
int lasterr = 0;
if (pri >= ANDROID_PRIORITY_BACKGROUND) {
@@ -327,12 +325,12 @@ int androidSetThreadPriority(pid_t tid, int pri)
errno = lasterr;
}
#endif
-
+
return rc;
}
int androidGetThreadPriority(pid_t tid) {
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
return getpriority(PRIO_PROCESS, tid);
#else
return ANDROID_PRIORITY_NORMAL;
@@ -349,9 +347,9 @@ namespace android {
* ===========================================================================
*/
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
// implemented as inlines in threads.h
-#elif defined(HAVE_WIN32_THREADS)
+#else
Mutex::Mutex()
{
@@ -413,9 +411,7 @@ status_t Mutex::tryLock()
return (dwWaitResult == WAIT_OBJECT_0) ? 0 : -1;
}
-#else
-#error "Somebody forgot to implement threads for this platform."
-#endif
+#endif // !defined(_WIN32)
/*
@@ -424,9 +420,9 @@ status_t Mutex::tryLock()
* ===========================================================================
*/
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
// implemented as inlines in threads.h
-#elif defined(HAVE_WIN32_THREADS)
+#else
/*
* Windows doesn't have a condition variable solution. It's possible
@@ -474,7 +470,7 @@ typedef struct WinCondition {
//printf("+++ wait: incr waitersCount to %d (tid=%ld)\n",
// condState->waitersCount, getThreadId());
LeaveCriticalSection(&condState->waitersCountLock);
-
+
DWORD timeout = INFINITE;
if (abstime) {
nsecs_t reltime = *abstime - systemTime();
@@ -482,27 +478,27 @@ typedef struct WinCondition {
reltime = 0;
timeout = reltime/1000000;
}
-
+
// Atomically release the external mutex and wait on the semaphore.
DWORD res =
SignalObjectAndWait(hMutex, condState->sema, timeout, FALSE);
-
+
//printf("+++ wait: awake (tid=%ld)\n", getThreadId());
-
+
// Reacquire lock to avoid race conditions.
EnterCriticalSection(&condState->waitersCountLock);
-
+
// No longer waiting.
condState->waitersCount--;
-
+
// Check to see if we're the last waiter after a broadcast.
bool lastWaiter = (condState->wasBroadcast && condState->waitersCount == 0);
-
+
//printf("+++ wait: lastWaiter=%d (wasBc=%d wc=%d)\n",
// lastWaiter, condState->wasBroadcast, condState->waitersCount);
-
+
LeaveCriticalSection(&condState->waitersCountLock);
-
+
// 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.
@@ -518,11 +514,11 @@ typedef struct WinCondition {
// Grab the internal mutex.
WaitForSingleObject(condState->internalMutex, INFINITE);
}
-
+
// Release the internal and grab the external.
ReleaseMutex(condState->internalMutex);
WaitForSingleObject(hMutex, INFINITE);
-
+
return res == WAIT_OBJECT_0 ? NO_ERROR : -1;
}
} WinCondition;
@@ -565,7 +561,7 @@ status_t Condition::wait(Mutex& mutex)
{
WinCondition* condState = (WinCondition*) mState;
HANDLE hMutex = (HANDLE) mutex.mState;
-
+
return ((WinCondition*)mState)->wait(condState, hMutex, NULL);
}
@@ -647,9 +643,7 @@ void Condition::broadcast()
ReleaseMutex(condState->internalMutex);
}
-#else
-#error "condition variables not supported on this platform"
-#endif
+#endif // !defined(_WIN32)
// ----------------------------------------------------------------------------
@@ -692,7 +686,7 @@ status_t Thread::run(const char* name, int32_t priority, size_t stack)
mStatus = NO_ERROR;
mExitPending = false;
mThread = thread_id_t(-1);
-
+
// hold a strong reference on ourself
mHoldSelf = this;
@@ -706,7 +700,7 @@ status_t Thread::run(const char* name, int32_t priority, size_t stack)
res = androidCreateRawThreadEtc(_threadLoop,
this, name, priority, stack, &mThread);
}
-
+
if (res == false) {
mStatus = UNKNOWN_ERROR; // something happened!
mRunning = false;
@@ -715,7 +709,7 @@ status_t Thread::run(const char* name, int32_t priority, size_t stack)
return UNKNOWN_ERROR;
}
-
+
// 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
@@ -779,14 +773,14 @@ int Thread::_threadLoop(void* user)
break;
}
}
-
+
// Release our strong reference, to let a chance to the thread
// to die a peaceful death.
strong.clear();
// And immediately, re-acquire a strong reference for the next loop
strong = weak.promote();
} while(strong != 0);
-
+
return 0;
}
@@ -807,7 +801,7 @@ status_t Thread::requestExitAndWait()
return WOULD_BLOCK;
}
-
+
mExitPending = true;
while (mRunning == true) {