blob: 5cf6b47a5cc93facaf5464005afad5c21721bb10 [file] [log] [blame]
Mathias Agopian2bd99592012-02-25 23:02:14 -08001/*
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
Yabin Cui4a6e5a32015-01-26 19:48:54 -080024#if !defined(_WIN32)
Mathias Agopian2bd99592012-02-25 23:02:14 -080025# include <pthread.h>
26#endif
27
28#include <utils/Condition.h>
29#include <utils/Errors.h>
30#include <utils/Mutex.h>
31#include <utils/RefBase.h>
32#include <utils/Timers.h>
33#include <utils/ThreadDefs.h>
34
35// ---------------------------------------------------------------------------
36namespace android {
37// ---------------------------------------------------------------------------
38
Steven Morelandb8f152d2017-12-18 16:14:13 -080039// DO NOT USE: please use std::thread
40
Mathias Agopian2bd99592012-02-25 23:02:14 -080041class Thread : virtual public RefBase
42{
43public:
44 // Create a Thread object, but doesn't create or start the associated
Steven Moreland90722d22022-05-10 22:57:49 +000045 // thread. See the run() method. This object must be used with RefBase/sp,
46 // like any other RefBase object, because they are conventionally promoted
47 // from bare pointers (Thread::run is particularly problematic here).
Chih-Hung Hsieh2a929962016-08-02 12:14:47 -070048 explicit Thread(bool canCallJava = true);
Mathias Agopian2bd99592012-02-25 23:02:14 -080049 virtual ~Thread();
50
51 // Start the thread in threadLoop() which needs to be implemented.
Jiyong Park4f301cd2018-10-29 23:06:23 +090052 // NOLINTNEXTLINE(google-default-arguments)
Brian Carlstrome71b9142016-03-12 16:08:12 -080053 virtual status_t run( const char* name,
Mathias Agopian2bd99592012-02-25 23:02:14 -080054 int32_t priority = PRIORITY_DEFAULT,
55 size_t stack = 0);
56
57 // Ask this object's thread to exit. This function is asynchronous, when the
58 // function returns the thread might still be running. Of course, this
59 // function can be called from a different thread.
60 virtual void requestExit();
61
62 // Good place to do one-time initializations
63 virtual status_t readyToRun();
64
65 // Call requestExit() and wait until this object's thread exits.
66 // BE VERY CAREFUL of deadlocks. In particular, it would be silly to call
67 // this function from this object's thread. Will return WOULD_BLOCK in
68 // that case.
69 status_t requestExitAndWait();
70
71 // Wait until this object's thread exits. Returns immediately if not yet running.
72 // Do not call from this object's thread; will return WOULD_BLOCK in that case.
73 status_t join();
74
Romain Guy31ba37f2013-03-11 14:34:56 -070075 // Indicates whether this thread is running or not.
76 bool isRunning() const;
77
Elliott Hughes9b828ad2015-07-30 08:47:35 -070078#if defined(__ANDROID__)
Elliott Hughesb7659612014-08-18 10:47:37 -070079 // Return the thread's kernel ID, same as the thread itself calling gettid(),
80 // or -1 if the thread is not running.
Mathias Agopian2bd99592012-02-25 23:02:14 -080081 pid_t getTid() const;
82#endif
83
84protected:
85 // exitPending() returns true if requestExit() has been called.
86 bool exitPending() const;
87
88private:
89 // Derived class must implement threadLoop(). The thread starts its life
90 // here. There are two ways of using the Thread object:
91 // 1) loop: if threadLoop() returns true, it will be called again if
92 // requestExit() wasn't called.
93 // 2) once: if threadLoop() returns false, the thread will exit upon return.
94 virtual bool threadLoop() = 0;
95
96private:
97 Thread& operator=(const Thread&);
98 static int _threadLoop(void* user);
99 const bool mCanCallJava;
100 // always hold mLock when reading or writing
101 thread_id_t mThread;
102 mutable Mutex mLock;
103 Condition mThreadExitedCondition;
104 status_t mStatus;
105 // note that all accesses of mExitPending and mRunning need to hold mLock
106 volatile bool mExitPending;
107 volatile bool mRunning;
108 sp<Thread> mHoldSelf;
Elliott Hughes9b828ad2015-07-30 08:47:35 -0700109#if defined(__ANDROID__)
Mathias Agopian2bd99592012-02-25 23:02:14 -0800110 // legacy for debugging, not used by getTid() as it is set by the child thread
111 // and so is not initialized until the child reaches that point
112 pid_t mTid;
113#endif
114};
115
Pirama Arumuga Nainar90affce2018-04-11 23:10:28 -0700116} // namespace android
Mathias Agopian2bd99592012-02-25 23:02:14 -0800117
118// ---------------------------------------------------------------------------
119#endif // _LIBS_UTILS_THREAD_H
120// ---------------------------------------------------------------------------