blob: 4de74229502e4b82635679af642685274c694fb6 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
2 * Copyright (c) 2011 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef SYSTEM_WRAPPERS_SOURCE_EVENT_POSIX_H_
12#define SYSTEM_WRAPPERS_SOURCE_EVENT_POSIX_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "system_wrappers/include/event_wrapper.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000015
jbauch555604a2016-04-26 03:13:22 -070016#include <memory>
17
niklase@google.com470e71d2011-07-07 08:21:25 +000018#include <pthread.h>
19#include <time.h>
20
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/platform_thread.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000022
23namespace webrtc {
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000024
25enum State {
26 kUp = 1,
27 kDown = 2
niklase@google.com470e71d2011-07-07 08:21:25 +000028};
29
Peter Boström64c03662015-04-08 11:24:19 +020030class EventTimerPosix : public EventTimerWrapper {
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000031 public:
Peter Boström64c03662015-04-08 11:24:19 +020032 EventTimerPosix();
33 ~EventTimerPosix() override;
niklase@google.com470e71d2011-07-07 08:21:25 +000034
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000035 EventTypeWrapper Wait(unsigned long max_time) override;
36 bool Set() override;
niklase@google.com470e71d2011-07-07 08:21:25 +000037
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000038 bool StartTimer(bool periodic, unsigned long time) override;
39 bool StopTimer() override;
niklase@google.com470e71d2011-07-07 08:21:25 +000040
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000041 private:
sprang53cf3462016-03-22 01:51:39 -070042 friend class EventTimerPosixTest;
43
tommi@webrtc.org27c0be92015-03-19 14:35:58 +000044 static bool Run(void* obj);
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000045 bool Process();
sprang53cf3462016-03-22 01:51:39 -070046 EventTypeWrapper Wait(timespec* end_at, bool reset_state);
niklase@google.com470e71d2011-07-07 08:21:25 +000047
sprang53cf3462016-03-22 01:51:39 -070048 virtual rtc::PlatformThread* CreateThread();
49
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000050 pthread_cond_t cond_;
51 pthread_mutex_t mutex_;
pbos@webrtc.orga8463712015-03-17 13:11:15 +000052 bool event_set_;
niklase@google.com470e71d2011-07-07 08:21:25 +000053
jbauch555604a2016-04-26 03:13:22 -070054 // TODO(pbos): Remove unique_ptr and use PlatformThread directly.
55 std::unique_ptr<rtc::PlatformThread> timer_thread_;
56 std::unique_ptr<EventTimerPosix> timer_event_;
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000057 timespec created_at_;
niklase@google.com470e71d2011-07-07 08:21:25 +000058
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000059 bool periodic_;
sprang53cf3462016-03-22 01:51:39 -070060 unsigned long time_ms_;
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000061 unsigned long count_;
sprang53cf3462016-03-22 01:51:39 -070062 bool is_stopping_;
niklase@google.com470e71d2011-07-07 08:21:25 +000063};
niklase@google.com470e71d2011-07-07 08:21:25 +000064
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000065} // namespace webrtc
66
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020067#endif // SYSTEM_WRAPPERS_SOURCE_EVENT_POSIX_H_