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 : SimpleThread(name_prefix, Options()) {} | 15 : SimpleThread(name_prefix, Options()) {} |
16 | 16 |
17 SimpleThread::SimpleThread(const std::string& name_prefix, | 17 SimpleThread::SimpleThread(const std::string& name_prefix, |
18 const Options& options) | 18 const Options& options) |
19 : name_prefix_(name_prefix), | 19 : name_prefix_(name_prefix), |
20 options_(options), | 20 options_(options), |
21 event_(WaitableEvent::ResetPolicy::MANUAL, | 21 id_event_(WaitableEvent::ResetPolicy::MANUAL, |
22 WaitableEvent::InitialState::NOT_SIGNALED) {} | 22 WaitableEvent::InitialState::NOT_SIGNALED) {} |
23 | 23 |
24 SimpleThread::~SimpleThread() { | 24 SimpleThread::~SimpleThread() { |
25 DCHECK(HasBeenStarted()) << "SimpleThread was never started."; | 25 #if DCHECK_IS_ON() |
26 DCHECK(!options_.joinable || HasBeenJoined()) | 26 DCHECK(has_been_started_) << "SimpleThread was never started."; |
| 27 DCHECK(!options_.joinable || has_been_joined_) |
27 << "Joinable SimpleThread destroyed without being Join()ed."; | 28 << "Joinable SimpleThread destroyed without being Join()ed."; |
| 29 #endif |
28 } | 30 } |
29 | 31 |
30 void SimpleThread::Start() { | 32 void SimpleThread::Start() { |
31 DCHECK(!HasBeenStarted()) << "Tried to Start a thread multiple times."; | 33 #if DCHECK_IS_ON() |
| 34 DCHECK(!has_been_started_) << "Tried to Start a thread multiple times."; |
| 35 |
| 36 // Set |has_been_started_| before creating the thread as no member access is |
| 37 // allowed after. |
| 38 has_been_started_ = true; |
| 39 #endif |
32 bool success = | 40 bool success = |
33 options_.joinable | 41 options_.joinable |
34 ? PlatformThread::CreateWithPriority(options_.stack_size, this, | 42 ? PlatformThread::CreateWithPriority(options_.stack_size, this, |
35 &thread_, options_.priority) | 43 &thread_, options_.priority) |
36 : PlatformThread::CreateNonJoinableWithPriority( | 44 : PlatformThread::CreateNonJoinableWithPriority( |
37 options_.stack_size, this, options_.priority); | 45 options_.stack_size, this, options_.priority); |
38 DCHECK(success); | 46 DCHECK(success); |
39 ThreadRestrictions::ScopedAllowWait allow_wait; | 47 |
40 event_.Wait(); // Wait for the thread to complete initialization. | 48 // No member access after creating the thread, |this| can be deleted at any |
| 49 // point after invoking Run() on non-joinable threads. |
41 } | 50 } |
42 | 51 |
43 void SimpleThread::Join() { | 52 void SimpleThread::Join() { |
| 53 #if DCHECK_IS_ON() |
44 DCHECK(options_.joinable) << "A non-joinable thread can't be joined."; | 54 DCHECK(options_.joinable) << "A non-joinable thread can't be joined."; |
45 DCHECK(HasBeenStarted()) << "Tried to Join a never-started thread."; | 55 DCHECK(has_been_started_) << "Tried to Join a never-started thread."; |
46 DCHECK(!HasBeenJoined()) << "Tried to Join a thread multiple times."; | 56 DCHECK(!has_been_joined_) << "Tried to Join a thread multiple times."; |
| 57 #endif |
47 PlatformThread::Join(thread_); | 58 PlatformThread::Join(thread_); |
48 thread_ = PlatformThreadHandle(); | 59 thread_ = PlatformThreadHandle(); |
49 joined_ = true; | 60 #if DCHECK_IS_ON() |
| 61 has_been_joined_ = true; |
| 62 #endif |
50 } | 63 } |
51 | 64 |
52 bool SimpleThread::HasBeenStarted() { | 65 PlatformThreadId SimpleThread::GetTid() const { |
53 ThreadRestrictions::ScopedAllowWait allow_wait; | 66 id_event_.Wait(); |
54 return event_.IsSignaled(); | 67 return tid_; |
55 } | 68 } |
56 | 69 |
57 void SimpleThread::ThreadMain() { | 70 void SimpleThread::ThreadMain() { |
58 tid_ = PlatformThread::CurrentId(); | 71 tid_ = PlatformThread::CurrentId(); |
| 72 id_event_.Signal(); |
| 73 |
59 // Construct our full name of the form "name_prefix_/TID". | 74 // Construct our full name of the form "name_prefix_/TID". |
60 std::string name(name_prefix_); | 75 std::string name(name_prefix_); |
61 name.push_back('/'); | 76 name.push_back('/'); |
62 name.append(IntToString(tid_)); | 77 name.append(IntToString(tid_)); |
63 PlatformThread::SetName(name); | 78 PlatformThread::SetName(name); |
64 | 79 |
65 // We've initialized our new thread, signal that we're done to Start(). | |
66 event_.Signal(); | |
67 | |
68 Run(); | 80 Run(); |
69 } | 81 } |
70 | 82 |
71 DelegateSimpleThread::DelegateSimpleThread(Delegate* delegate, | 83 DelegateSimpleThread::DelegateSimpleThread(Delegate* delegate, |
72 const std::string& name_prefix) | 84 const std::string& name_prefix) |
73 : DelegateSimpleThread(delegate, name_prefix, Options()) {} | 85 : DelegateSimpleThread(delegate, name_prefix, Options()) {} |
74 | 86 |
75 DelegateSimpleThread::DelegateSimpleThread(Delegate* delegate, | 87 DelegateSimpleThread::DelegateSimpleThread(Delegate* delegate, |
76 const std::string& name_prefix, | 88 const std::string& name_prefix, |
77 const Options& options) | 89 const Options& options) |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 | 172 |
161 // A NULL delegate pointer signals us to quit. | 173 // A NULL delegate pointer signals us to quit. |
162 if (!work) | 174 if (!work) |
163 break; | 175 break; |
164 | 176 |
165 work->Run(); | 177 work->Run(); |
166 } | 178 } |
167 } | 179 } |
168 | 180 |
169 } // namespace base | 181 } // namespace base |
OLD | NEW |