blob: bbf51f72db7b36c10ad412fa7f630f8ecec62d0b [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:
tommi@webrtc.org27c0be92015-03-19 14:35:58 +000040 static bool Run(void* obj);
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000041 bool Process();
pbos@webrtc.orga8463712015-03-17 13:11:15 +000042 EventTypeWrapper Wait(timespec* end_at);
niklase@google.com470e71d2011-07-07 08:21:25 +000043
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000044 private:
45 pthread_cond_t cond_;
46 pthread_mutex_t mutex_;
pbos@webrtc.orga8463712015-03-17 13:11:15 +000047 bool event_set_;
niklase@google.com470e71d2011-07-07 08:21:25 +000048
Peter Boström8c38e8b2015-11-26 17:45:47 +010049 // TODO(pbos): Remove scoped_ptr and use PlatformThread directly.
50 rtc::scoped_ptr<rtc::PlatformThread> timer_thread_;
Peter Boström64c03662015-04-08 11:24:19 +020051 rtc::scoped_ptr<EventTimerPosix> timer_event_;
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000052 timespec created_at_;
niklase@google.com470e71d2011-07-07 08:21:25 +000053
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000054 bool periodic_;
55 unsigned long time_; // In ms
56 unsigned long count_;
niklase@google.com470e71d2011-07-07 08:21:25 +000057};
niklase@google.com470e71d2011-07-07 08:21:25 +000058
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000059} // namespace webrtc
60
61#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_EVENT_POSIX_H_