media/omx-plugin/include/froyo/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/froyo/utils/threads.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,456 @@
     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 +/*
   1.302 + * Condition variable class.  The implementation is system-dependent.
   1.303 + *
   1.304 + * Condition variables are paired up with mutexes.  Lock the mutex,
   1.305 + * call wait(), then either re-wait() if things aren't quite what you want,
   1.306 + * or unlock the mutex and continue.  All threads calling wait() must
   1.307 + * use the same mutex for a given Condition.
   1.308 + */
   1.309 +class Condition {
   1.310 +public:
   1.311 +    enum {
   1.312 +        PRIVATE = 0,
   1.313 +        SHARED = 1
   1.314 +    };
   1.315 +
   1.316 +    Condition();
   1.317 +    Condition(int type);
   1.318 +    ~Condition();
   1.319 +    // Wait on the condition variable.  Lock the mutex before calling.
   1.320 +    status_t wait(Mutex& mutex);
   1.321 +    // same with relative timeout
   1.322 +    status_t waitRelative(Mutex& mutex, nsecs_t reltime);
   1.323 +    // Signal the condition variable, allowing one thread to continue.
   1.324 +    void signal();
   1.325 +    // Signal the condition variable, allowing all threads to continue.
   1.326 +    void broadcast();
   1.327 +
   1.328 +private:
   1.329 +#if defined(HAVE_PTHREADS)
   1.330 +    pthread_cond_t mCond;
   1.331 +#else
   1.332 +    void*   mState;
   1.333 +#endif
   1.334 +};
   1.335 +
   1.336 +#if defined(HAVE_PTHREADS)
   1.337 +
   1.338 +inline Condition::Condition() {
   1.339 +    pthread_cond_init(&mCond, NULL);
   1.340 +}
   1.341 +inline Condition::Condition(int type) {
   1.342 +    if (type == SHARED) {
   1.343 +        pthread_condattr_t attr;
   1.344 +        pthread_condattr_init(&attr);
   1.345 +        pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
   1.346 +        pthread_cond_init(&mCond, &attr);
   1.347 +        pthread_condattr_destroy(&attr);
   1.348 +    } else {
   1.349 +        pthread_cond_init(&mCond, NULL);
   1.350 +    }
   1.351 +}
   1.352 +inline Condition::~Condition() {
   1.353 +    pthread_cond_destroy(&mCond);
   1.354 +}
   1.355 +inline status_t Condition::wait(Mutex& mutex) {
   1.356 +    return -pthread_cond_wait(&mCond, &mutex.mMutex);
   1.357 +}
   1.358 +inline status_t Condition::waitRelative(Mutex& mutex, nsecs_t reltime) {
   1.359 +#if defined(HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE)
   1.360 +    struct timespec ts;
   1.361 +    ts.tv_sec  = reltime/1000000000;
   1.362 +    ts.tv_nsec = reltime%1000000000;
   1.363 +    return -pthread_cond_timedwait_relative_np(&mCond, &mutex.mMutex, &ts);
   1.364 +#else // HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE
   1.365 +    struct timespec ts;
   1.366 +#if defined(HAVE_POSIX_CLOCKS)
   1.367 +    clock_gettime(CLOCK_REALTIME, &ts);
   1.368 +#else // HAVE_POSIX_CLOCKS
   1.369 +    // we don't support the clocks here.
   1.370 +    struct timeval t;
   1.371 +    gettimeofday(&t, NULL);
   1.372 +    ts.tv_sec = t.tv_sec;
   1.373 +    ts.tv_nsec= t.tv_usec*1000;
   1.374 +#endif // HAVE_POSIX_CLOCKS
   1.375 +    ts.tv_sec += reltime/1000000000;
   1.376 +    ts.tv_nsec+= reltime%1000000000;
   1.377 +    if (ts.tv_nsec >= 1000000000) {
   1.378 +        ts.tv_nsec -= 1000000000;
   1.379 +        ts.tv_sec  += 1;
   1.380 +    }
   1.381 +    return -pthread_cond_timedwait(&mCond, &mutex.mMutex, &ts);
   1.382 +#endif // HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE
   1.383 +}
   1.384 +inline void Condition::signal() {
   1.385 +    pthread_cond_signal(&mCond);
   1.386 +}
   1.387 +inline void Condition::broadcast() {
   1.388 +    pthread_cond_broadcast(&mCond);
   1.389 +}
   1.390 +
   1.391 +#endif // HAVE_PTHREADS
   1.392 +
   1.393 +/*****************************************************************************/
   1.394 +
   1.395 +/*
   1.396 + * This is our spiffy thread object!
   1.397 + */
   1.398 +
   1.399 +class Thread : virtual public RefBase
   1.400 +{
   1.401 +public:
   1.402 +    // Create a Thread object, but doesn't create or start the associated
   1.403 +    // thread. See the run() method.
   1.404 +                        Thread(bool canCallJava = true);
   1.405 +    virtual             ~Thread();
   1.406 +
   1.407 +    // Start the thread in threadLoop() which needs to be implemented.
   1.408 +    virtual status_t    run(    const char* name = 0,
   1.409 +                                int32_t priority = PRIORITY_DEFAULT,
   1.410 +                                size_t stack = 0);
   1.411 +    
   1.412 +    // Ask this object's thread to exit. This function is asynchronous, when the
   1.413 +    // function returns the thread might still be running. Of course, this
   1.414 +    // function can be called from a different thread.
   1.415 +    virtual void        requestExit();
   1.416 +
   1.417 +    // Good place to do one-time initializations
   1.418 +    virtual status_t    readyToRun();
   1.419 +    
   1.420 +    // Call requestExit() and wait until this object's thread exits.
   1.421 +    // BE VERY CAREFUL of deadlocks. In particular, it would be silly to call
   1.422 +    // this function from this object's thread. Will return WOULD_BLOCK in
   1.423 +    // that case.
   1.424 +            status_t    requestExitAndWait();
   1.425 +
   1.426 +protected:
   1.427 +    // exitPending() returns true if requestExit() has been called.
   1.428 +            bool        exitPending() const;
   1.429 +    
   1.430 +private:
   1.431 +    // Derived class must implement threadLoop(). The thread starts its life
   1.432 +    // here. There are two ways of using the Thread object:
   1.433 +    // 1) loop: if threadLoop() returns true, it will be called again if
   1.434 +    //          requestExit() wasn't called.
   1.435 +    // 2) once: if threadLoop() returns false, the thread will exit upon return.
   1.436 +    virtual bool        threadLoop() = 0;
   1.437 +
   1.438 +private:
   1.439 +    Thread& operator=(const Thread&);
   1.440 +    static  int             _threadLoop(void* user);
   1.441 +    const   bool            mCanCallJava;
   1.442 +            thread_id_t     mThread;
   1.443 +            Mutex           mLock;
   1.444 +            Condition       mThreadExitedCondition;
   1.445 +            status_t        mStatus;
   1.446 +    volatile bool           mExitPending;
   1.447 +    volatile bool           mRunning;
   1.448 +            sp<Thread>      mHoldSelf;
   1.449 +#if HAVE_ANDROID_OS
   1.450 +            int             mTid;
   1.451 +#endif
   1.452 +};
   1.453 +
   1.454 +
   1.455 +}; // namespace android
   1.456 +
   1.457 +#endif  // __cplusplus
   1.458 +
   1.459 +#endif // _LIBS_UTILS_THREADS_H

mercurial