| 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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 : handle_(handle), | 82 : handle_(handle), |
| 83 id_(0) { | 83 id_(0) { |
| 84 } | 84 } |
| 85 | 85 |
| 86 PlatformThreadHandle(Handle handle, | 86 PlatformThreadHandle(Handle handle, |
| 87 PlatformThreadId id) | 87 PlatformThreadId id) |
| 88 : handle_(handle), | 88 : handle_(handle), |
| 89 id_(id) { | 89 id_(id) { |
| 90 } | 90 } |
| 91 | 91 |
| 92 // TODO(toyoshim): Remove id() and use PlatformThread::CurrentId() instead. |
| 93 // See http://crbug.com/468793 for related changes. |
| 92 PlatformThreadId id() const { | 94 PlatformThreadId id() const { |
| 93 return id_; | 95 return id_; |
| 94 } | 96 } |
| 95 | 97 |
| 96 bool is_equal(const PlatformThreadHandle& other) const { | 98 bool is_equal(const PlatformThreadHandle& other) const { |
| 97 return handle_ == other.handle_; | 99 return handle_ == other.handle_; |
| 98 } | 100 } |
| 99 | 101 |
| 100 bool is_null() const { | 102 bool is_null() const { |
| 101 return !handle_; | 103 return !handle_; |
| 102 } | 104 } |
| 103 | 105 |
| 104 Handle platform_handle() const { | 106 Handle platform_handle() const { |
| 105 return handle_; | 107 return handle_; |
| 106 } | 108 } |
| 107 | 109 |
| 108 private: | 110 private: |
| 109 Handle handle_; | 111 Handle handle_; |
| 110 PlatformThreadId id_; | 112 PlatformThreadId id_; |
| 111 }; | 113 }; |
| 112 | 114 |
| 113 const PlatformThreadId kInvalidThreadId(0); | 115 const PlatformThreadId kInvalidThreadId(0); |
| 114 | 116 |
| 115 // Valid values for SetThreadPriority(), listed in increasing order of | 117 // Valid values for priority of Thread::Options and SimpleThread::Options, and |
| 116 // importance. | 118 // SetCurrentThreadPriority(), listed in increasing order of importance. |
| 117 enum class ThreadPriority { | 119 enum class ThreadPriority { |
| 118 // Suitable for threads that shouldn't disrupt high priority work. | 120 // Suitable for threads that shouldn't disrupt high priority work. |
| 119 BACKGROUND, | 121 BACKGROUND, |
| 120 // Default priority level. | 122 // Default priority level. |
| 121 NORMAL, | 123 NORMAL, |
| 122 // Suitable for threads which generate data for the display (at ~60Hz). | 124 // Suitable for threads which generate data for the display (at ~60Hz). |
| 123 DISPLAY, | 125 DISPLAY, |
| 124 // Suitable for low-latency, glitch-resistant audio. | 126 // Suitable for low-latency, glitch-resistant audio. |
| 125 REALTIME_AUDIO, | 127 REALTIME_AUDIO, |
| 126 }; | 128 }; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 // |*thread_handle| will be assigned a handle to the newly created thread, | 171 // |*thread_handle| will be assigned a handle to the newly created thread, |
| 170 // and |delegate|'s ThreadMain method will be executed on the newly created | 172 // and |delegate|'s ThreadMain method will be executed on the newly created |
| 171 // thread. | 173 // thread. |
| 172 // NOTE: When you are done with the thread handle, you must call Join to | 174 // NOTE: When you are done with the thread handle, you must call Join to |
| 173 // release system resources associated with the thread. You must ensure that | 175 // release system resources associated with the thread. You must ensure that |
| 174 // the Delegate object outlives the thread. | 176 // the Delegate object outlives the thread. |
| 175 static bool Create(size_t stack_size, Delegate* delegate, | 177 static bool Create(size_t stack_size, Delegate* delegate, |
| 176 PlatformThreadHandle* thread_handle); | 178 PlatformThreadHandle* thread_handle); |
| 177 | 179 |
| 178 // CreateWithPriority() does the same thing as Create() except the priority of | 180 // CreateWithPriority() does the same thing as Create() except the priority of |
| 179 // the thread is set based on |priority|. Can be used in place of Create() | 181 // the thread is set based on |priority|. |
| 180 // followed by SetThreadPriority(). | |
| 181 static bool CreateWithPriority(size_t stack_size, Delegate* delegate, | 182 static bool CreateWithPriority(size_t stack_size, Delegate* delegate, |
| 182 PlatformThreadHandle* thread_handle, | 183 PlatformThreadHandle* thread_handle, |
| 183 ThreadPriority priority); | 184 ThreadPriority priority); |
| 184 | 185 |
| 185 // CreateNonJoinable() does the same thing as Create() except the thread | 186 // CreateNonJoinable() does the same thing as Create() except the thread |
| 186 // cannot be Join()'d. Therefore, it also does not output a | 187 // cannot be Join()'d. Therefore, it also does not output a |
| 187 // PlatformThreadHandle. | 188 // PlatformThreadHandle. |
| 188 static bool CreateNonJoinable(size_t stack_size, Delegate* delegate); | 189 static bool CreateNonJoinable(size_t stack_size, Delegate* delegate); |
| 189 | 190 |
| 190 // Joins with a thread created via the Create function. This function blocks | 191 // Joins with a thread created via the Create function. This function blocks |
| 191 // the caller until the designated thread exits. This will invalidate | 192 // the caller until the designated thread exits. This will invalidate |
| 192 // |thread_handle|. | 193 // |thread_handle|. |
| 193 static void Join(PlatformThreadHandle thread_handle); | 194 static void Join(PlatformThreadHandle thread_handle); |
| 194 | 195 |
| 195 // Toggles the target thread's priority at runtime. Prefer | 196 // Toggles the current thread's priority at runtime. A thread may not be able |
| 196 // CreateWithPriority() to set the thread's initial priority. | 197 // to raise its priority back up after lowering it if the process does not |
| 197 // NOTE: The call may fail if the caller thread is not the same as the | 198 // have a proper permission, e.g. CAP_SYS_NICE on Linux. |
| 198 // target thread on POSIX. For example, seccomp-bpf blocks it by default | 199 // Since changing other threads' priority is not permitted in favor of |
| 199 // in the sandbox. | 200 // security, this interface is restricted to change only the current thread |
| 200 static void SetThreadPriority(PlatformThreadHandle handle, | 201 // priority (https://crbug.com/399473). |
| 201 ThreadPriority priority); | 202 static void SetCurrentThreadPriority(ThreadPriority priority); |
| 202 | 203 |
| 203 static ThreadPriority GetThreadPriority(PlatformThreadHandle handle); | 204 static ThreadPriority GetCurrentThreadPriority(); |
| 204 | 205 |
| 205 private: | 206 private: |
| 206 DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformThread); | 207 DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformThread); |
| 207 }; | 208 }; |
| 208 | 209 |
| 209 } // namespace base | 210 } // namespace base |
| 210 | 211 |
| 211 #endif // BASE_THREADING_PLATFORM_THREAD_H_ | 212 #endif // BASE_THREADING_PLATFORM_THREAD_H_ |
| OLD | NEW |