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