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