1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/omx-plugin/include/gb/utils/threads.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,546 @@ 1.4 +/* 1.5 + * Copyright (C) 2007 The Android Open Source Project 1.6 + * 1.7 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.8 + * you may not use this file except in compliance with the License. 1.9 + * You may obtain a copy of the License at 1.10 + * 1.11 + * http://www.apache.org/licenses/LICENSE-2.0 1.12 + * 1.13 + * Unless required by applicable law or agreed to in writing, software 1.14 + * distributed under the License is distributed on an "AS IS" BASIS, 1.15 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.16 + * See the License for the specific language governing permissions and 1.17 + * limitations under the License. 1.18 + */ 1.19 + 1.20 +#ifndef _LIBS_UTILS_THREADS_H 1.21 +#define _LIBS_UTILS_THREADS_H 1.22 + 1.23 +#include <stdint.h> 1.24 +#include <sys/types.h> 1.25 +#include <time.h> 1.26 + 1.27 +#if defined(HAVE_PTHREADS) 1.28 +# include <pthread.h> 1.29 +#endif 1.30 + 1.31 +// ------------------------------------------------------------------ 1.32 +// C API 1.33 + 1.34 +#ifdef __cplusplus 1.35 +extern "C" { 1.36 +#endif 1.37 + 1.38 +typedef void* android_thread_id_t; 1.39 + 1.40 +typedef int (*android_thread_func_t)(void*); 1.41 + 1.42 +enum { 1.43 + /* 1.44 + * *********************************************** 1.45 + * ** Keep in sync with android.os.Process.java ** 1.46 + * *********************************************** 1.47 + * 1.48 + * This maps directly to the "nice" priorites we use in Android. 1.49 + * A thread priority should be chosen inverse-proportinally to 1.50 + * the amount of work the thread is expected to do. The more work 1.51 + * a thread will do, the less favorable priority it should get so that 1.52 + * it doesn't starve the system. Threads not behaving properly might 1.53 + * be "punished" by the kernel. 1.54 + * Use the levels below when appropriate. Intermediate values are 1.55 + * acceptable, preferably use the {MORE|LESS}_FAVORABLE constants below. 1.56 + */ 1.57 + ANDROID_PRIORITY_LOWEST = 19, 1.58 + 1.59 + /* use for background tasks */ 1.60 + ANDROID_PRIORITY_BACKGROUND = 10, 1.61 + 1.62 + /* most threads run at normal priority */ 1.63 + ANDROID_PRIORITY_NORMAL = 0, 1.64 + 1.65 + /* threads currently running a UI that the user is interacting with */ 1.66 + ANDROID_PRIORITY_FOREGROUND = -2, 1.67 + 1.68 + /* the main UI thread has a slightly more favorable priority */ 1.69 + ANDROID_PRIORITY_DISPLAY = -4, 1.70 + 1.71 + /* ui service treads might want to run at a urgent display (uncommon) */ 1.72 + ANDROID_PRIORITY_URGENT_DISPLAY = -8, 1.73 + 1.74 + /* all normal audio threads */ 1.75 + ANDROID_PRIORITY_AUDIO = -16, 1.76 + 1.77 + /* service audio threads (uncommon) */ 1.78 + ANDROID_PRIORITY_URGENT_AUDIO = -19, 1.79 + 1.80 + /* should never be used in practice. regular process might not 1.81 + * be allowed to use this level */ 1.82 + ANDROID_PRIORITY_HIGHEST = -20, 1.83 + 1.84 + ANDROID_PRIORITY_DEFAULT = ANDROID_PRIORITY_NORMAL, 1.85 + ANDROID_PRIORITY_MORE_FAVORABLE = -1, 1.86 + ANDROID_PRIORITY_LESS_FAVORABLE = +1, 1.87 +}; 1.88 + 1.89 +enum { 1.90 + ANDROID_TGROUP_DEFAULT = 0, 1.91 + ANDROID_TGROUP_BG_NONINTERACT = 1, 1.92 + ANDROID_TGROUP_FG_BOOST = 2, 1.93 + ANDROID_TGROUP_MAX = ANDROID_TGROUP_FG_BOOST, 1.94 +}; 1.95 + 1.96 +// Create and run a new thread. 1.97 +extern int androidCreateThread(android_thread_func_t, void *); 1.98 + 1.99 +// Create thread with lots of parameters 1.100 +extern int androidCreateThreadEtc(android_thread_func_t entryFunction, 1.101 + void *userData, 1.102 + const char* threadName, 1.103 + int32_t threadPriority, 1.104 + size_t threadStackSize, 1.105 + android_thread_id_t *threadId); 1.106 + 1.107 +// Get some sort of unique identifier for the current thread. 1.108 +extern android_thread_id_t androidGetThreadId(); 1.109 + 1.110 +// Low-level thread creation -- never creates threads that can 1.111 +// interact with the Java VM. 1.112 +extern int androidCreateRawThreadEtc(android_thread_func_t entryFunction, 1.113 + void *userData, 1.114 + const char* threadName, 1.115 + int32_t threadPriority, 1.116 + size_t threadStackSize, 1.117 + android_thread_id_t *threadId); 1.118 + 1.119 +// Used by the Java Runtime to control how threads are created, so that 1.120 +// they can be proper and lovely Java threads. 1.121 +typedef int (*android_create_thread_fn)(android_thread_func_t entryFunction, 1.122 + void *userData, 1.123 + const char* threadName, 1.124 + int32_t threadPriority, 1.125 + size_t threadStackSize, 1.126 + android_thread_id_t *threadId); 1.127 + 1.128 +extern void androidSetCreateThreadFunc(android_create_thread_fn func); 1.129 + 1.130 +// ------------------------------------------------------------------ 1.131 +// Extra functions working with raw pids. 1.132 + 1.133 +// Get pid for the current thread. 1.134 +extern pid_t androidGetTid(); 1.135 + 1.136 +// Change the scheduling group of a particular thread. The group 1.137 +// should be one of the ANDROID_TGROUP constants. Returns BAD_VALUE if 1.138 +// grp is out of range, else another non-zero value with errno set if 1.139 +// the operation failed. 1.140 +extern int androidSetThreadSchedulingGroup(pid_t tid, int grp); 1.141 + 1.142 +// Change the priority AND scheduling group of a particular thread. The priority 1.143 +// should be one of the ANDROID_PRIORITY constants. Returns INVALID_OPERATION 1.144 +// if the priority set failed, else another value if just the group set failed; 1.145 +// in either case errno is set. 1.146 +extern int androidSetThreadPriority(pid_t tid, int prio); 1.147 + 1.148 +#ifdef __cplusplus 1.149 +} 1.150 +#endif 1.151 + 1.152 +// ------------------------------------------------------------------ 1.153 +// C++ API 1.154 + 1.155 +#ifdef __cplusplus 1.156 + 1.157 +#include <utils/Errors.h> 1.158 +#include <utils/RefBase.h> 1.159 +#include <utils/Timers.h> 1.160 + 1.161 +namespace android { 1.162 + 1.163 +typedef android_thread_id_t thread_id_t; 1.164 + 1.165 +typedef android_thread_func_t thread_func_t; 1.166 + 1.167 +enum { 1.168 + PRIORITY_LOWEST = ANDROID_PRIORITY_LOWEST, 1.169 + PRIORITY_BACKGROUND = ANDROID_PRIORITY_BACKGROUND, 1.170 + PRIORITY_NORMAL = ANDROID_PRIORITY_NORMAL, 1.171 + PRIORITY_FOREGROUND = ANDROID_PRIORITY_FOREGROUND, 1.172 + PRIORITY_DISPLAY = ANDROID_PRIORITY_DISPLAY, 1.173 + PRIORITY_URGENT_DISPLAY = ANDROID_PRIORITY_URGENT_DISPLAY, 1.174 + PRIORITY_AUDIO = ANDROID_PRIORITY_AUDIO, 1.175 + PRIORITY_URGENT_AUDIO = ANDROID_PRIORITY_URGENT_AUDIO, 1.176 + PRIORITY_HIGHEST = ANDROID_PRIORITY_HIGHEST, 1.177 + PRIORITY_DEFAULT = ANDROID_PRIORITY_DEFAULT, 1.178 + PRIORITY_MORE_FAVORABLE = ANDROID_PRIORITY_MORE_FAVORABLE, 1.179 + PRIORITY_LESS_FAVORABLE = ANDROID_PRIORITY_LESS_FAVORABLE, 1.180 +}; 1.181 + 1.182 +// Create and run a new thread. 1.183 +inline bool createThread(thread_func_t f, void *a) { 1.184 + return androidCreateThread(f, a) ? true : false; 1.185 +} 1.186 + 1.187 +// Create thread with lots of parameters 1.188 +inline bool createThreadEtc(thread_func_t entryFunction, 1.189 + void *userData, 1.190 + const char* threadName = "android:unnamed_thread", 1.191 + int32_t threadPriority = PRIORITY_DEFAULT, 1.192 + size_t threadStackSize = 0, 1.193 + thread_id_t *threadId = 0) 1.194 +{ 1.195 + return androidCreateThreadEtc(entryFunction, userData, threadName, 1.196 + threadPriority, threadStackSize, threadId) ? true : false; 1.197 +} 1.198 + 1.199 +// Get some sort of unique identifier for the current thread. 1.200 +inline thread_id_t getThreadId() { 1.201 + return androidGetThreadId(); 1.202 +} 1.203 + 1.204 +/*****************************************************************************/ 1.205 + 1.206 +/* 1.207 + * Simple mutex class. The implementation is system-dependent. 1.208 + * 1.209 + * The mutex must be unlocked by the thread that locked it. They are not 1.210 + * recursive, i.e. the same thread can't lock it multiple times. 1.211 + */ 1.212 +class Mutex { 1.213 +public: 1.214 + enum { 1.215 + PRIVATE = 0, 1.216 + SHARED = 1 1.217 + }; 1.218 + 1.219 + Mutex(); 1.220 + Mutex(const char* name); 1.221 + Mutex(int type, const char* name = NULL); 1.222 + ~Mutex(); 1.223 + 1.224 + // lock or unlock the mutex 1.225 + status_t lock(); 1.226 + void unlock(); 1.227 + 1.228 + // lock if possible; returns 0 on success, error otherwise 1.229 + status_t tryLock(); 1.230 + 1.231 + // Manages the mutex automatically. It'll be locked when Autolock is 1.232 + // constructed and released when Autolock goes out of scope. 1.233 + class Autolock { 1.234 + public: 1.235 + inline Autolock(Mutex& mutex) : mLock(mutex) { mLock.lock(); } 1.236 + inline Autolock(Mutex* mutex) : mLock(*mutex) { mLock.lock(); } 1.237 + inline ~Autolock() { mLock.unlock(); } 1.238 + private: 1.239 + Mutex& mLock; 1.240 + }; 1.241 + 1.242 +private: 1.243 + friend class Condition; 1.244 + 1.245 + // A mutex cannot be copied 1.246 + Mutex(const Mutex&); 1.247 + Mutex& operator = (const Mutex&); 1.248 + 1.249 +#if defined(HAVE_PTHREADS) 1.250 + pthread_mutex_t mMutex; 1.251 +#else 1.252 + void _init(); 1.253 + void* mState; 1.254 +#endif 1.255 +}; 1.256 + 1.257 +#if defined(HAVE_PTHREADS) 1.258 + 1.259 +inline Mutex::Mutex() { 1.260 + pthread_mutex_init(&mMutex, NULL); 1.261 +} 1.262 +inline Mutex::Mutex(const char* name) { 1.263 + pthread_mutex_init(&mMutex, NULL); 1.264 +} 1.265 +inline Mutex::Mutex(int type, const char* name) { 1.266 + if (type == SHARED) { 1.267 + pthread_mutexattr_t attr; 1.268 + pthread_mutexattr_init(&attr); 1.269 + pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); 1.270 + pthread_mutex_init(&mMutex, &attr); 1.271 + pthread_mutexattr_destroy(&attr); 1.272 + } else { 1.273 + pthread_mutex_init(&mMutex, NULL); 1.274 + } 1.275 +} 1.276 +inline Mutex::~Mutex() { 1.277 + pthread_mutex_destroy(&mMutex); 1.278 +} 1.279 +inline status_t Mutex::lock() { 1.280 + return -pthread_mutex_lock(&mMutex); 1.281 +} 1.282 +inline void Mutex::unlock() { 1.283 + pthread_mutex_unlock(&mMutex); 1.284 +} 1.285 +inline status_t Mutex::tryLock() { 1.286 + return -pthread_mutex_trylock(&mMutex); 1.287 +} 1.288 + 1.289 +#endif // HAVE_PTHREADS 1.290 + 1.291 +/* 1.292 + * Automatic mutex. Declare one of these at the top of a function. 1.293 + * When the function returns, it will go out of scope, and release the 1.294 + * mutex. 1.295 + */ 1.296 + 1.297 +typedef Mutex::Autolock AutoMutex; 1.298 + 1.299 +/*****************************************************************************/ 1.300 + 1.301 +#if defined(HAVE_PTHREADS) 1.302 + 1.303 +/* 1.304 + * Simple mutex class. The implementation is system-dependent. 1.305 + * 1.306 + * The mutex must be unlocked by the thread that locked it. They are not 1.307 + * recursive, i.e. the same thread can't lock it multiple times. 1.308 + */ 1.309 +class RWLock { 1.310 +public: 1.311 + enum { 1.312 + PRIVATE = 0, 1.313 + SHARED = 1 1.314 + }; 1.315 + 1.316 + RWLock(); 1.317 + RWLock(const char* name); 1.318 + RWLock(int type, const char* name = NULL); 1.319 + ~RWLock(); 1.320 + 1.321 + status_t readLock(); 1.322 + status_t tryReadLock(); 1.323 + status_t writeLock(); 1.324 + status_t tryWriteLock(); 1.325 + void unlock(); 1.326 + 1.327 + class AutoRLock { 1.328 + public: 1.329 + inline AutoRLock(RWLock& rwlock) : mLock(rwlock) { mLock.readLock(); } 1.330 + inline ~AutoRLock() { mLock.unlock(); } 1.331 + private: 1.332 + RWLock& mLock; 1.333 + }; 1.334 + 1.335 + class AutoWLock { 1.336 + public: 1.337 + inline AutoWLock(RWLock& rwlock) : mLock(rwlock) { mLock.writeLock(); } 1.338 + inline ~AutoWLock() { mLock.unlock(); } 1.339 + private: 1.340 + RWLock& mLock; 1.341 + }; 1.342 + 1.343 +private: 1.344 + // A RWLock cannot be copied 1.345 + RWLock(const RWLock&); 1.346 + RWLock& operator = (const RWLock&); 1.347 + 1.348 + pthread_rwlock_t mRWLock; 1.349 +}; 1.350 + 1.351 +inline RWLock::RWLock() { 1.352 + pthread_rwlock_init(&mRWLock, NULL); 1.353 +} 1.354 +inline RWLock::RWLock(const char* name) { 1.355 + pthread_rwlock_init(&mRWLock, NULL); 1.356 +} 1.357 +inline RWLock::RWLock(int type, const char* name) { 1.358 + if (type == SHARED) { 1.359 + pthread_rwlockattr_t attr; 1.360 + pthread_rwlockattr_init(&attr); 1.361 + pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); 1.362 + pthread_rwlock_init(&mRWLock, &attr); 1.363 + pthread_rwlockattr_destroy(&attr); 1.364 + } else { 1.365 + pthread_rwlock_init(&mRWLock, NULL); 1.366 + } 1.367 +} 1.368 +inline RWLock::~RWLock() { 1.369 + pthread_rwlock_destroy(&mRWLock); 1.370 +} 1.371 +inline status_t RWLock::readLock() { 1.372 + return -pthread_rwlock_rdlock(&mRWLock); 1.373 +} 1.374 +inline status_t RWLock::tryReadLock() { 1.375 + return -pthread_rwlock_tryrdlock(&mRWLock); 1.376 +} 1.377 +inline status_t RWLock::writeLock() { 1.378 + return -pthread_rwlock_wrlock(&mRWLock); 1.379 +} 1.380 +inline status_t RWLock::tryWriteLock() { 1.381 + return -pthread_rwlock_trywrlock(&mRWLock); 1.382 +} 1.383 +inline void RWLock::unlock() { 1.384 + pthread_rwlock_unlock(&mRWLock); 1.385 +} 1.386 + 1.387 +#endif // HAVE_PTHREADS 1.388 + 1.389 +/*****************************************************************************/ 1.390 + 1.391 +/* 1.392 + * Condition variable class. The implementation is system-dependent. 1.393 + * 1.394 + * Condition variables are paired up with mutexes. Lock the mutex, 1.395 + * call wait(), then either re-wait() if things aren't quite what you want, 1.396 + * or unlock the mutex and continue. All threads calling wait() must 1.397 + * use the same mutex for a given Condition. 1.398 + */ 1.399 +class Condition { 1.400 +public: 1.401 + enum { 1.402 + PRIVATE = 0, 1.403 + SHARED = 1 1.404 + }; 1.405 + 1.406 + Condition(); 1.407 + Condition(int type); 1.408 + ~Condition(); 1.409 + // Wait on the condition variable. Lock the mutex before calling. 1.410 + status_t wait(Mutex& mutex); 1.411 + // same with relative timeout 1.412 + status_t waitRelative(Mutex& mutex, nsecs_t reltime); 1.413 + // Signal the condition variable, allowing one thread to continue. 1.414 + void signal(); 1.415 + // Signal the condition variable, allowing all threads to continue. 1.416 + void broadcast(); 1.417 + 1.418 +private: 1.419 +#if defined(HAVE_PTHREADS) 1.420 + pthread_cond_t mCond; 1.421 +#else 1.422 + void* mState; 1.423 +#endif 1.424 +}; 1.425 + 1.426 +#if defined(HAVE_PTHREADS) 1.427 + 1.428 +inline Condition::Condition() { 1.429 + pthread_cond_init(&mCond, NULL); 1.430 +} 1.431 +inline Condition::Condition(int type) { 1.432 + if (type == SHARED) { 1.433 + pthread_condattr_t attr; 1.434 + pthread_condattr_init(&attr); 1.435 + pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); 1.436 + pthread_cond_init(&mCond, &attr); 1.437 + pthread_condattr_destroy(&attr); 1.438 + } else { 1.439 + pthread_cond_init(&mCond, NULL); 1.440 + } 1.441 +} 1.442 +inline Condition::~Condition() { 1.443 + pthread_cond_destroy(&mCond); 1.444 +} 1.445 +inline status_t Condition::wait(Mutex& mutex) { 1.446 + return -pthread_cond_wait(&mCond, &mutex.mMutex); 1.447 +} 1.448 +inline status_t Condition::waitRelative(Mutex& mutex, nsecs_t reltime) { 1.449 +#if defined(HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE) 1.450 + struct timespec ts; 1.451 + ts.tv_sec = reltime/1000000000; 1.452 + ts.tv_nsec = reltime%1000000000; 1.453 + return -pthread_cond_timedwait_relative_np(&mCond, &mutex.mMutex, &ts); 1.454 +#else // HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE 1.455 + struct timespec ts; 1.456 +#if defined(HAVE_POSIX_CLOCKS) 1.457 + clock_gettime(CLOCK_REALTIME, &ts); 1.458 +#else // HAVE_POSIX_CLOCKS 1.459 + // we don't support the clocks here. 1.460 + struct timeval t; 1.461 + gettimeofday(&t, NULL); 1.462 + ts.tv_sec = t.tv_sec; 1.463 + ts.tv_nsec= t.tv_usec*1000; 1.464 +#endif // HAVE_POSIX_CLOCKS 1.465 + ts.tv_sec += reltime/1000000000; 1.466 + ts.tv_nsec+= reltime%1000000000; 1.467 + if (ts.tv_nsec >= 1000000000) { 1.468 + ts.tv_nsec -= 1000000000; 1.469 + ts.tv_sec += 1; 1.470 + } 1.471 + return -pthread_cond_timedwait(&mCond, &mutex.mMutex, &ts); 1.472 +#endif // HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE 1.473 +} 1.474 +inline void Condition::signal() { 1.475 + pthread_cond_signal(&mCond); 1.476 +} 1.477 +inline void Condition::broadcast() { 1.478 + pthread_cond_broadcast(&mCond); 1.479 +} 1.480 + 1.481 +#endif // HAVE_PTHREADS 1.482 + 1.483 +/*****************************************************************************/ 1.484 + 1.485 +/* 1.486 + * This is our spiffy thread object! 1.487 + */ 1.488 + 1.489 +class Thread : virtual public RefBase 1.490 +{ 1.491 +public: 1.492 + // Create a Thread object, but doesn't create or start the associated 1.493 + // thread. See the run() method. 1.494 + Thread(bool canCallJava = true); 1.495 + virtual ~Thread(); 1.496 + 1.497 + // Start the thread in threadLoop() which needs to be implemented. 1.498 + virtual status_t run( const char* name = 0, 1.499 + int32_t priority = PRIORITY_DEFAULT, 1.500 + size_t stack = 0); 1.501 + 1.502 + // Ask this object's thread to exit. This function is asynchronous, when the 1.503 + // function returns the thread might still be running. Of course, this 1.504 + // function can be called from a different thread. 1.505 + virtual void requestExit(); 1.506 + 1.507 + // Good place to do one-time initializations 1.508 + virtual status_t readyToRun(); 1.509 + 1.510 + // Call requestExit() and wait until this object's thread exits. 1.511 + // BE VERY CAREFUL of deadlocks. In particular, it would be silly to call 1.512 + // this function from this object's thread. Will return WOULD_BLOCK in 1.513 + // that case. 1.514 + status_t requestExitAndWait(); 1.515 + 1.516 +protected: 1.517 + // exitPending() returns true if requestExit() has been called. 1.518 + bool exitPending() const; 1.519 + 1.520 +private: 1.521 + // Derived class must implement threadLoop(). The thread starts its life 1.522 + // here. There are two ways of using the Thread object: 1.523 + // 1) loop: if threadLoop() returns true, it will be called again if 1.524 + // requestExit() wasn't called. 1.525 + // 2) once: if threadLoop() returns false, the thread will exit upon return. 1.526 + virtual bool threadLoop() = 0; 1.527 + 1.528 +private: 1.529 + Thread& operator=(const Thread&); 1.530 + static int _threadLoop(void* user); 1.531 + const bool mCanCallJava; 1.532 + thread_id_t mThread; 1.533 + Mutex mLock; 1.534 + Condition mThreadExitedCondition; 1.535 + status_t mStatus; 1.536 + volatile bool mExitPending; 1.537 + volatile bool mRunning; 1.538 + sp<Thread> mHoldSelf; 1.539 +#if HAVE_ANDROID_OS 1.540 + int mTid; 1.541 +#endif 1.542 +}; 1.543 + 1.544 + 1.545 +}; // namespace android 1.546 + 1.547 +#endif // __cplusplus 1.548 + 1.549 +#endif // _LIBS_UTILS_THREADS_H