| 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 |
| 16 #include "webrtc/base/constructormagic.h" | 15 // This header is deprecated and is just left here temporarily during |
| 17 #include "webrtc/base/event.h" | 16 // refactoring. See https://bugs.webrtc.org/7634 for more details. |
| 18 #include "webrtc/base/platform_thread_types.h" | 17 #include "webrtc/rtc_base/platform_thread.h" |
| 19 #include "webrtc/base/thread_checker.h" | |
| 20 | |
| 21 namespace rtc { | |
| 22 | |
| 23 PlatformThreadId CurrentThreadId(); | |
| 24 PlatformThreadRef CurrentThreadRef(); | |
| 25 | |
| 26 // Compares two thread identifiers for equality. | |
| 27 bool IsThreadRefEqual(const PlatformThreadRef& a, const PlatformThreadRef& b); | |
| 28 | |
| 29 // Sets the current thread name. | |
| 30 void SetCurrentThreadName(const char* name); | |
| 31 | |
| 32 // Callback function that the spawned thread will enter once spawned. | |
| 33 // A return value of false is interpreted as that the function has no | |
| 34 // more work to do and that the thread can be released. | |
| 35 typedef bool (*ThreadRunFunctionDeprecated)(void*); | |
| 36 typedef void (*ThreadRunFunction)(void*); | |
| 37 | |
| 38 enum ThreadPriority { | |
| 39 #ifdef WEBRTC_WIN | |
| 40 kLowPriority = THREAD_PRIORITY_BELOW_NORMAL, | |
| 41 kNormalPriority = THREAD_PRIORITY_NORMAL, | |
| 42 kHighPriority = THREAD_PRIORITY_ABOVE_NORMAL, | |
| 43 kHighestPriority = THREAD_PRIORITY_HIGHEST, | |
| 44 kRealtimePriority = THREAD_PRIORITY_TIME_CRITICAL | |
| 45 #else | |
| 46 kLowPriority = 1, | |
| 47 kNormalPriority = 2, | |
| 48 kHighPriority = 3, | |
| 49 kHighestPriority = 4, | |
| 50 kRealtimePriority = 5 | |
| 51 #endif | |
| 52 }; | |
| 53 | |
| 54 // Represents a simple worker thread. The implementation must be assumed | |
| 55 // to be single threaded, meaning that all methods of the class, must be | |
| 56 // called from the same thread, including instantiation. | |
| 57 class PlatformThread { | |
| 58 public: | |
| 59 PlatformThread(ThreadRunFunctionDeprecated func, | |
| 60 void* obj, | |
| 61 const char* thread_name); | |
| 62 PlatformThread(ThreadRunFunction func, | |
| 63 void* obj, | |
| 64 const char* thread_name, | |
| 65 ThreadPriority priority = kNormalPriority); | |
| 66 virtual ~PlatformThread(); | |
| 67 | |
| 68 const std::string& name() const { return name_; } | |
| 69 | |
| 70 // Spawns a thread and tries to set thread priority according to the priority | |
| 71 // from when CreateThread was called. | |
| 72 void Start(); | |
| 73 | |
| 74 bool IsRunning() const; | |
| 75 | |
| 76 // Returns an identifier for the worker thread that can be used to do | |
| 77 // thread checks. | |
| 78 PlatformThreadRef GetThreadRef() const; | |
| 79 | |
| 80 // Stops (joins) the spawned thread. | |
| 81 void Stop(); | |
| 82 | |
| 83 // Set the priority of the thread. Must be called when thread is running. | |
| 84 // TODO(tommi): Make private and only allow public support via ctor. | |
| 85 bool SetPriority(ThreadPriority priority); | |
| 86 | |
| 87 protected: | |
| 88 #if defined(WEBRTC_WIN) | |
| 89 // Exposed to derived classes to allow for special cases specific to Windows. | |
| 90 bool QueueAPC(PAPCFUNC apc_function, ULONG_PTR data); | |
| 91 #endif | |
| 92 | |
| 93 private: | |
| 94 void Run(); | |
| 95 | |
| 96 ThreadRunFunctionDeprecated const run_function_deprecated_ = nullptr; | |
| 97 ThreadRunFunction const run_function_ = nullptr; | |
| 98 const ThreadPriority priority_ = kNormalPriority; | |
| 99 void* const obj_; | |
| 100 // TODO(pbos): Make sure call sites use string literals and update to a const | |
| 101 // char* instead of a std::string. | |
| 102 const std::string name_; | |
| 103 rtc::ThreadChecker thread_checker_; | |
| 104 rtc::ThreadChecker spawned_thread_checker_; | |
| 105 #if defined(WEBRTC_WIN) | |
| 106 static DWORD WINAPI StartThread(void* param); | |
| 107 | |
| 108 bool stop_ = false; | |
| 109 HANDLE thread_ = nullptr; | |
| 110 DWORD thread_id_ = 0; | |
| 111 #else | |
| 112 static void* StartThread(void* param); | |
| 113 | |
| 114 // An atomic flag that we use to stop the thread. Only modified on the | |
| 115 // controlling thread and checked on the worker thread. | |
| 116 volatile int stop_flag_ = 0; | |
| 117 pthread_t thread_ = 0; | |
| 118 #endif // defined(WEBRTC_WIN) | |
| 119 RTC_DISALLOW_COPY_AND_ASSIGN(PlatformThread); | |
| 120 }; | |
| 121 | |
| 122 } // namespace rtc | |
| 123 | 18 |
| 124 #endif // WEBRTC_BASE_PLATFORM_THREAD_H_ | 19 #endif // WEBRTC_BASE_PLATFORM_THREAD_H_ |
| OLD | NEW |