xref: /aosp_15_r20/external/libchrome/base/threading/thread.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_THREADING_THREAD_H_
6*635a8641SAndroid Build Coastguard Worker #define BASE_THREADING_THREAD_H_
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker #include <stddef.h>
9*635a8641SAndroid Build Coastguard Worker 
10*635a8641SAndroid Build Coastguard Worker #include <memory>
11*635a8641SAndroid Build Coastguard Worker #include <string>
12*635a8641SAndroid Build Coastguard Worker 
13*635a8641SAndroid Build Coastguard Worker #include "base/base_export.h"
14*635a8641SAndroid Build Coastguard Worker #include "base/callback.h"
15*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
16*635a8641SAndroid Build Coastguard Worker #include "base/message_loop/message_loop.h"
17*635a8641SAndroid Build Coastguard Worker #include "base/message_loop/timer_slack.h"
18*635a8641SAndroid Build Coastguard Worker #include "base/sequence_checker.h"
19*635a8641SAndroid Build Coastguard Worker #include "base/single_thread_task_runner.h"
20*635a8641SAndroid Build Coastguard Worker #include "base/synchronization/atomic_flag.h"
21*635a8641SAndroid Build Coastguard Worker #include "base/synchronization/lock.h"
22*635a8641SAndroid Build Coastguard Worker #include "base/synchronization/waitable_event.h"
23*635a8641SAndroid Build Coastguard Worker #include "base/threading/platform_thread.h"
24*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
25*635a8641SAndroid Build Coastguard Worker 
26*635a8641SAndroid Build Coastguard Worker namespace base {
27*635a8641SAndroid Build Coastguard Worker 
28*635a8641SAndroid Build Coastguard Worker class MessagePump;
29*635a8641SAndroid Build Coastguard Worker class RunLoop;
30*635a8641SAndroid Build Coastguard Worker 
31*635a8641SAndroid Build Coastguard Worker // IMPORTANT: Instead of creating a base::Thread, consider using
32*635a8641SAndroid Build Coastguard Worker // base::Create(Sequenced|SingleThread)TaskRunnerWithTraits().
33*635a8641SAndroid Build Coastguard Worker //
34*635a8641SAndroid Build Coastguard Worker // A simple thread abstraction that establishes a MessageLoop on a new thread.
35*635a8641SAndroid Build Coastguard Worker // The consumer uses the MessageLoop of the thread to cause code to execute on
36*635a8641SAndroid Build Coastguard Worker // the thread.  When this object is destroyed the thread is terminated.  All
37*635a8641SAndroid Build Coastguard Worker // pending tasks queued on the thread's message loop will run to completion
38*635a8641SAndroid Build Coastguard Worker // before the thread is terminated.
39*635a8641SAndroid Build Coastguard Worker //
40*635a8641SAndroid Build Coastguard Worker // WARNING! SUBCLASSES MUST CALL Stop() IN THEIR DESTRUCTORS!  See ~Thread().
41*635a8641SAndroid Build Coastguard Worker //
42*635a8641SAndroid Build Coastguard Worker // After the thread is stopped, the destruction sequence is:
43*635a8641SAndroid Build Coastguard Worker //
44*635a8641SAndroid Build Coastguard Worker //  (1) Thread::CleanUp()
45*635a8641SAndroid Build Coastguard Worker //  (2) MessageLoop::~MessageLoop
46*635a8641SAndroid Build Coastguard Worker //  (3.b) MessageLoopCurrent::DestructionObserver::WillDestroyCurrentMessageLoop
47*635a8641SAndroid Build Coastguard Worker //
48*635a8641SAndroid Build Coastguard Worker // This API is not thread-safe: unless indicated otherwise its methods are only
49*635a8641SAndroid Build Coastguard Worker // valid from the owning sequence (which is the one from which Start() is
50*635a8641SAndroid Build Coastguard Worker // invoked -- should it differ from the one on which it was constructed).
51*635a8641SAndroid Build Coastguard Worker //
52*635a8641SAndroid Build Coastguard Worker // Sometimes it's useful to kick things off on the initial sequence (e.g.
53*635a8641SAndroid Build Coastguard Worker // construction, Start(), task_runner()), but to then hand the Thread over to a
54*635a8641SAndroid Build Coastguard Worker // pool of users for the last one of them to destroy it when done. For that use
55*635a8641SAndroid Build Coastguard Worker // case, Thread::DetachFromSequence() allows the owning sequence to give up
56*635a8641SAndroid Build Coastguard Worker // ownership. The caller is then responsible to ensure a happens-after
57*635a8641SAndroid Build Coastguard Worker // relationship between the DetachFromSequence() call and the next use of that
58*635a8641SAndroid Build Coastguard Worker // Thread object (including ~Thread()).
59*635a8641SAndroid Build Coastguard Worker class BASE_EXPORT Thread : PlatformThread::Delegate {
60*635a8641SAndroid Build Coastguard Worker  public:
61*635a8641SAndroid Build Coastguard Worker   struct BASE_EXPORT Options {
62*635a8641SAndroid Build Coastguard Worker     typedef Callback<std::unique_ptr<MessagePump>()> MessagePumpFactory;
63*635a8641SAndroid Build Coastguard Worker 
64*635a8641SAndroid Build Coastguard Worker     Options();
65*635a8641SAndroid Build Coastguard Worker     Options(MessageLoop::Type type, size_t size);
66*635a8641SAndroid Build Coastguard Worker     Options(const Options& other);
67*635a8641SAndroid Build Coastguard Worker     ~Options();
68*635a8641SAndroid Build Coastguard Worker 
69*635a8641SAndroid Build Coastguard Worker     // Specifies the type of message loop that will be allocated on the thread.
70*635a8641SAndroid Build Coastguard Worker     // This is ignored if message_pump_factory.is_null() is false.
71*635a8641SAndroid Build Coastguard Worker     MessageLoop::Type message_loop_type = MessageLoop::TYPE_DEFAULT;
72*635a8641SAndroid Build Coastguard Worker 
73*635a8641SAndroid Build Coastguard Worker     // Specifies timer slack for thread message loop.
74*635a8641SAndroid Build Coastguard Worker     TimerSlack timer_slack = TIMER_SLACK_NONE;
75*635a8641SAndroid Build Coastguard Worker 
76*635a8641SAndroid Build Coastguard Worker     // Used to create the MessagePump for the MessageLoop. The callback is Run()
77*635a8641SAndroid Build Coastguard Worker     // on the thread. If message_pump_factory.is_null(), then a MessagePump
78*635a8641SAndroid Build Coastguard Worker     // appropriate for |message_loop_type| is created. Setting this forces the
79*635a8641SAndroid Build Coastguard Worker     // MessageLoop::Type to TYPE_CUSTOM.
80*635a8641SAndroid Build Coastguard Worker     MessagePumpFactory message_pump_factory;
81*635a8641SAndroid Build Coastguard Worker 
82*635a8641SAndroid Build Coastguard Worker     // Specifies the maximum stack size that the thread is allowed to use.
83*635a8641SAndroid Build Coastguard Worker     // This does not necessarily correspond to the thread's initial stack size.
84*635a8641SAndroid Build Coastguard Worker     // A value of 0 indicates that the default maximum should be used.
85*635a8641SAndroid Build Coastguard Worker     size_t stack_size = 0;
86*635a8641SAndroid Build Coastguard Worker 
87*635a8641SAndroid Build Coastguard Worker     // Specifies the initial thread priority.
88*635a8641SAndroid Build Coastguard Worker     ThreadPriority priority = ThreadPriority::NORMAL;
89*635a8641SAndroid Build Coastguard Worker 
90*635a8641SAndroid Build Coastguard Worker     // If false, the thread will not be joined on destruction. This is intended
91*635a8641SAndroid Build Coastguard Worker     // for threads that want TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN
92*635a8641SAndroid Build Coastguard Worker     // semantics. Non-joinable threads can't be joined (must be leaked and
93*635a8641SAndroid Build Coastguard Worker     // can't be destroyed or Stop()'ed).
94*635a8641SAndroid Build Coastguard Worker     // TODO(gab): allow non-joinable instances to be deleted without causing
95*635a8641SAndroid Build Coastguard Worker     // user-after-frees (proposal @ https://crbug.com/629139#c14)
96*635a8641SAndroid Build Coastguard Worker     bool joinable = true;
97*635a8641SAndroid Build Coastguard Worker   };
98*635a8641SAndroid Build Coastguard Worker 
99*635a8641SAndroid Build Coastguard Worker   // Constructor.
100*635a8641SAndroid Build Coastguard Worker   // name is a display string to identify the thread.
101*635a8641SAndroid Build Coastguard Worker   explicit Thread(const std::string& name);
102*635a8641SAndroid Build Coastguard Worker 
103*635a8641SAndroid Build Coastguard Worker   // Destroys the thread, stopping it if necessary.
104*635a8641SAndroid Build Coastguard Worker   //
105*635a8641SAndroid Build Coastguard Worker   // NOTE: ALL SUBCLASSES OF Thread MUST CALL Stop() IN THEIR DESTRUCTORS (or
106*635a8641SAndroid Build Coastguard Worker   // guarantee Stop() is explicitly called before the subclass is destroyed).
107*635a8641SAndroid Build Coastguard Worker   // This is required to avoid a data race between the destructor modifying the
108*635a8641SAndroid Build Coastguard Worker   // vtable, and the thread's ThreadMain calling the virtual method Run().  It
109*635a8641SAndroid Build Coastguard Worker   // also ensures that the CleanUp() virtual method is called on the subclass
110*635a8641SAndroid Build Coastguard Worker   // before it is destructed.
111*635a8641SAndroid Build Coastguard Worker   ~Thread() override;
112*635a8641SAndroid Build Coastguard Worker 
113*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
114*635a8641SAndroid Build Coastguard Worker   // Causes the thread to initialize COM.  This must be called before calling
115*635a8641SAndroid Build Coastguard Worker   // Start() or StartWithOptions().  If |use_mta| is false, the thread is also
116*635a8641SAndroid Build Coastguard Worker   // started with a TYPE_UI message loop.  It is an error to call
117*635a8641SAndroid Build Coastguard Worker   // init_com_with_mta(false) and then StartWithOptions() with any message loop
118*635a8641SAndroid Build Coastguard Worker   // type other than TYPE_UI.
init_com_with_mta(bool use_mta)119*635a8641SAndroid Build Coastguard Worker   void init_com_with_mta(bool use_mta) {
120*635a8641SAndroid Build Coastguard Worker     DCHECK(!message_loop_);
121*635a8641SAndroid Build Coastguard Worker     com_status_ = use_mta ? MTA : STA;
122*635a8641SAndroid Build Coastguard Worker   }
123*635a8641SAndroid Build Coastguard Worker #endif
124*635a8641SAndroid Build Coastguard Worker 
125*635a8641SAndroid Build Coastguard Worker   // Starts the thread.  Returns true if the thread was successfully started;
126*635a8641SAndroid Build Coastguard Worker   // otherwise, returns false.  Upon successful return, the message_loop()
127*635a8641SAndroid Build Coastguard Worker   // getter will return non-null.
128*635a8641SAndroid Build Coastguard Worker   //
129*635a8641SAndroid Build Coastguard Worker   // Note: This function can't be called on Windows with the loader lock held;
130*635a8641SAndroid Build Coastguard Worker   // i.e. during a DllMain, global object construction or destruction, atexit()
131*635a8641SAndroid Build Coastguard Worker   // callback.
132*635a8641SAndroid Build Coastguard Worker   bool Start();
133*635a8641SAndroid Build Coastguard Worker 
134*635a8641SAndroid Build Coastguard Worker   // Starts the thread. Behaves exactly like Start in addition to allow to
135*635a8641SAndroid Build Coastguard Worker   // override the default options.
136*635a8641SAndroid Build Coastguard Worker   //
137*635a8641SAndroid Build Coastguard Worker   // Note: This function can't be called on Windows with the loader lock held;
138*635a8641SAndroid Build Coastguard Worker   // i.e. during a DllMain, global object construction or destruction, atexit()
139*635a8641SAndroid Build Coastguard Worker   // callback.
140*635a8641SAndroid Build Coastguard Worker   bool StartWithOptions(const Options& options);
141*635a8641SAndroid Build Coastguard Worker 
142*635a8641SAndroid Build Coastguard Worker   // Starts the thread and wait for the thread to start and run initialization
143*635a8641SAndroid Build Coastguard Worker   // before returning. It's same as calling Start() and then
144*635a8641SAndroid Build Coastguard Worker   // WaitUntilThreadStarted().
145*635a8641SAndroid Build Coastguard Worker   // Note that using this (instead of Start() or StartWithOptions() causes
146*635a8641SAndroid Build Coastguard Worker   // jank on the calling thread, should be used only in testing code.
147*635a8641SAndroid Build Coastguard Worker   bool StartAndWaitForTesting();
148*635a8641SAndroid Build Coastguard Worker 
149*635a8641SAndroid Build Coastguard Worker   // Blocks until the thread starts running. Called within StartAndWait().
150*635a8641SAndroid Build Coastguard Worker   // Note that calling this causes jank on the calling thread, must be used
151*635a8641SAndroid Build Coastguard Worker   // carefully for production code.
152*635a8641SAndroid Build Coastguard Worker   bool WaitUntilThreadStarted() const;
153*635a8641SAndroid Build Coastguard Worker 
154*635a8641SAndroid Build Coastguard Worker   // Blocks until all tasks previously posted to this thread have been executed.
155*635a8641SAndroid Build Coastguard Worker   void FlushForTesting();
156*635a8641SAndroid Build Coastguard Worker 
157*635a8641SAndroid Build Coastguard Worker   // Signals the thread to exit and returns once the thread has exited. The
158*635a8641SAndroid Build Coastguard Worker   // Thread object is completely reset and may be used as if it were newly
159*635a8641SAndroid Build Coastguard Worker   // constructed (i.e., Start may be called again). Can only be called if
160*635a8641SAndroid Build Coastguard Worker   // |joinable_|.
161*635a8641SAndroid Build Coastguard Worker   //
162*635a8641SAndroid Build Coastguard Worker   // Stop may be called multiple times and is simply ignored if the thread is
163*635a8641SAndroid Build Coastguard Worker   // already stopped or currently stopping.
164*635a8641SAndroid Build Coastguard Worker   //
165*635a8641SAndroid Build Coastguard Worker   // Start/Stop are not thread-safe and callers that desire to invoke them from
166*635a8641SAndroid Build Coastguard Worker   // different threads must ensure mutual exclusion.
167*635a8641SAndroid Build Coastguard Worker   //
168*635a8641SAndroid Build Coastguard Worker   // NOTE: If you are a consumer of Thread, it is not necessary to call this
169*635a8641SAndroid Build Coastguard Worker   // before deleting your Thread objects, as the destructor will do it.
170*635a8641SAndroid Build Coastguard Worker   // IF YOU ARE A SUBCLASS OF Thread, YOU MUST CALL THIS IN YOUR DESTRUCTOR.
171*635a8641SAndroid Build Coastguard Worker   void Stop();
172*635a8641SAndroid Build Coastguard Worker 
173*635a8641SAndroid Build Coastguard Worker   // Signals the thread to exit in the near future.
174*635a8641SAndroid Build Coastguard Worker   //
175*635a8641SAndroid Build Coastguard Worker   // WARNING: This function is not meant to be commonly used. Use at your own
176*635a8641SAndroid Build Coastguard Worker   // risk. Calling this function will cause message_loop() to become invalid in
177*635a8641SAndroid Build Coastguard Worker   // the near future. This function was created to workaround a specific
178*635a8641SAndroid Build Coastguard Worker   // deadlock on Windows with printer worker thread. In any other case, Stop()
179*635a8641SAndroid Build Coastguard Worker   // should be used.
180*635a8641SAndroid Build Coastguard Worker   //
181*635a8641SAndroid Build Coastguard Worker   // Call Stop() to reset the thread object once it is known that the thread has
182*635a8641SAndroid Build Coastguard Worker   // quit.
183*635a8641SAndroid Build Coastguard Worker   void StopSoon();
184*635a8641SAndroid Build Coastguard Worker 
185*635a8641SAndroid Build Coastguard Worker   // Detaches the owning sequence, indicating that the next call to this API
186*635a8641SAndroid Build Coastguard Worker   // (including ~Thread()) can happen from a different sequence (to which it
187*635a8641SAndroid Build Coastguard Worker   // will be rebound). This call itself must happen on the current owning
188*635a8641SAndroid Build Coastguard Worker   // sequence and the caller must ensure the next API call has a happens-after
189*635a8641SAndroid Build Coastguard Worker   // relationship with this one.
190*635a8641SAndroid Build Coastguard Worker   void DetachFromSequence();
191*635a8641SAndroid Build Coastguard Worker 
192*635a8641SAndroid Build Coastguard Worker   // Returns the message loop for this thread.  Use the MessageLoop's
193*635a8641SAndroid Build Coastguard Worker   // PostTask methods to execute code on the thread.  This only returns
194*635a8641SAndroid Build Coastguard Worker   // non-null after a successful call to Start.  After Stop has been called,
195*635a8641SAndroid Build Coastguard Worker   // this will return nullptr.
196*635a8641SAndroid Build Coastguard Worker   //
197*635a8641SAndroid Build Coastguard Worker   // NOTE: You must not call this MessageLoop's Quit method directly.  Use
198*635a8641SAndroid Build Coastguard Worker   // the Thread's Stop method instead.
199*635a8641SAndroid Build Coastguard Worker   //
200*635a8641SAndroid Build Coastguard Worker   // In addition to this Thread's owning sequence, this can also safely be
201*635a8641SAndroid Build Coastguard Worker   // called from the underlying thread itself.
message_loop()202*635a8641SAndroid Build Coastguard Worker   MessageLoop* message_loop() const {
203*635a8641SAndroid Build Coastguard Worker     // This class doesn't provide synchronization around |message_loop_| and as
204*635a8641SAndroid Build Coastguard Worker     // such only the owner should access it (and the underlying thread which
205*635a8641SAndroid Build Coastguard Worker     // never sees it before it's set). In practice, many callers are coming from
206*635a8641SAndroid Build Coastguard Worker     // unrelated threads but provide their own implicit (e.g. memory barriers
207*635a8641SAndroid Build Coastguard Worker     // from task posting) or explicit (e.g. locks) synchronization making the
208*635a8641SAndroid Build Coastguard Worker     // access of |message_loop_| safe... Changing all of those callers is
209*635a8641SAndroid Build Coastguard Worker     // unfeasible; instead verify that they can reliably see
210*635a8641SAndroid Build Coastguard Worker     // |message_loop_ != nullptr| without synchronization as a proof that their
211*635a8641SAndroid Build Coastguard Worker     // external synchronization catches the unsynchronized effects of Start().
212*635a8641SAndroid Build Coastguard Worker     // TODO(gab): Despite all of the above this test has to be disabled for now
213*635a8641SAndroid Build Coastguard Worker     // per crbug.com/629139#c6.
214*635a8641SAndroid Build Coastguard Worker     // DCHECK(owning_sequence_checker_.CalledOnValidSequence() ||
215*635a8641SAndroid Build Coastguard Worker     //        (id_event_.IsSignaled() && id_ == PlatformThread::CurrentId()) ||
216*635a8641SAndroid Build Coastguard Worker     //        message_loop_);
217*635a8641SAndroid Build Coastguard Worker     return message_loop_;
218*635a8641SAndroid Build Coastguard Worker   }
219*635a8641SAndroid Build Coastguard Worker 
220*635a8641SAndroid Build Coastguard Worker   // Returns a TaskRunner for this thread. Use the TaskRunner's PostTask
221*635a8641SAndroid Build Coastguard Worker   // methods to execute code on the thread. Returns nullptr if the thread is not
222*635a8641SAndroid Build Coastguard Worker   // running (e.g. before Start or after Stop have been called). Callers can
223*635a8641SAndroid Build Coastguard Worker   // hold on to this even after the thread is gone; in this situation, attempts
224*635a8641SAndroid Build Coastguard Worker   // to PostTask() will fail.
225*635a8641SAndroid Build Coastguard Worker   //
226*635a8641SAndroid Build Coastguard Worker   // In addition to this Thread's owning sequence, this can also safely be
227*635a8641SAndroid Build Coastguard Worker   // called from the underlying thread itself.
task_runner()228*635a8641SAndroid Build Coastguard Worker   scoped_refptr<SingleThreadTaskRunner> task_runner() const {
229*635a8641SAndroid Build Coastguard Worker     // Refer to the DCHECK and comment inside |message_loop()|.
230*635a8641SAndroid Build Coastguard Worker     DCHECK(owning_sequence_checker_.CalledOnValidSequence() ||
231*635a8641SAndroid Build Coastguard Worker            (id_event_.IsSignaled() && id_ == PlatformThread::CurrentId()) ||
232*635a8641SAndroid Build Coastguard Worker            message_loop_);
233*635a8641SAndroid Build Coastguard Worker     return message_loop_ ? message_loop_->task_runner() : nullptr;
234*635a8641SAndroid Build Coastguard Worker   }
235*635a8641SAndroid Build Coastguard Worker 
236*635a8641SAndroid Build Coastguard Worker   // Returns the name of this thread (for display in debugger too).
thread_name()237*635a8641SAndroid Build Coastguard Worker   const std::string& thread_name() const { return name_; }
238*635a8641SAndroid Build Coastguard Worker 
239*635a8641SAndroid Build Coastguard Worker   // Returns the thread ID.  Should not be called before the first Start*()
240*635a8641SAndroid Build Coastguard Worker   // call.  Keeps on returning the same ID even after a Stop() call. The next
241*635a8641SAndroid Build Coastguard Worker   // Start*() call renews the ID.
242*635a8641SAndroid Build Coastguard Worker   //
243*635a8641SAndroid Build Coastguard Worker   // WARNING: This function will block if the thread hasn't started yet.
244*635a8641SAndroid Build Coastguard Worker   //
245*635a8641SAndroid Build Coastguard Worker   // This method is thread-safe.
246*635a8641SAndroid Build Coastguard Worker   PlatformThreadId GetThreadId() const;
247*635a8641SAndroid Build Coastguard Worker 
248*635a8641SAndroid Build Coastguard Worker   // Returns the current thread handle. If called before Start*() returns or
249*635a8641SAndroid Build Coastguard Worker   // after Stop() returns, an empty thread handle will be returned.
250*635a8641SAndroid Build Coastguard Worker   //
251*635a8641SAndroid Build Coastguard Worker   // This method is thread-safe.
252*635a8641SAndroid Build Coastguard Worker   //
253*635a8641SAndroid Build Coastguard Worker   // TODO(robliao): Remove this when it no longer needs to be temporarily
254*635a8641SAndroid Build Coastguard Worker   // exposed for http://crbug.com/717380.
255*635a8641SAndroid Build Coastguard Worker   PlatformThreadHandle GetThreadHandle() const;
256*635a8641SAndroid Build Coastguard Worker 
257*635a8641SAndroid Build Coastguard Worker   // Returns true if the thread has been started, and not yet stopped.
258*635a8641SAndroid Build Coastguard Worker   bool IsRunning() const;
259*635a8641SAndroid Build Coastguard Worker 
260*635a8641SAndroid Build Coastguard Worker  protected:
261*635a8641SAndroid Build Coastguard Worker   // Called just prior to starting the message loop
Init()262*635a8641SAndroid Build Coastguard Worker   virtual void Init() {}
263*635a8641SAndroid Build Coastguard Worker 
264*635a8641SAndroid Build Coastguard Worker   // Called to start the run loop
265*635a8641SAndroid Build Coastguard Worker   virtual void Run(RunLoop* run_loop);
266*635a8641SAndroid Build Coastguard Worker 
267*635a8641SAndroid Build Coastguard Worker   // Called just after the message loop ends
CleanUp()268*635a8641SAndroid Build Coastguard Worker   virtual void CleanUp() {}
269*635a8641SAndroid Build Coastguard Worker 
270*635a8641SAndroid Build Coastguard Worker   static void SetThreadWasQuitProperly(bool flag);
271*635a8641SAndroid Build Coastguard Worker   static bool GetThreadWasQuitProperly();
272*635a8641SAndroid Build Coastguard Worker 
273*635a8641SAndroid Build Coastguard Worker   // Bind this Thread to an existing MessageLoop instead of starting a new one.
274*635a8641SAndroid Build Coastguard Worker   // TODO(gab): Remove this after ios/ has undergone the same surgery as
275*635a8641SAndroid Build Coastguard Worker   // BrowserThreadImpl (ref.
276*635a8641SAndroid Build Coastguard Worker   // https://chromium-review.googlesource.com/c/chromium/src/+/969104).
277*635a8641SAndroid Build Coastguard Worker   void SetMessageLoop(MessageLoop* message_loop);
278*635a8641SAndroid Build Coastguard Worker 
using_external_message_loop()279*635a8641SAndroid Build Coastguard Worker   bool using_external_message_loop() const {
280*635a8641SAndroid Build Coastguard Worker     return using_external_message_loop_;
281*635a8641SAndroid Build Coastguard Worker   }
282*635a8641SAndroid Build Coastguard Worker 
283*635a8641SAndroid Build Coastguard Worker  private:
284*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
285*635a8641SAndroid Build Coastguard Worker   enum ComStatus {
286*635a8641SAndroid Build Coastguard Worker     NONE,
287*635a8641SAndroid Build Coastguard Worker     STA,
288*635a8641SAndroid Build Coastguard Worker     MTA,
289*635a8641SAndroid Build Coastguard Worker   };
290*635a8641SAndroid Build Coastguard Worker #endif
291*635a8641SAndroid Build Coastguard Worker 
292*635a8641SAndroid Build Coastguard Worker   // PlatformThread::Delegate methods:
293*635a8641SAndroid Build Coastguard Worker   void ThreadMain() override;
294*635a8641SAndroid Build Coastguard Worker 
295*635a8641SAndroid Build Coastguard Worker   void ThreadQuitHelper();
296*635a8641SAndroid Build Coastguard Worker 
297*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
298*635a8641SAndroid Build Coastguard Worker   // Whether this thread needs to initialize COM, and if so, in what mode.
299*635a8641SAndroid Build Coastguard Worker   ComStatus com_status_ = NONE;
300*635a8641SAndroid Build Coastguard Worker #endif
301*635a8641SAndroid Build Coastguard Worker 
302*635a8641SAndroid Build Coastguard Worker   // Mirrors the Options::joinable field used to start this thread. Verified
303*635a8641SAndroid Build Coastguard Worker   // on Stop() -- non-joinable threads can't be joined (must be leaked).
304*635a8641SAndroid Build Coastguard Worker   bool joinable_ = true;
305*635a8641SAndroid Build Coastguard Worker 
306*635a8641SAndroid Build Coastguard Worker   // If true, we're in the middle of stopping, and shouldn't access
307*635a8641SAndroid Build Coastguard Worker   // |message_loop_|. It may non-nullptr and invalid.
308*635a8641SAndroid Build Coastguard Worker   // Should be written on the thread that created this thread. Also read data
309*635a8641SAndroid Build Coastguard Worker   // could be wrong on other threads.
310*635a8641SAndroid Build Coastguard Worker   bool stopping_ = false;
311*635a8641SAndroid Build Coastguard Worker 
312*635a8641SAndroid Build Coastguard Worker   // True while inside of Run().
313*635a8641SAndroid Build Coastguard Worker   bool running_ = false;
314*635a8641SAndroid Build Coastguard Worker   mutable base::Lock running_lock_;  // Protects |running_|.
315*635a8641SAndroid Build Coastguard Worker 
316*635a8641SAndroid Build Coastguard Worker   // The thread's handle.
317*635a8641SAndroid Build Coastguard Worker   PlatformThreadHandle thread_;
318*635a8641SAndroid Build Coastguard Worker   mutable base::Lock thread_lock_;  // Protects |thread_|.
319*635a8641SAndroid Build Coastguard Worker 
320*635a8641SAndroid Build Coastguard Worker   // The thread's id once it has started.
321*635a8641SAndroid Build Coastguard Worker   PlatformThreadId id_ = kInvalidThreadId;
322*635a8641SAndroid Build Coastguard Worker   // Protects |id_| which must only be read while it's signaled.
323*635a8641SAndroid Build Coastguard Worker   mutable WaitableEvent id_event_;
324*635a8641SAndroid Build Coastguard Worker 
325*635a8641SAndroid Build Coastguard Worker   // The thread's MessageLoop and RunLoop. Valid only while the thread is alive.
326*635a8641SAndroid Build Coastguard Worker   // Set by the created thread.
327*635a8641SAndroid Build Coastguard Worker   MessageLoop* message_loop_ = nullptr;
328*635a8641SAndroid Build Coastguard Worker   RunLoop* run_loop_ = nullptr;
329*635a8641SAndroid Build Coastguard Worker 
330*635a8641SAndroid Build Coastguard Worker   // True only if |message_loop_| was externally provided by |SetMessageLoop()|
331*635a8641SAndroid Build Coastguard Worker   // in which case this Thread has no underlying |thread_| and should merely
332*635a8641SAndroid Build Coastguard Worker   // drop |message_loop_| on Stop(). In that event, this remains true after
333*635a8641SAndroid Build Coastguard Worker   // Stop() was invoked so that subclasses can use this state to build their own
334*635a8641SAndroid Build Coastguard Worker   // cleanup logic as required.
335*635a8641SAndroid Build Coastguard Worker   bool using_external_message_loop_ = false;
336*635a8641SAndroid Build Coastguard Worker 
337*635a8641SAndroid Build Coastguard Worker   // Stores Options::timer_slack_ until the message loop has been bound to
338*635a8641SAndroid Build Coastguard Worker   // a thread.
339*635a8641SAndroid Build Coastguard Worker   TimerSlack message_loop_timer_slack_ = TIMER_SLACK_NONE;
340*635a8641SAndroid Build Coastguard Worker 
341*635a8641SAndroid Build Coastguard Worker   // The name of the thread.  Used for debugging purposes.
342*635a8641SAndroid Build Coastguard Worker   const std::string name_;
343*635a8641SAndroid Build Coastguard Worker 
344*635a8641SAndroid Build Coastguard Worker   // Signaled when the created thread gets ready to use the message loop.
345*635a8641SAndroid Build Coastguard Worker   mutable WaitableEvent start_event_;
346*635a8641SAndroid Build Coastguard Worker 
347*635a8641SAndroid Build Coastguard Worker   // This class is not thread-safe, use this to verify access from the owning
348*635a8641SAndroid Build Coastguard Worker   // sequence of the Thread.
349*635a8641SAndroid Build Coastguard Worker   SequenceChecker owning_sequence_checker_;
350*635a8641SAndroid Build Coastguard Worker 
351*635a8641SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(Thread);
352*635a8641SAndroid Build Coastguard Worker };
353*635a8641SAndroid Build Coastguard Worker 
354*635a8641SAndroid Build Coastguard Worker }  // namespace base
355*635a8641SAndroid Build Coastguard Worker 
356*635a8641SAndroid Build Coastguard Worker #endif  // BASE_THREADING_THREAD_H_
357