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 // // Start will return after the Thread has been successfully started and | 33 // // The newly created thread will invoke runner->Run(), and run until it |
34 // // initialized. The newly created thread will invoke runner->Run(), and | 34 // // returns. |
35 // // run until it returns. | |
36 // thread.Join(); // Wait until the thread has exited. You *MUST* Join! | 35 // thread.Join(); // Wait until the thread has exited. You *MUST* Join! |
37 // // The SimpleThread object is still valid, however you may not call Join | 36 // // The SimpleThread object is still valid, however you may not call Join |
38 // // or Start again. | 37 // // or Start again. |
39 | 38 |
40 #ifndef BASE_THREADING_SIMPLE_THREAD_H_ | 39 #ifndef BASE_THREADING_SIMPLE_THREAD_H_ |
41 #define BASE_THREADING_SIMPLE_THREAD_H_ | 40 #define BASE_THREADING_SIMPLE_THREAD_H_ |
42 | 41 |
43 #include <stddef.h> | 42 #include <stddef.h> |
44 | 43 |
45 #include <queue> | 44 #include <queue> |
46 #include <string> | 45 #include <string> |
47 #include <vector> | 46 #include <vector> |
48 | 47 |
49 #include "base/base_export.h" | 48 #include "base/base_export.h" |
50 #include "base/compiler_specific.h" | 49 #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, only valid after Start(). | 99 // Return the thread id. |
100 PlatformThreadId tid() { return tid_; } | 100 PlatformThreadId GetTid() const; |
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_; } | |
107 | 101 |
108 // Overridden from PlatformThread::Delegate: | 102 // Overridden from PlatformThread::Delegate: |
109 void ThreadMain() override; | 103 void ThreadMain() override; |
110 | 104 |
111 private: | 105 private: |
112 const std::string name_prefix_; | 106 const std::string name_prefix_; |
113 std::string name_; | 107 std::string name_; |
114 const Options options_; | 108 const Options options_; |
115 PlatformThreadHandle thread_; // PlatformThread handle, reset after Join. | 109 |
116 WaitableEvent event_; // Signaled if Start() was ever called. | 110 // PlatformThread handle, reset after Join. |
117 PlatformThreadId tid_ = kInvalidThreadId; // The backing thread's id. | 111 PlatformThreadHandle thread_; |
118 bool joined_ = false; // True if Join has been called. | 112 |
| 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 |
119 | 123 |
120 DISALLOW_COPY_AND_ASSIGN(SimpleThread); | 124 DISALLOW_COPY_AND_ASSIGN(SimpleThread); |
121 }; | 125 }; |
122 | 126 |
123 // A SimpleThread which delegates Run() to its Delegate. Non-joinable | 127 // A SimpleThread which delegates Run() to its Delegate. Non-joinable |
124 // DelegateSimpleThread are safe to delete after Run() was invoked, their | 128 // DelegateSimpleThread are safe to delete after Run() was invoked, their |
125 // Delegates are also safe to delete after that point from this class' point of | 129 // Delegates are also safe to delete after that point from this class' point of |
126 // view (although implementations must of course make sure that Run() will not | 130 // view (although implementations must of course make sure that Run() will not |
127 // use their Delegate's member state after its deletion). | 131 // use their Delegate's member state after its deletion). |
128 class BASE_EXPORT DelegateSimpleThread : public SimpleThread { | 132 class BASE_EXPORT DelegateSimpleThread : public SimpleThread { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 std::queue<Delegate*> delegates_; | 194 std::queue<Delegate*> delegates_; |
191 base::Lock lock_; // Locks delegates_ | 195 base::Lock lock_; // Locks delegates_ |
192 WaitableEvent dry_; // Not signaled when there is no work to do. | 196 WaitableEvent dry_; // Not signaled when there is no work to do. |
193 | 197 |
194 DISALLOW_COPY_AND_ASSIGN(DelegateSimpleThreadPool); | 198 DISALLOW_COPY_AND_ASSIGN(DelegateSimpleThreadPool); |
195 }; | 199 }; |
196 | 200 |
197 } // namespace base | 201 } // namespace base |
198 | 202 |
199 #endif // BASE_THREADING_SIMPLE_THREAD_H_ | 203 #endif // BASE_THREADING_SIMPLE_THREAD_H_ |
OLD | NEW |