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/threading/platform_thread.h" | 8 #include "base/threading/platform_thread.h" |
| 9 #include "base/threading/thread_restrictions.h" |
9 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
10 | 11 |
11 namespace base { | 12 namespace base { |
12 | 13 |
13 SimpleThread::SimpleThread(const std::string& name_prefix) | 14 SimpleThread::SimpleThread(const std::string& name_prefix) |
14 : name_prefix_(name_prefix), name_(name_prefix), | 15 : name_prefix_(name_prefix), name_(name_prefix), |
15 thread_(), event_(true, false), tid_(0), joined_(false) { | 16 thread_(), event_(true, false), tid_(0), joined_(false) { |
16 } | 17 } |
17 | 18 |
18 SimpleThread::SimpleThread(const std::string& name_prefix, | 19 SimpleThread::SimpleThread(const std::string& name_prefix, |
19 const Options& options) | 20 const Options& options) |
20 : name_prefix_(name_prefix), name_(name_prefix), options_(options), | 21 : name_prefix_(name_prefix), name_(name_prefix), options_(options), |
21 thread_(), event_(true, false), tid_(0), joined_(false) { | 22 thread_(), event_(true, false), tid_(0), joined_(false) { |
22 } | 23 } |
23 | 24 |
24 SimpleThread::~SimpleThread() { | 25 SimpleThread::~SimpleThread() { |
25 DCHECK(HasBeenStarted()) << "SimpleThread was never started."; | 26 DCHECK(HasBeenStarted()) << "SimpleThread was never started."; |
26 DCHECK(HasBeenJoined()) << "SimpleThread destroyed without being Join()ed."; | 27 DCHECK(HasBeenJoined()) << "SimpleThread destroyed without being Join()ed."; |
27 } | 28 } |
28 | 29 |
29 void SimpleThread::Start() { | 30 void SimpleThread::Start() { |
30 DCHECK(!HasBeenStarted()) << "Tried to Start a thread multiple times."; | 31 DCHECK(!HasBeenStarted()) << "Tried to Start a thread multiple times."; |
31 bool success = PlatformThread::Create(options_.stack_size(), this, &thread_); | 32 bool success = PlatformThread::Create(options_.stack_size(), this, &thread_); |
32 DCHECK(success); | 33 DCHECK(success); |
| 34 base::ThreadRestrictions::ScopedAllowWait allow_wait; |
33 event_.Wait(); // Wait for the thread to complete initialization. | 35 event_.Wait(); // Wait for the thread to complete initialization. |
34 } | 36 } |
35 | 37 |
36 void SimpleThread::Join() { | 38 void SimpleThread::Join() { |
37 DCHECK(HasBeenStarted()) << "Tried to Join a never-started thread."; | 39 DCHECK(HasBeenStarted()) << "Tried to Join a never-started thread."; |
38 DCHECK(!HasBeenJoined()) << "Tried to Join a thread multiple times."; | 40 DCHECK(!HasBeenJoined()) << "Tried to Join a thread multiple times."; |
39 PlatformThread::Join(thread_); | 41 PlatformThread::Join(thread_); |
40 joined_ = true; | 42 joined_ = true; |
41 } | 43 } |
42 | 44 |
| 45 bool SimpleThread::HasBeenStarted() { |
| 46 base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| 47 return event_.IsSignaled(); |
| 48 } |
| 49 |
43 void SimpleThread::ThreadMain() { | 50 void SimpleThread::ThreadMain() { |
44 tid_ = PlatformThread::CurrentId(); | 51 tid_ = PlatformThread::CurrentId(); |
45 // Construct our full name of the form "name_prefix_/TID". | 52 // Construct our full name of the form "name_prefix_/TID". |
46 name_.push_back('/'); | 53 name_.push_back('/'); |
47 name_.append(IntToString(tid_)); | 54 name_.append(IntToString(tid_)); |
48 PlatformThread::SetName(name_.c_str()); | 55 PlatformThread::SetName(name_.c_str()); |
49 | 56 |
50 // We've initialized our new thread, signal that we're done to Start(). | 57 // We've initialized our new thread, signal that we're done to Start(). |
51 event_.Signal(); | 58 event_.Signal(); |
52 | 59 |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 | 150 |
144 // A NULL delegate pointer signals us to quit. | 151 // A NULL delegate pointer signals us to quit. |
145 if (!work) | 152 if (!work) |
146 break; | 153 break; |
147 | 154 |
148 work->Run(); | 155 work->Run(); |
149 } | 156 } |
150 } | 157 } |
151 | 158 |
152 } // namespace base | 159 } // namespace base |
OLD | NEW |