1b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org/*
2b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *
4b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  Use of this source code is governed by a BSD-style license
5b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  that can be found in the LICENSE file in the root of the source
6b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  tree. An additional intellectual property rights grant can be found
7b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  in the file PATENTS.  All contributing project authors may
8b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
9b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org */
10b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
11b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_THREAD_POSIX_H_
12b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#define WEBRTC_SYSTEM_WRAPPERS_SOURCE_THREAD_POSIX_H_
13b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
1469e3fb96715bd0b45c1561010fe8eda162ade1adphoglund@webrtc.org#include "webrtc/system_wrappers/interface/thread_wrapper.h"
1569e3fb96715bd0b45c1561010fe8eda162ade1adphoglund@webrtc.org
16b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#include <pthread.h>
17b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
18b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgnamespace webrtc {
19b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
20b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgclass CriticalSectionWrapper;
21b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgclass EventWrapper;
22b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
2369e3fb96715bd0b45c1561010fe8eda162ade1adphoglund@webrtc.orgint ConvertToSystemPriority(ThreadPriority priority, int min_prio,
2469e3fb96715bd0b45c1561010fe8eda162ade1adphoglund@webrtc.org                            int max_prio);
25b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
2669e3fb96715bd0b45c1561010fe8eda162ade1adphoglund@webrtc.orgclass ThreadPosix : public ThreadWrapper {
2769e3fb96715bd0b45c1561010fe8eda162ade1adphoglund@webrtc.org public:
2869e3fb96715bd0b45c1561010fe8eda162ade1adphoglund@webrtc.org  static ThreadWrapper* Create(ThreadRunFunction func, ThreadObj obj,
2969e3fb96715bd0b45c1561010fe8eda162ade1adphoglund@webrtc.org                               ThreadPriority prio, const char* thread_name);
30b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
3169e3fb96715bd0b45c1561010fe8eda162ade1adphoglund@webrtc.org  ThreadPosix(ThreadRunFunction func, ThreadObj obj, ThreadPriority prio,
3269e3fb96715bd0b45c1561010fe8eda162ade1adphoglund@webrtc.org              const char* thread_name);
337affcd2236a1e8e9d5d70f21dc7a5c385f509d37pbos@webrtc.org  virtual ~ThreadPosix();
34b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
3569e3fb96715bd0b45c1561010fe8eda162ade1adphoglund@webrtc.org  // From ThreadWrapper.
367affcd2236a1e8e9d5d70f21dc7a5c385f509d37pbos@webrtc.org  virtual void SetNotAlive() OVERRIDE;
377affcd2236a1e8e9d5d70f21dc7a5c385f509d37pbos@webrtc.org  virtual bool Start(unsigned int& id) OVERRIDE;
3869e3fb96715bd0b45c1561010fe8eda162ade1adphoglund@webrtc.org  // Not implemented on Mac.
3969e3fb96715bd0b45c1561010fe8eda162ade1adphoglund@webrtc.org  virtual bool SetAffinity(const int* processor_numbers,
407affcd2236a1e8e9d5d70f21dc7a5c385f509d37pbos@webrtc.org                           unsigned int amount_of_processors) OVERRIDE;
417affcd2236a1e8e9d5d70f21dc7a5c385f509d37pbos@webrtc.org  virtual bool Stop() OVERRIDE;
42b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
4369e3fb96715bd0b45c1561010fe8eda162ade1adphoglund@webrtc.org  void Run();
44b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
4569e3fb96715bd0b45c1561010fe8eda162ade1adphoglund@webrtc.org private:
4669e3fb96715bd0b45c1561010fe8eda162ade1adphoglund@webrtc.org  int Construct();
47b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
4869e3fb96715bd0b45c1561010fe8eda162ade1adphoglund@webrtc.org private:
4969e3fb96715bd0b45c1561010fe8eda162ade1adphoglund@webrtc.org  ThreadRunFunction   run_function_;
5069e3fb96715bd0b45c1561010fe8eda162ade1adphoglund@webrtc.org  ThreadObj           obj_;
51b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
5269e3fb96715bd0b45c1561010fe8eda162ade1adphoglund@webrtc.org  // Internal state.
5369e3fb96715bd0b45c1561010fe8eda162ade1adphoglund@webrtc.org  CriticalSectionWrapper* crit_state_;  // Protects alive_ and dead_
5469e3fb96715bd0b45c1561010fe8eda162ade1adphoglund@webrtc.org  bool                    alive_;
5569e3fb96715bd0b45c1561010fe8eda162ade1adphoglund@webrtc.org  bool                    dead_;
5669e3fb96715bd0b45c1561010fe8eda162ade1adphoglund@webrtc.org  ThreadPriority          prio_;
5769e3fb96715bd0b45c1561010fe8eda162ade1adphoglund@webrtc.org  EventWrapper*           event_;
58b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
5969e3fb96715bd0b45c1561010fe8eda162ade1adphoglund@webrtc.org  // Zero-terminated thread name string.
6069e3fb96715bd0b45c1561010fe8eda162ade1adphoglund@webrtc.org  char                    name_[kThreadMaxNameLength];
6169e3fb96715bd0b45c1561010fe8eda162ade1adphoglund@webrtc.org  bool                    set_thread_name_;
62b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
6369e3fb96715bd0b45c1561010fe8eda162ade1adphoglund@webrtc.org  // Handle to thread.
64b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#if (defined(WEBRTC_LINUX) || defined(WEBRTC_ANDROID))
6569e3fb96715bd0b45c1561010fe8eda162ade1adphoglund@webrtc.org  pid_t                   pid_;
66b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#endif
6769e3fb96715bd0b45c1561010fe8eda162ade1adphoglund@webrtc.org  pthread_attr_t          attr_;
6869e3fb96715bd0b45c1561010fe8eda162ade1adphoglund@webrtc.org  pthread_t               thread_;
69b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org};
7069e3fb96715bd0b45c1561010fe8eda162ade1adphoglund@webrtc.org
713b89e10f31160da35b408fd00cb8f89d2b08862dpbos@webrtc.org}  // namespace webrtc
72b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
7369e3fb96715bd0b45c1561010fe8eda162ade1adphoglund@webrtc.org#endif  // WEBRTC_SYSTEM_WRAPPERS_SOURCE_THREAD_POSIX_H_
74