~mir-team/mir/in-process-egl+input-conglomeration

« back to all changes in this revision

Viewing changes to 3rd_party/android-input/android/frameworks/native/include/utils/Thread.h

Merged trunk and fixed issues

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
17
 
#ifndef _LIBS_UTILS_THREAD_H
18
 
#define _LIBS_UTILS_THREAD_H
19
 
 
20
 
#include <stdint.h>
21
 
#include <sys/types.h>
22
 
#include <time.h>
23
 
 
24
 
#if defined(HAVE_PTHREADS)
25
 
# include <pthread.h>
26
 
#endif
27
 
 
28
 
#include ANDROIDFW_UTILS(Condition.h)
29
 
#include ANDROIDFW_UTILS(Mutex.h)
30
 
#include ANDROIDFW_UTILS(RefBase.h)
31
 
#include ANDROIDFW_UTILS(ThreadDefs.h)
32
 
 
33
 
// ---------------------------------------------------------------------------
34
 
namespace android {
35
 
// ---------------------------------------------------------------------------
36
 
 
37
 
class Thread : virtual public RefBase
38
 
{
39
 
public:
40
 
    // Create a Thread object, but doesn't create or start the associated
41
 
    // thread. See the run() method.
42
 
                        Thread(bool canCallJava = true);
43
 
    virtual             ~Thread();
44
 
 
45
 
    // Start the thread in threadLoop() which needs to be implemented.
46
 
    virtual status_t    run(    const char* name = 0,
47
 
                                int32_t priority = PRIORITY_DEFAULT,
48
 
                                size_t stack = 0);
49
 
    
50
 
    // Ask this object's thread to exit. This function is asynchronous, when the
51
 
    // function returns the thread might still be running. Of course, this
52
 
    // function can be called from a different thread.
53
 
    virtual void        requestExit();
54
 
 
55
 
    // Good place to do one-time initializations
56
 
    virtual status_t    readyToRun();
57
 
    
58
 
    // Call requestExit() and wait until this object's thread exits.
59
 
    // BE VERY CAREFUL of deadlocks. In particular, it would be silly to call
60
 
    // this function from this object's thread. Will return WOULD_BLOCK in
61
 
    // that case.
62
 
    virtual status_t    requestExitAndWait();
63
 
 
64
 
    // Wait until this object's thread exits. Returns immediately if not yet running.
65
 
    // Do not call from this object's thread; will return WOULD_BLOCK in that case.
66
 
            status_t    join();
67
 
 
68
 
#ifdef HAVE_ANDROID_OS
69
 
    // Return the thread's kernel ID, same as the thread itself calling gettid() or
70
 
    // androidGetTid(), or -1 if the thread is not running.
71
 
            pid_t       getTid() const;
72
 
#endif
73
 
 
74
 
protected:
75
 
    // exitPending() returns true if requestExit() has been called.
76
 
            bool        exitPending() const;
77
 
    
78
 
private:
79
 
    // Derived class must implement threadLoop(). The thread starts its life
80
 
    // here. There are two ways of using the Thread object:
81
 
    // 1) loop: if threadLoop() returns true, it will be called again if
82
 
    //          requestExit() wasn't called.
83
 
    // 2) once: if threadLoop() returns false, the thread will exit upon return.
84
 
    virtual bool        threadLoop() = 0;
85
 
 
86
 
private:
87
 
    Thread& operator=(const Thread&);
88
 
    static  int             _threadLoop(void* user);
89
 
    const   bool            mCanCallJava;
90
 
    // always hold mLock when reading or writing
91
 
            thread_id_t     mThread;
92
 
    mutable Mutex           mLock;
93
 
            Condition       mThreadExitedCondition;
94
 
            status_t        mStatus;
95
 
    // note that all accesses of mExitPending and mRunning need to hold mLock
96
 
    volatile bool           mExitPending;
97
 
    volatile bool           mRunning;
98
 
            sp<Thread>      mHoldSelf;
99
 
#ifdef HAVE_ANDROID_OS
100
 
    // legacy for debugging, not used by getTid() as it is set by the child thread
101
 
    // and so is not initialized until the child reaches that point
102
 
            pid_t           mTid;
103
 
#endif
104
 
};
105
 
 
106
 
 
107
 
} // namespace android
108
 
 
109
 
// ---------------------------------------------------------------------------
110
 
#endif // _LIBS_UTILS_THREAD_H
111
 
// ---------------------------------------------------------------------------