niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
henrike@webrtc.org | 143abd9 | 2012-02-08 19:39:38 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
ajm@google.com | b5c49ff | 2011-08-01 17:04:04 +0000 | [diff] [blame] | 11 | #ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_THREAD_POSIX_H_ |
| 12 | #define WEBRTC_SYSTEM_WRAPPERS_SOURCE_THREAD_POSIX_H_ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 14 | #include "webrtc/system_wrappers/interface/thread_wrapper.h" |
| 15 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 16 | #include <pthread.h> |
| 17 | |
| 18 | namespace webrtc { |
hta@webrtc.org | 4030013 | 2012-05-23 15:49:48 +0000 | [diff] [blame] | 19 | |
| 20 | class CriticalSectionWrapper; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 21 | class EventWrapper; |
| 22 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 23 | int ConvertToSystemPriority(ThreadPriority priority, int min_prio, |
| 24 | int max_prio); |
henrike@webrtc.org | 5ba4411 | 2012-10-05 14:36:54 +0000 | [diff] [blame] | 25 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 26 | class ThreadPosix : public ThreadWrapper { |
| 27 | public: |
| 28 | static ThreadWrapper* Create(ThreadRunFunction func, ThreadObj obj, |
| 29 | ThreadPriority prio, const char* thread_name); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 30 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 31 | ThreadPosix(ThreadRunFunction func, ThreadObj obj, ThreadPriority prio, |
| 32 | const char* thread_name); |
pbos@webrtc.org | a2a2718 | 2013-08-01 17:26:15 +0000 | [diff] [blame] | 33 | virtual ~ThreadPosix(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 35 | // From ThreadWrapper. |
pbos@webrtc.org | a2a2718 | 2013-08-01 17:26:15 +0000 | [diff] [blame] | 36 | virtual void SetNotAlive() OVERRIDE; |
| 37 | virtual bool Start(unsigned int& id) OVERRIDE; |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 38 | // Not implemented on Mac. |
| 39 | virtual bool SetAffinity(const int* processor_numbers, |
pbos@webrtc.org | a2a2718 | 2013-08-01 17:26:15 +0000 | [diff] [blame] | 40 | unsigned int amount_of_processors) OVERRIDE; |
| 41 | virtual bool Stop() OVERRIDE; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 42 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 43 | void Run(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 44 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 45 | private: |
| 46 | int Construct(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 47 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 48 | private: |
| 49 | ThreadRunFunction run_function_; |
| 50 | ThreadObj obj_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 51 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 52 | // Internal state. |
| 53 | CriticalSectionWrapper* crit_state_; // Protects alive_ and dead_ |
| 54 | bool alive_; |
| 55 | bool dead_; |
| 56 | ThreadPriority prio_; |
| 57 | EventWrapper* event_; |
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 | // Zero-terminated thread name string. |
| 60 | char name_[kThreadMaxNameLength]; |
| 61 | bool set_thread_name_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 62 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 63 | // Handle to thread. |
pwestin@webrtc.org | df9866f | 2012-01-11 08:57:47 +0000 | [diff] [blame] | 64 | #if (defined(WEBRTC_LINUX) || defined(WEBRTC_ANDROID)) |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 65 | pid_t pid_; |
pwestin@webrtc.org | df9866f | 2012-01-11 08:57:47 +0000 | [diff] [blame] | 66 | #endif |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 67 | pthread_attr_t attr_; |
| 68 | pthread_t thread_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 69 | }; |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 70 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 71 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 72 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 73 | #endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_THREAD_POSIX_H_ |