Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(354)

Side by Side Diff: base/threading/platform_thread.h

Issue 1647803004: Move base to DEPS (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/threading/non_thread_safe_unittest.cc ('k') | base/threading/platform_thread_android.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
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.
8
9 #ifndef BASE_THREADING_PLATFORM_THREAD_H_
10 #define BASE_THREADING_PLATFORM_THREAD_H_
11
12 #include "base/base_export.h"
13 #include "base/basictypes.h"
14 #include "base/time/time.h"
15 #include "build/build_config.h"
16
17 #if defined(OS_WIN)
18 #include <windows.h>
19 #elif defined(OS_POSIX)
20 #include <pthread.h>
21 #include <unistd.h>
22 #endif
23
24 namespace base {
25
26 // Used for logging. Always an integer value.
27 #if defined(OS_WIN)
28 typedef DWORD PlatformThreadId;
29 #elif defined(OS_POSIX)
30 typedef pid_t PlatformThreadId;
31 #endif
32
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 {
42 public:
43 #if defined(OS_WIN)
44 typedef DWORD RefType;
45 #elif defined(OS_POSIX)
46 typedef pthread_t RefType;
47 #endif
48 PlatformThreadRef()
49 : id_(0) {
50 }
51
52 explicit PlatformThreadRef(RefType id)
53 : id_(id) {
54 }
55
56 bool operator==(PlatformThreadRef other) const {
57 return id_ == other.id_;
58 }
59
60 bool is_null() const {
61 return id_ == 0;
62 }
63 private:
64 RefType id_;
65 };
66
67 // Used to operate on threads.
68 class PlatformThreadHandle {
69 public:
70 #if defined(OS_WIN)
71 typedef void* Handle;
72 #elif defined(OS_POSIX)
73 typedef pthread_t Handle;
74 #endif
75
76 PlatformThreadHandle()
77 : handle_(0),
78 id_(0) {
79 }
80
81 explicit PlatformThreadHandle(Handle handle)
82 : handle_(handle),
83 id_(0) {
84 }
85
86 PlatformThreadHandle(Handle handle,
87 PlatformThreadId id)
88 : handle_(handle),
89 id_(id) {
90 }
91
92 // TODO(toyoshim): Remove id() and use PlatformThread::CurrentId() instead.
93 PlatformThreadId id() const {
94 return id_;
95 }
96
97 bool is_equal(const PlatformThreadHandle& other) const {
98 return handle_ == other.handle_;
99 }
100
101 bool is_null() const {
102 return !handle_;
103 }
104
105 Handle platform_handle() const {
106 return handle_;
107 }
108
109 private:
110 Handle handle_;
111 PlatformThreadId id_;
112 };
113
114 const PlatformThreadId kInvalidThreadId(0);
115
116 // Valid values for priority of Thread::Options and SimpleThread::Options, and
117 // SetCurrentThreadPriority(), listed in increasing order of importance.
118 enum class ThreadPriority {
119 // Suitable for threads that shouldn't disrupt high priority work.
120 BACKGROUND,
121 // Default priority level.
122 NORMAL,
123 // Suitable for threads which generate data for the display (at ~60Hz).
124 DISPLAY,
125 // Suitable for low-latency, glitch-resistant audio.
126 REALTIME_AUDIO,
127 };
128
129 // A namespace for low-level thread functions.
130 class BASE_EXPORT PlatformThread {
131 public:
132 // Implement this interface to run code on a background thread. Your
133 // ThreadMain method will be called on the newly created thread.
134 class BASE_EXPORT Delegate {
135 public:
136 virtual void ThreadMain() = 0;
137
138 protected:
139 virtual ~Delegate() {}
140 };
141
142 // Gets the current thread id, which may be useful for logging purposes.
143 static PlatformThreadId CurrentId();
144
145 // Gets the current thread reference, which can be used to check if
146 // we're on the right thread quickly.
147 static PlatformThreadRef CurrentRef();
148
149 // Get the handle representing the current thread. On Windows, this is a
150 // pseudo handle constant which will always represent the thread using it and
151 // hence should not be shared with other threads nor be used to differentiate
152 // the current thread from another.
153 static PlatformThreadHandle CurrentHandle();
154
155 // Yield the current thread so another thread can be scheduled.
156 static void YieldCurrentThread();
157
158 // Sleeps for the specified duration.
159 static void Sleep(base::TimeDelta duration);
160
161 // Sets the thread name visible to debuggers/tools. This has no effect
162 // otherwise.
163 static void SetName(const std::string& name);
164
165 // Gets the thread name, if previously set by SetName.
166 static const char* GetName();
167
168 // Creates a new thread. The |stack_size| parameter can be 0 to indicate
169 // that the default stack size should be used. Upon success,
170 // |*thread_handle| will be assigned a handle to the newly created thread,
171 // and |delegate|'s ThreadMain method will be executed on the newly created
172 // thread.
173 // NOTE: When you are done with the thread handle, you must call Join to
174 // release system resources associated with the thread. You must ensure that
175 // the Delegate object outlives the thread.
176 static bool Create(size_t stack_size, Delegate* delegate,
177 PlatformThreadHandle* thread_handle);
178
179 // CreateWithPriority() does the same thing as Create() except the priority of
180 // the thread is set based on |priority|.
181 static bool CreateWithPriority(size_t stack_size, Delegate* delegate,
182 PlatformThreadHandle* thread_handle,
183 ThreadPriority priority);
184
185 // CreateNonJoinable() does the same thing as Create() except the thread
186 // cannot be Join()'d. Therefore, it also does not output a
187 // PlatformThreadHandle.
188 static bool CreateNonJoinable(size_t stack_size, Delegate* delegate);
189
190 // Joins with a thread created via the Create function. This function blocks
191 // the caller until the designated thread exits. This will invalidate
192 // |thread_handle|.
193 static void Join(PlatformThreadHandle thread_handle);
194
195 // Toggles the current thread's priority at runtime. A thread may not be able
196 // to raise its priority back up after lowering it if the process does not
197 // have a proper permission, e.g. CAP_SYS_NICE on Linux.
198 // Since changing other threads' priority is not permitted in favor of
199 // security, this interface is restricted to change only the current thread
200 // priority (https://crbug.com/399473).
201 static void SetCurrentThreadPriority(ThreadPriority priority);
202
203 static ThreadPriority GetCurrentThreadPriority();
204
205 private:
206 DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformThread);
207 };
208
209 } // namespace base
210
211 #endif // BASE_THREADING_PLATFORM_THREAD_H_
OLDNEW
« no previous file with comments | « base/threading/non_thread_safe_unittest.cc ('k') | base/threading/platform_thread_android.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698