Chromium Code Reviews| 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 |
| 11 #ifndef WEBRTC_BASE_PLATFORM_THREAD_H_ | 11 #ifndef WEBRTC_BASE_PLATFORM_THREAD_H_ |
| 12 #define WEBRTC_BASE_PLATFORM_THREAD_H_ | 12 #define WEBRTC_BASE_PLATFORM_THREAD_H_ |
| 13 | 13 |
| 14 #include <string> | |
| 15 | |
| 14 #if defined(WEBRTC_WIN) | 16 #if defined(WEBRTC_WIN) |
| 15 #include <winsock2.h> | 17 #include <winsock2.h> |
| 16 #include <windows.h> | 18 #include <windows.h> |
| 17 #elif defined(WEBRTC_POSIX) | 19 #elif defined(WEBRTC_POSIX) |
| 18 #include <pthread.h> | 20 #include <pthread.h> |
| 19 #include <unistd.h> | 21 #include <unistd.h> |
| 20 #endif | 22 #endif |
| 21 | 23 |
| 24 #include "webrtc/base/event.h" | |
| 25 #include "webrtc/base/scoped_ptr.h" | |
| 26 | |
| 22 namespace rtc { | 27 namespace rtc { |
| 23 | 28 |
| 29 // ThreadChecker depends on this file so we can't include it here. | |
| 30 class ThreadChecker; | |
| 31 | |
| 24 #if defined(WEBRTC_WIN) | 32 #if defined(WEBRTC_WIN) |
| 25 typedef DWORD PlatformThreadId; | 33 typedef DWORD PlatformThreadId; |
| 26 typedef DWORD PlatformThreadRef; | 34 typedef DWORD PlatformThreadRef; |
| 27 #elif defined(WEBRTC_POSIX) | 35 #elif defined(WEBRTC_POSIX) |
| 28 typedef pid_t PlatformThreadId; | 36 typedef pid_t PlatformThreadId; |
| 29 typedef pthread_t PlatformThreadRef; | 37 typedef pthread_t PlatformThreadRef; |
| 30 #endif | 38 #endif |
| 31 | 39 |
| 32 PlatformThreadId CurrentThreadId(); | 40 PlatformThreadId CurrentThreadId(); |
| 33 PlatformThreadRef CurrentThreadRef(); | 41 PlatformThreadRef CurrentThreadRef(); |
| 34 | 42 |
| 35 // Compares two thread identifiers for equality. | 43 // Compares two thread identifiers for equality. |
| 36 bool IsThreadRefEqual(const PlatformThreadRef& a, const PlatformThreadRef& b); | 44 bool IsThreadRefEqual(const PlatformThreadRef& a, const PlatformThreadRef& b); |
| 37 | 45 |
| 38 // Sets the current thread name. | 46 // Sets the current thread name. |
| 39 void SetCurrentThreadName(const char* name); | 47 void SetCurrentThreadName(const char* name); |
| 40 | 48 |
| 41 } // namespace rtc | 49 } // namespace rtc |
| 42 | 50 |
| 51 // TODO(pbos): Merge with namespace rtc. | |
| 52 namespace webrtc { | |
| 53 | |
| 54 // Callback function that the spawned thread will enter once spawned. | |
| 55 // A return value of false is interpreted as that the function has no | |
| 56 // more work to do and that the thread can be released. | |
| 57 typedef bool (*ThreadRunFunction)(void*); | |
| 58 | |
| 59 enum ThreadPriority { | |
| 60 #ifdef WEBRTC_WIN | |
| 61 kLowPriority = THREAD_PRIORITY_BELOW_NORMAL, | |
| 62 kNormalPriority = THREAD_PRIORITY_NORMAL, | |
| 63 kHighPriority = THREAD_PRIORITY_ABOVE_NORMAL, | |
| 64 kHighestPriority = THREAD_PRIORITY_HIGHEST, | |
| 65 kRealtimePriority = THREAD_PRIORITY_TIME_CRITICAL | |
| 66 #else | |
| 67 kLowPriority = 1, | |
| 68 kNormalPriority = 2, | |
| 69 kHighPriority = 3, | |
| 70 kHighestPriority = 4, | |
| 71 kRealtimePriority = 5 | |
| 72 #endif | |
| 73 }; | |
| 74 | |
| 75 // Represents a simple worker thread. The implementation must be assumed | |
| 76 // to be single threaded, meaning that all methods of the class, must be | |
| 77 // called from the same thread, including instantiation. | |
| 78 // TODO(tommi): There's no need for this to be a virtual interface since there's | |
| 79 // only ever a single implementation of it. | |
| 80 class PlatformThread { | |
| 81 public: | |
| 82 PlatformThread(ThreadRunFunction func, void* obj, const char* thread_name); | |
| 83 virtual ~PlatformThread(); | |
| 84 | |
| 85 // Factory method. Constructor disabled. | |
| 86 // | |
| 87 // func Pointer to a, by user, specified callback function. | |
| 88 // obj Object associated with the thread. Passed in the callback | |
| 89 // function. | |
| 90 // prio Thread priority. May require root/admin rights. | |
| 91 // thread_name NULL terminated thread name, will be visable in the Windows | |
| 92 // debugger. | |
| 93 // TODO(pbos): Move users onto explicit initialization/member ownership | |
| 94 // instead of additional heap allocation due to CreateThread. | |
| 95 static rtc::scoped_ptr<PlatformThread> CreateThread(ThreadRunFunction func, | |
| 96 void* obj, | |
| 97 const char* thread_name); | |
| 98 | |
| 99 // Get the current thread's thread ID. | |
| 100 // NOTE: This is a static method. It returns the id of the calling thread, | |
| 101 // *not* the id of the worker thread that a PlatformThread instance | |
| 102 // represents. | |
| 103 // TODO(tommi): Move outside of the PlatformThread class to avoid confusion. | |
| 104 static uint32_t GetThreadId(); | |
|
tommi
2015/11/23 14:49:11
this should be gone now (cl in cq)
pbos-webrtc
2015/11/23 15:05:40
Done.
| |
| 105 | |
| 106 // Tries to spawns a thread and returns true if that was successful. | |
| 107 // Additionally, it tries to set thread priority according to the priority | |
| 108 // from when CreateThread was called. However, failure to set priority will | |
| 109 // not result in a false return value. | |
| 110 // TODO(pbos): Make void not war. | |
| 111 bool Start(); | |
| 112 | |
| 113 // Stops the spawned thread and waits for it to be reclaimed with a timeout | |
| 114 // of two seconds. Will return false if the thread was not reclaimed. | |
| 115 // Multiple tries to Stop are allowed (e.g. to wait longer than 2 seconds). | |
| 116 // It's ok to call Stop() even if the spawned thread has been reclaimed. | |
| 117 // TODO(pbos): Make void not war. | |
| 118 bool Stop(); | |
| 119 | |
| 120 // Set the priority of the worker thread. Must be called when thread | |
| 121 // is running. | |
| 122 bool SetPriority(ThreadPriority priority); | |
| 123 | |
| 124 #if defined(WEBRTC_POSIX) | |
| 125 // This is public only for testing, do not use. | |
| 126 static int ConvertToSystemPriority(ThreadPriority priority, | |
|
tommi
2015/11/23 14:49:12
this should be in the posix implementation only an
pbos-webrtc
2015/11/23 15:05:40
Done.
| |
| 127 int min_prio, | |
| 128 int max_prio); | |
| 129 #endif // defined(WEBRTC_POSIX) | |
| 130 | |
| 131 private: | |
| 132 void Run(); | |
| 133 | |
| 134 ThreadRunFunction const run_function_; | |
| 135 void* const obj_; | |
| 136 // TODO(pbos): Make sure call sites use string constants and update to a const | |
|
tommi
2015/11/23 14:49:11
nit: s/string constants/literals
pbos-webrtc
2015/11/23 15:05:40
Done.
| |
| 137 // char* instead of a std::string. | |
| 138 const std::string name_; | |
| 139 rtc::scoped_ptr<rtc::ThreadChecker> thread_checker_; | |
|
tommi
2015/11/23 14:49:11
as per offline discussion, split PlatformThreadId
pbos-webrtc
2015/11/23 15:05:40
Done.
| |
| 140 #if defined(WEBRTC_WIN) | |
| 141 static DWORD WINAPI StartThread(void* param); | |
| 142 | |
| 143 bool stop_; | |
| 144 HANDLE thread_; | |
| 145 #else | |
| 146 static void* StartThread(void* param); | |
| 147 | |
| 148 rtc::Event stop_event_; | |
| 149 | |
| 150 pthread_t thread_; | |
| 151 #endif // defined(WEBRTC_WIN) | |
| 152 }; | |
| 153 | |
| 154 } // namespace webrtc | |
| 155 | |
| 43 #endif // WEBRTC_BASE_PLATFORM_THREAD_H_ | 156 #endif // WEBRTC_BASE_PLATFORM_THREAD_H_ |
| OLD | NEW |