blob: c2025fd8dcfc0f38a405795a130a69120d7122ec [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
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000014#include "webrtc/system_wrappers/interface/thread_wrapper.h"
15
niklase@google.com470e71d2011-07-07 08:21:25 +000016#include <pthread.h>
17
18namespace webrtc {
hta@webrtc.org40300132012-05-23 15:49:48 +000019
20class CriticalSectionWrapper;
niklase@google.com470e71d2011-07-07 08:21:25 +000021class EventWrapper;
22
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000023int ConvertToSystemPriority(ThreadPriority priority, int min_prio,
24 int max_prio);
henrike@webrtc.org5ba44112012-10-05 14:36:54 +000025
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000026class ThreadPosix : public ThreadWrapper {
27 public:
28 static ThreadWrapper* Create(ThreadRunFunction func, ThreadObj obj,
29 ThreadPriority prio, const char* thread_name);
niklase@google.com470e71d2011-07-07 08:21:25 +000030
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000031 ThreadPosix(ThreadRunFunction func, ThreadObj obj, ThreadPriority prio,
32 const char* thread_name);
pbos@webrtc.orga2a27182013-08-01 17:26:15 +000033 virtual ~ThreadPosix();
niklase@google.com470e71d2011-07-07 08:21:25 +000034
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000035 // From ThreadWrapper.
pbos@webrtc.orga2a27182013-08-01 17:26:15 +000036 virtual void SetNotAlive() OVERRIDE;
37 virtual bool Start(unsigned int& id) OVERRIDE;
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000038 // Not implemented on Mac.
39 virtual bool SetAffinity(const int* processor_numbers,
pbos@webrtc.orga2a27182013-08-01 17:26:15 +000040 unsigned int amount_of_processors) OVERRIDE;
41 virtual bool Stop() OVERRIDE;
niklase@google.com470e71d2011-07-07 08:21:25 +000042
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000043 void Run();
niklase@google.com470e71d2011-07-07 08:21:25 +000044
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000045 private:
46 int Construct();
niklase@google.com470e71d2011-07-07 08:21:25 +000047
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000048 private:
49 ThreadRunFunction run_function_;
50 ThreadObj obj_;
niklase@google.com470e71d2011-07-07 08:21:25 +000051
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000052 // Internal state.
53 CriticalSectionWrapper* crit_state_; // Protects alive_ and dead_
54 bool alive_;
55 bool dead_;
56 ThreadPriority prio_;
57 EventWrapper* event_;
niklase@google.com470e71d2011-07-07 08:21:25 +000058
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000059 // Zero-terminated thread name string.
60 char name_[kThreadMaxNameLength];
61 bool set_thread_name_;
niklase@google.com470e71d2011-07-07 08:21:25 +000062
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000063 // Handle to thread.
pwestin@webrtc.orgdf9866f2012-01-11 08:57:47 +000064#if (defined(WEBRTC_LINUX) || defined(WEBRTC_ANDROID))
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000065 pid_t pid_;
pwestin@webrtc.orgdf9866f2012-01-11 08:57:47 +000066#endif
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000067 pthread_attr_t attr_;
68 pthread_t thread_;
niklase@google.com470e71d2011-07-07 08:21:25 +000069};
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000070
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000071} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +000072
phoglund@webrtc.orgec9c9422013-01-02 08:45:03 +000073#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_THREAD_POSIX_H_