f524512d0e3c9a27b2c5f5fc6ce17b782848311b
[iec.git] / src / type3_AndroidCloud / anbox-master / external / android-emugl / shared / emugl / common / thread.h
1 // Copyright (C) 2014 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef EMUGL_COMMON_THREAD_H
16 #define EMUGL_COMMON_THREAD_H
17
18 #ifdef _WIN32
19 #include <windows.h>
20 #else
21 #include <pthread.h>
22 #endif
23
24 #include <stdint.h>
25
26 namespace emugl {
27
28 // Wrapper class for platform-specific threads.
29 // To create your own thread, define a sub-class of emugl::Thread
30 // and override its main() method.
31 //
32 // For example:
33 //
34 //    class MyThread : public emugl::Thread {
35 //    public:
36 //        MyThread() : Thread() {}
37 //
38 //        virtual intptr_t main() {
39 //            ... main thread loop implementation
40 //            return 0;
41 //        }
42 //    };
43 //
44 //    ...
45 //
46 //    // Create new instance, but does not start it.
47 //    MyThread* thread = new MyThread();
48 //
49 //    // Start the thread.
50 //    thread->start();
51 //
52 //    // Wait for thread completion, and gets result into |exitStatus|.
53 //    int exitStatus;
54 //    thread->wait(&exitStatus);
55 //
56 class Thread {
57 public:
58     // Public constructor.
59     Thread();
60
61     // Virtual destructor.
62     virtual ~Thread();
63
64     // Override this method in your own thread sub-classes. This will
65     // be called when start() is invoked on the Thread instance.
66     virtual intptr_t main() = 0;
67
68     // Start a thread instance. Return true on success, false otherwise
69     // (e.g. if the thread was already started or terminated).
70     bool start();
71
72     // Wait for thread termination and retrieve exist status into
73     // |*exitStatus|. Return true on success, false otherwise.
74     // NOTE: |exitStatus| can be NULL.
75     bool  wait(intptr_t *exitStatus);
76
77     // Check whether a thread has terminated. On success, return true
78     // and sets |*exitStatus|. On failure, return false.
79     // NOTE: |exitStatus| can be NULL.
80     bool tryWait(intptr_t *exitStatus);
81
82 private:
83 #ifdef _WIN32
84     static DWORD WINAPI thread_main(void* arg);
85
86     HANDLE mThread;
87     DWORD mThreadId;
88     CRITICAL_SECTION mLock;
89 #else // !WIN32
90     static void* thread_main(void* arg);
91
92     pthread_t mThread;
93     pthread_mutex_t mLock;
94     bool mJoined;
95 #endif
96     intptr_t mExitStatus;
97     bool mIsRunning;
98 };
99
100 }  // namespace emugl
101
102 #endif  // EMUGL_COMMON_THREAD_H
103