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
3 // found in the LICENSE file.
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.
7 // You should instead be using a message-loop driven Thread, see thread.h.
9 #ifndef BASE_THREADING_PLATFORM_THREAD_H_
10 #define BASE_THREADING_PLATFORM_THREAD_H_
12 #include "base/base_export.h"
13 #include "base/basictypes.h"
14 #include "base/time/time.h"
15 #include "build/build_config.h"
19 #elif defined(OS_POSIX)
26 // Used for logging. Always an integer value.
28 typedef DWORD PlatformThreadId
;
29 #elif defined(OS_POSIX)
30 typedef pid_t PlatformThreadId
;
33 // Used for thread checking and debugging.
34 // Meant to be as fast as possible.
35 // These are produced by PlatformThread::CurrentRef(), and used to later
36 // check if we are on the same thread or not by using ==. These are safe
37 // to copy between threads, but can't be copied to another process as they
38 // have no meaning there. Also, the internal identifier can be re-used
39 // after a thread dies, so a PlatformThreadRef cannot be reliably used
40 // to distinguish a new thread from an old, dead thread.
41 class PlatformThreadRef
{
44 typedef DWORD RefType
;
45 #elif defined(OS_POSIX)
46 typedef pthread_t RefType
;
52 explicit PlatformThreadRef(RefType id
)
56 bool operator==(PlatformThreadRef other
) const {
57 return id_
== other
.id_
;
60 bool is_null() const {
67 // Used to operate on threads.
68 class PlatformThreadHandle
{
72 #elif defined(OS_POSIX)
73 typedef pthread_t Handle
;
76 PlatformThreadHandle() : handle_(0) {}
78 explicit PlatformThreadHandle(Handle handle
) : handle_(handle
) {}
80 bool is_equal(const PlatformThreadHandle
& other
) const {
81 return handle_
== other
.handle_
;
84 bool is_null() const {
88 Handle
platform_handle() const {
96 const PlatformThreadId
kInvalidThreadId(0);
98 // Valid values for priority of Thread::Options and SimpleThread::Options, and
99 // SetCurrentThreadPriority(), listed in increasing order of importance.
100 enum class ThreadPriority
{
101 // Suitable for threads that shouldn't disrupt high priority work.
103 // Default priority level.
105 // Suitable for threads which generate data for the display (at ~60Hz).
107 // Suitable for low-latency, glitch-resistant audio.
111 // A namespace for low-level thread functions.
112 class BASE_EXPORT PlatformThread
{
114 // Implement this interface to run code on a background thread. Your
115 // ThreadMain method will be called on the newly created thread.
116 class BASE_EXPORT Delegate
{
118 virtual void ThreadMain() = 0;
121 virtual ~Delegate() {}
124 // Gets the current thread id, which may be useful for logging purposes.
125 static PlatformThreadId
CurrentId();
127 // Gets the current thread reference, which can be used to check if
128 // we're on the right thread quickly.
129 static PlatformThreadRef
CurrentRef();
131 // Get the handle representing the current thread. On Windows, this is a
132 // pseudo handle constant which will always represent the thread using it and
133 // hence should not be shared with other threads nor be used to differentiate
134 // the current thread from another.
135 static PlatformThreadHandle
CurrentHandle();
137 // Yield the current thread so another thread can be scheduled.
138 static void YieldCurrentThread();
140 // Sleeps for the specified duration.
141 static void Sleep(base::TimeDelta duration
);
143 // Sets the thread name visible to debuggers/tools. This has no effect
145 static void SetName(const std::string
& name
);
147 // Gets the thread name, if previously set by SetName.
148 static const char* GetName();
150 // Creates a new thread. The |stack_size| parameter can be 0 to indicate
151 // that the default stack size should be used. Upon success,
152 // |*thread_handle| will be assigned a handle to the newly created thread,
153 // and |delegate|'s ThreadMain method will be executed on the newly created
155 // NOTE: When you are done with the thread handle, you must call Join to
156 // release system resources associated with the thread. You must ensure that
157 // the Delegate object outlives the thread.
158 static bool Create(size_t stack_size
,
160 PlatformThreadHandle
* thread_handle
) {
161 return CreateWithPriority(stack_size
, delegate
, thread_handle
,
162 ThreadPriority::NORMAL
);
165 // CreateWithPriority() does the same thing as Create() except the priority of
166 // the thread is set based on |priority|.
167 static bool CreateWithPriority(size_t stack_size
, Delegate
* delegate
,
168 PlatformThreadHandle
* thread_handle
,
169 ThreadPriority priority
);
171 // CreateNonJoinable() does the same thing as Create() except the thread
172 // cannot be Join()'d. Therefore, it also does not output a
173 // PlatformThreadHandle.
174 static bool CreateNonJoinable(size_t stack_size
, Delegate
* delegate
);
176 // Joins with a thread created via the Create function. This function blocks
177 // the caller until the designated thread exits. This will invalidate
179 static void Join(PlatformThreadHandle thread_handle
);
181 // Toggles the current thread's priority at runtime. A thread may not be able
182 // to raise its priority back up after lowering it if the process does not
183 // have a proper permission, e.g. CAP_SYS_NICE on Linux.
184 // Since changing other threads' priority is not permitted in favor of
185 // security, this interface is restricted to change only the current thread
186 // priority (https://crbug.com/399473).
187 static void SetCurrentThreadPriority(ThreadPriority priority
);
189 static ThreadPriority
GetCurrentThreadPriority();
192 DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformThread
);
197 #endif // BASE_THREADING_PLATFORM_THREAD_H_