Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(326)

Side by Side Diff: base/threading/simple_thread.cc

Issue 2204333003: Add joinable option to SimpleThread::Options as was just done for Thread. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@b1_nonjoinable_thread
Patch Set: +friend for ScopedAllowWait.. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/threading/simple_thread.h" 5 #include "base/threading/simple_thread.h"
6 6
7 #include "base/logging.h"
8 #include "base/strings/string_number_conversions.h" 7 #include "base/strings/string_number_conversions.h"
9 #include "base/threading/platform_thread.h" 8 #include "base/threading/platform_thread.h"
10 #include "base/threading/thread_restrictions.h" 9 #include "base/threading/thread_restrictions.h"
11 10
12 namespace base { 11 namespace base {
13 12
14 SimpleThread::SimpleThread(const std::string& name_prefix) 13 SimpleThread::SimpleThread(const std::string& name_prefix)
15 : name_prefix_(name_prefix), 14 : SimpleThread(name_prefix, Options()) {}
16 name_(name_prefix),
17 thread_(),
18 event_(WaitableEvent::ResetPolicy::MANUAL,
19 WaitableEvent::InitialState::NOT_SIGNALED),
20 tid_(0),
21 joined_(false) {}
22 15
23 SimpleThread::SimpleThread(const std::string& name_prefix, 16 SimpleThread::SimpleThread(const std::string& name_prefix,
24 const Options& options) 17 const Options& options)
25 : name_prefix_(name_prefix), 18 : name_prefix_(name_prefix),
26 name_(name_prefix), 19 name_(name_prefix),
27 options_(options), 20 options_(options),
28 thread_(),
29 event_(WaitableEvent::ResetPolicy::MANUAL, 21 event_(WaitableEvent::ResetPolicy::MANUAL,
30 WaitableEvent::InitialState::NOT_SIGNALED), 22 WaitableEvent::InitialState::NOT_SIGNALED) {}
31 tid_(0),
32 joined_(false) {}
33 23
34 SimpleThread::~SimpleThread() { 24 SimpleThread::~SimpleThread() {
35 DCHECK(HasBeenStarted()) << "SimpleThread was never started."; 25 DCHECK(HasBeenStarted()) << "SimpleThread was never started.";
36 DCHECK(HasBeenJoined()) << "SimpleThread destroyed without being Join()ed."; 26 DCHECK(!options_.joinable || HasBeenJoined())
27 << "Joinable SimpleThread destroyed without being Join()ed.";
37 } 28 }
38 29
39 void SimpleThread::Start() { 30 void SimpleThread::Start() {
40 DCHECK(!HasBeenStarted()) << "Tried to Start a thread multiple times."; 31 DCHECK(!HasBeenStarted()) << "Tried to Start a thread multiple times.";
41 bool success; 32 bool success =
42 if (options_.priority() == ThreadPriority::NORMAL) { 33 options_.joinable
43 success = PlatformThread::Create(options_.stack_size(), this, &thread_); 34 ? PlatformThread::CreateWithPriority(options_.stack_size, this,
44 } else { 35 &thread_, options_.priority)
45 success = PlatformThread::CreateWithPriority(options_.stack_size(), this, 36 : PlatformThread::CreateNonJoinableWithPriority(
46 &thread_, options_.priority()); 37 options_.stack_size, this, options_.priority);
47 }
48 DCHECK(success); 38 DCHECK(success);
49 base::ThreadRestrictions::ScopedAllowWait allow_wait; 39 ThreadRestrictions::ScopedAllowWait allow_wait;
50 event_.Wait(); // Wait for the thread to complete initialization. 40 event_.Wait(); // Wait for the thread to complete initialization.
51 } 41 }
52 42
53 void SimpleThread::Join() { 43 void SimpleThread::Join() {
44 DCHECK(options_.joinable) << "A non-joinable thread can't be joined.";
54 DCHECK(HasBeenStarted()) << "Tried to Join a never-started thread."; 45 DCHECK(HasBeenStarted()) << "Tried to Join a never-started thread.";
55 DCHECK(!HasBeenJoined()) << "Tried to Join a thread multiple times."; 46 DCHECK(!HasBeenJoined()) << "Tried to Join a thread multiple times.";
56 PlatformThread::Join(thread_); 47 PlatformThread::Join(thread_);
48 thread_ = PlatformThreadHandle();
57 joined_ = true; 49 joined_ = true;
58 } 50 }
59 51
60 bool SimpleThread::HasBeenStarted() { 52 bool SimpleThread::HasBeenStarted() {
61 base::ThreadRestrictions::ScopedAllowWait allow_wait; 53 ThreadRestrictions::ScopedAllowWait allow_wait;
62 return event_.IsSignaled(); 54 return event_.IsSignaled();
63 } 55 }
64 56
65 void SimpleThread::ThreadMain() { 57 void SimpleThread::ThreadMain() {
66 tid_ = PlatformThread::CurrentId(); 58 tid_ = PlatformThread::CurrentId();
67 // Construct our full name of the form "name_prefix_/TID". 59 // Construct our full name of the form "name_prefix_/TID".
68 name_.push_back('/'); 60 name_.push_back('/');
69 name_.append(IntToString(tid_)); 61 name_.append(IntToString(tid_));
70 PlatformThread::SetName(name_); 62 PlatformThread::SetName(name_);
71 63
72 // We've initialized our new thread, signal that we're done to Start(). 64 // We've initialized our new thread, signal that we're done to Start().
73 event_.Signal(); 65 event_.Signal();
74 66
75 Run(); 67 Run();
76 } 68 }
77 69
70 DelegateSimpleThread::Delegate::Delegate()
71 #if DCHECK_IS_ON()
72 : active_runners_decremented_(WaitableEvent::ResetPolicy::AUTOMATIC,
73 WaitableEvent::InitialState::NOT_SIGNALED)
74 #endif
75 {
76 }
77
78 DelegateSimpleThread::Delegate::~Delegate() {
79 #if DCHECK_IS_ON()
80 // A Delegate is allowed to be destroyed after returning from Run(). This
81 // means that destruction can legally occur between the end of Run() and
82 // |active_runners_| being decremented in RunHelper(). Allow a minimal
83 // reasonable delay for this race to unwind.
84 constexpr TimeDelta kMaxCompletionWait = TimeDelta::FromMilliseconds(200);
85 TimeDelta remaining_time = kMaxCompletionWait;
86 const TimeTicks max_completion_time = TimeTicks::Now() + kMaxCompletionWait;
87 do {
88 // At least a single Wait() call is required (even when |active_runners_| is
89 // already zero) in order to synchronize with the matching Signal() call;
90 // otherwise there is a race between it and |active_runners_decremented_|'s
91 // destructor (as highlighted by TSAN); hence the do-while.
92 ThreadRestrictions::ScopedAllowWait allow_wait;
93 active_runners_decremented_.TimedWait(remaining_time);
Lei Zhang 2016/08/10 21:12:38 Can we just Wait() instead of doing TimedWait() ?
gab 2016/08/10 22:37:00 That would make the check not as good IMO as it wo
94 remaining_time = max_completion_time - TimeTicks::Now();
95 } while (!AtomicRefCountIsZero(&active_runners_) &&
96 remaining_time > TimeDelta());
97
98 DCHECK(AtomicRefCountIsZero(&active_runners_))
99 << "Delegate cannot be deleted with Run() in progress.";
100 #endif // DCHECK_IS_ON()
101 }
102
103 void DelegateSimpleThread::Delegate::RunHelper() {
104 #if DCHECK_IS_ON()
105 AtomicRefCountInc(&active_runners_);
106 #endif // DCHECK_IS_ON()
107
108 Run();
109
110 #if DCHECK_IS_ON()
111 AtomicRefCountDec(&active_runners_);
112 active_runners_decremented_.Signal();
113 #endif // DCHECK_IS_ON()
114 }
115
78 DelegateSimpleThread::DelegateSimpleThread(Delegate* delegate, 116 DelegateSimpleThread::DelegateSimpleThread(Delegate* delegate,
79 const std::string& name_prefix) 117 const std::string& name_prefix)
80 : SimpleThread(name_prefix), 118 : DelegateSimpleThread(delegate, name_prefix, Options()) {}
81 delegate_(delegate) {
82 }
83 119
84 DelegateSimpleThread::DelegateSimpleThread(Delegate* delegate, 120 DelegateSimpleThread::DelegateSimpleThread(Delegate* delegate,
85 const std::string& name_prefix, 121 const std::string& name_prefix,
86 const Options& options) 122 const Options& options)
87 : SimpleThread(name_prefix, options), 123 : SimpleThread(name_prefix, options),
88 delegate_(delegate) { 124 delegate_(delegate) {
125 DCHECK(delegate_);
89 } 126 }
90 127
91 DelegateSimpleThread::~DelegateSimpleThread() { 128 DelegateSimpleThread::~DelegateSimpleThread() = default;
92 }
93 129
94 void DelegateSimpleThread::Run() { 130 void DelegateSimpleThread::Run() {
95 DCHECK(delegate_) << "Tried to call Run without a delegate (called twice?)"; 131 #if DCHECK_IS_ON()
96 delegate_->Run(); 132 // |ran_| must be verified and updated before calling RunHelper() as non-
97 delegate_ = NULL; 133 // joinable DelegateSimpleThread can be deleted before it returns.
134 DCHECK(!ran_) << "Ran twice?";
135 ran_ = true;
136 #endif // DCHECK_IS_ON()
137
138 delegate_->RunHelper();
98 } 139 }
99 140
100 DelegateSimpleThreadPool::DelegateSimpleThreadPool( 141 DelegateSimpleThreadPool::DelegateSimpleThreadPool(
101 const std::string& name_prefix, 142 const std::string& name_prefix,
102 int num_threads) 143 int num_threads)
103 : name_prefix_(name_prefix), 144 : name_prefix_(name_prefix),
104 num_threads_(num_threads), 145 num_threads_(num_threads),
105 dry_(WaitableEvent::ResetPolicy::MANUAL, 146 dry_(WaitableEvent::ResetPolicy::MANUAL,
106 WaitableEvent::InitialState::NOT_SIGNALED) {} 147 WaitableEvent::InitialState::NOT_SIGNALED) {}
107 148
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 201
161 // Signal to any other threads that we're currently out of work. 202 // Signal to any other threads that we're currently out of work.
162 if (delegates_.empty()) 203 if (delegates_.empty())
163 dry_.Reset(); 204 dry_.Reset();
164 } 205 }
165 206
166 // A NULL delegate pointer signals us to quit. 207 // A NULL delegate pointer signals us to quit.
167 if (!work) 208 if (!work)
168 break; 209 break;
169 210
170 work->Run(); 211 work->RunHelper();
171 } 212 }
172 } 213 }
173 214
174 } // namespace base 215 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698