blob: af3715ee8b222186672f24eb031f2eb3827acd0b [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
ajm@google.comb5c49ff2011-08-01 17:04:04 +000011#ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_EVENT_POSIX_H_
12#define WEBRTC_SYSTEM_WRAPPERS_SOURCE_EVENT_POSIX_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
Henrik Kjellander98f53512015-10-28 18:17:40 +010014#include "webrtc/system_wrappers/include/event_wrapper.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000015
16#include <pthread.h>
17#include <time.h>
18
pbos12411ef2015-11-23 14:47:56 -080019#include "webrtc/base/platform_thread.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000020
21namespace webrtc {
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000022
23enum State {
24 kUp = 1,
25 kDown = 2
niklase@google.com470e71d2011-07-07 08:21:25 +000026};
27
Peter Boström64c03662015-04-08 11:24:19 +020028class EventTimerPosix : public EventTimerWrapper {
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000029 public:
Peter Boström64c03662015-04-08 11:24:19 +020030 EventTimerPosix();
31 ~EventTimerPosix() override;
niklase@google.com470e71d2011-07-07 08:21:25 +000032
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000033 EventTypeWrapper Wait(unsigned long max_time) override;
34 bool Set() override;
niklase@google.com470e71d2011-07-07 08:21:25 +000035
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000036 bool StartTimer(bool periodic, unsigned long time) override;
37 bool StopTimer() override;
niklase@google.com470e71d2011-07-07 08:21:25 +000038
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000039 private:
sprang53cf3462016-03-22 01:51:39 -070040 friend class EventTimerPosixTest;
41
tommi@webrtc.org27c0be92015-03-19 14:35:58 +000042 static bool Run(void* obj);
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000043 bool Process();
sprang53cf3462016-03-22 01:51:39 -070044 EventTypeWrapper Wait(timespec* end_at, bool reset_state);
niklase@google.com470e71d2011-07-07 08:21:25 +000045
sprang53cf3462016-03-22 01:51:39 -070046 virtual rtc::PlatformThread* CreateThread();
47
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000048 pthread_cond_t cond_;
49 pthread_mutex_t mutex_;
pbos@webrtc.orga8463712015-03-17 13:11:15 +000050 bool event_set_;
niklase@google.com470e71d2011-07-07 08:21:25 +000051
Peter Boström8c38e8b2015-11-26 17:45:47 +010052 // TODO(pbos): Remove scoped_ptr and use PlatformThread directly.
53 rtc::scoped_ptr<rtc::PlatformThread> timer_thread_;
Peter Boström64c03662015-04-08 11:24:19 +020054 rtc::scoped_ptr<EventTimerPosix> timer_event_;
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000055 timespec created_at_;
niklase@google.com470e71d2011-07-07 08:21:25 +000056
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000057 bool periodic_;
sprang53cf3462016-03-22 01:51:39 -070058 unsigned long time_ms_;
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000059 unsigned long count_;
sprang53cf3462016-03-22 01:51:39 -070060 bool is_stopping_;
niklase@google.com470e71d2011-07-07 08:21:25 +000061};
niklase@google.com470e71d2011-07-07 08:21:25 +000062
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000063} // namespace webrtc
64
65#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_EVENT_POSIX_H_