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 #if defined(WEBRTC_WIN) | 14 #include <string> |
| 15 #include <winsock2.h> | 15 |
| 16 #include <windows.h> | 16 #include "webrtc/base/constructormagic.h" |
| 17 #elif defined(WEBRTC_POSIX) | 17 #include "webrtc/base/event.h" |
| 18 #include <pthread.h> | 18 #include "webrtc/base/platform_thread_types.h" |
| 19 #include <unistd.h> | 19 #include "webrtc/base/scoped_ptr.h" |
| 20 #endif | 20 #include "webrtc/base/thread_checker.h" |
| 21 | 21 |
| 22 namespace rtc { | 22 namespace rtc { |
| 23 | 23 |
| 24 #if defined(WEBRTC_WIN) | |
| 25 typedef DWORD PlatformThreadId; | |
| 26 typedef DWORD PlatformThreadRef; | |
| 27 #elif defined(WEBRTC_POSIX) | |
| 28 typedef pid_t PlatformThreadId; | |
| 29 typedef pthread_t PlatformThreadRef; | |
| 30 #endif | |
| 31 | |
| 32 PlatformThreadId CurrentThreadId(); | 24 PlatformThreadId CurrentThreadId(); |
| 33 PlatformThreadRef CurrentThreadRef(); | 25 PlatformThreadRef CurrentThreadRef(); |
| 34 | 26 |
| 35 // Compares two thread identifiers for equality. | 27 // Compares two thread identifiers for equality. |
| 36 bool IsThreadRefEqual(const PlatformThreadRef& a, const PlatformThreadRef& b); | 28 bool IsThreadRefEqual(const PlatformThreadRef& a, const PlatformThreadRef& b); |
| 37 | 29 |
| 38 // Sets the current thread name. | 30 // Sets the current thread name. |
| 39 void SetCurrentThreadName(const char* name); | 31 void SetCurrentThreadName(const char* name); |
| 40 | 32 |
| 41 } // namespace rtc | 33 } // namespace rtc |
| 42 | 34 |
| 35 // TODO(pbos): Merge with namespace rtc. | |
| 36 namespace webrtc { | |
|
tommi
2015/11/23 15:54:28
nit: threading?
(or is there a reason for having r
pbos-webrtc
2015/11/23 16:18:22
Not under rtc, this is just webrtc::. Not moved ye
tommi
2015/11/23 16:51:50
Acknowledged.
| |
| 37 | |
| 38 // Callback function that the spawned thread will enter once spawned. | |
| 39 // 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. | |
| 41 typedef bool (*ThreadRunFunction)(void*); | |
| 42 | |
| 43 enum ThreadPriority { | |
| 44 #ifdef WEBRTC_WIN | |
| 45 kLowPriority = THREAD_PRIORITY_BELOW_NORMAL, | |
| 46 kNormalPriority = THREAD_PRIORITY_NORMAL, | |
| 47 kHighPriority = THREAD_PRIORITY_ABOVE_NORMAL, | |
| 48 kHighestPriority = THREAD_PRIORITY_HIGHEST, | |
| 49 kRealtimePriority = THREAD_PRIORITY_TIME_CRITICAL | |
| 50 #else | |
| 51 kLowPriority = 1, | |
| 52 kNormalPriority = 2, | |
| 53 kHighPriority = 3, | |
| 54 kHighestPriority = 4, | |
| 55 kRealtimePriority = 5 | |
| 56 #endif | |
| 57 }; | |
| 58 | |
| 59 // Represents a simple worker thread. The implementation must be assumed | |
| 60 // to be single threaded, meaning that all methods of the class, must be | |
| 61 // 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 { | |
| 65 public: | |
| 66 PlatformThread(ThreadRunFunction func, void* obj, const char* thread_name); | |
| 67 virtual ~PlatformThread(); | |
| 68 | |
| 69 // Factory method. Constructor disabled. | |
| 70 // | |
| 71 // func Pointer to a, by user, specified callback function. | |
| 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 | |
| 83 // Tries to spawns a thread and returns true if that was successful. | |
| 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 | |
| 90 // Stops the spawned thread and waits for it to be reclaimed with a timeout | |
| 91 // of two seconds. Will return false if the thread was not reclaimed. | |
| 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 | |
| 97 // Set the priority of the worker thread. Must be called when thread | |
| 98 // is running. | |
| 99 bool SetPriority(ThreadPriority priority); | |
| 100 | |
| 101 private: | |
| 102 void Run(); | |
| 103 | |
| 104 ThreadRunFunction const run_function_; | |
| 105 void* const obj_; | |
| 106 // TODO(pbos): Make sure call sites use string literals and update to a const | |
| 107 // char* instead of a std::string. | |
| 108 const std::string name_; | |
| 109 rtc::ThreadChecker thread_checker_; | |
| 110 #if defined(WEBRTC_WIN) | |
| 111 static DWORD WINAPI StartThread(void* param); | |
| 112 | |
| 113 bool stop_; | |
| 114 HANDLE thread_; | |
| 115 #else | |
| 116 static void* StartThread(void* param); | |
| 117 | |
| 118 rtc::Event stop_event_; | |
| 119 | |
| 120 pthread_t thread_; | |
| 121 #endif // defined(WEBRTC_WIN) | |
| 122 RTC_DISALLOW_COPY_AND_ASSIGN(PlatformThread); | |
| 123 }; | |
| 124 | |
| 125 } // namespace webrtc | |
| 126 | |
| 43 #endif // WEBRTC_BASE_PLATFORM_THREAD_H_ | 127 #endif // WEBRTC_BASE_PLATFORM_THREAD_H_ |
| OLD | NEW |