OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 *NOT* be using this class directly. PlatformThread is | 5 // WARNING: You should *NOT* be using this class directly. PlatformThread is |
6 // the low-level platform-specific abstraction to the OS's threading interface. | 6 // the low-level platform-specific abstraction to the OS's threading interface. |
7 // You should instead be using a message-loop driven Thread, see thread.h. | 7 // You should instead be using a message-loop driven Thread, see thread.h. |
8 | 8 |
9 #ifndef BASE_THREADING_PLATFORM_THREAD_H_ | 9 #ifndef BASE_THREADING_PLATFORM_THREAD_H_ |
10 #define BASE_THREADING_PLATFORM_THREAD_H_ | 10 #define BASE_THREADING_PLATFORM_THREAD_H_ |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 private: | 104 private: |
105 friend class PlatformThread; | 105 friend class PlatformThread; |
106 | 106 |
107 Handle handle_; | 107 Handle handle_; |
108 PlatformThreadId id_; | 108 PlatformThreadId id_; |
109 }; | 109 }; |
110 | 110 |
111 const PlatformThreadId kInvalidThreadId(0); | 111 const PlatformThreadId kInvalidThreadId(0); |
112 | 112 |
113 // Valid values for SetThreadPriority() | 113 // Valid values for SetThreadPriority() |
114 enum ThreadPriority{ | 114 enum ThreadPriority { |
115 kThreadPriority_Normal, | 115 kThreadPriority_Normal, |
116 // Suitable for low-latency, glitch-resistant audio. | 116 // Suitable for low-latency, glitch-resistant audio. |
117 kThreadPriority_RealtimeAudio, | 117 kThreadPriority_RealtimeAudio, |
118 // Suitable for threads which generate data for the display (at ~60Hz). | 118 // Suitable for threads which generate data for the display (at ~60Hz). |
119 kThreadPriority_Display, | 119 kThreadPriority_Display, |
120 // Suitable for threads that shouldn't disrupt high priority work. | 120 // Suitable for threads that shouldn't disrupt high priority work. |
121 kThreadPriority_Background | 121 kThreadPriority_Background |
122 }; | 122 }; |
123 | 123 |
124 // A namespace for low-level thread functions. | 124 // A namespace for low-level thread functions. |
125 class BASE_EXPORT PlatformThread { | 125 class BASE_EXPORT PlatformThread { |
126 public: | 126 public: |
127 // Implement this interface to run code on a background thread. Your | 127 // Implement this interface to run code on a background thread. Your |
128 // ThreadMain method will be called on the newly created thread. | 128 // ThreadMain method will be called on the newly created thread. |
129 class BASE_EXPORT Delegate { | 129 class BASE_EXPORT Delegate { |
130 public: | 130 public: |
131 virtual void ThreadMain() = 0; | 131 virtual void ThreadMain() = 0; |
132 | 132 |
133 protected: | 133 protected: |
134 virtual ~Delegate() {} | 134 virtual ~Delegate() {} |
135 }; | 135 }; |
136 | 136 |
137 // Gets the current thread id, which may be useful for logging purposes. | 137 // Gets the current thread id, which may be useful for logging purposes. |
138 static PlatformThreadId CurrentId(); | 138 static PlatformThreadId CurrentId(); |
139 | 139 |
140 // Gets the current thread reference, which can be used to check if | 140 // Gets the current thread reference, which can be used to check if |
141 // we're on the right thread quickly. | 141 // we're on the right thread quickly. |
142 static PlatformThreadRef CurrentRef(); | 142 static PlatformThreadRef CurrentRef(); |
143 | 143 |
144 // Get the current handle. | 144 // Get the handle representing the current thread. On Windows, this is a |
| 145 // pseudo handle constant which will always represent the thread using it and |
| 146 // hence should not be shared with other threads nor be used to differentiate |
| 147 // the current thread from another. |
145 static PlatformThreadHandle CurrentHandle(); | 148 static PlatformThreadHandle CurrentHandle(); |
146 | 149 |
147 // Yield the current thread so another thread can be scheduled. | 150 // Yield the current thread so another thread can be scheduled. |
148 static void YieldCurrentThread(); | 151 static void YieldCurrentThread(); |
149 | 152 |
150 // Sleeps for the specified duration. | 153 // Sleeps for the specified duration. |
151 static void Sleep(base::TimeDelta duration); | 154 static void Sleep(base::TimeDelta duration); |
152 | 155 |
153 // Sets the thread name visible to debuggers/tools. This has no effect | 156 // Sets the thread name visible to debuggers/tools. This has no effect |
154 // otherwise. This name pointer is not copied internally. Thus, it must stay | 157 // otherwise. This name pointer is not copied internally. Thus, it must stay |
155 // valid until the thread ends. | 158 // valid until the thread ends. |
156 static void SetName(const char* name); | 159 static void SetName(const char* name); |
157 | 160 |
158 // Gets the thread name, if previously set by SetName. | 161 // Gets the thread name, if previously set by SetName. |
159 static const char* GetName(); | 162 static const char* GetName(); |
160 | 163 |
161 // Creates a new thread. The |stack_size| parameter can be 0 to indicate | 164 // Creates a new thread. The |stack_size| parameter can be 0 to indicate |
162 // that the default stack size should be used. Upon success, | 165 // that the default stack size should be used. Upon success, |
163 // |*thread_handle| will be assigned a handle to the newly created thread, | 166 // |*thread_handle| will be assigned a handle to the newly created thread, |
164 // and |delegate|'s ThreadMain method will be executed on the newly created | 167 // and |delegate|'s ThreadMain method will be executed on the newly created |
165 // thread. | 168 // thread. |
166 // NOTE: When you are done with the thread handle, you must call Join to | 169 // NOTE: When you are done with the thread handle, you must call Join to |
167 // release system resources associated with the thread. You must ensure that | 170 // release system resources associated with the thread. You must ensure that |
168 // the Delegate object outlives the thread. | 171 // the Delegate object outlives the thread. |
169 static bool Create(size_t stack_size, Delegate* delegate, | 172 static bool Create(size_t stack_size, Delegate* delegate, |
170 PlatformThreadHandle* thread_handle); | 173 PlatformThreadHandle* thread_handle); |
171 | 174 |
172 // CreateWithPriority() does the same thing as Create() except the priority of | 175 // CreateWithPriority() does the same thing as Create() except the priority of |
173 // the thread is set based on |priority|. Can be used in place of Create() | 176 // the thread is set based on |priority|. Can be used in place of Create() |
174 // followed by SetThreadPriority(). SetThreadPriority() has not been | 177 // followed by SetThreadPriority(). |
175 // implemented on the Linux platform yet, this is the only way to get a high | |
176 // priority thread on Linux. | |
177 static bool CreateWithPriority(size_t stack_size, Delegate* delegate, | 178 static bool CreateWithPriority(size_t stack_size, Delegate* delegate, |
178 PlatformThreadHandle* thread_handle, | 179 PlatformThreadHandle* thread_handle, |
179 ThreadPriority priority); | 180 ThreadPriority priority); |
180 | 181 |
181 // CreateNonJoinable() does the same thing as Create() except the thread | 182 // CreateNonJoinable() does the same thing as Create() except the thread |
182 // cannot be Join()'d. Therefore, it also does not output a | 183 // cannot be Join()'d. Therefore, it also does not output a |
183 // PlatformThreadHandle. | 184 // PlatformThreadHandle. |
184 static bool CreateNonJoinable(size_t stack_size, Delegate* delegate); | 185 static bool CreateNonJoinable(size_t stack_size, Delegate* delegate); |
185 | 186 |
186 // Joins with a thread created via the Create function. This function blocks | 187 // Joins with a thread created via the Create function. This function blocks |
187 // the caller until the designated thread exits. This will invalidate | 188 // the caller until the designated thread exits. This will invalidate |
188 // |thread_handle|. | 189 // |thread_handle|. |
189 static void Join(PlatformThreadHandle thread_handle); | 190 static void Join(PlatformThreadHandle thread_handle); |
190 | 191 |
191 static void SetThreadPriority(PlatformThreadHandle handle, | 192 static void SetThreadPriority(PlatformThreadHandle handle, |
192 ThreadPriority priority); | 193 ThreadPriority priority); |
193 | 194 |
| 195 static ThreadPriority GetThreadPriority(PlatformThreadHandle handle); |
| 196 |
194 private: | 197 private: |
195 DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformThread); | 198 DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformThread); |
196 }; | 199 }; |
197 | 200 |
198 } // namespace base | 201 } // namespace base |
199 | 202 |
200 #endif // BASE_THREADING_PLATFORM_THREAD_H_ | 203 #endif // BASE_THREADING_PLATFORM_THREAD_H_ |
OLD | NEW |