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