blob: 72d6753e533ecbe027784cf56d8f64af95878ac6 [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
Karl Wiberg79eb1d92017-11-08 12:26:07 +010025enum State { kUp = 1, kDown = 2 };
niklase@google.com470e71d2011-07-07 08:21:25 +000026
Peter Boström64c03662015-04-08 11:24:19 +020027class EventTimerPosix : public EventTimerWrapper {
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000028 public:
Peter Boström64c03662015-04-08 11:24:19 +020029 EventTimerPosix();
30 ~EventTimerPosix() override;
niklase@google.com470e71d2011-07-07 08:21:25 +000031
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000032 EventTypeWrapper Wait(unsigned long max_time) override;
33 bool Set() override;
niklase@google.com470e71d2011-07-07 08:21:25 +000034
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000035 bool StartTimer(bool periodic, unsigned long time) override;
36 bool StopTimer() override;
niklase@google.com470e71d2011-07-07 08:21:25 +000037
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000038 private:
sprang53cf3462016-03-22 01:51:39 -070039 friend class EventTimerPosixTest;
40
tommi@webrtc.org27c0be92015-03-19 14:35:58 +000041 static bool Run(void* obj);
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000042 bool Process();
sprang53cf3462016-03-22 01:51:39 -070043 EventTypeWrapper Wait(timespec* end_at, bool reset_state);
niklase@google.com470e71d2011-07-07 08:21:25 +000044
sprang53cf3462016-03-22 01:51:39 -070045 virtual rtc::PlatformThread* CreateThread();
46
Karl Wiberg79eb1d92017-11-08 12:26:07 +010047 pthread_cond_t cond_;
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000048 pthread_mutex_t mutex_;
pbos@webrtc.orga8463712015-03-17 13:11:15 +000049 bool event_set_;
niklase@google.com470e71d2011-07-07 08:21:25 +000050
jbauch555604a2016-04-26 03:13:22 -070051 // TODO(pbos): Remove unique_ptr and use PlatformThread directly.
52 std::unique_ptr<rtc::PlatformThread> timer_thread_;
53 std::unique_ptr<EventTimerPosix> timer_event_;
Karl Wiberg79eb1d92017-11-08 12:26:07 +010054 timespec created_at_;
niklase@google.com470e71d2011-07-07 08:21:25 +000055
Karl Wiberg79eb1d92017-11-08 12:26:07 +010056 bool periodic_;
sprang53cf3462016-03-22 01:51:39 -070057 unsigned long time_ms_;
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000058 unsigned long count_;
sprang53cf3462016-03-22 01:51:39 -070059 bool is_stopping_;
niklase@google.com470e71d2011-07-07 08:21:25 +000060};
niklase@google.com470e71d2011-07-07 08:21:25 +000061
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000062} // namespace webrtc
63
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020064#endif // SYSTEM_WRAPPERS_SOURCE_EVENT_POSIX_H_