blob: 64bc4025d7f85ae1b784242c19101f0cf89fe1a5 [file] [log] [blame]
The Android Open Source Projectcbb10112009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Project7a4c8392009-03-05 14:34:35 -080017// #define LOG_NDEBUG 0
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080018#define LOG_TAG "libutils.threads"
19
Mark Salyzyn5bed8032014-04-30 11:10:46 -070020#include <assert.h>
Mathias Agopian22dbf392017-02-28 15:06:51 -080021#include <utils/Thread.h>
22#include <utils/AndroidThreads.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080023
Yabin Cui4a6e5a32015-01-26 19:48:54 -080024#if !defined(_WIN32)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080025# include <sys/resource.h>
Yabin Cui4a6e5a32015-01-26 19:48:54 -080026#else
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080027# include <windows.h>
28# include <stdint.h>
29# include <process.h>
30# define HAVE_CREATETHREAD // Cygwin, vs. HAVE__BEGINTHREADEX for MinGW
31#endif
32
Elliott Hughes292ccd32014-12-15 12:52:53 -080033#if defined(__linux__)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080034#include <sys/prctl.h>
35#endif
36
Mark Salyzyn5bed8032014-04-30 11:10:46 -070037#include <utils/Log.h>
38
39#include <cutils/sched_policy.h>
40
Elliott Hughes9b828ad2015-07-30 08:47:35 -070041#if defined(__ANDROID__)
Mark Salyzyn5bed8032014-04-30 11:10:46 -070042# define __android_unused
43#else
44# define __android_unused __attribute__((__unused__))
45#endif
46
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080047/*
48 * ===========================================================================
49 * Thread wrappers
50 * ===========================================================================
51 */
52
53using namespace android;
54
55// ----------------------------------------------------------------------------
Yabin Cui4a6e5a32015-01-26 19:48:54 -080056#if !defined(_WIN32)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080057// ----------------------------------------------------------------------------
58
59/*
Dianne Hackborn16d217e2010-09-03 17:07:07 -070060 * Create and run a new thread.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080061 *
62 * We create it "detached", so it cleans up after itself.
63 */
64
65typedef void* (*android_pthread_entry)(void*);
66
67struct thread_data_t {
68 thread_func_t entryFunction;
69 void* userData;
70 int priority;
71 char * threadName;
72
73 // we use this trampoline when we need to set the priority with
Glenn Kastend731f072011-07-11 15:59:22 -070074 // nice/setpriority, and name with prctl.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080075 static int trampoline(const thread_data_t* t) {
76 thread_func_t f = t->entryFunction;
77 void* u = t->userData;
78 int prio = t->priority;
79 char * name = t->threadName;
80 delete t;
81 setpriority(PRIO_PROCESS, 0, prio);
Glenn Kastenfe34e452012-04-30 16:03:30 -070082 if (prio >= ANDROID_PRIORITY_BACKGROUND) {
83 set_sched_policy(0, SP_BACKGROUND);
84 } else {
85 set_sched_policy(0, SP_FOREGROUND);
Dianne Hackborna78bab02010-09-09 15:50:18 -070086 }
Yabin Cui4a6e5a32015-01-26 19:48:54 -080087
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080088 if (name) {
Mathias Agopian6090df82013-03-07 15:34:28 -080089 androidSetThreadName(name);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080090 free(name);
91 }
92 return f(u);
93 }
94};
95
Mathias Agopian6090df82013-03-07 15:34:28 -080096void androidSetThreadName(const char* name) {
Elliott Hughes292ccd32014-12-15 12:52:53 -080097#if defined(__linux__)
Mathias Agopian6090df82013-03-07 15:34:28 -080098 // Mac OS doesn't have this, and we build libutil for the host too
99 int hasAt = 0;
100 int hasDot = 0;
101 const char *s = name;
102 while (*s) {
103 if (*s == '.') hasDot = 1;
104 else if (*s == '@') hasAt = 1;
105 s++;
106 }
107 int len = s - name;
108 if (len < 15 || hasAt || !hasDot) {
109 s = name;
110 } else {
111 s = name + len - 15;
112 }
113 prctl(PR_SET_NAME, (unsigned long) s, 0, 0, 0);
114#endif
115}
116
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800117int androidCreateRawThreadEtc(android_thread_func_t entryFunction,
118 void *userData,
Mark Salyzyn5bed8032014-04-30 11:10:46 -0700119 const char* threadName __android_unused,
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800120 int32_t threadPriority,
121 size_t threadStackSize,
122 android_thread_id_t *threadId)
123{
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800124 pthread_attr_t attr;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800125 pthread_attr_init(&attr);
126 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
127
Elliott Hughes9b828ad2015-07-30 08:47:35 -0700128#if defined(__ANDROID__) /* valgrind is rejecting RT-priority create reqs */
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800129 if (threadPriority != PRIORITY_DEFAULT || threadName != NULL) {
Glenn Kastend731f072011-07-11 15:59:22 -0700130 // Now that the pthread_t has a method to find the associated
131 // android_thread_id_t (pid) from pthread_t, it would be possible to avoid
132 // this trampoline in some cases as the parent could set the properties
133 // for the child. However, there would be a race condition because the
134 // child becomes ready immediately, and it doesn't work for the name.
135 // prctl(PR_SET_NAME) only works for self; prctl(PR_SET_THREAD_NAME) was
136 // proposed but not yet accepted.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800137 thread_data_t* t = new thread_data_t;
138 t->priority = threadPriority;
139 t->threadName = threadName ? strdup(threadName) : NULL;
140 t->entryFunction = entryFunction;
141 t->userData = userData;
142 entryFunction = (android_thread_func_t)&thread_data_t::trampoline;
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800143 userData = t;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800144 }
145#endif
146
147 if (threadStackSize) {
148 pthread_attr_setstacksize(&attr, threadStackSize);
149 }
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800150
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800151 errno = 0;
152 pthread_t thread;
153 int result = pthread_create(&thread, &attr,
154 (android_pthread_entry)entryFunction, userData);
Le-Chun Wud8734d12011-07-14 14:27:18 -0700155 pthread_attr_destroy(&attr);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800156 if (result != 0) {
Elliott Hughes6ed68cc2015-06-30 08:22:24 -0700157 ALOGE("androidCreateRawThreadEtc failed (entry=%p, res=%d, %s)\n"
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800158 "(android threadPriority=%d)",
Elliott Hughes6ed68cc2015-06-30 08:22:24 -0700159 entryFunction, result, strerror(errno), threadPriority);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800160 return 0;
161 }
162
Glenn Kastena538e262011-06-02 08:59:28 -0700163 // Note that *threadID is directly available to the parent only, as it is
164 // assigned after the child starts. Use memory barrier / lock if the child
165 // or other threads also need access.
Yi Konge1731a42018-07-16 18:11:34 -0700166 if (threadId != nullptr) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800167 *threadId = (android_thread_id_t)thread; // XXX: this is not portable
168 }
169 return 1;
170}
171
Elliott Hughes9b828ad2015-07-30 08:47:35 -0700172#if defined(__ANDROID__)
Glenn Kastend731f072011-07-11 15:59:22 -0700173static pthread_t android_thread_id_t_to_pthread(android_thread_id_t thread)
174{
175 return (pthread_t) thread;
176}
177#endif
178
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800179android_thread_id_t androidGetThreadId()
180{
181 return (android_thread_id_t)pthread_self();
182}
183
184// ----------------------------------------------------------------------------
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800185#else // !defined(_WIN32)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800186// ----------------------------------------------------------------------------
187
188/*
189 * Trampoline to make us __stdcall-compliant.
190 *
191 * We're expected to delete "vDetails" when we're done.
192 */
193struct threadDetails {
194 int (*func)(void*);
195 void* arg;
196};
197static __stdcall unsigned int threadIntermediary(void* vDetails)
198{
199 struct threadDetails* pDetails = (struct threadDetails*) vDetails;
200 int result;
201
202 result = (*(pDetails->func))(pDetails->arg);
203
204 delete pDetails;
205
Steve Block8b4cf772011-10-12 17:27:03 +0100206 ALOG(LOG_VERBOSE, "thread", "thread exiting\n");
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800207 return (unsigned int) result;
208}
209
210/*
211 * Create and run a new thread.
212 */
213static bool doCreateThread(android_thread_func_t fn, void* arg, android_thread_id_t *id)
214{
215 HANDLE hThread;
216 struct threadDetails* pDetails = new threadDetails; // must be on heap
217 unsigned int thrdaddr;
218
219 pDetails->func = fn;
220 pDetails->arg = arg;
221
222#if defined(HAVE__BEGINTHREADEX)
223 hThread = (HANDLE) _beginthreadex(NULL, 0, threadIntermediary, pDetails, 0,
224 &thrdaddr);
225 if (hThread == 0)
226#elif defined(HAVE_CREATETHREAD)
227 hThread = CreateThread(NULL, 0,
228 (LPTHREAD_START_ROUTINE) threadIntermediary,
229 (void*) pDetails, 0, (DWORD*) &thrdaddr);
230 if (hThread == NULL)
231#endif
232 {
Steve Block8b4cf772011-10-12 17:27:03 +0100233 ALOG(LOG_WARN, "thread", "WARNING: thread create failed\n");
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800234 return false;
235 }
236
237#if defined(HAVE_CREATETHREAD)
238 /* close the management handle */
239 CloseHandle(hThread);
240#endif
241
242 if (id != NULL) {
243 *id = (android_thread_id_t)thrdaddr;
244 }
245
246 return true;
247}
248
249int androidCreateRawThreadEtc(android_thread_func_t fn,
250 void *userData,
Mark Salyzyn5bed8032014-04-30 11:10:46 -0700251 const char* /*threadName*/,
252 int32_t /*threadPriority*/,
253 size_t /*threadStackSize*/,
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800254 android_thread_id_t *threadId)
255{
256 return doCreateThread( fn, userData, threadId);
257}
258
259android_thread_id_t androidGetThreadId()
260{
261 return (android_thread_id_t)GetCurrentThreadId();
262}
263
264// ----------------------------------------------------------------------------
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800265#endif // !defined(_WIN32)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800266
267// ----------------------------------------------------------------------------
268
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800269int androidCreateThread(android_thread_func_t fn, void* arg)
270{
271 return createThreadEtc(fn, arg);
272}
273
274int androidCreateThreadGetID(android_thread_func_t fn, void *arg, android_thread_id_t *id)
275{
276 return createThreadEtc(fn, arg, "android:unnamed_thread",
277 PRIORITY_DEFAULT, 0, id);
278}
279
280static android_create_thread_fn gCreateThreadFn = androidCreateRawThreadEtc;
281
282int androidCreateThreadEtc(android_thread_func_t entryFunction,
283 void *userData,
284 const char* threadName,
285 int32_t threadPriority,
286 size_t threadStackSize,
287 android_thread_id_t *threadId)
288{
289 return gCreateThreadFn(entryFunction, userData, threadName,
290 threadPriority, threadStackSize, threadId);
291}
292
293void androidSetCreateThreadFunc(android_create_thread_fn func)
294{
295 gCreateThreadFn = func;
296}
297
Elliott Hughes9b828ad2015-07-30 08:47:35 -0700298#if defined(__ANDROID__)
Dianne Hackborn235af972009-12-07 17:59:37 -0800299int androidSetThreadPriority(pid_t tid, int pri)
300{
301 int rc = 0;
302 int lasterr = 0;
303
Glenn Kastenfe34e452012-04-30 16:03:30 -0700304 if (pri >= ANDROID_PRIORITY_BACKGROUND) {
305 rc = set_sched_policy(tid, SP_BACKGROUND);
306 } else if (getpriority(PRIO_PROCESS, tid) >= ANDROID_PRIORITY_BACKGROUND) {
307 rc = set_sched_policy(tid, SP_FOREGROUND);
Dianne Hackborn235af972009-12-07 17:59:37 -0800308 }
309
310 if (rc) {
311 lasterr = errno;
312 }
313
314 if (setpriority(PRIO_PROCESS, tid, pri) < 0) {
315 rc = INVALID_OPERATION;
316 } else {
317 errno = lasterr;
318 }
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800319
Dianne Hackborn235af972009-12-07 17:59:37 -0800320 return rc;
321}
322
Andreas Huber8ddbed92011-09-15 12:21:40 -0700323int androidGetThreadPriority(pid_t tid) {
324 return getpriority(PRIO_PROCESS, tid);
325}
326
Jeff Brown27e6eaa2012-03-16 22:18:39 -0700327#endif
Glenn Kasten6fbe0a82011-06-22 16:20:37 -0700328
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800329namespace android {
330
331/*
332 * ===========================================================================
333 * Mutex class
334 * ===========================================================================
335 */
336
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800337#if !defined(_WIN32)
Mathias Agopian15554362009-07-12 23:11:20 -0700338// implemented as inlines in threads.h
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800339#else
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800340
341Mutex::Mutex()
342{
343 HANDLE hMutex;
344
345 assert(sizeof(hMutex) == sizeof(mState));
346
347 hMutex = CreateMutex(NULL, FALSE, NULL);
348 mState = (void*) hMutex;
349}
350
Dan Willemsen528f1442017-11-29 18:06:11 -0800351Mutex::Mutex(const char* /*name*/)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800352{
353 // XXX: name not used for now
354 HANDLE hMutex;
355
David 'Digit' Turner9bafd122009-08-01 00:20:17 +0200356 assert(sizeof(hMutex) == sizeof(mState));
357
358 hMutex = CreateMutex(NULL, FALSE, NULL);
359 mState = (void*) hMutex;
360}
361
Dan Willemsen528f1442017-11-29 18:06:11 -0800362Mutex::Mutex(int /*type*/, const char* /*name*/)
David 'Digit' Turner9bafd122009-08-01 00:20:17 +0200363{
364 // XXX: type and name not used for now
365 HANDLE hMutex;
366
367 assert(sizeof(hMutex) == sizeof(mState));
368
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800369 hMutex = CreateMutex(NULL, FALSE, NULL);
370 mState = (void*) hMutex;
371}
372
373Mutex::~Mutex()
374{
375 CloseHandle((HANDLE) mState);
376}
377
378status_t Mutex::lock()
379{
380 DWORD dwWaitResult;
381 dwWaitResult = WaitForSingleObject((HANDLE) mState, INFINITE);
Elliott Hughes643268f2018-10-08 11:10:11 -0700382 return dwWaitResult != WAIT_OBJECT_0 ? -1 : OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800383}
384
385void Mutex::unlock()
386{
387 if (!ReleaseMutex((HANDLE) mState))
Steve Block8b4cf772011-10-12 17:27:03 +0100388 ALOG(LOG_WARN, "thread", "WARNING: bad result from unlocking mutex\n");
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800389}
390
391status_t Mutex::tryLock()
392{
393 DWORD dwWaitResult;
394
395 dwWaitResult = WaitForSingleObject((HANDLE) mState, 0);
396 if (dwWaitResult != WAIT_OBJECT_0 && dwWaitResult != WAIT_TIMEOUT)
Steve Block8b4cf772011-10-12 17:27:03 +0100397 ALOG(LOG_WARN, "thread", "WARNING: bad result from try-locking mutex\n");
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800398 return (dwWaitResult == WAIT_OBJECT_0) ? 0 : -1;
399}
400
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800401#endif // !defined(_WIN32)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800402
403
404/*
405 * ===========================================================================
406 * Condition class
407 * ===========================================================================
408 */
409
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800410#if !defined(_WIN32)
Mathias Agopian15554362009-07-12 23:11:20 -0700411// implemented as inlines in threads.h
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800412#else
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800413
414/*
415 * Windows doesn't have a condition variable solution. It's possible
416 * to create one, but it's easy to get it wrong. For a discussion, and
417 * the origin of this implementation, see:
418 *
419 * http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
420 *
421 * The implementation shown on the page does NOT follow POSIX semantics.
422 * As an optimization they require acquiring the external mutex before
423 * calling signal() and broadcast(), whereas POSIX only requires grabbing
424 * it before calling wait(). The implementation here has been un-optimized
425 * to have the correct behavior.
426 */
427typedef struct WinCondition {
428 // Number of waiting threads.
429 int waitersCount;
430
431 // Serialize access to waitersCount.
432 CRITICAL_SECTION waitersCountLock;
433
434 // Semaphore used to queue up threads waiting for the condition to
435 // become signaled.
436 HANDLE sema;
437
438 // An auto-reset event used by the broadcast/signal thread to wait
439 // for all the waiting thread(s) to wake up and be released from
440 // the semaphore.
441 HANDLE waitersDone;
442
443 // This mutex wouldn't be necessary if we required that the caller
444 // lock the external mutex before calling signal() and broadcast().
445 // I'm trying to mimic pthread semantics though.
446 HANDLE internalMutex;
447
448 // Keeps track of whether we were broadcasting or signaling. This
449 // allows us to optimize the code if we're just signaling.
450 bool wasBroadcast;
451
452 status_t wait(WinCondition* condState, HANDLE hMutex, nsecs_t* abstime)
453 {
454 // Increment the wait count, avoiding race conditions.
455 EnterCriticalSection(&condState->waitersCountLock);
456 condState->waitersCount++;
457 //printf("+++ wait: incr waitersCount to %d (tid=%ld)\n",
458 // condState->waitersCount, getThreadId());
459 LeaveCriticalSection(&condState->waitersCountLock);
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800460
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800461 DWORD timeout = INFINITE;
462 if (abstime) {
463 nsecs_t reltime = *abstime - systemTime();
464 if (reltime < 0)
465 reltime = 0;
466 timeout = reltime/1000000;
467 }
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800468
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800469 // Atomically release the external mutex and wait on the semaphore.
470 DWORD res =
471 SignalObjectAndWait(hMutex, condState->sema, timeout, FALSE);
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800472
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800473 //printf("+++ wait: awake (tid=%ld)\n", getThreadId());
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800474
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800475 // Reacquire lock to avoid race conditions.
476 EnterCriticalSection(&condState->waitersCountLock);
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800477
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800478 // No longer waiting.
479 condState->waitersCount--;
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800480
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800481 // Check to see if we're the last waiter after a broadcast.
482 bool lastWaiter = (condState->wasBroadcast && condState->waitersCount == 0);
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800483
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800484 //printf("+++ wait: lastWaiter=%d (wasBc=%d wc=%d)\n",
485 // lastWaiter, condState->wasBroadcast, condState->waitersCount);
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800486
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800487 LeaveCriticalSection(&condState->waitersCountLock);
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800488
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800489 // If we're the last waiter thread during this particular broadcast
490 // then signal broadcast() that we're all awake. It'll drop the
491 // internal mutex.
492 if (lastWaiter) {
493 // Atomically signal the "waitersDone" event and wait until we
494 // can acquire the internal mutex. We want to do this in one step
495 // because it ensures that everybody is in the mutex FIFO before
496 // any thread has a chance to run. Without it, another thread
497 // could wake up, do work, and hop back in ahead of us.
498 SignalObjectAndWait(condState->waitersDone, condState->internalMutex,
499 INFINITE, FALSE);
500 } else {
501 // Grab the internal mutex.
502 WaitForSingleObject(condState->internalMutex, INFINITE);
503 }
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800504
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800505 // Release the internal and grab the external.
506 ReleaseMutex(condState->internalMutex);
507 WaitForSingleObject(hMutex, INFINITE);
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800508
Elliott Hughes643268f2018-10-08 11:10:11 -0700509 return res == WAIT_OBJECT_0 ? OK : -1;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800510 }
511} WinCondition;
512
513/*
514 * Constructor. Set up the WinCondition stuff.
515 */
516Condition::Condition()
517{
518 WinCondition* condState = new WinCondition;
519
520 condState->waitersCount = 0;
521 condState->wasBroadcast = false;
522 // semaphore: no security, initial value of 0
523 condState->sema = CreateSemaphore(NULL, 0, 0x7fffffff, NULL);
524 InitializeCriticalSection(&condState->waitersCountLock);
525 // auto-reset event, not signaled initially
526 condState->waitersDone = CreateEvent(NULL, FALSE, FALSE, NULL);
527 // used so we don't have to lock external mutex on signal/broadcast
528 condState->internalMutex = CreateMutex(NULL, FALSE, NULL);
529
530 mState = condState;
531}
532
533/*
534 * Destructor. Free Windows resources as well as our allocated storage.
535 */
536Condition::~Condition()
537{
538 WinCondition* condState = (WinCondition*) mState;
539 if (condState != NULL) {
540 CloseHandle(condState->sema);
541 CloseHandle(condState->waitersDone);
542 delete condState;
543 }
544}
545
546
547status_t Condition::wait(Mutex& mutex)
548{
549 WinCondition* condState = (WinCondition*) mState;
550 HANDLE hMutex = (HANDLE) mutex.mState;
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800551
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800552 return ((WinCondition*)mState)->wait(condState, hMutex, NULL);
553}
554
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800555status_t Condition::waitRelative(Mutex& mutex, nsecs_t reltime)
556{
David 'Digit' Turner9bafd122009-08-01 00:20:17 +0200557 WinCondition* condState = (WinCondition*) mState;
558 HANDLE hMutex = (HANDLE) mutex.mState;
559 nsecs_t absTime = systemTime()+reltime;
560
561 return ((WinCondition*)mState)->wait(condState, hMutex, &absTime);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800562}
563
564/*
565 * Signal the condition variable, allowing one thread to continue.
566 */
567void Condition::signal()
568{
569 WinCondition* condState = (WinCondition*) mState;
570
571 // Lock the internal mutex. This ensures that we don't clash with
572 // broadcast().
573 WaitForSingleObject(condState->internalMutex, INFINITE);
574
575 EnterCriticalSection(&condState->waitersCountLock);
576 bool haveWaiters = (condState->waitersCount > 0);
577 LeaveCriticalSection(&condState->waitersCountLock);
578
579 // If no waiters, then this is a no-op. Otherwise, knock the semaphore
580 // down a notch.
581 if (haveWaiters)
582 ReleaseSemaphore(condState->sema, 1, 0);
583
584 // Release internal mutex.
585 ReleaseMutex(condState->internalMutex);
586}
587
588/*
589 * Signal the condition variable, allowing all threads to continue.
590 *
591 * First we have to wake up all threads waiting on the semaphore, then
592 * we wait until all of the threads have actually been woken before
593 * releasing the internal mutex. This ensures that all threads are woken.
594 */
595void Condition::broadcast()
596{
597 WinCondition* condState = (WinCondition*) mState;
598
599 // Lock the internal mutex. This keeps the guys we're waking up
600 // from getting too far.
601 WaitForSingleObject(condState->internalMutex, INFINITE);
602
603 EnterCriticalSection(&condState->waitersCountLock);
604 bool haveWaiters = false;
605
606 if (condState->waitersCount > 0) {
607 haveWaiters = true;
608 condState->wasBroadcast = true;
609 }
610
611 if (haveWaiters) {
612 // Wake up all the waiters.
613 ReleaseSemaphore(condState->sema, condState->waitersCount, 0);
614
615 LeaveCriticalSection(&condState->waitersCountLock);
616
617 // Wait for all awakened threads to acquire the counting semaphore.
618 // The last guy who was waiting sets this.
619 WaitForSingleObject(condState->waitersDone, INFINITE);
620
621 // Reset wasBroadcast. (No crit section needed because nobody
622 // else can wake up to poke at it.)
623 condState->wasBroadcast = 0;
624 } else {
625 // nothing to do
626 LeaveCriticalSection(&condState->waitersCountLock);
627 }
628
629 // Release internal mutex.
630 ReleaseMutex(condState->internalMutex);
631}
632
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800633#endif // !defined(_WIN32)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800634
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800635// ----------------------------------------------------------------------------
636
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800637/*
638 * This is our thread object!
639 */
640
641Thread::Thread(bool canCallJava)
Elliott Hughes643268f2018-10-08 11:10:11 -0700642 : mCanCallJava(canCallJava),
643 mThread(thread_id_t(-1)),
644 mLock("Thread::mLock"),
645 mStatus(OK),
646 mExitPending(false),
647 mRunning(false)
Elliott Hughes9b828ad2015-07-30 08:47:35 -0700648#if defined(__ANDROID__)
Elliott Hughes643268f2018-10-08 11:10:11 -0700649 ,
650 mTid(-1)
Glenn Kasten966a48f2011-02-01 11:32:29 -0800651#endif
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800652{
653}
654
655Thread::~Thread()
656{
657}
658
659status_t Thread::readyToRun()
660{
Elliott Hughes643268f2018-10-08 11:10:11 -0700661 return OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800662}
663
664status_t Thread::run(const char* name, int32_t priority, size_t stack)
665{
Brian Carlstrome71b9142016-03-12 16:08:12 -0800666 LOG_ALWAYS_FATAL_IF(name == nullptr, "thread name not provided to Thread::run");
667
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800668 Mutex::Autolock _l(mLock);
669
670 if (mRunning) {
671 // thread already started
672 return INVALID_OPERATION;
673 }
674
675 // reset status and exitPending to their default value, so we can
676 // try again after an error happened (either below, or in readyToRun())
Elliott Hughes643268f2018-10-08 11:10:11 -0700677 mStatus = OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800678 mExitPending = false;
679 mThread = thread_id_t(-1);
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800680
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800681 // hold a strong reference on ourself
682 mHoldSelf = this;
683
The Android Open Source Project7a4c8392009-03-05 14:34:35 -0800684 mRunning = true;
685
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800686 bool res;
687 if (mCanCallJava) {
688 res = createThreadEtc(_threadLoop,
689 this, name, priority, stack, &mThread);
690 } else {
691 res = androidCreateRawThreadEtc(_threadLoop,
692 this, name, priority, stack, &mThread);
693 }
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800694
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800695 if (res == false) {
696 mStatus = UNKNOWN_ERROR; // something happened!
697 mRunning = false;
698 mThread = thread_id_t(-1);
The Android Open Source Project7a4c8392009-03-05 14:34:35 -0800699 mHoldSelf.clear(); // "this" may have gone away after this.
700
701 return UNKNOWN_ERROR;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800702 }
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800703
The Android Open Source Project7a4c8392009-03-05 14:34:35 -0800704 // Do not refer to mStatus here: The thread is already running (may, in fact
Elliott Hughes643268f2018-10-08 11:10:11 -0700705 // already have exited with a valid mStatus result). The OK indication
The Android Open Source Project7a4c8392009-03-05 14:34:35 -0800706 // here merely indicates successfully starting the thread and does not
707 // imply successful termination/execution.
Elliott Hughes643268f2018-10-08 11:10:11 -0700708 return OK;
Glenn Kasten966a48f2011-02-01 11:32:29 -0800709
710 // Exiting scope of mLock is a memory barrier and allows new thread to run
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800711}
712
713int Thread::_threadLoop(void* user)
714{
715 Thread* const self = static_cast<Thread*>(user);
Glenn Kasten966a48f2011-02-01 11:32:29 -0800716
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800717 sp<Thread> strong(self->mHoldSelf);
718 wp<Thread> weak(strong);
719 self->mHoldSelf.clear();
720
Elliott Hughes9b828ad2015-07-30 08:47:35 -0700721#if defined(__ANDROID__)
Mathias Agopian51ce3ad2009-09-09 02:38:13 -0700722 // this is very useful for debugging with gdb
723 self->mTid = gettid();
724#endif
725
The Android Open Source Project7a4c8392009-03-05 14:34:35 -0800726 bool first = true;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800727
728 do {
The Android Open Source Project7a4c8392009-03-05 14:34:35 -0800729 bool result;
730 if (first) {
731 first = false;
732 self->mStatus = self->readyToRun();
Elliott Hughes643268f2018-10-08 11:10:11 -0700733 result = (self->mStatus == OK);
The Android Open Source Project7a4c8392009-03-05 14:34:35 -0800734
Glenn Kasten966a48f2011-02-01 11:32:29 -0800735 if (result && !self->exitPending()) {
The Android Open Source Project7a4c8392009-03-05 14:34:35 -0800736 // Binder threads (and maybe others) rely on threadLoop
737 // running at least once after a successful ::readyToRun()
738 // (unless, of course, the thread has already been asked to exit
739 // at that point).
740 // This is because threads are essentially used like this:
741 // (new ThreadSubclass())->run();
742 // The caller therefore does not retain a strong reference to
743 // the thread and the thread would simply disappear after the
744 // successful ::readyToRun() call instead of entering the
745 // threadLoop at least once.
746 result = self->threadLoop();
747 }
748 } else {
749 result = self->threadLoop();
750 }
751
Glenn Kasten966a48f2011-02-01 11:32:29 -0800752 // establish a scope for mLock
753 {
754 Mutex::Autolock _l(self->mLock);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800755 if (result == false || self->mExitPending) {
756 self->mExitPending = true;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800757 self->mRunning = false;
Eric Laurentfe2c4632011-01-04 11:58:04 -0800758 // clear thread ID so that requestExitAndWait() does not exit if
759 // called by a new thread using the same thread ID as this one.
760 self->mThread = thread_id_t(-1);
Glenn Kasten966a48f2011-02-01 11:32:29 -0800761 // note that interested observers blocked in requestExitAndWait are
762 // awoken by broadcast, but blocked on mLock until break exits scope
Mathias Agopian51ce3ad2009-09-09 02:38:13 -0700763 self->mThreadExitedCondition.broadcast();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800764 break;
765 }
Glenn Kasten966a48f2011-02-01 11:32:29 -0800766 }
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800767
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800768 // Release our strong reference, to let a chance to the thread
769 // to die a peaceful death.
770 strong.clear();
Mathias Agopian51ce3ad2009-09-09 02:38:13 -0700771 // And immediately, re-acquire a strong reference for the next loop
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800772 strong = weak.promote();
Yi Konge1731a42018-07-16 18:11:34 -0700773 } while(strong != nullptr);
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800774
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800775 return 0;
776}
777
778void Thread::requestExit()
779{
Glenn Kasten966a48f2011-02-01 11:32:29 -0800780 Mutex::Autolock _l(mLock);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800781 mExitPending = true;
782}
783
784status_t Thread::requestExitAndWait()
785{
Glenn Kastena538e262011-06-02 08:59:28 -0700786 Mutex::Autolock _l(mLock);
The Android Open Source Project7a4c8392009-03-05 14:34:35 -0800787 if (mThread == getThreadId()) {
Steve Block61d341b2012-01-05 23:22:43 +0000788 ALOGW(
The Android Open Source Project7a4c8392009-03-05 14:34:35 -0800789 "Thread (this=%p): don't call waitForExit() from this "
790 "Thread object's thread. It's a guaranteed deadlock!",
791 this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800792
The Android Open Source Project7a4c8392009-03-05 14:34:35 -0800793 return WOULD_BLOCK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800794 }
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800795
Glenn Kastena538e262011-06-02 08:59:28 -0700796 mExitPending = true;
The Android Open Source Project7a4c8392009-03-05 14:34:35 -0800797
The Android Open Source Project7a4c8392009-03-05 14:34:35 -0800798 while (mRunning == true) {
799 mThreadExitedCondition.wait(mLock);
800 }
Glenn Kasten966a48f2011-02-01 11:32:29 -0800801 // This next line is probably not needed any more, but is being left for
802 // historical reference. Note that each interested party will clear flag.
The Android Open Source Project7a4c8392009-03-05 14:34:35 -0800803 mExitPending = false;
804
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800805 return mStatus;
806}
807
Glenn Kasten6839e8e2011-06-23 12:55:29 -0700808status_t Thread::join()
809{
810 Mutex::Autolock _l(mLock);
811 if (mThread == getThreadId()) {
Steve Block61d341b2012-01-05 23:22:43 +0000812 ALOGW(
Glenn Kasten6839e8e2011-06-23 12:55:29 -0700813 "Thread (this=%p): don't call join() from this "
814 "Thread object's thread. It's a guaranteed deadlock!",
815 this);
816
817 return WOULD_BLOCK;
818 }
819
820 while (mRunning == true) {
821 mThreadExitedCondition.wait(mLock);
822 }
823
824 return mStatus;
825}
826
Romain Guy31ba37f2013-03-11 14:34:56 -0700827bool Thread::isRunning() const {
828 Mutex::Autolock _l(mLock);
829 return mRunning;
830}
831
Elliott Hughes9b828ad2015-07-30 08:47:35 -0700832#if defined(__ANDROID__)
Glenn Kastend731f072011-07-11 15:59:22 -0700833pid_t Thread::getTid() const
834{
835 // mTid is not defined until the child initializes it, and the caller may need it earlier
836 Mutex::Autolock _l(mLock);
837 pid_t tid;
838 if (mRunning) {
839 pthread_t pthread = android_thread_id_t_to_pthread(mThread);
Elliott Hughes7bf5f202014-09-12 10:19:08 -0700840 tid = pthread_gettid_np(pthread);
Glenn Kastend731f072011-07-11 15:59:22 -0700841 } else {
842 ALOGW("Thread (this=%p): getTid() is undefined before run()", this);
843 tid = -1;
844 }
845 return tid;
846}
847#endif
848
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800849bool Thread::exitPending() const
850{
Glenn Kasten966a48f2011-02-01 11:32:29 -0800851 Mutex::Autolock _l(mLock);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800852 return mExitPending;
853}
854
855
856
857}; // namespace android