media/omx-plugin/include/ics/utils/threads.h

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

mercurial