xref: /aosp_15_r20/external/libchrome/base/threading/simple_thread.cc (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2010 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 #include "base/threading/simple_thread.h"
6*635a8641SAndroid Build Coastguard Worker 
7*635a8641SAndroid Build Coastguard Worker #include "base/logging.h"
8*635a8641SAndroid Build Coastguard Worker #include "base/strings/string_number_conversions.h"
9*635a8641SAndroid Build Coastguard Worker #include "base/threading/platform_thread.h"
10*635a8641SAndroid Build Coastguard Worker #include "base/threading/thread_restrictions.h"
11*635a8641SAndroid Build Coastguard Worker 
12*635a8641SAndroid Build Coastguard Worker namespace base {
13*635a8641SAndroid Build Coastguard Worker 
SimpleThread(const std::string & name_prefix)14*635a8641SAndroid Build Coastguard Worker SimpleThread::SimpleThread(const std::string& name_prefix)
15*635a8641SAndroid Build Coastguard Worker     : SimpleThread(name_prefix, Options()) {}
16*635a8641SAndroid Build Coastguard Worker 
SimpleThread(const std::string & name_prefix,const Options & options)17*635a8641SAndroid Build Coastguard Worker SimpleThread::SimpleThread(const std::string& name_prefix,
18*635a8641SAndroid Build Coastguard Worker                            const Options& options)
19*635a8641SAndroid Build Coastguard Worker     : name_prefix_(name_prefix),
20*635a8641SAndroid Build Coastguard Worker       options_(options),
21*635a8641SAndroid Build Coastguard Worker       event_(WaitableEvent::ResetPolicy::MANUAL,
22*635a8641SAndroid Build Coastguard Worker              WaitableEvent::InitialState::NOT_SIGNALED) {}
23*635a8641SAndroid Build Coastguard Worker 
~SimpleThread()24*635a8641SAndroid Build Coastguard Worker SimpleThread::~SimpleThread() {
25*635a8641SAndroid Build Coastguard Worker   DCHECK(HasBeenStarted()) << "SimpleThread was never started.";
26*635a8641SAndroid Build Coastguard Worker   DCHECK(!options_.joinable || HasBeenJoined())
27*635a8641SAndroid Build Coastguard Worker       << "Joinable SimpleThread destroyed without being Join()ed.";
28*635a8641SAndroid Build Coastguard Worker }
29*635a8641SAndroid Build Coastguard Worker 
Start()30*635a8641SAndroid Build Coastguard Worker void SimpleThread::Start() {
31*635a8641SAndroid Build Coastguard Worker   StartAsync();
32*635a8641SAndroid Build Coastguard Worker   ThreadRestrictions::ScopedAllowWait allow_wait;
33*635a8641SAndroid Build Coastguard Worker   event_.Wait();  // Wait for the thread to complete initialization.
34*635a8641SAndroid Build Coastguard Worker }
35*635a8641SAndroid Build Coastguard Worker 
Join()36*635a8641SAndroid Build Coastguard Worker void SimpleThread::Join() {
37*635a8641SAndroid Build Coastguard Worker   DCHECK(options_.joinable) << "A non-joinable thread can't be joined.";
38*635a8641SAndroid Build Coastguard Worker   DCHECK(HasStartBeenAttempted()) << "Tried to Join a never-started thread.";
39*635a8641SAndroid Build Coastguard Worker   DCHECK(!HasBeenJoined()) << "Tried to Join a thread multiple times.";
40*635a8641SAndroid Build Coastguard Worker   BeforeJoin();
41*635a8641SAndroid Build Coastguard Worker   PlatformThread::Join(thread_);
42*635a8641SAndroid Build Coastguard Worker   thread_ = PlatformThreadHandle();
43*635a8641SAndroid Build Coastguard Worker   joined_ = true;
44*635a8641SAndroid Build Coastguard Worker }
45*635a8641SAndroid Build Coastguard Worker 
StartAsync()46*635a8641SAndroid Build Coastguard Worker void SimpleThread::StartAsync() {
47*635a8641SAndroid Build Coastguard Worker   DCHECK(!HasStartBeenAttempted()) << "Tried to Start a thread multiple times.";
48*635a8641SAndroid Build Coastguard Worker   start_called_ = true;
49*635a8641SAndroid Build Coastguard Worker   BeforeStart();
50*635a8641SAndroid Build Coastguard Worker   bool success =
51*635a8641SAndroid Build Coastguard Worker       options_.joinable
52*635a8641SAndroid Build Coastguard Worker           ? PlatformThread::CreateWithPriority(options_.stack_size, this,
53*635a8641SAndroid Build Coastguard Worker                                                &thread_, options_.priority)
54*635a8641SAndroid Build Coastguard Worker           : PlatformThread::CreateNonJoinableWithPriority(
55*635a8641SAndroid Build Coastguard Worker                 options_.stack_size, this, options_.priority);
56*635a8641SAndroid Build Coastguard Worker   CHECK(success);
57*635a8641SAndroid Build Coastguard Worker }
58*635a8641SAndroid Build Coastguard Worker 
tid()59*635a8641SAndroid Build Coastguard Worker PlatformThreadId SimpleThread::tid() {
60*635a8641SAndroid Build Coastguard Worker   DCHECK(HasBeenStarted());
61*635a8641SAndroid Build Coastguard Worker   return tid_;
62*635a8641SAndroid Build Coastguard Worker }
63*635a8641SAndroid Build Coastguard Worker 
HasBeenStarted()64*635a8641SAndroid Build Coastguard Worker bool SimpleThread::HasBeenStarted() {
65*635a8641SAndroid Build Coastguard Worker   ThreadRestrictions::ScopedAllowWait allow_wait;
66*635a8641SAndroid Build Coastguard Worker   return event_.IsSignaled();
67*635a8641SAndroid Build Coastguard Worker }
68*635a8641SAndroid Build Coastguard Worker 
ThreadMain()69*635a8641SAndroid Build Coastguard Worker void SimpleThread::ThreadMain() {
70*635a8641SAndroid Build Coastguard Worker   tid_ = PlatformThread::CurrentId();
71*635a8641SAndroid Build Coastguard Worker   // Construct our full name of the form "name_prefix_/TID".
72*635a8641SAndroid Build Coastguard Worker   std::string name(name_prefix_);
73*635a8641SAndroid Build Coastguard Worker   name.push_back('/');
74*635a8641SAndroid Build Coastguard Worker   name.append(IntToString(tid_));
75*635a8641SAndroid Build Coastguard Worker   PlatformThread::SetName(name);
76*635a8641SAndroid Build Coastguard Worker 
77*635a8641SAndroid Build Coastguard Worker   // We've initialized our new thread, signal that we're done to Start().
78*635a8641SAndroid Build Coastguard Worker   event_.Signal();
79*635a8641SAndroid Build Coastguard Worker 
80*635a8641SAndroid Build Coastguard Worker   BeforeRun();
81*635a8641SAndroid Build Coastguard Worker   Run();
82*635a8641SAndroid Build Coastguard Worker }
83*635a8641SAndroid Build Coastguard Worker 
DelegateSimpleThread(Delegate * delegate,const std::string & name_prefix)84*635a8641SAndroid Build Coastguard Worker DelegateSimpleThread::DelegateSimpleThread(Delegate* delegate,
85*635a8641SAndroid Build Coastguard Worker                                            const std::string& name_prefix)
86*635a8641SAndroid Build Coastguard Worker     : DelegateSimpleThread(delegate, name_prefix, Options()) {}
87*635a8641SAndroid Build Coastguard Worker 
DelegateSimpleThread(Delegate * delegate,const std::string & name_prefix,const Options & options)88*635a8641SAndroid Build Coastguard Worker DelegateSimpleThread::DelegateSimpleThread(Delegate* delegate,
89*635a8641SAndroid Build Coastguard Worker                                            const std::string& name_prefix,
90*635a8641SAndroid Build Coastguard Worker                                            const Options& options)
91*635a8641SAndroid Build Coastguard Worker     : SimpleThread(name_prefix, options),
92*635a8641SAndroid Build Coastguard Worker       delegate_(delegate) {
93*635a8641SAndroid Build Coastguard Worker   DCHECK(delegate_);
94*635a8641SAndroid Build Coastguard Worker }
95*635a8641SAndroid Build Coastguard Worker 
96*635a8641SAndroid Build Coastguard Worker DelegateSimpleThread::~DelegateSimpleThread() = default;
97*635a8641SAndroid Build Coastguard Worker 
Run()98*635a8641SAndroid Build Coastguard Worker void DelegateSimpleThread::Run() {
99*635a8641SAndroid Build Coastguard Worker   DCHECK(delegate_) << "Tried to call Run without a delegate (called twice?)";
100*635a8641SAndroid Build Coastguard Worker 
101*635a8641SAndroid Build Coastguard Worker   // Non-joinable DelegateSimpleThreads are allowed to be deleted during Run().
102*635a8641SAndroid Build Coastguard Worker   // Member state must not be accessed after invoking Run().
103*635a8641SAndroid Build Coastguard Worker   Delegate* delegate = delegate_;
104*635a8641SAndroid Build Coastguard Worker   delegate_ = nullptr;
105*635a8641SAndroid Build Coastguard Worker   delegate->Run();
106*635a8641SAndroid Build Coastguard Worker }
107*635a8641SAndroid Build Coastguard Worker 
DelegateSimpleThreadPool(const std::string & name_prefix,int num_threads)108*635a8641SAndroid Build Coastguard Worker DelegateSimpleThreadPool::DelegateSimpleThreadPool(
109*635a8641SAndroid Build Coastguard Worker     const std::string& name_prefix,
110*635a8641SAndroid Build Coastguard Worker     int num_threads)
111*635a8641SAndroid Build Coastguard Worker     : name_prefix_(name_prefix),
112*635a8641SAndroid Build Coastguard Worker       num_threads_(num_threads),
113*635a8641SAndroid Build Coastguard Worker       dry_(WaitableEvent::ResetPolicy::MANUAL,
114*635a8641SAndroid Build Coastguard Worker            WaitableEvent::InitialState::NOT_SIGNALED) {}
115*635a8641SAndroid Build Coastguard Worker 
~DelegateSimpleThreadPool()116*635a8641SAndroid Build Coastguard Worker DelegateSimpleThreadPool::~DelegateSimpleThreadPool() {
117*635a8641SAndroid Build Coastguard Worker   DCHECK(threads_.empty());
118*635a8641SAndroid Build Coastguard Worker   DCHECK(delegates_.empty());
119*635a8641SAndroid Build Coastguard Worker   DCHECK(!dry_.IsSignaled());
120*635a8641SAndroid Build Coastguard Worker }
121*635a8641SAndroid Build Coastguard Worker 
Start()122*635a8641SAndroid Build Coastguard Worker void DelegateSimpleThreadPool::Start() {
123*635a8641SAndroid Build Coastguard Worker   DCHECK(threads_.empty()) << "Start() called with outstanding threads.";
124*635a8641SAndroid Build Coastguard Worker   for (int i = 0; i < num_threads_; ++i) {
125*635a8641SAndroid Build Coastguard Worker     DelegateSimpleThread* thread = new DelegateSimpleThread(this, name_prefix_);
126*635a8641SAndroid Build Coastguard Worker     thread->Start();
127*635a8641SAndroid Build Coastguard Worker     threads_.push_back(thread);
128*635a8641SAndroid Build Coastguard Worker   }
129*635a8641SAndroid Build Coastguard Worker }
130*635a8641SAndroid Build Coastguard Worker 
JoinAll()131*635a8641SAndroid Build Coastguard Worker void DelegateSimpleThreadPool::JoinAll() {
132*635a8641SAndroid Build Coastguard Worker   DCHECK(!threads_.empty()) << "JoinAll() called with no outstanding threads.";
133*635a8641SAndroid Build Coastguard Worker 
134*635a8641SAndroid Build Coastguard Worker   // Tell all our threads to quit their worker loop.
135*635a8641SAndroid Build Coastguard Worker   AddWork(nullptr, num_threads_);
136*635a8641SAndroid Build Coastguard Worker 
137*635a8641SAndroid Build Coastguard Worker   // Join and destroy all the worker threads.
138*635a8641SAndroid Build Coastguard Worker   for (int i = 0; i < num_threads_; ++i) {
139*635a8641SAndroid Build Coastguard Worker     threads_[i]->Join();
140*635a8641SAndroid Build Coastguard Worker     delete threads_[i];
141*635a8641SAndroid Build Coastguard Worker   }
142*635a8641SAndroid Build Coastguard Worker   threads_.clear();
143*635a8641SAndroid Build Coastguard Worker   DCHECK(delegates_.empty());
144*635a8641SAndroid Build Coastguard Worker }
145*635a8641SAndroid Build Coastguard Worker 
AddWork(Delegate * delegate,int repeat_count)146*635a8641SAndroid Build Coastguard Worker void DelegateSimpleThreadPool::AddWork(Delegate* delegate, int repeat_count) {
147*635a8641SAndroid Build Coastguard Worker   AutoLock locked(lock_);
148*635a8641SAndroid Build Coastguard Worker   for (int i = 0; i < repeat_count; ++i)
149*635a8641SAndroid Build Coastguard Worker     delegates_.push(delegate);
150*635a8641SAndroid Build Coastguard Worker   // If we were empty, signal that we have work now.
151*635a8641SAndroid Build Coastguard Worker   if (!dry_.IsSignaled())
152*635a8641SAndroid Build Coastguard Worker     dry_.Signal();
153*635a8641SAndroid Build Coastguard Worker }
154*635a8641SAndroid Build Coastguard Worker 
Run()155*635a8641SAndroid Build Coastguard Worker void DelegateSimpleThreadPool::Run() {
156*635a8641SAndroid Build Coastguard Worker   Delegate* work = nullptr;
157*635a8641SAndroid Build Coastguard Worker 
158*635a8641SAndroid Build Coastguard Worker   while (true) {
159*635a8641SAndroid Build Coastguard Worker     dry_.Wait();
160*635a8641SAndroid Build Coastguard Worker     {
161*635a8641SAndroid Build Coastguard Worker       AutoLock locked(lock_);
162*635a8641SAndroid Build Coastguard Worker       if (!dry_.IsSignaled())
163*635a8641SAndroid Build Coastguard Worker         continue;
164*635a8641SAndroid Build Coastguard Worker 
165*635a8641SAndroid Build Coastguard Worker       DCHECK(!delegates_.empty());
166*635a8641SAndroid Build Coastguard Worker       work = delegates_.front();
167*635a8641SAndroid Build Coastguard Worker       delegates_.pop();
168*635a8641SAndroid Build Coastguard Worker 
169*635a8641SAndroid Build Coastguard Worker       // Signal to any other threads that we're currently out of work.
170*635a8641SAndroid Build Coastguard Worker       if (delegates_.empty())
171*635a8641SAndroid Build Coastguard Worker         dry_.Reset();
172*635a8641SAndroid Build Coastguard Worker     }
173*635a8641SAndroid Build Coastguard Worker 
174*635a8641SAndroid Build Coastguard Worker     // A NULL delegate pointer signals us to quit.
175*635a8641SAndroid Build Coastguard Worker     if (!work)
176*635a8641SAndroid Build Coastguard Worker       break;
177*635a8641SAndroid Build Coastguard Worker 
178*635a8641SAndroid Build Coastguard Worker     work->Run();
179*635a8641SAndroid Build Coastguard Worker   }
180*635a8641SAndroid Build Coastguard Worker }
181*635a8641SAndroid Build Coastguard Worker 
182*635a8641SAndroid Build Coastguard Worker }  // namespace base
183