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