Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (C) 2007 The Android Open Source Project |
michael@0 | 3 | * |
michael@0 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
michael@0 | 5 | * you may not use this file except in compliance with the License. |
michael@0 | 6 | * You may obtain a copy of the License at |
michael@0 | 7 | * |
michael@0 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 9 | * |
michael@0 | 10 | * Unless required by applicable law or agreed to in writing, software |
michael@0 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
michael@0 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
michael@0 | 13 | * See the License for the specific language governing permissions and |
michael@0 | 14 | * limitations under the License. |
michael@0 | 15 | */ |
michael@0 | 16 | |
michael@0 | 17 | #ifndef _LIBS_UTILS_THREADS_H |
michael@0 | 18 | #define _LIBS_UTILS_THREADS_H |
michael@0 | 19 | |
michael@0 | 20 | #include <stdint.h> |
michael@0 | 21 | #include <sys/types.h> |
michael@0 | 22 | #include <time.h> |
michael@0 | 23 | |
michael@0 | 24 | #if defined(HAVE_PTHREADS) |
michael@0 | 25 | # include <pthread.h> |
michael@0 | 26 | #endif |
michael@0 | 27 | |
michael@0 | 28 | // ------------------------------------------------------------------ |
michael@0 | 29 | // C API |
michael@0 | 30 | |
michael@0 | 31 | #ifdef __cplusplus |
michael@0 | 32 | extern "C" { |
michael@0 | 33 | #endif |
michael@0 | 34 | |
michael@0 | 35 | typedef void* android_thread_id_t; |
michael@0 | 36 | |
michael@0 | 37 | typedef int (*android_thread_func_t)(void*); |
michael@0 | 38 | |
michael@0 | 39 | enum { |
michael@0 | 40 | /* |
michael@0 | 41 | * *********************************************** |
michael@0 | 42 | * ** Keep in sync with android.os.Process.java ** |
michael@0 | 43 | * *********************************************** |
michael@0 | 44 | * |
michael@0 | 45 | * This maps directly to the "nice" priorites we use in Android. |
michael@0 | 46 | * A thread priority should be chosen inverse-proportinally to |
michael@0 | 47 | * the amount of work the thread is expected to do. The more work |
michael@0 | 48 | * a thread will do, the less favorable priority it should get so that |
michael@0 | 49 | * it doesn't starve the system. Threads not behaving properly might |
michael@0 | 50 | * be "punished" by the kernel. |
michael@0 | 51 | * Use the levels below when appropriate. Intermediate values are |
michael@0 | 52 | * acceptable, preferably use the {MORE|LESS}_FAVORABLE constants below. |
michael@0 | 53 | */ |
michael@0 | 54 | ANDROID_PRIORITY_LOWEST = 19, |
michael@0 | 55 | |
michael@0 | 56 | /* use for background tasks */ |
michael@0 | 57 | ANDROID_PRIORITY_BACKGROUND = 10, |
michael@0 | 58 | |
michael@0 | 59 | /* most threads run at normal priority */ |
michael@0 | 60 | ANDROID_PRIORITY_NORMAL = 0, |
michael@0 | 61 | |
michael@0 | 62 | /* threads currently running a UI that the user is interacting with */ |
michael@0 | 63 | ANDROID_PRIORITY_FOREGROUND = -2, |
michael@0 | 64 | |
michael@0 | 65 | /* the main UI thread has a slightly more favorable priority */ |
michael@0 | 66 | ANDROID_PRIORITY_DISPLAY = -4, |
michael@0 | 67 | |
michael@0 | 68 | /* ui service treads might want to run at a urgent display (uncommon) */ |
michael@0 | 69 | ANDROID_PRIORITY_URGENT_DISPLAY = -8, |
michael@0 | 70 | |
michael@0 | 71 | /* all normal audio threads */ |
michael@0 | 72 | ANDROID_PRIORITY_AUDIO = -16, |
michael@0 | 73 | |
michael@0 | 74 | /* service audio threads (uncommon) */ |
michael@0 | 75 | ANDROID_PRIORITY_URGENT_AUDIO = -19, |
michael@0 | 76 | |
michael@0 | 77 | /* should never be used in practice. regular process might not |
michael@0 | 78 | * be allowed to use this level */ |
michael@0 | 79 | ANDROID_PRIORITY_HIGHEST = -20, |
michael@0 | 80 | |
michael@0 | 81 | ANDROID_PRIORITY_DEFAULT = ANDROID_PRIORITY_NORMAL, |
michael@0 | 82 | ANDROID_PRIORITY_MORE_FAVORABLE = -1, |
michael@0 | 83 | ANDROID_PRIORITY_LESS_FAVORABLE = +1, |
michael@0 | 84 | }; |
michael@0 | 85 | |
michael@0 | 86 | enum { |
michael@0 | 87 | ANDROID_TGROUP_DEFAULT = 0, |
michael@0 | 88 | ANDROID_TGROUP_BG_NONINTERACT = 1, |
michael@0 | 89 | ANDROID_TGROUP_FG_BOOST = 2, |
michael@0 | 90 | ANDROID_TGROUP_MAX = ANDROID_TGROUP_FG_BOOST, |
michael@0 | 91 | }; |
michael@0 | 92 | |
michael@0 | 93 | // Create and run a new thread. |
michael@0 | 94 | extern int androidCreateThread(android_thread_func_t, void *); |
michael@0 | 95 | |
michael@0 | 96 | // Create thread with lots of parameters |
michael@0 | 97 | extern int androidCreateThreadEtc(android_thread_func_t entryFunction, |
michael@0 | 98 | void *userData, |
michael@0 | 99 | const char* threadName, |
michael@0 | 100 | int32_t threadPriority, |
michael@0 | 101 | size_t threadStackSize, |
michael@0 | 102 | android_thread_id_t *threadId); |
michael@0 | 103 | |
michael@0 | 104 | // Get some sort of unique identifier for the current thread. |
michael@0 | 105 | extern android_thread_id_t androidGetThreadId(); |
michael@0 | 106 | |
michael@0 | 107 | // Low-level thread creation -- never creates threads that can |
michael@0 | 108 | // interact with the Java VM. |
michael@0 | 109 | extern int androidCreateRawThreadEtc(android_thread_func_t entryFunction, |
michael@0 | 110 | void *userData, |
michael@0 | 111 | const char* threadName, |
michael@0 | 112 | int32_t threadPriority, |
michael@0 | 113 | size_t threadStackSize, |
michael@0 | 114 | android_thread_id_t *threadId); |
michael@0 | 115 | |
michael@0 | 116 | // Used by the Java Runtime to control how threads are created, so that |
michael@0 | 117 | // they can be proper and lovely Java threads. |
michael@0 | 118 | typedef int (*android_create_thread_fn)(android_thread_func_t entryFunction, |
michael@0 | 119 | void *userData, |
michael@0 | 120 | const char* threadName, |
michael@0 | 121 | int32_t threadPriority, |
michael@0 | 122 | size_t threadStackSize, |
michael@0 | 123 | android_thread_id_t *threadId); |
michael@0 | 124 | |
michael@0 | 125 | extern void androidSetCreateThreadFunc(android_create_thread_fn func); |
michael@0 | 126 | |
michael@0 | 127 | // ------------------------------------------------------------------ |
michael@0 | 128 | // Extra functions working with raw pids. |
michael@0 | 129 | |
michael@0 | 130 | // Get pid for the current thread. |
michael@0 | 131 | extern pid_t androidGetTid(); |
michael@0 | 132 | |
michael@0 | 133 | // Change the scheduling group of a particular thread. The group |
michael@0 | 134 | // should be one of the ANDROID_TGROUP constants. Returns BAD_VALUE if |
michael@0 | 135 | // grp is out of range, else another non-zero value with errno set if |
michael@0 | 136 | // the operation failed. |
michael@0 | 137 | extern int androidSetThreadSchedulingGroup(pid_t tid, int grp); |
michael@0 | 138 | |
michael@0 | 139 | // Change the priority AND scheduling group of a particular thread. The priority |
michael@0 | 140 | // should be one of the ANDROID_PRIORITY constants. Returns INVALID_OPERATION |
michael@0 | 141 | // if the priority set failed, else another value if just the group set failed; |
michael@0 | 142 | // in either case errno is set. |
michael@0 | 143 | extern int androidSetThreadPriority(pid_t tid, int prio); |
michael@0 | 144 | |
michael@0 | 145 | #ifdef __cplusplus |
michael@0 | 146 | } |
michael@0 | 147 | #endif |
michael@0 | 148 | |
michael@0 | 149 | // ------------------------------------------------------------------ |
michael@0 | 150 | // C++ API |
michael@0 | 151 | |
michael@0 | 152 | #ifdef __cplusplus |
michael@0 | 153 | |
michael@0 | 154 | #include <utils/Errors.h> |
michael@0 | 155 | #include <utils/RefBase.h> |
michael@0 | 156 | #include <utils/Timers.h> |
michael@0 | 157 | |
michael@0 | 158 | namespace android { |
michael@0 | 159 | |
michael@0 | 160 | typedef android_thread_id_t thread_id_t; |
michael@0 | 161 | |
michael@0 | 162 | typedef android_thread_func_t thread_func_t; |
michael@0 | 163 | |
michael@0 | 164 | enum { |
michael@0 | 165 | PRIORITY_LOWEST = ANDROID_PRIORITY_LOWEST, |
michael@0 | 166 | PRIORITY_BACKGROUND = ANDROID_PRIORITY_BACKGROUND, |
michael@0 | 167 | PRIORITY_NORMAL = ANDROID_PRIORITY_NORMAL, |
michael@0 | 168 | PRIORITY_FOREGROUND = ANDROID_PRIORITY_FOREGROUND, |
michael@0 | 169 | PRIORITY_DISPLAY = ANDROID_PRIORITY_DISPLAY, |
michael@0 | 170 | PRIORITY_URGENT_DISPLAY = ANDROID_PRIORITY_URGENT_DISPLAY, |
michael@0 | 171 | PRIORITY_AUDIO = ANDROID_PRIORITY_AUDIO, |
michael@0 | 172 | PRIORITY_URGENT_AUDIO = ANDROID_PRIORITY_URGENT_AUDIO, |
michael@0 | 173 | PRIORITY_HIGHEST = ANDROID_PRIORITY_HIGHEST, |
michael@0 | 174 | PRIORITY_DEFAULT = ANDROID_PRIORITY_DEFAULT, |
michael@0 | 175 | PRIORITY_MORE_FAVORABLE = ANDROID_PRIORITY_MORE_FAVORABLE, |
michael@0 | 176 | PRIORITY_LESS_FAVORABLE = ANDROID_PRIORITY_LESS_FAVORABLE, |
michael@0 | 177 | }; |
michael@0 | 178 | |
michael@0 | 179 | // Create and run a new thread. |
michael@0 | 180 | inline bool createThread(thread_func_t f, void *a) { |
michael@0 | 181 | return androidCreateThread(f, a) ? true : false; |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | // Create thread with lots of parameters |
michael@0 | 185 | inline bool createThreadEtc(thread_func_t entryFunction, |
michael@0 | 186 | void *userData, |
michael@0 | 187 | const char* threadName = "android:unnamed_thread", |
michael@0 | 188 | int32_t threadPriority = PRIORITY_DEFAULT, |
michael@0 | 189 | size_t threadStackSize = 0, |
michael@0 | 190 | thread_id_t *threadId = 0) |
michael@0 | 191 | { |
michael@0 | 192 | return androidCreateThreadEtc(entryFunction, userData, threadName, |
michael@0 | 193 | threadPriority, threadStackSize, threadId) ? true : false; |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | // Get some sort of unique identifier for the current thread. |
michael@0 | 197 | inline thread_id_t getThreadId() { |
michael@0 | 198 | return androidGetThreadId(); |
michael@0 | 199 | } |
michael@0 | 200 | |
michael@0 | 201 | /*****************************************************************************/ |
michael@0 | 202 | |
michael@0 | 203 | /* |
michael@0 | 204 | * Simple mutex class. The implementation is system-dependent. |
michael@0 | 205 | * |
michael@0 | 206 | * The mutex must be unlocked by the thread that locked it. They are not |
michael@0 | 207 | * recursive, i.e. the same thread can't lock it multiple times. |
michael@0 | 208 | */ |
michael@0 | 209 | class Mutex { |
michael@0 | 210 | public: |
michael@0 | 211 | enum { |
michael@0 | 212 | PRIVATE = 0, |
michael@0 | 213 | SHARED = 1 |
michael@0 | 214 | }; |
michael@0 | 215 | |
michael@0 | 216 | Mutex(); |
michael@0 | 217 | Mutex(const char* name); |
michael@0 | 218 | Mutex(int type, const char* name = NULL); |
michael@0 | 219 | ~Mutex(); |
michael@0 | 220 | |
michael@0 | 221 | // lock or unlock the mutex |
michael@0 | 222 | status_t lock(); |
michael@0 | 223 | void unlock(); |
michael@0 | 224 | |
michael@0 | 225 | // lock if possible; returns 0 on success, error otherwise |
michael@0 | 226 | status_t tryLock(); |
michael@0 | 227 | |
michael@0 | 228 | // Manages the mutex automatically. It'll be locked when Autolock is |
michael@0 | 229 | // constructed and released when Autolock goes out of scope. |
michael@0 | 230 | class Autolock { |
michael@0 | 231 | public: |
michael@0 | 232 | inline Autolock(Mutex& mutex) : mLock(mutex) { mLock.lock(); } |
michael@0 | 233 | inline Autolock(Mutex* mutex) : mLock(*mutex) { mLock.lock(); } |
michael@0 | 234 | inline ~Autolock() { mLock.unlock(); } |
michael@0 | 235 | private: |
michael@0 | 236 | Mutex& mLock; |
michael@0 | 237 | }; |
michael@0 | 238 | |
michael@0 | 239 | private: |
michael@0 | 240 | friend class Condition; |
michael@0 | 241 | |
michael@0 | 242 | // A mutex cannot be copied |
michael@0 | 243 | Mutex(const Mutex&); |
michael@0 | 244 | Mutex& operator = (const Mutex&); |
michael@0 | 245 | |
michael@0 | 246 | #if defined(HAVE_PTHREADS) |
michael@0 | 247 | pthread_mutex_t mMutex; |
michael@0 | 248 | #else |
michael@0 | 249 | void _init(); |
michael@0 | 250 | void* mState; |
michael@0 | 251 | #endif |
michael@0 | 252 | }; |
michael@0 | 253 | |
michael@0 | 254 | #if defined(HAVE_PTHREADS) |
michael@0 | 255 | |
michael@0 | 256 | inline Mutex::Mutex() { |
michael@0 | 257 | pthread_mutex_init(&mMutex, NULL); |
michael@0 | 258 | } |
michael@0 | 259 | inline Mutex::Mutex(const char* name) { |
michael@0 | 260 | pthread_mutex_init(&mMutex, NULL); |
michael@0 | 261 | } |
michael@0 | 262 | inline Mutex::Mutex(int type, const char* name) { |
michael@0 | 263 | if (type == SHARED) { |
michael@0 | 264 | pthread_mutexattr_t attr; |
michael@0 | 265 | pthread_mutexattr_init(&attr); |
michael@0 | 266 | pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); |
michael@0 | 267 | pthread_mutex_init(&mMutex, &attr); |
michael@0 | 268 | pthread_mutexattr_destroy(&attr); |
michael@0 | 269 | } else { |
michael@0 | 270 | pthread_mutex_init(&mMutex, NULL); |
michael@0 | 271 | } |
michael@0 | 272 | } |
michael@0 | 273 | inline Mutex::~Mutex() { |
michael@0 | 274 | pthread_mutex_destroy(&mMutex); |
michael@0 | 275 | } |
michael@0 | 276 | inline status_t Mutex::lock() { |
michael@0 | 277 | return -pthread_mutex_lock(&mMutex); |
michael@0 | 278 | } |
michael@0 | 279 | inline void Mutex::unlock() { |
michael@0 | 280 | pthread_mutex_unlock(&mMutex); |
michael@0 | 281 | } |
michael@0 | 282 | inline status_t Mutex::tryLock() { |
michael@0 | 283 | return -pthread_mutex_trylock(&mMutex); |
michael@0 | 284 | } |
michael@0 | 285 | |
michael@0 | 286 | #endif // HAVE_PTHREADS |
michael@0 | 287 | |
michael@0 | 288 | /* |
michael@0 | 289 | * Automatic mutex. Declare one of these at the top of a function. |
michael@0 | 290 | * When the function returns, it will go out of scope, and release the |
michael@0 | 291 | * mutex. |
michael@0 | 292 | */ |
michael@0 | 293 | |
michael@0 | 294 | typedef Mutex::Autolock AutoMutex; |
michael@0 | 295 | |
michael@0 | 296 | /*****************************************************************************/ |
michael@0 | 297 | |
michael@0 | 298 | #if defined(HAVE_PTHREADS) |
michael@0 | 299 | |
michael@0 | 300 | /* |
michael@0 | 301 | * Simple mutex class. The implementation is system-dependent. |
michael@0 | 302 | * |
michael@0 | 303 | * The mutex must be unlocked by the thread that locked it. They are not |
michael@0 | 304 | * recursive, i.e. the same thread can't lock it multiple times. |
michael@0 | 305 | */ |
michael@0 | 306 | class RWLock { |
michael@0 | 307 | public: |
michael@0 | 308 | enum { |
michael@0 | 309 | PRIVATE = 0, |
michael@0 | 310 | SHARED = 1 |
michael@0 | 311 | }; |
michael@0 | 312 | |
michael@0 | 313 | RWLock(); |
michael@0 | 314 | RWLock(const char* name); |
michael@0 | 315 | RWLock(int type, const char* name = NULL); |
michael@0 | 316 | ~RWLock(); |
michael@0 | 317 | |
michael@0 | 318 | status_t readLock(); |
michael@0 | 319 | status_t tryReadLock(); |
michael@0 | 320 | status_t writeLock(); |
michael@0 | 321 | status_t tryWriteLock(); |
michael@0 | 322 | void unlock(); |
michael@0 | 323 | |
michael@0 | 324 | class AutoRLock { |
michael@0 | 325 | public: |
michael@0 | 326 | inline AutoRLock(RWLock& rwlock) : mLock(rwlock) { mLock.readLock(); } |
michael@0 | 327 | inline ~AutoRLock() { mLock.unlock(); } |
michael@0 | 328 | private: |
michael@0 | 329 | RWLock& mLock; |
michael@0 | 330 | }; |
michael@0 | 331 | |
michael@0 | 332 | class AutoWLock { |
michael@0 | 333 | public: |
michael@0 | 334 | inline AutoWLock(RWLock& rwlock) : mLock(rwlock) { mLock.writeLock(); } |
michael@0 | 335 | inline ~AutoWLock() { mLock.unlock(); } |
michael@0 | 336 | private: |
michael@0 | 337 | RWLock& mLock; |
michael@0 | 338 | }; |
michael@0 | 339 | |
michael@0 | 340 | private: |
michael@0 | 341 | // A RWLock cannot be copied |
michael@0 | 342 | RWLock(const RWLock&); |
michael@0 | 343 | RWLock& operator = (const RWLock&); |
michael@0 | 344 | |
michael@0 | 345 | pthread_rwlock_t mRWLock; |
michael@0 | 346 | }; |
michael@0 | 347 | |
michael@0 | 348 | inline RWLock::RWLock() { |
michael@0 | 349 | pthread_rwlock_init(&mRWLock, NULL); |
michael@0 | 350 | } |
michael@0 | 351 | inline RWLock::RWLock(const char* name) { |
michael@0 | 352 | pthread_rwlock_init(&mRWLock, NULL); |
michael@0 | 353 | } |
michael@0 | 354 | inline RWLock::RWLock(int type, const char* name) { |
michael@0 | 355 | if (type == SHARED) { |
michael@0 | 356 | pthread_rwlockattr_t attr; |
michael@0 | 357 | pthread_rwlockattr_init(&attr); |
michael@0 | 358 | pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); |
michael@0 | 359 | pthread_rwlock_init(&mRWLock, &attr); |
michael@0 | 360 | pthread_rwlockattr_destroy(&attr); |
michael@0 | 361 | } else { |
michael@0 | 362 | pthread_rwlock_init(&mRWLock, NULL); |
michael@0 | 363 | } |
michael@0 | 364 | } |
michael@0 | 365 | inline RWLock::~RWLock() { |
michael@0 | 366 | pthread_rwlock_destroy(&mRWLock); |
michael@0 | 367 | } |
michael@0 | 368 | inline status_t RWLock::readLock() { |
michael@0 | 369 | return -pthread_rwlock_rdlock(&mRWLock); |
michael@0 | 370 | } |
michael@0 | 371 | inline status_t RWLock::tryReadLock() { |
michael@0 | 372 | return -pthread_rwlock_tryrdlock(&mRWLock); |
michael@0 | 373 | } |
michael@0 | 374 | inline status_t RWLock::writeLock() { |
michael@0 | 375 | return -pthread_rwlock_wrlock(&mRWLock); |
michael@0 | 376 | } |
michael@0 | 377 | inline status_t RWLock::tryWriteLock() { |
michael@0 | 378 | return -pthread_rwlock_trywrlock(&mRWLock); |
michael@0 | 379 | } |
michael@0 | 380 | inline void RWLock::unlock() { |
michael@0 | 381 | pthread_rwlock_unlock(&mRWLock); |
michael@0 | 382 | } |
michael@0 | 383 | |
michael@0 | 384 | #endif // HAVE_PTHREADS |
michael@0 | 385 | |
michael@0 | 386 | /*****************************************************************************/ |
michael@0 | 387 | |
michael@0 | 388 | /* |
michael@0 | 389 | * Condition variable class. The implementation is system-dependent. |
michael@0 | 390 | * |
michael@0 | 391 | * Condition variables are paired up with mutexes. Lock the mutex, |
michael@0 | 392 | * call wait(), then either re-wait() if things aren't quite what you want, |
michael@0 | 393 | * or unlock the mutex and continue. All threads calling wait() must |
michael@0 | 394 | * use the same mutex for a given Condition. |
michael@0 | 395 | */ |
michael@0 | 396 | class Condition { |
michael@0 | 397 | public: |
michael@0 | 398 | enum { |
michael@0 | 399 | PRIVATE = 0, |
michael@0 | 400 | SHARED = 1 |
michael@0 | 401 | }; |
michael@0 | 402 | |
michael@0 | 403 | Condition(); |
michael@0 | 404 | Condition(int type); |
michael@0 | 405 | ~Condition(); |
michael@0 | 406 | // Wait on the condition variable. Lock the mutex before calling. |
michael@0 | 407 | status_t wait(Mutex& mutex); |
michael@0 | 408 | // same with relative timeout |
michael@0 | 409 | status_t waitRelative(Mutex& mutex, nsecs_t reltime); |
michael@0 | 410 | // Signal the condition variable, allowing one thread to continue. |
michael@0 | 411 | void signal(); |
michael@0 | 412 | // Signal the condition variable, allowing all threads to continue. |
michael@0 | 413 | void broadcast(); |
michael@0 | 414 | |
michael@0 | 415 | private: |
michael@0 | 416 | #if defined(HAVE_PTHREADS) |
michael@0 | 417 | pthread_cond_t mCond; |
michael@0 | 418 | #else |
michael@0 | 419 | void* mState; |
michael@0 | 420 | #endif |
michael@0 | 421 | }; |
michael@0 | 422 | |
michael@0 | 423 | #if defined(HAVE_PTHREADS) |
michael@0 | 424 | |
michael@0 | 425 | inline Condition::Condition() { |
michael@0 | 426 | pthread_cond_init(&mCond, NULL); |
michael@0 | 427 | } |
michael@0 | 428 | inline Condition::Condition(int type) { |
michael@0 | 429 | if (type == SHARED) { |
michael@0 | 430 | pthread_condattr_t attr; |
michael@0 | 431 | pthread_condattr_init(&attr); |
michael@0 | 432 | pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); |
michael@0 | 433 | pthread_cond_init(&mCond, &attr); |
michael@0 | 434 | pthread_condattr_destroy(&attr); |
michael@0 | 435 | } else { |
michael@0 | 436 | pthread_cond_init(&mCond, NULL); |
michael@0 | 437 | } |
michael@0 | 438 | } |
michael@0 | 439 | inline Condition::~Condition() { |
michael@0 | 440 | pthread_cond_destroy(&mCond); |
michael@0 | 441 | } |
michael@0 | 442 | inline status_t Condition::wait(Mutex& mutex) { |
michael@0 | 443 | return -pthread_cond_wait(&mCond, &mutex.mMutex); |
michael@0 | 444 | } |
michael@0 | 445 | inline status_t Condition::waitRelative(Mutex& mutex, nsecs_t reltime) { |
michael@0 | 446 | #if defined(HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE) |
michael@0 | 447 | struct timespec ts; |
michael@0 | 448 | ts.tv_sec = reltime/1000000000; |
michael@0 | 449 | ts.tv_nsec = reltime%1000000000; |
michael@0 | 450 | return -pthread_cond_timedwait_relative_np(&mCond, &mutex.mMutex, &ts); |
michael@0 | 451 | #else // HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE |
michael@0 | 452 | struct timespec ts; |
michael@0 | 453 | #if defined(HAVE_POSIX_CLOCKS) |
michael@0 | 454 | clock_gettime(CLOCK_REALTIME, &ts); |
michael@0 | 455 | #else // HAVE_POSIX_CLOCKS |
michael@0 | 456 | // we don't support the clocks here. |
michael@0 | 457 | struct timeval t; |
michael@0 | 458 | gettimeofday(&t, NULL); |
michael@0 | 459 | ts.tv_sec = t.tv_sec; |
michael@0 | 460 | ts.tv_nsec= t.tv_usec*1000; |
michael@0 | 461 | #endif // HAVE_POSIX_CLOCKS |
michael@0 | 462 | ts.tv_sec += reltime/1000000000; |
michael@0 | 463 | ts.tv_nsec+= reltime%1000000000; |
michael@0 | 464 | if (ts.tv_nsec >= 1000000000) { |
michael@0 | 465 | ts.tv_nsec -= 1000000000; |
michael@0 | 466 | ts.tv_sec += 1; |
michael@0 | 467 | } |
michael@0 | 468 | return -pthread_cond_timedwait(&mCond, &mutex.mMutex, &ts); |
michael@0 | 469 | #endif // HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE |
michael@0 | 470 | } |
michael@0 | 471 | inline void Condition::signal() { |
michael@0 | 472 | pthread_cond_signal(&mCond); |
michael@0 | 473 | } |
michael@0 | 474 | inline void Condition::broadcast() { |
michael@0 | 475 | pthread_cond_broadcast(&mCond); |
michael@0 | 476 | } |
michael@0 | 477 | |
michael@0 | 478 | #endif // HAVE_PTHREADS |
michael@0 | 479 | |
michael@0 | 480 | /*****************************************************************************/ |
michael@0 | 481 | |
michael@0 | 482 | /* |
michael@0 | 483 | * This is our spiffy thread object! |
michael@0 | 484 | */ |
michael@0 | 485 | |
michael@0 | 486 | class Thread : virtual public RefBase |
michael@0 | 487 | { |
michael@0 | 488 | public: |
michael@0 | 489 | // Create a Thread object, but doesn't create or start the associated |
michael@0 | 490 | // thread. See the run() method. |
michael@0 | 491 | Thread(bool canCallJava = true); |
michael@0 | 492 | virtual ~Thread(); |
michael@0 | 493 | |
michael@0 | 494 | // Start the thread in threadLoop() which needs to be implemented. |
michael@0 | 495 | virtual status_t run( const char* name = 0, |
michael@0 | 496 | int32_t priority = PRIORITY_DEFAULT, |
michael@0 | 497 | size_t stack = 0); |
michael@0 | 498 | |
michael@0 | 499 | // Ask this object's thread to exit. This function is asynchronous, when the |
michael@0 | 500 | // function returns the thread might still be running. Of course, this |
michael@0 | 501 | // function can be called from a different thread. |
michael@0 | 502 | virtual void requestExit(); |
michael@0 | 503 | |
michael@0 | 504 | // Good place to do one-time initializations |
michael@0 | 505 | virtual status_t readyToRun(); |
michael@0 | 506 | |
michael@0 | 507 | // Call requestExit() and wait until this object's thread exits. |
michael@0 | 508 | // BE VERY CAREFUL of deadlocks. In particular, it would be silly to call |
michael@0 | 509 | // this function from this object's thread. Will return WOULD_BLOCK in |
michael@0 | 510 | // that case. |
michael@0 | 511 | status_t requestExitAndWait(); |
michael@0 | 512 | |
michael@0 | 513 | protected: |
michael@0 | 514 | // exitPending() returns true if requestExit() has been called. |
michael@0 | 515 | bool exitPending() const; |
michael@0 | 516 | |
michael@0 | 517 | private: |
michael@0 | 518 | // Derived class must implement threadLoop(). The thread starts its life |
michael@0 | 519 | // here. There are two ways of using the Thread object: |
michael@0 | 520 | // 1) loop: if threadLoop() returns true, it will be called again if |
michael@0 | 521 | // requestExit() wasn't called. |
michael@0 | 522 | // 2) once: if threadLoop() returns false, the thread will exit upon return. |
michael@0 | 523 | virtual bool threadLoop() = 0; |
michael@0 | 524 | |
michael@0 | 525 | private: |
michael@0 | 526 | Thread& operator=(const Thread&); |
michael@0 | 527 | static int _threadLoop(void* user); |
michael@0 | 528 | const bool mCanCallJava; |
michael@0 | 529 | thread_id_t mThread; |
michael@0 | 530 | Mutex mLock; |
michael@0 | 531 | Condition mThreadExitedCondition; |
michael@0 | 532 | status_t mStatus; |
michael@0 | 533 | volatile bool mExitPending; |
michael@0 | 534 | volatile bool mRunning; |
michael@0 | 535 | sp<Thread> mHoldSelf; |
michael@0 | 536 | #if HAVE_ANDROID_OS |
michael@0 | 537 | int mTid; |
michael@0 | 538 | #endif |
michael@0 | 539 | }; |
michael@0 | 540 | |
michael@0 | 541 | |
michael@0 | 542 | }; // namespace android |
michael@0 | 543 | |
michael@0 | 544 | #endif // __cplusplus |
michael@0 | 545 | |
michael@0 | 546 | #endif // _LIBS_UTILS_THREADS_H |