OLD | NEW |
---|---|
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" | 7 #include "base/logging.h" |
8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
9 #include "base/threading/platform_thread.h" | 9 #include "base/threading/platform_thread.h" |
10 #include "base/threading/thread_restrictions.h" | 10 #include "base/threading/thread_restrictions.h" |
11 | 11 |
12 namespace base { | 12 namespace base { |
13 | 13 |
14 SimpleThread::SimpleThread(const std::string& name_prefix) | 14 SimpleThread::SimpleThread(const std::string& name_prefix) |
15 : name_prefix_(name_prefix), | 15 : 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 | 16 |
23 SimpleThread::SimpleThread(const std::string& name_prefix, | 17 SimpleThread::SimpleThread(const std::string& name_prefix, |
24 const Options& options) | 18 const Options& options) |
25 : name_prefix_(name_prefix), | 19 : name_prefix_(name_prefix), |
26 name_(name_prefix), | 20 name_(name_prefix), |
27 options_(options), | 21 options_(options), |
28 thread_(), | |
29 event_(WaitableEvent::ResetPolicy::MANUAL, | 22 event_(WaitableEvent::ResetPolicy::MANUAL, |
30 WaitableEvent::InitialState::NOT_SIGNALED), | 23 WaitableEvent::InitialState::NOT_SIGNALED) {} |
31 tid_(0), | |
32 joined_(false) {} | |
33 | 24 |
34 SimpleThread::~SimpleThread() { | 25 SimpleThread::~SimpleThread() { |
35 DCHECK(HasBeenStarted()) << "SimpleThread was never started."; | 26 DCHECK(HasBeenStarted()) << "SimpleThread was never started."; |
36 DCHECK(HasBeenJoined()) << "SimpleThread destroyed without being Join()ed."; | 27 DCHECK(thread_.is_null()) << "Joinable SimpleThread destroyed without being " |
28 << "Join()ed."; | |
37 } | 29 } |
38 | 30 |
39 void SimpleThread::Start() { | 31 void SimpleThread::Start() { |
40 DCHECK(!HasBeenStarted()) << "Tried to Start a thread multiple times."; | 32 DCHECK(!HasBeenStarted()) << "Tried to Start a thread multiple times."; |
41 bool success; | 33 bool success = options_.joinable |
42 if (options_.priority() == ThreadPriority::NORMAL) { | 34 ? PlatformThread::CreateWithPriority(options_.stack_size, this, |
43 success = PlatformThread::Create(options_.stack_size(), this, &thread_); | 35 &thread_, options_.priority) |
44 } else { | 36 : PlatformThread::CreateNonJoinableWithPriority( |
45 success = PlatformThread::CreateWithPriority(options_.stack_size(), this, | 37 options_.stack_size, this, options_.priority); |
46 &thread_, options_.priority()); | |
47 } | |
48 DCHECK(success); | 38 DCHECK(success); |
49 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 39 base::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() { |
54 DCHECK(HasBeenStarted()) << "Tried to Join a never-started thread."; | 44 DCHECK(HasBeenStarted()) << "Tried to Join a never-started thread."; |
45 DCHECK(!thread_.is_null()) << "A non-joinable thread can't be joined."; | |
Lei Zhang
2016/08/03 23:24:11
Put this before or after the HasBeen twins?
gab
2016/08/04 14:35:00
Done.
| |
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 base::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(); |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
165 | 157 |
166 // A NULL delegate pointer signals us to quit. | 158 // A NULL delegate pointer signals us to quit. |
167 if (!work) | 159 if (!work) |
168 break; | 160 break; |
169 | 161 |
170 work->Run(); | 162 work->Run(); |
171 } | 163 } |
172 } | 164 } |
173 | 165 |
174 } // namespace base | 166 } // namespace base |
OLD | NEW |