niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 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 | // System independant wrapper for spawning threads |
| 12 | // Note: the spawned thread will loop over the callback function until stopped. |
| 13 | // Note: The callback function is expected to return every 2 seconds or more |
| 14 | // often. |
| 15 | |
| 16 | #ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_THREAD_WRAPPER_H_ |
| 17 | #define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_THREAD_WRAPPER_H_ |
| 18 | |
tommi@webrtc.org | b6817d7 | 2015-03-20 15:51:39 +0000 | [diff] [blame^] | 19 | #if defined(WEBRTC_WIN) |
| 20 | #include <windows.h> |
| 21 | #endif |
| 22 | |
tommi@webrtc.org | 361981f | 2015-03-19 14:44:18 +0000 | [diff] [blame] | 23 | #include "webrtc/base/scoped_ptr.h" |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 24 | #include "webrtc/common_types.h" |
| 25 | #include "webrtc/typedefs.h" |
pwestin@webrtc.org | b54d727 | 2012-01-11 08:28:04 +0000 | [diff] [blame] | 26 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 27 | namespace webrtc { |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 28 | |
phoglund@webrtc.org | c63f788 | 2011-10-24 13:20:09 +0000 | [diff] [blame] | 29 | // Callback function that the spawned thread will enter once spawned. |
| 30 | // A return value of false is interpreted as that the function has no |
| 31 | // more work to do and that the thread can be released. |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 32 | typedef bool(*ThreadRunFunction)(void*); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 33 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 34 | enum ThreadPriority { |
tommi@webrtc.org | b6817d7 | 2015-03-20 15:51:39 +0000 | [diff] [blame^] | 35 | #ifdef WEBRTC_WIN |
| 36 | kLowPriority = THREAD_PRIORITY_BELOW_NORMAL, |
| 37 | kNormalPriority = THREAD_PRIORITY_NORMAL, |
| 38 | kHighPriority = THREAD_PRIORITY_ABOVE_NORMAL, |
| 39 | kHighestPriority = THREAD_PRIORITY_HIGHEST, |
| 40 | kRealtimePriority = THREAD_PRIORITY_TIME_CRITICAL |
| 41 | #else |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 42 | kLowPriority = 1, |
| 43 | kNormalPriority = 2, |
| 44 | kHighPriority = 3, |
| 45 | kHighestPriority = 4, |
| 46 | kRealtimePriority = 5 |
tommi@webrtc.org | b6817d7 | 2015-03-20 15:51:39 +0000 | [diff] [blame^] | 47 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 50 | // Represents a simple worker thread. The implementation must be assumed |
| 51 | // to be single threaded, meaning that all methods of the class, must be |
| 52 | // called from the same thread, including instantiation. |
tommi@webrtc.org | 361981f | 2015-03-19 14:44:18 +0000 | [diff] [blame] | 53 | // TODO(tommi): There's no need for this to be a virtual interface since there's |
| 54 | // only ever a single implementation of it. |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 55 | class ThreadWrapper { |
| 56 | public: |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 57 | virtual ~ThreadWrapper() {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 58 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 59 | // Factory method. Constructor disabled. |
| 60 | // |
| 61 | // func Pointer to a, by user, specified callback function. |
| 62 | // obj Object associated with the thread. Passed in the callback |
| 63 | // function. |
| 64 | // prio Thread priority. May require root/admin rights. |
| 65 | // thread_name NULL terminated thread name, will be visable in the Windows |
| 66 | // debugger. |
tommi@webrtc.org | 361981f | 2015-03-19 14:44:18 +0000 | [diff] [blame] | 67 | static rtc::scoped_ptr<ThreadWrapper> CreateThread(ThreadRunFunction func, |
tommi@webrtc.org | b6817d7 | 2015-03-20 15:51:39 +0000 | [diff] [blame^] | 68 | void* obj, const char* thread_name); |
pwestin@webrtc.org | b54d727 | 2012-01-11 08:28:04 +0000 | [diff] [blame] | 69 | |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 70 | // Get the current thread's thread ID. |
| 71 | // NOTE: This is a static method. It returns the id of the calling thread, |
| 72 | // *not* the id of the worker thread that a ThreadWrapper instance represents. |
| 73 | // TODO(tommi): Move outside of the ThreadWrapper class to avoid confusion. |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 74 | static uint32_t GetThreadId(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 75 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 76 | // Tries to spawns a thread and returns true if that was successful. |
| 77 | // Additionally, it tries to set thread priority according to the priority |
| 78 | // from when CreateThread was called. However, failure to set priority will |
| 79 | // not result in a false return value. |
pbos@webrtc.org | 8663973 | 2015-03-13 00:06:21 +0000 | [diff] [blame] | 80 | virtual bool Start() = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 81 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 82 | // Stops the spawned thread and waits for it to be reclaimed with a timeout |
| 83 | // of two seconds. Will return false if the thread was not reclaimed. |
| 84 | // Multiple tries to Stop are allowed (e.g. to wait longer than 2 seconds). |
| 85 | // It's ok to call Stop() even if the spawned thread has been reclaimed. |
| 86 | virtual bool Stop() = 0; |
tommi@webrtc.org | b6817d7 | 2015-03-20 15:51:39 +0000 | [diff] [blame^] | 87 | |
| 88 | // Set the priority of the worker thread. Must be called when thread |
| 89 | // is running. |
| 90 | virtual bool SetPriority(ThreadPriority priority) = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 91 | }; |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 92 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 93 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 94 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 95 | #endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_THREAD_WRAPPER_H_ |