1/*
2 *  Copyright (c) 2012 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#ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_THREAD_POSIX_H_
12#define WEBRTC_SYSTEM_WRAPPERS_SOURCE_THREAD_POSIX_H_
13
14#include "webrtc/system_wrappers/interface/thread_wrapper.h"
15
16#include <pthread.h>
17
18namespace webrtc {
19
20class CriticalSectionWrapper;
21class EventWrapper;
22
23int ConvertToSystemPriority(ThreadPriority priority, int min_prio,
24                            int max_prio);
25
26class ThreadPosix : public ThreadWrapper {
27 public:
28  static ThreadWrapper* Create(ThreadRunFunction func, ThreadObj obj,
29                               ThreadPriority prio, const char* thread_name);
30
31  ThreadPosix(ThreadRunFunction func, ThreadObj obj, ThreadPriority prio,
32              const char* thread_name);
33  virtual ~ThreadPosix();
34
35  // From ThreadWrapper.
36  virtual void SetNotAlive() OVERRIDE;
37  virtual bool Start(unsigned int& id) OVERRIDE;
38  // Not implemented on Mac.
39  virtual bool SetAffinity(const int* processor_numbers,
40                           unsigned int amount_of_processors) OVERRIDE;
41  virtual bool Stop() OVERRIDE;
42
43  void Run();
44
45 private:
46  int Construct();
47
48 private:
49  ThreadRunFunction   run_function_;
50  ThreadObj           obj_;
51
52  // Internal state.
53  CriticalSectionWrapper* crit_state_;  // Protects alive_ and dead_
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                    set_thread_name_;
62
63  // Handle to thread.
64#if (defined(WEBRTC_LINUX) || defined(WEBRTC_ANDROID))
65  pid_t                   pid_;
66#endif
67  pthread_attr_t          attr_;
68  pthread_t               thread_;
69};
70
71}  // namespace webrtc
72
73#endif  // WEBRTC_SYSTEM_WRAPPERS_SOURCE_THREAD_POSIX_H_
74