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.h" | 14 #include "base/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 // PlatformThreadHandle should not be assumed to be a numeric type, since the | |
27 // standard intends to allow pthread_t to be a structure. This means you | |
28 // should not initialize it to a value, like 0. If it's a member variable, the | |
29 // constructor can safely "value initialize" using () in the initializer list. | |
30 #if defined(OS_WIN) | 26 #if defined(OS_WIN) |
31 typedef DWORD PlatformThreadId; | 27 typedef DWORD PlatformThreadId; |
32 typedef void* PlatformThreadHandle; // HANDLE | |
33 const PlatformThreadHandle kNullThreadHandle = NULL; | |
34 #elif defined(OS_POSIX) | 28 #elif defined(OS_POSIX) |
35 typedef pthread_t PlatformThreadHandle; | |
36 const PlatformThreadHandle kNullThreadHandle = 0; | |
37 typedef pid_t PlatformThreadId; | 29 typedef pid_t PlatformThreadId; |
38 #endif | 30 #endif |
39 | 31 |
40 const PlatformThreadId kInvalidThreadId = 0; | 32 class PlatformThreadHandle { |
| 33 public: |
| 34 #if defined(OS_WIN) |
| 35 typedef void* Handle; |
| 36 #elif defined(OS_POSIX) |
| 37 typedef pthread_t Handle; |
| 38 #endif |
| 39 |
| 40 PlatformThreadHandle() |
| 41 : handle_(0), |
| 42 id_(0) { |
| 43 } |
| 44 |
| 45 explicit PlatformThreadHandle(Handle handle) |
| 46 : handle_(handle), |
| 47 id_(0) { |
| 48 } |
| 49 |
| 50 PlatformThreadHandle(Handle handle, |
| 51 PlatformThreadId id) |
| 52 : handle_(handle), |
| 53 id_(id) { |
| 54 } |
| 55 |
| 56 bool is_equal(const PlatformThreadHandle& other) { |
| 57 return handle_ == other.handle_; |
| 58 } |
| 59 |
| 60 bool is_null() { |
| 61 return !handle_; |
| 62 } |
| 63 |
| 64 Handle platform_handle() { |
| 65 return handle_; |
| 66 } |
| 67 |
| 68 private: |
| 69 friend class PlatformThread; |
| 70 |
| 71 Handle handle_; |
| 72 PlatformThreadId id_; |
| 73 }; |
| 74 |
| 75 const PlatformThreadId kInvalidThreadId(0); |
41 | 76 |
42 // Valid values for SetThreadPriority() | 77 // Valid values for SetThreadPriority() |
43 enum ThreadPriority{ | 78 enum ThreadPriority{ |
44 kThreadPriority_Normal, | 79 kThreadPriority_Normal, |
45 // Suitable for low-latency, glitch-resistant audio. | 80 // Suitable for low-latency, glitch-resistant audio. |
46 kThreadPriority_RealtimeAudio | 81 kThreadPriority_RealtimeAudio, |
| 82 // Suitable for threads which generate data for the display (at ~60Hz). |
| 83 kThreadPriority_Display, |
| 84 // Suitable for threads that shouldn't disrupt high priority work. |
| 85 kThreadPriority_Background |
47 }; | 86 }; |
48 | 87 |
49 // A namespace for low-level thread functions. | 88 // A namespace for low-level thread functions. |
50 class BASE_EXPORT PlatformThread { | 89 class BASE_EXPORT PlatformThread { |
51 public: | 90 public: |
52 // Implement this interface to run code on a background thread. Your | 91 // Implement this interface to run code on a background thread. Your |
53 // ThreadMain method will be called on the newly created thread. | 92 // ThreadMain method will be called on the newly created thread. |
54 class BASE_EXPORT Delegate { | 93 class BASE_EXPORT Delegate { |
55 public: | 94 public: |
56 virtual void ThreadMain() = 0; | 95 virtual void ThreadMain() = 0; |
57 | 96 |
58 protected: | 97 protected: |
59 virtual ~Delegate() {} | 98 virtual ~Delegate() {} |
60 }; | 99 }; |
61 | 100 |
62 // Gets the current thread id, which may be useful for logging purposes. | 101 // Gets the current thread id, which may be useful for logging purposes. |
63 static PlatformThreadId CurrentId(); | 102 static PlatformThreadId CurrentId(); |
64 | 103 |
| 104 // Get the current handle. |
| 105 static PlatformThreadHandle CurrentHandle(); |
| 106 |
65 // Yield the current thread so another thread can be scheduled. | 107 // Yield the current thread so another thread can be scheduled. |
66 static void YieldCurrentThread(); | 108 static void YieldCurrentThread(); |
67 | 109 |
68 // Sleeps for the specified duration. | 110 // Sleeps for the specified duration. |
69 static void Sleep(base::TimeDelta duration); | 111 static void Sleep(base::TimeDelta duration); |
70 | 112 |
71 // Sets the thread name visible to debuggers/tools. This has no effect | 113 // Sets the thread name visible to debuggers/tools. This has no effect |
72 // otherwise. This name pointer is not copied internally. Thus, it must stay | 114 // otherwise. This name pointer is not copied internally. Thus, it must stay |
73 // valid until the thread ends. | 115 // valid until the thread ends. |
74 static void SetName(const char* name); | 116 static void SetName(const char* name); |
(...skipping 24 matching lines...) Expand all Loading... |
99 // CreateNonJoinable() does the same thing as Create() except the thread | 141 // CreateNonJoinable() does the same thing as Create() except the thread |
100 // cannot be Join()'d. Therefore, it also does not output a | 142 // cannot be Join()'d. Therefore, it also does not output a |
101 // PlatformThreadHandle. | 143 // PlatformThreadHandle. |
102 static bool CreateNonJoinable(size_t stack_size, Delegate* delegate); | 144 static bool CreateNonJoinable(size_t stack_size, Delegate* delegate); |
103 | 145 |
104 // Joins with a thread created via the Create function. This function blocks | 146 // Joins with a thread created via the Create function. This function blocks |
105 // the caller until the designated thread exits. This will invalidate | 147 // the caller until the designated thread exits. This will invalidate |
106 // |thread_handle|. | 148 // |thread_handle|. |
107 static void Join(PlatformThreadHandle thread_handle); | 149 static void Join(PlatformThreadHandle thread_handle); |
108 | 150 |
109 // Sets the priority of the thread specified in |handle| to |priority|. | |
110 // This does not work on Linux, use CreateWithPriority() instead. | |
111 static void SetThreadPriority(PlatformThreadHandle handle, | 151 static void SetThreadPriority(PlatformThreadHandle handle, |
112 ThreadPriority priority); | 152 ThreadPriority priority); |
113 | 153 |
114 private: | 154 private: |
115 DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformThread); | 155 DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformThread); |
116 }; | 156 }; |
117 | 157 |
118 } // namespace base | 158 } // namespace base |
119 | 159 |
120 #endif // BASE_THREADING_PLATFORM_THREAD_H_ | 160 #endif // BASE_THREADING_PLATFORM_THREAD_H_ |
OLD | NEW |