OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 12 matching lines...) Expand all Loading... |
23 | 23 |
24 PlatformThreadId CurrentThreadId(); | 24 PlatformThreadId CurrentThreadId(); |
25 PlatformThreadRef CurrentThreadRef(); | 25 PlatformThreadRef CurrentThreadRef(); |
26 | 26 |
27 // Compares two thread identifiers for equality. | 27 // Compares two thread identifiers for equality. |
28 bool IsThreadRefEqual(const PlatformThreadRef& a, const PlatformThreadRef& b); | 28 bool IsThreadRefEqual(const PlatformThreadRef& a, const PlatformThreadRef& b); |
29 | 29 |
30 // Sets the current thread name. | 30 // Sets the current thread name. |
31 void SetCurrentThreadName(const char* name); | 31 void SetCurrentThreadName(const char* name); |
32 | 32 |
33 } // namespace rtc | |
34 | |
35 // TODO(pbos): Merge with namespace rtc. | |
36 namespace webrtc { | |
37 | |
38 // Callback function that the spawned thread will enter once spawned. | 33 // Callback function that the spawned thread will enter once spawned. |
39 // A return value of false is interpreted as that the function has no | 34 // A return value of false is interpreted as that the function has no |
40 // more work to do and that the thread can be released. | 35 // more work to do and that the thread can be released. |
41 typedef bool (*ThreadRunFunction)(void*); | 36 typedef bool (*ThreadRunFunction)(void*); |
42 | 37 |
43 enum ThreadPriority { | 38 enum ThreadPriority { |
44 #ifdef WEBRTC_WIN | 39 #ifdef WEBRTC_WIN |
45 kLowPriority = THREAD_PRIORITY_BELOW_NORMAL, | 40 kLowPriority = THREAD_PRIORITY_BELOW_NORMAL, |
46 kNormalPriority = THREAD_PRIORITY_NORMAL, | 41 kNormalPriority = THREAD_PRIORITY_NORMAL, |
47 kHighPriority = THREAD_PRIORITY_ABOVE_NORMAL, | 42 kHighPriority = THREAD_PRIORITY_ABOVE_NORMAL, |
48 kHighestPriority = THREAD_PRIORITY_HIGHEST, | 43 kHighestPriority = THREAD_PRIORITY_HIGHEST, |
49 kRealtimePriority = THREAD_PRIORITY_TIME_CRITICAL | 44 kRealtimePriority = THREAD_PRIORITY_TIME_CRITICAL |
50 #else | 45 #else |
51 kLowPriority = 1, | 46 kLowPriority = 1, |
52 kNormalPriority = 2, | 47 kNormalPriority = 2, |
53 kHighPriority = 3, | 48 kHighPriority = 3, |
54 kHighestPriority = 4, | 49 kHighestPriority = 4, |
55 kRealtimePriority = 5 | 50 kRealtimePriority = 5 |
56 #endif | 51 #endif |
57 }; | 52 }; |
58 | 53 |
59 // Represents a simple worker thread. The implementation must be assumed | 54 // Represents a simple worker thread. The implementation must be assumed |
60 // to be single threaded, meaning that all methods of the class, must be | 55 // to be single threaded, meaning that all methods of the class, must be |
61 // called from the same thread, including instantiation. | 56 // called from the same thread, including instantiation. |
62 // TODO(tommi): There's no need for this to be a virtual interface since there's | |
63 // only ever a single implementation of it. | |
64 class PlatformThread { | 57 class PlatformThread { |
65 public: | 58 public: |
66 PlatformThread(ThreadRunFunction func, void* obj, const char* thread_name); | 59 PlatformThread(ThreadRunFunction func, void* obj, const char* thread_name); |
67 virtual ~PlatformThread(); | 60 virtual ~PlatformThread(); |
68 | 61 |
69 // Factory method. Constructor disabled. | 62 // Spawns a thread and tries to set thread priority according to the priority |
70 // | 63 // from when CreateThread was called. |
71 // func Pointer to a, by user, specified callback function. | 64 void Start(); |
72 // obj Object associated with the thread. Passed in the callback | |
73 // function. | |
74 // prio Thread priority. May require root/admin rights. | |
75 // thread_name NULL terminated thread name, will be visable in the Windows | |
76 // debugger. | |
77 // TODO(pbos): Move users onto explicit initialization/member ownership | |
78 // instead of additional heap allocation due to CreateThread. | |
79 static rtc::scoped_ptr<PlatformThread> CreateThread(ThreadRunFunction func, | |
80 void* obj, | |
81 const char* thread_name); | |
82 | 65 |
83 // Tries to spawns a thread and returns true if that was successful. | 66 bool IsRunning() const; |
84 // Additionally, it tries to set thread priority according to the priority | |
85 // from when CreateThread was called. However, failure to set priority will | |
86 // not result in a false return value. | |
87 // TODO(pbos): Make void not war. | |
88 bool Start(); | |
89 | 67 |
90 // Stops the spawned thread and waits for it to be reclaimed with a timeout | 68 // Stops (joins) the spawned thread. |
91 // of two seconds. Will return false if the thread was not reclaimed. | 69 void Stop(); |
92 // Multiple tries to Stop are allowed (e.g. to wait longer than 2 seconds). | |
93 // It's ok to call Stop() even if the spawned thread has been reclaimed. | |
94 // TODO(pbos): Make void not war. | |
95 bool Stop(); | |
96 | 70 |
97 // Set the priority of the worker thread. Must be called when thread | 71 // Set the priority of the thread. Must be called when thread is running. |
98 // is running. | |
99 bool SetPriority(ThreadPriority priority); | 72 bool SetPriority(ThreadPriority priority); |
100 | 73 |
101 private: | 74 private: |
102 void Run(); | 75 void Run(); |
103 | 76 |
104 ThreadRunFunction const run_function_; | 77 ThreadRunFunction const run_function_; |
105 void* const obj_; | 78 void* const obj_; |
106 // TODO(pbos): Make sure call sites use string literals and update to a const | 79 // TODO(pbos): Make sure call sites use string literals and update to a const |
107 // char* instead of a std::string. | 80 // char* instead of a std::string. |
108 const std::string name_; | 81 const std::string name_; |
109 rtc::ThreadChecker thread_checker_; | 82 rtc::ThreadChecker thread_checker_; |
110 #if defined(WEBRTC_WIN) | 83 #if defined(WEBRTC_WIN) |
111 static DWORD WINAPI StartThread(void* param); | 84 static DWORD WINAPI StartThread(void* param); |
112 | 85 |
113 bool stop_; | 86 bool stop_; |
114 HANDLE thread_; | 87 HANDLE thread_; |
115 #else | 88 #else |
116 static void* StartThread(void* param); | 89 static void* StartThread(void* param); |
117 | 90 |
118 rtc::Event stop_event_; | 91 rtc::Event stop_event_; |
119 | 92 |
120 pthread_t thread_; | 93 pthread_t thread_; |
121 #endif // defined(WEBRTC_WIN) | 94 #endif // defined(WEBRTC_WIN) |
122 RTC_DISALLOW_COPY_AND_ASSIGN(PlatformThread); | 95 RTC_DISALLOW_COPY_AND_ASSIGN(PlatformThread); |
123 }; | 96 }; |
124 | 97 |
125 } // namespace webrtc | 98 } // namespace rtc |
126 | 99 |
127 #endif // WEBRTC_BASE_PLATFORM_THREAD_H_ | 100 #endif // WEBRTC_BASE_PLATFORM_THREAD_H_ |
OLD | NEW |