The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 Project | 7a4c839 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 17 | // #define LOG_NDEBUG 0 |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | #define LOG_TAG "libutils.threads" |
| 19 | |
Mark Salyzyn | 5bed803 | 2014-04-30 11:10:46 -0700 | [diff] [blame] | 20 | #include <assert.h> |
Mathias Agopian | 22dbf39 | 2017-02-28 15:06:51 -0800 | [diff] [blame] | 21 | #include <utils/AndroidThreads.h> |
Rick Yiu | f7f4442 | 2019-12-26 19:35:03 +0800 | [diff] [blame] | 22 | #include <utils/Thread.h> |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 24 | #if !defined(_WIN32) |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | # include <sys/resource.h> |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 26 | #else |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | # 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 Hughes | 292ccd3 | 2014-12-15 12:52:53 -0800 | [diff] [blame] | 33 | #if defined(__linux__) |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | #include <sys/prctl.h> |
| 35 | #endif |
| 36 | |
Mark Salyzyn | 5bed803 | 2014-04-30 11:10:46 -0700 | [diff] [blame] | 37 | #include <utils/Log.h> |
| 38 | |
Rick Yiu | f7f4442 | 2019-12-26 19:35:03 +0800 | [diff] [blame] | 39 | #if defined(__ANDROID__) |
| 40 | #include <processgroup/processgroup.h> |
Suren Baghdasaryan | 0284333 | 2018-12-21 12:30:16 -0800 | [diff] [blame] | 41 | #include <processgroup/sched_policy.h> |
Rick Yiu | f7f4442 | 2019-12-26 19:35:03 +0800 | [diff] [blame] | 42 | #endif |
Mark Salyzyn | 5bed803 | 2014-04-30 11:10:46 -0700 | [diff] [blame] | 43 | |
Elliott Hughes | 9b828ad | 2015-07-30 08:47:35 -0700 | [diff] [blame] | 44 | #if defined(__ANDROID__) |
Mark Salyzyn | 5bed803 | 2014-04-30 11:10:46 -0700 | [diff] [blame] | 45 | # define __android_unused |
| 46 | #else |
| 47 | # define __android_unused __attribute__((__unused__)) |
| 48 | #endif |
| 49 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | /* |
| 51 | * =========================================================================== |
| 52 | * Thread wrappers |
| 53 | * =========================================================================== |
| 54 | */ |
| 55 | |
| 56 | using namespace android; |
| 57 | |
| 58 | // ---------------------------------------------------------------------------- |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 59 | #if !defined(_WIN32) |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | // ---------------------------------------------------------------------------- |
| 61 | |
| 62 | /* |
Dianne Hackborn | 16d217e | 2010-09-03 17:07:07 -0700 | [diff] [blame] | 63 | * Create and run a new thread. |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | * |
| 65 | * We create it "detached", so it cleans up after itself. |
| 66 | */ |
| 67 | |
| 68 | typedef void* (*android_pthread_entry)(void*); |
| 69 | |
Rick Yiu | f7f4442 | 2019-12-26 19:35:03 +0800 | [diff] [blame] | 70 | #if defined(__ANDROID__) |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | struct thread_data_t { |
| 72 | thread_func_t entryFunction; |
| 73 | void* userData; |
| 74 | int priority; |
| 75 | char * threadName; |
| 76 | |
| 77 | // we use this trampoline when we need to set the priority with |
Glenn Kasten | d731f07 | 2011-07-11 15:59:22 -0700 | [diff] [blame] | 78 | // nice/setpriority, and name with prctl. |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | static int trampoline(const thread_data_t* t) { |
| 80 | thread_func_t f = t->entryFunction; |
| 81 | void* u = t->userData; |
| 82 | int prio = t->priority; |
| 83 | char * name = t->threadName; |
| 84 | delete t; |
| 85 | setpriority(PRIO_PROCESS, 0, prio); |
Rick Yiu | f7f4442 | 2019-12-26 19:35:03 +0800 | [diff] [blame] | 86 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | if (name) { |
Mathias Agopian | 6090df8 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 88 | androidSetThreadName(name); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | free(name); |
| 90 | } |
| 91 | return f(u); |
| 92 | } |
| 93 | }; |
Rick Yiu | f7f4442 | 2019-12-26 19:35:03 +0800 | [diff] [blame] | 94 | #endif |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | |
Mathias Agopian | 6090df8 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 96 | void androidSetThreadName(const char* name) { |
Elliott Hughes | 292ccd3 | 2014-12-15 12:52:53 -0800 | [diff] [blame] | 97 | #if defined(__linux__) |
Mathias Agopian | 6090df8 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 98 | // 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 Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | int androidCreateRawThreadEtc(android_thread_func_t entryFunction, |
| 118 | void *userData, |
Mark Salyzyn | 5bed803 | 2014-04-30 11:10:46 -0700 | [diff] [blame] | 119 | const char* threadName __android_unused, |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | int32_t threadPriority, |
| 121 | size_t threadStackSize, |
| 122 | android_thread_id_t *threadId) |
| 123 | { |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 124 | pthread_attr_t attr; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | pthread_attr_init(&attr); |
| 126 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 127 | |
Elliott Hughes | 9b828ad | 2015-07-30 08:47:35 -0700 | [diff] [blame] | 128 | #if defined(__ANDROID__) /* valgrind is rejecting RT-priority create reqs */ |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | if (threadPriority != PRIORITY_DEFAULT || threadName != NULL) { |
Glenn Kasten | d731f07 | 2011-07-11 15:59:22 -0700 | [diff] [blame] | 130 | // 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 Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | 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 Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 143 | userData = t; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | } |
| 145 | #endif |
| 146 | |
| 147 | if (threadStackSize) { |
| 148 | pthread_attr_setstacksize(&attr, threadStackSize); |
| 149 | } |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 150 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | errno = 0; |
| 152 | pthread_t thread; |
| 153 | int result = pthread_create(&thread, &attr, |
| 154 | (android_pthread_entry)entryFunction, userData); |
Le-Chun Wu | d8734d1 | 2011-07-14 14:27:18 -0700 | [diff] [blame] | 155 | pthread_attr_destroy(&attr); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | if (result != 0) { |
Elliott Hughes | 6ed68cc | 2015-06-30 08:22:24 -0700 | [diff] [blame] | 157 | ALOGE("androidCreateRawThreadEtc failed (entry=%p, res=%d, %s)\n" |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | "(android threadPriority=%d)", |
Elliott Hughes | 6ed68cc | 2015-06-30 08:22:24 -0700 | [diff] [blame] | 159 | entryFunction, result, strerror(errno), threadPriority); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | return 0; |
| 161 | } |
| 162 | |
Glenn Kasten | a538e26 | 2011-06-02 08:59:28 -0700 | [diff] [blame] | 163 | // 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 Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 166 | if (threadId != nullptr) { |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | *threadId = (android_thread_id_t)thread; // XXX: this is not portable |
| 168 | } |
| 169 | return 1; |
| 170 | } |
| 171 | |
Elliott Hughes | 9b828ad | 2015-07-30 08:47:35 -0700 | [diff] [blame] | 172 | #if defined(__ANDROID__) |
Glenn Kasten | d731f07 | 2011-07-11 15:59:22 -0700 | [diff] [blame] | 173 | static 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 Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | android_thread_id_t androidGetThreadId() |
| 180 | { |
| 181 | return (android_thread_id_t)pthread_self(); |
| 182 | } |
| 183 | |
| 184 | // ---------------------------------------------------------------------------- |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 185 | #else // !defined(_WIN32) |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | // ---------------------------------------------------------------------------- |
| 187 | |
| 188 | /* |
| 189 | * Trampoline to make us __stdcall-compliant. |
| 190 | * |
| 191 | * We're expected to delete "vDetails" when we're done. |
| 192 | */ |
| 193 | struct threadDetails { |
| 194 | int (*func)(void*); |
| 195 | void* arg; |
| 196 | }; |
| 197 | static __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 Block | 8b4cf77 | 2011-10-12 17:27:03 +0100 | [diff] [blame] | 206 | ALOG(LOG_VERBOSE, "thread", "thread exiting\n"); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | return (unsigned int) result; |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * Create and run a new thread. |
| 212 | */ |
| 213 | static 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 Block | 8b4cf77 | 2011-10-12 17:27:03 +0100 | [diff] [blame] | 233 | ALOG(LOG_WARN, "thread", "WARNING: thread create failed\n"); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | 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 | |
| 249 | int androidCreateRawThreadEtc(android_thread_func_t fn, |
| 250 | void *userData, |
Mark Salyzyn | 5bed803 | 2014-04-30 11:10:46 -0700 | [diff] [blame] | 251 | const char* /*threadName*/, |
| 252 | int32_t /*threadPriority*/, |
| 253 | size_t /*threadStackSize*/, |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | android_thread_id_t *threadId) |
| 255 | { |
| 256 | return doCreateThread( fn, userData, threadId); |
| 257 | } |
| 258 | |
| 259 | android_thread_id_t androidGetThreadId() |
| 260 | { |
| 261 | return (android_thread_id_t)GetCurrentThreadId(); |
| 262 | } |
| 263 | |
| 264 | // ---------------------------------------------------------------------------- |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 265 | #endif // !defined(_WIN32) |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | |
| 267 | // ---------------------------------------------------------------------------- |
| 268 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 269 | int androidCreateThread(android_thread_func_t fn, void* arg) |
| 270 | { |
| 271 | return createThreadEtc(fn, arg); |
| 272 | } |
| 273 | |
| 274 | int 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 | |
| 280 | static android_create_thread_fn gCreateThreadFn = androidCreateRawThreadEtc; |
| 281 | |
| 282 | int 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 | |
| 293 | void androidSetCreateThreadFunc(android_create_thread_fn func) |
| 294 | { |
| 295 | gCreateThreadFn = func; |
| 296 | } |
| 297 | |
Elliott Hughes | 9b828ad | 2015-07-30 08:47:35 -0700 | [diff] [blame] | 298 | #if defined(__ANDROID__) |
Rick Yiu | fa02bb9 | 2020-09-27 11:21:11 +0800 | [diff] [blame] | 299 | int androidSetThreadPriority(pid_t tid, int pri) |
| 300 | { |
Dianne Hackborn | 235af97 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 301 | int rc = 0; |
Rick Yiu | f7f4442 | 2019-12-26 19:35:03 +0800 | [diff] [blame] | 302 | int curr_pri = getpriority(PRIO_PROCESS, tid); |
| 303 | |
| 304 | if (curr_pri == pri) { |
| 305 | return rc; |
| 306 | } |
Dianne Hackborn | 235af97 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 307 | |
Dianne Hackborn | 235af97 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 308 | if (setpriority(PRIO_PROCESS, tid, pri) < 0) { |
| 309 | rc = INVALID_OPERATION; |
| 310 | } else { |
Greg Kaiser | 044be6b | 2022-02-08 07:37:13 -0800 | [diff] [blame] | 311 | errno = 0; |
Dianne Hackborn | 235af97 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 312 | } |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 313 | |
Dianne Hackborn | 235af97 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 314 | return rc; |
| 315 | } |
| 316 | |
Andreas Huber | 8ddbed9 | 2011-09-15 12:21:40 -0700 | [diff] [blame] | 317 | int androidGetThreadPriority(pid_t tid) { |
| 318 | return getpriority(PRIO_PROCESS, tid); |
| 319 | } |
| 320 | |
Jeff Brown | 27e6eaa | 2012-03-16 22:18:39 -0700 | [diff] [blame] | 321 | #endif |
Glenn Kasten | 6fbe0a8 | 2011-06-22 16:20:37 -0700 | [diff] [blame] | 322 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | namespace android { |
| 324 | |
| 325 | /* |
| 326 | * =========================================================================== |
| 327 | * Mutex class |
| 328 | * =========================================================================== |
| 329 | */ |
| 330 | |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 331 | #if !defined(_WIN32) |
Mathias Agopian | 1555436 | 2009-07-12 23:11:20 -0700 | [diff] [blame] | 332 | // implemented as inlines in threads.h |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 333 | #else |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 | |
| 335 | Mutex::Mutex() |
| 336 | { |
| 337 | HANDLE hMutex; |
| 338 | |
| 339 | assert(sizeof(hMutex) == sizeof(mState)); |
| 340 | |
| 341 | hMutex = CreateMutex(NULL, FALSE, NULL); |
| 342 | mState = (void*) hMutex; |
| 343 | } |
| 344 | |
Dan Willemsen | 528f144 | 2017-11-29 18:06:11 -0800 | [diff] [blame] | 345 | Mutex::Mutex(const char* /*name*/) |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 346 | { |
| 347 | // XXX: name not used for now |
| 348 | HANDLE hMutex; |
| 349 | |
David 'Digit' Turner | 9bafd12 | 2009-08-01 00:20:17 +0200 | [diff] [blame] | 350 | assert(sizeof(hMutex) == sizeof(mState)); |
| 351 | |
| 352 | hMutex = CreateMutex(NULL, FALSE, NULL); |
| 353 | mState = (void*) hMutex; |
| 354 | } |
| 355 | |
Dan Willemsen | 528f144 | 2017-11-29 18:06:11 -0800 | [diff] [blame] | 356 | Mutex::Mutex(int /*type*/, const char* /*name*/) |
David 'Digit' Turner | 9bafd12 | 2009-08-01 00:20:17 +0200 | [diff] [blame] | 357 | { |
| 358 | // XXX: type and name not used for now |
| 359 | HANDLE hMutex; |
| 360 | |
| 361 | assert(sizeof(hMutex) == sizeof(mState)); |
| 362 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | hMutex = CreateMutex(NULL, FALSE, NULL); |
| 364 | mState = (void*) hMutex; |
| 365 | } |
| 366 | |
| 367 | Mutex::~Mutex() |
| 368 | { |
| 369 | CloseHandle((HANDLE) mState); |
| 370 | } |
| 371 | |
| 372 | status_t Mutex::lock() |
| 373 | { |
| 374 | DWORD dwWaitResult; |
| 375 | dwWaitResult = WaitForSingleObject((HANDLE) mState, INFINITE); |
Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 376 | return dwWaitResult != WAIT_OBJECT_0 ? -1 : OK; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | void Mutex::unlock() |
| 380 | { |
| 381 | if (!ReleaseMutex((HANDLE) mState)) |
Steve Block | 8b4cf77 | 2011-10-12 17:27:03 +0100 | [diff] [blame] | 382 | ALOG(LOG_WARN, "thread", "WARNING: bad result from unlocking mutex\n"); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | status_t Mutex::tryLock() |
| 386 | { |
| 387 | DWORD dwWaitResult; |
| 388 | |
| 389 | dwWaitResult = WaitForSingleObject((HANDLE) mState, 0); |
| 390 | if (dwWaitResult != WAIT_OBJECT_0 && dwWaitResult != WAIT_TIMEOUT) |
Steve Block | 8b4cf77 | 2011-10-12 17:27:03 +0100 | [diff] [blame] | 391 | ALOG(LOG_WARN, "thread", "WARNING: bad result from try-locking mutex\n"); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 392 | return (dwWaitResult == WAIT_OBJECT_0) ? 0 : -1; |
| 393 | } |
| 394 | |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 395 | #endif // !defined(_WIN32) |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 396 | |
| 397 | |
| 398 | /* |
| 399 | * =========================================================================== |
| 400 | * Condition class |
| 401 | * =========================================================================== |
| 402 | */ |
| 403 | |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 404 | #if !defined(_WIN32) |
Mathias Agopian | 1555436 | 2009-07-12 23:11:20 -0700 | [diff] [blame] | 405 | // implemented as inlines in threads.h |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 406 | #else |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 407 | |
| 408 | /* |
| 409 | * Windows doesn't have a condition variable solution. It's possible |
| 410 | * to create one, but it's easy to get it wrong. For a discussion, and |
| 411 | * the origin of this implementation, see: |
| 412 | * |
| 413 | * http://www.cs.wustl.edu/~schmidt/win32-cv-1.html |
| 414 | * |
| 415 | * The implementation shown on the page does NOT follow POSIX semantics. |
| 416 | * As an optimization they require acquiring the external mutex before |
| 417 | * calling signal() and broadcast(), whereas POSIX only requires grabbing |
| 418 | * it before calling wait(). The implementation here has been un-optimized |
| 419 | * to have the correct behavior. |
| 420 | */ |
| 421 | typedef struct WinCondition { |
| 422 | // Number of waiting threads. |
| 423 | int waitersCount; |
| 424 | |
| 425 | // Serialize access to waitersCount. |
| 426 | CRITICAL_SECTION waitersCountLock; |
| 427 | |
| 428 | // Semaphore used to queue up threads waiting for the condition to |
| 429 | // become signaled. |
| 430 | HANDLE sema; |
| 431 | |
| 432 | // An auto-reset event used by the broadcast/signal thread to wait |
| 433 | // for all the waiting thread(s) to wake up and be released from |
| 434 | // the semaphore. |
| 435 | HANDLE waitersDone; |
| 436 | |
| 437 | // This mutex wouldn't be necessary if we required that the caller |
| 438 | // lock the external mutex before calling signal() and broadcast(). |
| 439 | // I'm trying to mimic pthread semantics though. |
| 440 | HANDLE internalMutex; |
| 441 | |
| 442 | // Keeps track of whether we were broadcasting or signaling. This |
| 443 | // allows us to optimize the code if we're just signaling. |
| 444 | bool wasBroadcast; |
| 445 | |
| 446 | status_t wait(WinCondition* condState, HANDLE hMutex, nsecs_t* abstime) |
| 447 | { |
| 448 | // Increment the wait count, avoiding race conditions. |
| 449 | EnterCriticalSection(&condState->waitersCountLock); |
| 450 | condState->waitersCount++; |
| 451 | //printf("+++ wait: incr waitersCount to %d (tid=%ld)\n", |
| 452 | // condState->waitersCount, getThreadId()); |
| 453 | LeaveCriticalSection(&condState->waitersCountLock); |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 454 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 455 | DWORD timeout = INFINITE; |
| 456 | if (abstime) { |
| 457 | nsecs_t reltime = *abstime - systemTime(); |
| 458 | if (reltime < 0) |
| 459 | reltime = 0; |
| 460 | timeout = reltime/1000000; |
| 461 | } |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 462 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | // Atomically release the external mutex and wait on the semaphore. |
| 464 | DWORD res = |
| 465 | SignalObjectAndWait(hMutex, condState->sema, timeout, FALSE); |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 466 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 467 | //printf("+++ wait: awake (tid=%ld)\n", getThreadId()); |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 468 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 469 | // Reacquire lock to avoid race conditions. |
| 470 | EnterCriticalSection(&condState->waitersCountLock); |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 471 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 472 | // No longer waiting. |
| 473 | condState->waitersCount--; |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 474 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 475 | // Check to see if we're the last waiter after a broadcast. |
| 476 | bool lastWaiter = (condState->wasBroadcast && condState->waitersCount == 0); |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 477 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 478 | //printf("+++ wait: lastWaiter=%d (wasBc=%d wc=%d)\n", |
| 479 | // lastWaiter, condState->wasBroadcast, condState->waitersCount); |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 480 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 481 | LeaveCriticalSection(&condState->waitersCountLock); |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 482 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 483 | // If we're the last waiter thread during this particular broadcast |
| 484 | // then signal broadcast() that we're all awake. It'll drop the |
| 485 | // internal mutex. |
| 486 | if (lastWaiter) { |
| 487 | // Atomically signal the "waitersDone" event and wait until we |
| 488 | // can acquire the internal mutex. We want to do this in one step |
| 489 | // because it ensures that everybody is in the mutex FIFO before |
| 490 | // any thread has a chance to run. Without it, another thread |
| 491 | // could wake up, do work, and hop back in ahead of us. |
| 492 | SignalObjectAndWait(condState->waitersDone, condState->internalMutex, |
| 493 | INFINITE, FALSE); |
| 494 | } else { |
| 495 | // Grab the internal mutex. |
| 496 | WaitForSingleObject(condState->internalMutex, INFINITE); |
| 497 | } |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 498 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 499 | // Release the internal and grab the external. |
| 500 | ReleaseMutex(condState->internalMutex); |
| 501 | WaitForSingleObject(hMutex, INFINITE); |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 502 | |
Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 503 | return res == WAIT_OBJECT_0 ? OK : -1; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 504 | } |
| 505 | } WinCondition; |
| 506 | |
| 507 | /* |
| 508 | * Constructor. Set up the WinCondition stuff. |
| 509 | */ |
| 510 | Condition::Condition() |
| 511 | { |
| 512 | WinCondition* condState = new WinCondition; |
| 513 | |
| 514 | condState->waitersCount = 0; |
| 515 | condState->wasBroadcast = false; |
| 516 | // semaphore: no security, initial value of 0 |
| 517 | condState->sema = CreateSemaphore(NULL, 0, 0x7fffffff, NULL); |
| 518 | InitializeCriticalSection(&condState->waitersCountLock); |
| 519 | // auto-reset event, not signaled initially |
| 520 | condState->waitersDone = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 521 | // used so we don't have to lock external mutex on signal/broadcast |
| 522 | condState->internalMutex = CreateMutex(NULL, FALSE, NULL); |
| 523 | |
| 524 | mState = condState; |
| 525 | } |
| 526 | |
| 527 | /* |
| 528 | * Destructor. Free Windows resources as well as our allocated storage. |
| 529 | */ |
| 530 | Condition::~Condition() |
| 531 | { |
| 532 | WinCondition* condState = (WinCondition*) mState; |
| 533 | if (condState != NULL) { |
| 534 | CloseHandle(condState->sema); |
| 535 | CloseHandle(condState->waitersDone); |
| 536 | delete condState; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | |
| 541 | status_t Condition::wait(Mutex& mutex) |
| 542 | { |
| 543 | WinCondition* condState = (WinCondition*) mState; |
| 544 | HANDLE hMutex = (HANDLE) mutex.mState; |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 545 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 546 | return ((WinCondition*)mState)->wait(condState, hMutex, NULL); |
| 547 | } |
| 548 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | status_t Condition::waitRelative(Mutex& mutex, nsecs_t reltime) |
| 550 | { |
David 'Digit' Turner | 9bafd12 | 2009-08-01 00:20:17 +0200 | [diff] [blame] | 551 | WinCondition* condState = (WinCondition*) mState; |
| 552 | HANDLE hMutex = (HANDLE) mutex.mState; |
| 553 | nsecs_t absTime = systemTime()+reltime; |
| 554 | |
| 555 | return ((WinCondition*)mState)->wait(condState, hMutex, &absTime); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | /* |
| 559 | * Signal the condition variable, allowing one thread to continue. |
| 560 | */ |
| 561 | void Condition::signal() |
| 562 | { |
| 563 | WinCondition* condState = (WinCondition*) mState; |
| 564 | |
| 565 | // Lock the internal mutex. This ensures that we don't clash with |
| 566 | // broadcast(). |
| 567 | WaitForSingleObject(condState->internalMutex, INFINITE); |
| 568 | |
| 569 | EnterCriticalSection(&condState->waitersCountLock); |
| 570 | bool haveWaiters = (condState->waitersCount > 0); |
| 571 | LeaveCriticalSection(&condState->waitersCountLock); |
| 572 | |
| 573 | // If no waiters, then this is a no-op. Otherwise, knock the semaphore |
| 574 | // down a notch. |
| 575 | if (haveWaiters) |
| 576 | ReleaseSemaphore(condState->sema, 1, 0); |
| 577 | |
| 578 | // Release internal mutex. |
| 579 | ReleaseMutex(condState->internalMutex); |
| 580 | } |
| 581 | |
| 582 | /* |
| 583 | * Signal the condition variable, allowing all threads to continue. |
| 584 | * |
| 585 | * First we have to wake up all threads waiting on the semaphore, then |
| 586 | * we wait until all of the threads have actually been woken before |
| 587 | * releasing the internal mutex. This ensures that all threads are woken. |
| 588 | */ |
| 589 | void Condition::broadcast() |
| 590 | { |
| 591 | WinCondition* condState = (WinCondition*) mState; |
| 592 | |
| 593 | // Lock the internal mutex. This keeps the guys we're waking up |
| 594 | // from getting too far. |
| 595 | WaitForSingleObject(condState->internalMutex, INFINITE); |
| 596 | |
| 597 | EnterCriticalSection(&condState->waitersCountLock); |
| 598 | bool haveWaiters = false; |
| 599 | |
| 600 | if (condState->waitersCount > 0) { |
| 601 | haveWaiters = true; |
| 602 | condState->wasBroadcast = true; |
| 603 | } |
| 604 | |
| 605 | if (haveWaiters) { |
| 606 | // Wake up all the waiters. |
| 607 | ReleaseSemaphore(condState->sema, condState->waitersCount, 0); |
| 608 | |
| 609 | LeaveCriticalSection(&condState->waitersCountLock); |
| 610 | |
| 611 | // Wait for all awakened threads to acquire the counting semaphore. |
| 612 | // The last guy who was waiting sets this. |
| 613 | WaitForSingleObject(condState->waitersDone, INFINITE); |
| 614 | |
| 615 | // Reset wasBroadcast. (No crit section needed because nobody |
| 616 | // else can wake up to poke at it.) |
| 617 | condState->wasBroadcast = 0; |
| 618 | } else { |
| 619 | // nothing to do |
| 620 | LeaveCriticalSection(&condState->waitersCountLock); |
| 621 | } |
| 622 | |
| 623 | // Release internal mutex. |
| 624 | ReleaseMutex(condState->internalMutex); |
| 625 | } |
| 626 | |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 627 | #endif // !defined(_WIN32) |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 628 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 629 | // ---------------------------------------------------------------------------- |
| 630 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 631 | /* |
| 632 | * This is our thread object! |
| 633 | */ |
| 634 | |
| 635 | Thread::Thread(bool canCallJava) |
Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 636 | : mCanCallJava(canCallJava), |
| 637 | mThread(thread_id_t(-1)), |
| 638 | mLock("Thread::mLock"), |
| 639 | mStatus(OK), |
| 640 | mExitPending(false), |
| 641 | mRunning(false) |
Elliott Hughes | 9b828ad | 2015-07-30 08:47:35 -0700 | [diff] [blame] | 642 | #if defined(__ANDROID__) |
Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 643 | , |
| 644 | mTid(-1) |
Glenn Kasten | 966a48f | 2011-02-01 11:32:29 -0800 | [diff] [blame] | 645 | #endif |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 646 | { |
| 647 | } |
| 648 | |
| 649 | Thread::~Thread() |
| 650 | { |
| 651 | } |
| 652 | |
| 653 | status_t Thread::readyToRun() |
| 654 | { |
Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 655 | return OK; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | status_t Thread::run(const char* name, int32_t priority, size_t stack) |
| 659 | { |
Brian Carlstrom | e71b914 | 2016-03-12 16:08:12 -0800 | [diff] [blame] | 660 | LOG_ALWAYS_FATAL_IF(name == nullptr, "thread name not provided to Thread::run"); |
| 661 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 662 | Mutex::Autolock _l(mLock); |
| 663 | |
| 664 | if (mRunning) { |
| 665 | // thread already started |
| 666 | return INVALID_OPERATION; |
| 667 | } |
| 668 | |
| 669 | // reset status and exitPending to their default value, so we can |
| 670 | // try again after an error happened (either below, or in readyToRun()) |
Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 671 | mStatus = OK; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 672 | mExitPending = false; |
| 673 | mThread = thread_id_t(-1); |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 674 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 675 | // hold a strong reference on ourself |
| 676 | mHoldSelf = this; |
| 677 | |
The Android Open Source Project | 7a4c839 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 678 | mRunning = true; |
| 679 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 680 | bool res; |
| 681 | if (mCanCallJava) { |
| 682 | res = createThreadEtc(_threadLoop, |
| 683 | this, name, priority, stack, &mThread); |
| 684 | } else { |
| 685 | res = androidCreateRawThreadEtc(_threadLoop, |
| 686 | this, name, priority, stack, &mThread); |
| 687 | } |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 688 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 689 | if (res == false) { |
| 690 | mStatus = UNKNOWN_ERROR; // something happened! |
| 691 | mRunning = false; |
| 692 | mThread = thread_id_t(-1); |
The Android Open Source Project | 7a4c839 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 693 | mHoldSelf.clear(); // "this" may have gone away after this. |
| 694 | |
| 695 | return UNKNOWN_ERROR; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 696 | } |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 697 | |
The Android Open Source Project | 7a4c839 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 698 | // Do not refer to mStatus here: The thread is already running (may, in fact |
Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 699 | // already have exited with a valid mStatus result). The OK indication |
The Android Open Source Project | 7a4c839 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 700 | // here merely indicates successfully starting the thread and does not |
| 701 | // imply successful termination/execution. |
Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 702 | return OK; |
Glenn Kasten | 966a48f | 2011-02-01 11:32:29 -0800 | [diff] [blame] | 703 | |
| 704 | // Exiting scope of mLock is a memory barrier and allows new thread to run |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | int Thread::_threadLoop(void* user) |
| 708 | { |
| 709 | Thread* const self = static_cast<Thread*>(user); |
Glenn Kasten | 966a48f | 2011-02-01 11:32:29 -0800 | [diff] [blame] | 710 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 711 | sp<Thread> strong(self->mHoldSelf); |
| 712 | wp<Thread> weak(strong); |
| 713 | self->mHoldSelf.clear(); |
| 714 | |
Elliott Hughes | 9b828ad | 2015-07-30 08:47:35 -0700 | [diff] [blame] | 715 | #if defined(__ANDROID__) |
Mathias Agopian | 51ce3ad | 2009-09-09 02:38:13 -0700 | [diff] [blame] | 716 | // this is very useful for debugging with gdb |
| 717 | self->mTid = gettid(); |
| 718 | #endif |
| 719 | |
The Android Open Source Project | 7a4c839 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 720 | bool first = true; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 721 | |
| 722 | do { |
The Android Open Source Project | 7a4c839 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 723 | bool result; |
| 724 | if (first) { |
| 725 | first = false; |
| 726 | self->mStatus = self->readyToRun(); |
Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 727 | result = (self->mStatus == OK); |
The Android Open Source Project | 7a4c839 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 728 | |
Glenn Kasten | 966a48f | 2011-02-01 11:32:29 -0800 | [diff] [blame] | 729 | if (result && !self->exitPending()) { |
The Android Open Source Project | 7a4c839 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 730 | // Binder threads (and maybe others) rely on threadLoop |
| 731 | // running at least once after a successful ::readyToRun() |
| 732 | // (unless, of course, the thread has already been asked to exit |
| 733 | // at that point). |
| 734 | // This is because threads are essentially used like this: |
| 735 | // (new ThreadSubclass())->run(); |
| 736 | // The caller therefore does not retain a strong reference to |
| 737 | // the thread and the thread would simply disappear after the |
| 738 | // successful ::readyToRun() call instead of entering the |
| 739 | // threadLoop at least once. |
| 740 | result = self->threadLoop(); |
| 741 | } |
| 742 | } else { |
| 743 | result = self->threadLoop(); |
| 744 | } |
| 745 | |
Glenn Kasten | 966a48f | 2011-02-01 11:32:29 -0800 | [diff] [blame] | 746 | // establish a scope for mLock |
| 747 | { |
| 748 | Mutex::Autolock _l(self->mLock); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 749 | if (result == false || self->mExitPending) { |
| 750 | self->mExitPending = true; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 751 | self->mRunning = false; |
Eric Laurent | fe2c463 | 2011-01-04 11:58:04 -0800 | [diff] [blame] | 752 | // clear thread ID so that requestExitAndWait() does not exit if |
| 753 | // called by a new thread using the same thread ID as this one. |
| 754 | self->mThread = thread_id_t(-1); |
Glenn Kasten | 966a48f | 2011-02-01 11:32:29 -0800 | [diff] [blame] | 755 | // note that interested observers blocked in requestExitAndWait are |
| 756 | // awoken by broadcast, but blocked on mLock until break exits scope |
Mathias Agopian | 51ce3ad | 2009-09-09 02:38:13 -0700 | [diff] [blame] | 757 | self->mThreadExitedCondition.broadcast(); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 758 | break; |
| 759 | } |
Glenn Kasten | 966a48f | 2011-02-01 11:32:29 -0800 | [diff] [blame] | 760 | } |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 761 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 762 | // Release our strong reference, to let a chance to the thread |
| 763 | // to die a peaceful death. |
| 764 | strong.clear(); |
Mathias Agopian | 51ce3ad | 2009-09-09 02:38:13 -0700 | [diff] [blame] | 765 | // And immediately, re-acquire a strong reference for the next loop |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 766 | strong = weak.promote(); |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 767 | } while(strong != nullptr); |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 768 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | void Thread::requestExit() |
| 773 | { |
Glenn Kasten | 966a48f | 2011-02-01 11:32:29 -0800 | [diff] [blame] | 774 | Mutex::Autolock _l(mLock); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 775 | mExitPending = true; |
| 776 | } |
| 777 | |
| 778 | status_t Thread::requestExitAndWait() |
| 779 | { |
Glenn Kasten | a538e26 | 2011-06-02 08:59:28 -0700 | [diff] [blame] | 780 | Mutex::Autolock _l(mLock); |
The Android Open Source Project | 7a4c839 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 781 | if (mThread == getThreadId()) { |
Steve Block | 61d341b | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 782 | ALOGW( |
The Android Open Source Project | 7a4c839 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 783 | "Thread (this=%p): don't call waitForExit() from this " |
| 784 | "Thread object's thread. It's a guaranteed deadlock!", |
| 785 | this); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 786 | |
The Android Open Source Project | 7a4c839 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 787 | return WOULD_BLOCK; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 788 | } |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 789 | |
Glenn Kasten | a538e26 | 2011-06-02 08:59:28 -0700 | [diff] [blame] | 790 | mExitPending = true; |
The Android Open Source Project | 7a4c839 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 791 | |
The Android Open Source Project | 7a4c839 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 792 | while (mRunning == true) { |
| 793 | mThreadExitedCondition.wait(mLock); |
| 794 | } |
Glenn Kasten | 966a48f | 2011-02-01 11:32:29 -0800 | [diff] [blame] | 795 | // This next line is probably not needed any more, but is being left for |
| 796 | // historical reference. Note that each interested party will clear flag. |
The Android Open Source Project | 7a4c839 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 797 | mExitPending = false; |
| 798 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 799 | return mStatus; |
| 800 | } |
| 801 | |
Glenn Kasten | 6839e8e | 2011-06-23 12:55:29 -0700 | [diff] [blame] | 802 | status_t Thread::join() |
| 803 | { |
| 804 | Mutex::Autolock _l(mLock); |
| 805 | if (mThread == getThreadId()) { |
Steve Block | 61d341b | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 806 | ALOGW( |
Glenn Kasten | 6839e8e | 2011-06-23 12:55:29 -0700 | [diff] [blame] | 807 | "Thread (this=%p): don't call join() from this " |
| 808 | "Thread object's thread. It's a guaranteed deadlock!", |
| 809 | this); |
| 810 | |
| 811 | return WOULD_BLOCK; |
| 812 | } |
| 813 | |
| 814 | while (mRunning == true) { |
| 815 | mThreadExitedCondition.wait(mLock); |
| 816 | } |
| 817 | |
| 818 | return mStatus; |
| 819 | } |
| 820 | |
Romain Guy | 31ba37f | 2013-03-11 14:34:56 -0700 | [diff] [blame] | 821 | bool Thread::isRunning() const { |
| 822 | Mutex::Autolock _l(mLock); |
| 823 | return mRunning; |
| 824 | } |
| 825 | |
Elliott Hughes | 9b828ad | 2015-07-30 08:47:35 -0700 | [diff] [blame] | 826 | #if defined(__ANDROID__) |
Glenn Kasten | d731f07 | 2011-07-11 15:59:22 -0700 | [diff] [blame] | 827 | pid_t Thread::getTid() const |
| 828 | { |
| 829 | // mTid is not defined until the child initializes it, and the caller may need it earlier |
| 830 | Mutex::Autolock _l(mLock); |
| 831 | pid_t tid; |
| 832 | if (mRunning) { |
| 833 | pthread_t pthread = android_thread_id_t_to_pthread(mThread); |
Elliott Hughes | 7bf5f20 | 2014-09-12 10:19:08 -0700 | [diff] [blame] | 834 | tid = pthread_gettid_np(pthread); |
Glenn Kasten | d731f07 | 2011-07-11 15:59:22 -0700 | [diff] [blame] | 835 | } else { |
| 836 | ALOGW("Thread (this=%p): getTid() is undefined before run()", this); |
| 837 | tid = -1; |
| 838 | } |
| 839 | return tid; |
| 840 | } |
| 841 | #endif |
| 842 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 843 | bool Thread::exitPending() const |
| 844 | { |
Glenn Kasten | 966a48f | 2011-02-01 11:32:29 -0800 | [diff] [blame] | 845 | Mutex::Autolock _l(mLock); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 846 | return mExitPending; |
| 847 | } |
| 848 | |
| 849 | |
| 850 | |
| 851 | }; // namespace android |