blob: f83e2f9b02c14c462abad9a9dd2989353531649e [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
henrike@webrtc.org143abd92012-02-08 19:39:38 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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.comb5c49ff2011-08-01 17:04:04 +000011#ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_THREAD_POSIX_H_
12#define WEBRTC_SYSTEM_WRAPPERS_SOURCE_THREAD_POSIX_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
14#include "thread_wrapper.h"
15#include <pthread.h>
16
17namespace webrtc {
hta@webrtc.org40300132012-05-23 15:49:48 +000018
19class CriticalSectionWrapper;
niklase@google.com470e71d2011-07-07 08:21:25 +000020class EventWrapper;
21
henrike@webrtc.org5ba44112012-10-05 14:36:54 +000022int ConvertToSystemPriority(ThreadPriority priority, int minPrio, int maxPrio);
23
ajm@google.comb5c49ff2011-08-01 17:04:04 +000024class ThreadPosix : public ThreadWrapper
niklase@google.com470e71d2011-07-07 08:21:25 +000025{
26public:
27 static ThreadWrapper* Create(ThreadRunFunction func, ThreadObj obj,
28 ThreadPriority prio, const char* threadName);
29
ajm@google.comb5c49ff2011-08-01 17:04:04 +000030 ThreadPosix(ThreadRunFunction func, ThreadObj obj, ThreadPriority prio,
niklase@google.com470e71d2011-07-07 08:21:25 +000031 const char* threadName);
ajm@google.comb5c49ff2011-08-01 17:04:04 +000032 ~ThreadPosix();
niklase@google.com470e71d2011-07-07 08:21:25 +000033
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.com470e71d2011-07-07 08:21:25 +000041
42 void Run();
43
44private:
45 int Construct();
46
47private:
48 // processing function
49 ThreadRunFunction _runFunction;
50 ThreadObj _obj;
51
52 // internal state
hta@webrtc.org40300132012-05-23 15:49:48 +000053 CriticalSectionWrapper* _crit_state; // Protects _alive and _dead
niklase@google.com470e71d2011-07-07 08:21:25 +000054 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.orgdf9866f2012-01-11 08:57:47 +000064#if (defined(WEBRTC_LINUX) || defined(WEBRTC_ANDROID))
pwestin@webrtc.orgb54d7272012-01-11 08:28:04 +000065 pid_t _pid;
pwestin@webrtc.orgdf9866f2012-01-11 08:57:47 +000066#endif
henrike@webrtc.org143abd92012-02-08 19:39:38 +000067 pthread_attr_t _attr;
68 pthread_t _thread;
niklase@google.com470e71d2011-07-07 08:21:25 +000069};
70} // namespace webrtc
71
ajm@google.comb5c49ff2011-08-01 17:04:04 +000072#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_THREAD_POSIX_H_