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 | |
| 14 | #include "thread_wrapper.h" |
| 15 | #include <pthread.h> |
| 16 | |
| 17 | namespace webrtc { |
hta@webrtc.org | 4030013 | 2012-05-23 15:49:48 +0000 | [diff] [blame] | 18 | |
| 19 | class CriticalSectionWrapper; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | class EventWrapper; |
| 21 | |
henrike@webrtc.org | 5ba4411 | 2012-10-05 14:36:54 +0000 | [diff] [blame] | 22 | int ConvertToSystemPriority(ThreadPriority priority, int minPrio, int maxPrio); |
| 23 | |
ajm@google.com | b5c49ff | 2011-08-01 17:04:04 +0000 | [diff] [blame] | 24 | class ThreadPosix : public ThreadWrapper |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 25 | { |
| 26 | public: |
| 27 | static ThreadWrapper* Create(ThreadRunFunction func, ThreadObj obj, |
| 28 | ThreadPriority prio, const char* threadName); |
| 29 | |
ajm@google.com | b5c49ff | 2011-08-01 17:04:04 +0000 | [diff] [blame] | 30 | ThreadPosix(ThreadRunFunction func, ThreadObj obj, ThreadPriority prio, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 31 | const char* threadName); |
ajm@google.com | b5c49ff | 2011-08-01 17:04:04 +0000 | [diff] [blame] | 32 | ~ThreadPosix(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 33 | |
| 34 | // From ThreadWrapper |
| 35 | virtual void SetNotAlive(); |
| 36 | virtual bool Start(unsigned int& id); |
| 37 | // Not implemented on Mac |
| 38 | virtual bool SetAffinity(const int* processorNumbers, |
| 39 | unsigned int amountOfProcessors); |
| 40 | virtual bool Stop(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 41 | |
| 42 | void Run(); |
| 43 | |
| 44 | private: |
| 45 | int Construct(); |
| 46 | |
| 47 | private: |
| 48 | // processing function |
| 49 | ThreadRunFunction _runFunction; |
| 50 | ThreadObj _obj; |
| 51 | |
| 52 | // internal state |
hta@webrtc.org | 4030013 | 2012-05-23 15:49:48 +0000 | [diff] [blame] | 53 | CriticalSectionWrapper* _crit_state; // Protects _alive and _dead |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | bool _alive; |
| 55 | bool _dead; |
| 56 | ThreadPriority _prio; |
| 57 | EventWrapper* _event; |
| 58 | |
| 59 | // zero-terminated thread name string |
| 60 | char _name[kThreadMaxNameLength]; |
| 61 | bool _setThreadName; |
| 62 | |
| 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)) |
pwestin@webrtc.org | b54d727 | 2012-01-11 08:28:04 +0000 | [diff] [blame] | 65 | pid_t _pid; |
pwestin@webrtc.org | df9866f | 2012-01-11 08:57:47 +0000 | [diff] [blame] | 66 | #endif |
henrike@webrtc.org | 143abd9 | 2012-02-08 19:39:38 +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 | }; |
| 70 | } // namespace webrtc |
| 71 | |
ajm@google.com | b5c49ff | 2011-08-01 17:04:04 +0000 | [diff] [blame] | 72 | #endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_THREAD_POSIX_H_ |