OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // WARNING: You should probably be using Thread (thread.h) instead. Thread is | 5 // WARNING: You should probably be using Thread (thread.h) instead. Thread is |
6 // Chrome's message-loop based Thread abstraction, and if you are a | 6 // Chrome's message-loop based Thread abstraction, and if you are a |
7 // thread running in the browser, there will likely be assumptions | 7 // thread running in the browser, there will likely be assumptions |
8 // that your thread will have an associated message loop. | 8 // that your thread will have an associated message loop. |
9 // | 9 // |
10 // This is a simple thread interface that backs to a native operating system | 10 // This is a simple thread interface that backs to a native operating system |
(...skipping 12 matching lines...) Expand all Loading... |
23 // | 23 // |
24 // Thread Safety: A SimpleThread is not completely thread safe. It is safe to | 24 // Thread Safety: A SimpleThread is not completely thread safe. It is safe to |
25 // access it from the creating thread or from the newly created thread. This | 25 // access it from the creating thread or from the newly created thread. This |
26 // implies that the creator thread should be the thread that calls Join. | 26 // implies that the creator thread should be the thread that calls Join. |
27 // | 27 // |
28 // Example: | 28 // Example: |
29 // class MyThreadRunner : public DelegateSimpleThread::Delegate { ... }; | 29 // class MyThreadRunner : public DelegateSimpleThread::Delegate { ... }; |
30 // MyThreadRunner runner; | 30 // MyThreadRunner runner; |
31 // DelegateSimpleThread thread(&runner, "good_name_here"); | 31 // DelegateSimpleThread thread(&runner, "good_name_here"); |
32 // thread.Start(); | 32 // thread.Start(); |
33 // // The newly created thread will invoke runner->Run(), and run until it | 33 // // Start will return after the Thread has been successfully started and |
34 // // returns. | 34 // // initialized. The newly created thread will invoke runner->Run(), and |
| 35 // // run until it returns. |
35 // thread.Join(); // Wait until the thread has exited. You *MUST* Join! | 36 // thread.Join(); // Wait until the thread has exited. You *MUST* Join! |
36 // // The SimpleThread object is still valid, however you may not call Join | 37 // // The SimpleThread object is still valid, however you may not call Join |
37 // // or Start again. | 38 // // or Start again. |
38 | 39 |
39 #ifndef BASE_THREADING_SIMPLE_THREAD_H_ | 40 #ifndef BASE_THREADING_SIMPLE_THREAD_H_ |
40 #define BASE_THREADING_SIMPLE_THREAD_H_ | 41 #define BASE_THREADING_SIMPLE_THREAD_H_ |
41 | 42 |
42 #include <stddef.h> | 43 #include <stddef.h> |
43 | 44 |
44 #include <queue> | 45 #include <queue> |
45 #include <string> | 46 #include <string> |
46 #include <vector> | 47 #include <vector> |
47 | 48 |
48 #include "base/base_export.h" | 49 #include "base/base_export.h" |
49 #include "base/compiler_specific.h" | 50 #include "base/compiler_specific.h" |
50 #include "base/logging.h" | |
51 #include "base/macros.h" | 51 #include "base/macros.h" |
52 #include "base/synchronization/lock.h" | 52 #include "base/synchronization/lock.h" |
53 #include "base/synchronization/waitable_event.h" | 53 #include "base/synchronization/waitable_event.h" |
54 #include "base/threading/platform_thread.h" | 54 #include "base/threading/platform_thread.h" |
55 | 55 |
56 namespace base { | 56 namespace base { |
57 | 57 |
58 // This is the base SimpleThread. You can derive from it and implement the | 58 // This is the base SimpleThread. You can derive from it and implement the |
59 // virtual Run method, or you can use the DelegateSimpleThread interface. | 59 // virtual Run method, or you can use the DelegateSimpleThread interface. |
60 class BASE_EXPORT SimpleThread : public PlatformThread::Delegate { | 60 class BASE_EXPORT SimpleThread : public PlatformThread::Delegate { |
(...skipping 28 matching lines...) Expand all Loading... |
89 SimpleThread(const std::string& name_prefix, const Options& options); | 89 SimpleThread(const std::string& name_prefix, const Options& options); |
90 | 90 |
91 ~SimpleThread() override; | 91 ~SimpleThread() override; |
92 | 92 |
93 virtual void Start(); | 93 virtual void Start(); |
94 virtual void Join(); | 94 virtual void Join(); |
95 | 95 |
96 // Subclasses should override the Run method. | 96 // Subclasses should override the Run method. |
97 virtual void Run() = 0; | 97 virtual void Run() = 0; |
98 | 98 |
99 // Return the thread id. | 99 // Return the thread id, only valid after Start(). |
100 PlatformThreadId GetTid() const; | 100 PlatformThreadId tid() { return tid_; } |
| 101 |
| 102 // Return True if Start() has ever been called. |
| 103 bool HasBeenStarted(); |
| 104 |
| 105 // Return True if Join() has ever been called. |
| 106 bool HasBeenJoined() { return joined_; } |
101 | 107 |
102 // Overridden from PlatformThread::Delegate: | 108 // Overridden from PlatformThread::Delegate: |
103 void ThreadMain() override; | 109 void ThreadMain() override; |
104 | 110 |
105 private: | 111 private: |
106 const std::string name_prefix_; | 112 const std::string name_prefix_; |
107 std::string name_; | 113 std::string name_; |
108 const Options options_; | 114 const Options options_; |
109 | 115 PlatformThreadHandle thread_; // PlatformThread handle, reset after Join. |
110 // PlatformThread handle, reset after Join. | 116 WaitableEvent event_; // Signaled if Start() was ever called. |
111 PlatformThreadHandle thread_; | 117 PlatformThreadId tid_ = kInvalidThreadId; // The backing thread's id. |
112 | 118 bool joined_ = false; // True if Join has been called. |
113 // Signaled once |tid_| is set. | |
114 mutable WaitableEvent id_event_; | |
115 | |
116 // The backing thread's id. | |
117 PlatformThreadId tid_ = kInvalidThreadId; | |
118 | |
119 #if DCHECK_IS_ON() | |
120 bool has_been_started_ = false; | |
121 bool has_been_joined_ = false; | |
122 #endif | |
123 | 119 |
124 DISALLOW_COPY_AND_ASSIGN(SimpleThread); | 120 DISALLOW_COPY_AND_ASSIGN(SimpleThread); |
125 }; | 121 }; |
126 | 122 |
127 // A SimpleThread which delegates Run() to its Delegate. Non-joinable | 123 // A SimpleThread which delegates Run() to its Delegate. Non-joinable |
128 // DelegateSimpleThread are safe to delete after Run() was invoked, their | 124 // DelegateSimpleThread are safe to delete after Run() was invoked, their |
129 // Delegates are also safe to delete after that point from this class' point of | 125 // Delegates are also safe to delete after that point from this class' point of |
130 // view (although implementations must of course make sure that Run() will not | 126 // view (although implementations must of course make sure that Run() will not |
131 // use their Delegate's member state after its deletion). | 127 // use their Delegate's member state after its deletion). |
132 class BASE_EXPORT DelegateSimpleThread : public SimpleThread { | 128 class BASE_EXPORT DelegateSimpleThread : public SimpleThread { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 std::queue<Delegate*> delegates_; | 190 std::queue<Delegate*> delegates_; |
195 base::Lock lock_; // Locks delegates_ | 191 base::Lock lock_; // Locks delegates_ |
196 WaitableEvent dry_; // Not signaled when there is no work to do. | 192 WaitableEvent dry_; // Not signaled when there is no work to do. |
197 | 193 |
198 DISALLOW_COPY_AND_ASSIGN(DelegateSimpleThreadPool); | 194 DISALLOW_COPY_AND_ASSIGN(DelegateSimpleThreadPool); |
199 }; | 195 }; |
200 | 196 |
201 } // namespace base | 197 } // namespace base |
202 | 198 |
203 #endif // BASE_THREADING_SIMPLE_THREAD_H_ | 199 #endif // BASE_THREADING_SIMPLE_THREAD_H_ |
OLD | NEW |