Chromium Code Reviews| Index: webrtc/base/platform_thread.h |
| diff --git a/webrtc/base/platform_thread.h b/webrtc/base/platform_thread.h |
| index 50033b39282e976cd3f3f4ec35cb52b641249843..e929c449943e2f67ba0a974101ae2b7d19703eae 100644 |
| --- a/webrtc/base/platform_thread.h |
| +++ b/webrtc/base/platform_thread.h |
| @@ -11,6 +11,8 @@ |
| #ifndef WEBRTC_BASE_PLATFORM_THREAD_H_ |
| #define WEBRTC_BASE_PLATFORM_THREAD_H_ |
| +#include <string> |
| + |
| #if defined(WEBRTC_WIN) |
| #include <winsock2.h> |
| #include <windows.h> |
| @@ -19,8 +21,14 @@ |
| #include <unistd.h> |
| #endif |
| +#include "webrtc/base/event.h" |
| +#include "webrtc/base/scoped_ptr.h" |
| + |
| namespace rtc { |
| +// ThreadChecker depends on this file so we can't include it here. |
| +class ThreadChecker; |
| + |
| #if defined(WEBRTC_WIN) |
| typedef DWORD PlatformThreadId; |
| typedef DWORD PlatformThreadRef; |
| @@ -40,4 +48,109 @@ void SetCurrentThreadName(const char* name); |
| } // namespace rtc |
| +// TODO(pbos): Merge with namespace rtc. |
| +namespace webrtc { |
| + |
| +// Callback function that the spawned thread will enter once spawned. |
| +// A return value of false is interpreted as that the function has no |
| +// more work to do and that the thread can be released. |
| +typedef bool (*ThreadRunFunction)(void*); |
| + |
| +enum ThreadPriority { |
| +#ifdef WEBRTC_WIN |
| + kLowPriority = THREAD_PRIORITY_BELOW_NORMAL, |
| + kNormalPriority = THREAD_PRIORITY_NORMAL, |
| + kHighPriority = THREAD_PRIORITY_ABOVE_NORMAL, |
| + kHighestPriority = THREAD_PRIORITY_HIGHEST, |
| + kRealtimePriority = THREAD_PRIORITY_TIME_CRITICAL |
| +#else |
| + kLowPriority = 1, |
| + kNormalPriority = 2, |
| + kHighPriority = 3, |
| + kHighestPriority = 4, |
| + kRealtimePriority = 5 |
| +#endif |
| +}; |
| + |
| +// Represents a simple worker thread. The implementation must be assumed |
| +// to be single threaded, meaning that all methods of the class, must be |
| +// called from the same thread, including instantiation. |
| +// TODO(tommi): There's no need for this to be a virtual interface since there's |
| +// only ever a single implementation of it. |
| +class PlatformThread { |
| + public: |
| + PlatformThread(ThreadRunFunction func, void* obj, const char* thread_name); |
| + virtual ~PlatformThread(); |
| + |
| + // Factory method. Constructor disabled. |
| + // |
| + // func Pointer to a, by user, specified callback function. |
| + // obj Object associated with the thread. Passed in the callback |
| + // function. |
| + // prio Thread priority. May require root/admin rights. |
| + // thread_name NULL terminated thread name, will be visable in the Windows |
| + // debugger. |
| + // TODO(pbos): Move users onto explicit initialization/member ownership |
| + // instead of additional heap allocation due to CreateThread. |
| + static rtc::scoped_ptr<PlatformThread> CreateThread(ThreadRunFunction func, |
| + void* obj, |
| + const char* thread_name); |
| + |
| + // Get the current thread's thread ID. |
| + // NOTE: This is a static method. It returns the id of the calling thread, |
| + // *not* the id of the worker thread that a PlatformThread instance |
| + // represents. |
| + // TODO(tommi): Move outside of the PlatformThread class to avoid confusion. |
| + 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.
|
| + |
| + // Tries to spawns a thread and returns true if that was successful. |
| + // Additionally, it tries to set thread priority according to the priority |
| + // from when CreateThread was called. However, failure to set priority will |
| + // not result in a false return value. |
| + // TODO(pbos): Make void not war. |
| + bool Start(); |
| + |
| + // Stops the spawned thread and waits for it to be reclaimed with a timeout |
| + // of two seconds. Will return false if the thread was not reclaimed. |
| + // Multiple tries to Stop are allowed (e.g. to wait longer than 2 seconds). |
| + // It's ok to call Stop() even if the spawned thread has been reclaimed. |
| + // TODO(pbos): Make void not war. |
| + bool Stop(); |
| + |
| + // Set the priority of the worker thread. Must be called when thread |
| + // is running. |
| + bool SetPriority(ThreadPriority priority); |
| + |
| +#if defined(WEBRTC_POSIX) |
| + // This is public only for testing, do not use. |
| + 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.
|
| + int min_prio, |
| + int max_prio); |
| +#endif // defined(WEBRTC_POSIX) |
| + |
| + private: |
| + void Run(); |
| + |
| + ThreadRunFunction const run_function_; |
| + void* const obj_; |
| + // 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.
|
| + // char* instead of a std::string. |
| + const std::string name_; |
| + 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.
|
| +#if defined(WEBRTC_WIN) |
| + static DWORD WINAPI StartThread(void* param); |
| + |
| + bool stop_; |
| + HANDLE thread_; |
| +#else |
| + static void* StartThread(void* param); |
| + |
| + rtc::Event stop_event_; |
| + |
| + pthread_t thread_; |
| +#endif // defined(WEBRTC_WIN) |
| +}; |
| + |
| +} // namespace webrtc |
| + |
| #endif // WEBRTC_BASE_PLATFORM_THREAD_H_ |