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