Tommi | bebc690 | 2015-05-18 09:51:42 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #ifndef WEBRTC_BASE_PLATFORM_THREAD_H_ |
| 12 | #define WEBRTC_BASE_PLATFORM_THREAD_H_ |
| 13 | |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame^] | 14 | #include <string> |
| 15 | |
| 16 | #include "webrtc/base/constructormagic.h" |
| 17 | #include "webrtc/base/event.h" |
| 18 | #include "webrtc/base/platform_thread_types.h" |
| 19 | #include "webrtc/base/scoped_ptr.h" |
| 20 | #include "webrtc/base/thread_checker.h" |
Tommi | bebc690 | 2015-05-18 09:51:42 +0200 | [diff] [blame] | 21 | |
| 22 | namespace rtc { |
| 23 | |
Tommi | bebc690 | 2015-05-18 09:51:42 +0200 | [diff] [blame] | 24 | PlatformThreadId CurrentThreadId(); |
| 25 | PlatformThreadRef CurrentThreadRef(); |
| 26 | |
| 27 | // Compares two thread identifiers for equality. |
| 28 | bool IsThreadRefEqual(const PlatformThreadRef& a, const PlatformThreadRef& b); |
| 29 | |
Tommi | ea14f0a | 2015-05-18 13:51:06 +0200 | [diff] [blame] | 30 | // Sets the current thread name. |
| 31 | void SetCurrentThreadName(const char* name); |
| 32 | |
Tommi | bebc690 | 2015-05-18 09:51:42 +0200 | [diff] [blame] | 33 | } // namespace rtc |
| 34 | |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame^] | 35 | // TODO(pbos): Merge with namespace rtc. |
| 36 | namespace webrtc { |
| 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 | |
Tommi | bebc690 | 2015-05-18 09:51:42 +0200 | [diff] [blame] | 127 | #endif // WEBRTC_BASE_PLATFORM_THREAD_H_ |