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 |
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), | |
jar (doing other things)
2013/05/17 22:51:48
nit: colon should be indented by 4 (same on lines
epenner
2013/05/17 23:46:35
Done.
| |
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 equals(const PlatformThreadHandle& other) { | |
jar (doing other things)
2013/05/17 22:51:48
nit: probably better is
is_equal
or
IsEqual
epenner
2013/05/17 23:46:35
Done.
| |
57 return handle_ == other.handle_; | |
58 } | |
59 | |
60 bool isNull() { | |
jar (doing other things)
2013/05/17 22:51:48
nit: bouncy caps, or hacker style... but this was
epenner
2013/05/17 23:46:35
Done.
| |
61 return !handle_; | |
62 } | |
63 | |
64 Handle platform_handle() { | |
65 return handle_; | |
66 } | |
67 | |
68 public: | |
jar (doing other things)
2013/05/17 22:51:48
typo: private
epenner
2013/05/17 23:46:35
Thanks! I must have switched it back temporarily a
| |
69 friend class PlatformThread; | |
70 friend void* ThreadFunc(void* params); | |
71 | |
72 Handle handle_; | |
73 PlatformThreadId id_; | |
74 }; | |
75 | |
76 const PlatformThreadHandle kNullThreadHandle(0); | |
40 const PlatformThreadId kInvalidThreadId = 0; | 77 const PlatformThreadId kInvalidThreadId = 0; |
jar (doing other things)
2013/05/17 22:51:48
nit: probably should use the same initializer styl
epenner
2013/05/17 23:46:35
Done.
| |
41 | 78 |
79 | |
42 // Valid values for SetThreadPriority() | 80 // Valid values for SetThreadPriority() |
43 enum ThreadPriority{ | 81 enum ThreadPriority{ |
44 kThreadPriority_Normal, | 82 kThreadPriority_Normal, |
45 // Suitable for low-latency, glitch-resistant audio. | 83 // Suitable for low-latency, glitch-resistant audio. |
46 kThreadPriority_RealtimeAudio | 84 kThreadPriority_RealtimeAudio, |
85 // Suitable for threads which generate data for the display (at ~60Hz). | |
86 kThreadPriority_Display, | |
87 // Suitable for threads that shouldn't disrupt high priority work. | |
88 kThreadPriority_Background | |
47 }; | 89 }; |
48 | 90 |
49 // A namespace for low-level thread functions. | 91 // A namespace for low-level thread functions. |
50 class BASE_EXPORT PlatformThread { | 92 class BASE_EXPORT PlatformThread { |
51 public: | 93 public: |
52 // Implement this interface to run code on a background thread. Your | 94 // Implement this interface to run code on a background thread. Your |
53 // ThreadMain method will be called on the newly created thread. | 95 // ThreadMain method will be called on the newly created thread. |
54 class BASE_EXPORT Delegate { | 96 class BASE_EXPORT Delegate { |
55 public: | 97 public: |
56 virtual void ThreadMain() = 0; | 98 virtual void ThreadMain() = 0; |
57 | 99 |
58 protected: | 100 protected: |
59 virtual ~Delegate() {} | 101 virtual ~Delegate() {} |
60 }; | 102 }; |
61 | 103 |
62 // Gets the current thread id, which may be useful for logging purposes. | 104 // Gets the current thread id, which may be useful for logging purposes. |
63 static PlatformThreadId CurrentId(); | 105 static PlatformThreadId CurrentId(); |
64 | 106 |
107 // Get the current handle. | |
108 static PlatformThreadHandle CurrentHandle(); | |
109 | |
65 // Yield the current thread so another thread can be scheduled. | 110 // Yield the current thread so another thread can be scheduled. |
66 static void YieldCurrentThread(); | 111 static void YieldCurrentThread(); |
67 | 112 |
68 // Sleeps for the specified duration. | 113 // Sleeps for the specified duration. |
69 static void Sleep(base::TimeDelta duration); | 114 static void Sleep(base::TimeDelta duration); |
70 | 115 |
71 // Sets the thread name visible to debuggers/tools. This has no effect | 116 // 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 | 117 // otherwise. This name pointer is not copied internally. Thus, it must stay |
73 // valid until the thread ends. | 118 // valid until the thread ends. |
74 static void SetName(const char* name); | 119 static void SetName(const char* name); |
(...skipping 24 matching lines...) Expand all Loading... | |
99 // CreateNonJoinable() does the same thing as Create() except the thread | 144 // CreateNonJoinable() does the same thing as Create() except the thread |
100 // cannot be Join()'d. Therefore, it also does not output a | 145 // cannot be Join()'d. Therefore, it also does not output a |
101 // PlatformThreadHandle. | 146 // PlatformThreadHandle. |
102 static bool CreateNonJoinable(size_t stack_size, Delegate* delegate); | 147 static bool CreateNonJoinable(size_t stack_size, Delegate* delegate); |
103 | 148 |
104 // Joins with a thread created via the Create function. This function blocks | 149 // Joins with a thread created via the Create function. This function blocks |
105 // the caller until the designated thread exits. This will invalidate | 150 // the caller until the designated thread exits. This will invalidate |
106 // |thread_handle|. | 151 // |thread_handle|. |
107 static void Join(PlatformThreadHandle thread_handle); | 152 static void Join(PlatformThreadHandle thread_handle); |
108 | 153 |
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, | 154 static void SetThreadPriority(PlatformThreadHandle handle, |
112 ThreadPriority priority); | 155 ThreadPriority priority); |
113 | 156 |
114 private: | 157 private: |
115 DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformThread); | 158 DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformThread); |
116 }; | 159 }; |
117 | 160 |
118 } // namespace base | 161 } // namespace base |
119 | 162 |
120 #endif // BASE_THREADING_PLATFORM_THREAD_H_ | 163 #endif // BASE_THREADING_PLATFORM_THREAD_H_ |
OLD | NEW |