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