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, |
rvargas (doing something else)
2015/03/19 22:19:21
Let's order this according to priority.
gab
2015/03/30 20:14:45
I was thinking about that too, but I was thinking
rvargas (doing something else)
2015/03/30 22:31:22
I doesn't have to be part of this CL, but we need
| |
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. Extra |
121 // background properties (low IO/memory priority) can be achieved on some | |
122 // platforms if this mode is set from the current thread. | |
rvargas (doing something else)
2015/03/19 22:19:21
nit: "if this mode"? Do you mean this priority?
gab
2015/03/30 20:14:45
Done.
| |
121 kThreadPriority_Background | 123 kThreadPriority_Background |
122 }; | 124 }; |
123 | 125 |
124 // A namespace for low-level thread functions. | 126 // A namespace for low-level thread functions. |
125 class BASE_EXPORT PlatformThread { | 127 class BASE_EXPORT PlatformThread { |
126 public: | 128 public: |
127 // Implement this interface to run code on a background thread. Your | 129 // Implement this interface to run code on a background thread. Your |
128 // ThreadMain method will be called on the newly created thread. | 130 // ThreadMain method will be called on the newly created thread. |
129 class BASE_EXPORT Delegate { | 131 class BASE_EXPORT Delegate { |
130 public: | 132 public: |
131 virtual void ThreadMain() = 0; | 133 virtual void ThreadMain() = 0; |
132 | 134 |
133 protected: | 135 protected: |
134 virtual ~Delegate() {} | 136 virtual ~Delegate() {} |
135 }; | 137 }; |
136 | 138 |
137 // Gets the current thread id, which may be useful for logging purposes. | 139 // Gets the current thread id, which may be useful for logging purposes. |
138 static PlatformThreadId CurrentId(); | 140 static PlatformThreadId CurrentId(); |
139 | 141 |
140 // Gets the current thread reference, which can be used to check if | 142 // Gets the current thread reference, which can be used to check if |
141 // we're on the right thread quickly. | 143 // we're on the right thread quickly. |
142 static PlatformThreadRef CurrentRef(); | 144 static PlatformThreadRef CurrentRef(); |
143 | 145 |
144 // Get the current handle. | 146 // Get the handle representing the current thread. On some platforms, this can |
147 // be a pseudo handle constant which will always represent the thread using it | |
148 // and hence should not be shared with other threads nor be used to | |
149 // differentiate the current thread from another. | |
145 static PlatformThreadHandle CurrentHandle(); | 150 static PlatformThreadHandle CurrentHandle(); |
146 | 151 |
147 // Yield the current thread so another thread can be scheduled. | 152 // Yield the current thread so another thread can be scheduled. |
148 static void YieldCurrentThread(); | 153 static void YieldCurrentThread(); |
149 | 154 |
150 // Sleeps for the specified duration. | 155 // Sleeps for the specified duration. |
151 static void Sleep(base::TimeDelta duration); | 156 static void Sleep(base::TimeDelta duration); |
152 | 157 |
153 // Sets the thread name visible to debuggers/tools. This has no effect | 158 // 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 | 159 // otherwise. This name pointer is not copied internally. Thus, it must stay |
155 // valid until the thread ends. | 160 // valid until the thread ends. |
156 static void SetName(const char* name); | 161 static void SetName(const char* name); |
157 | 162 |
158 // Gets the thread name, if previously set by SetName. | 163 // Gets the thread name, if previously set by SetName. |
159 static const char* GetName(); | 164 static const char* GetName(); |
160 | 165 |
161 // Creates a new thread. The |stack_size| parameter can be 0 to indicate | 166 // Creates a new thread. The |stack_size| parameter can be 0 to indicate |
162 // that the default stack size should be used. Upon success, | 167 // that the default stack size should be used. Upon success, |
163 // |*thread_handle| will be assigned a handle to the newly created thread, | 168 // |*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 | 169 // and |delegate|'s ThreadMain method will be executed on the newly created |
165 // thread. | 170 // thread. |
166 // NOTE: When you are done with the thread handle, you must call Join to | 171 // 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 | 172 // release system resources associated with the thread. You must ensure that |
168 // the Delegate object outlives the thread. | 173 // the Delegate object outlives the thread. |
169 static bool Create(size_t stack_size, Delegate* delegate, | 174 static bool Create(size_t stack_size, Delegate* delegate, |
170 PlatformThreadHandle* thread_handle); | 175 PlatformThreadHandle* thread_handle); |
171 | 176 |
172 // CreateWithPriority() does the same thing as Create() except the priority of | 177 // 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() | 178 // the thread is set based on |priority|. Can be used in place of Create() |
174 // followed by SetThreadPriority(). SetThreadPriority() has not been | 179 // 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, | 180 static bool CreateWithPriority(size_t stack_size, Delegate* delegate, |
178 PlatformThreadHandle* thread_handle, | 181 PlatformThreadHandle* thread_handle, |
179 ThreadPriority priority); | 182 ThreadPriority priority); |
180 | 183 |
181 // CreateNonJoinable() does the same thing as Create() except the thread | 184 // CreateNonJoinable() does the same thing as Create() except the thread |
182 // cannot be Join()'d. Therefore, it also does not output a | 185 // cannot be Join()'d. Therefore, it also does not output a |
183 // PlatformThreadHandle. | 186 // PlatformThreadHandle. |
184 static bool CreateNonJoinable(size_t stack_size, Delegate* delegate); | 187 static bool CreateNonJoinable(size_t stack_size, Delegate* delegate); |
185 | 188 |
186 // Joins with a thread created via the Create function. This function blocks | 189 // Joins with a thread created via the Create function. This function blocks |
187 // the caller until the designated thread exits. This will invalidate | 190 // the caller until the designated thread exits. This will invalidate |
188 // |thread_handle|. | 191 // |thread_handle|. |
189 static void Join(PlatformThreadHandle thread_handle); | 192 static void Join(PlatformThreadHandle thread_handle); |
190 | 193 |
191 static void SetThreadPriority(PlatformThreadHandle handle, | 194 static void SetThreadPriority(PlatformThreadHandle handle, |
192 ThreadPriority priority); | 195 ThreadPriority priority); |
193 | 196 |
197 static ThreadPriority GetThreadPriority(PlatformThreadHandle handle); | |
198 | |
194 private: | 199 private: |
195 DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformThread); | 200 DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformThread); |
196 }; | 201 }; |
197 | 202 |
198 } // namespace base | 203 } // namespace base |
199 | 204 |
200 #endif // BASE_THREADING_PLATFORM_THREAD_H_ | 205 #endif // BASE_THREADING_PLATFORM_THREAD_H_ |
OLD | NEW |