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_ |
11 | 11 |
12 #include "base/base_export.h" | 12 #include "base/base_export.h" |
13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
15 #include "build/build_config.h" | 15 #include "build/build_config.h" |
16 | 16 |
17 #if defined(OS_WIN) | 17 #if defined(OS_WIN) |
18 #include <windows.h> | 18 #include <windows.h> |
19 #elif defined(OS_POSIX) | 19 #elif defined(OS_POSIX) |
20 #include <pthread.h> | 20 #include <pthread.h> |
21 #include <unistd.h> | 21 #include <unistd.h> |
22 #endif | 22 #endif |
23 | 23 |
24 namespace base { | 24 namespace base { |
25 | 25 |
| 26 // Used for logging. Always an integer value. |
26 #if defined(OS_WIN) | 27 #if defined(OS_WIN) |
27 typedef DWORD PlatformThreadId; | 28 typedef DWORD PlatformThreadId; |
28 #elif defined(OS_POSIX) | 29 #elif defined(OS_POSIX) |
29 typedef pid_t PlatformThreadId; | 30 typedef pid_t PlatformThreadId; |
30 #endif | 31 #endif |
31 | 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. |
32 class PlatformThreadHandle { | 68 class PlatformThreadHandle { |
33 public: | 69 public: |
34 #if defined(OS_WIN) | 70 #if defined(OS_WIN) |
35 typedef void* Handle; | 71 typedef void* Handle; |
36 #elif defined(OS_POSIX) | 72 #elif defined(OS_POSIX) |
37 typedef pthread_t Handle; | 73 typedef pthread_t Handle; |
38 #endif | 74 #endif |
39 | 75 |
40 PlatformThreadHandle() | 76 PlatformThreadHandle() |
41 : handle_(0), | 77 : handle_(0), |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 public: | 130 public: |
95 virtual void ThreadMain() = 0; | 131 virtual void ThreadMain() = 0; |
96 | 132 |
97 protected: | 133 protected: |
98 virtual ~Delegate() {} | 134 virtual ~Delegate() {} |
99 }; | 135 }; |
100 | 136 |
101 // 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. |
102 static PlatformThreadId CurrentId(); | 138 static PlatformThreadId CurrentId(); |
103 | 139 |
| 140 // Gets the current thread reference, which can be used to check if |
| 141 // we're on the right thread quickly. |
| 142 static PlatformThreadRef CurrentRef(); |
| 143 |
104 // Get the current handle. | 144 // Get the current handle. |
105 static PlatformThreadHandle CurrentHandle(); | 145 static PlatformThreadHandle CurrentHandle(); |
106 | 146 |
107 // Yield the current thread so another thread can be scheduled. | 147 // Yield the current thread so another thread can be scheduled. |
108 static void YieldCurrentThread(); | 148 static void YieldCurrentThread(); |
109 | 149 |
110 // Sleeps for the specified duration. | 150 // Sleeps for the specified duration. |
111 static void Sleep(base::TimeDelta duration); | 151 static void Sleep(base::TimeDelta duration); |
112 | 152 |
113 // Sets the thread name visible to debuggers/tools. This has no effect | 153 // Sets the thread name visible to debuggers/tools. This has no effect |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 static void SetThreadPriority(PlatformThreadHandle handle, | 191 static void SetThreadPriority(PlatformThreadHandle handle, |
152 ThreadPriority priority); | 192 ThreadPriority priority); |
153 | 193 |
154 private: | 194 private: |
155 DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformThread); | 195 DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformThread); |
156 }; | 196 }; |
157 | 197 |
158 } // namespace base | 198 } // namespace base |
159 | 199 |
160 #endif // BASE_THREADING_PLATFORM_THREAD_H_ | 200 #endif // BASE_THREADING_PLATFORM_THREAD_H_ |
OLD | NEW |