blob: 49c2da045016250d9f459d56866a6b1727a85f43 [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
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000014#include "webrtc/system_wrappers/interface/event_wrapper.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000015
16#include <pthread.h>
17#include <time.h>
18
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000019#include "webrtc/system_wrappers/interface/thread_wrapper.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
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000028class EventPosix : public EventWrapper {
29 public:
30 static EventWrapper* Create();
niklase@google.com470e71d2011-07-07 08:21:25 +000031
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000032 ~EventPosix() override;
niklase@google.com470e71d2011-07-07 08:21:25 +000033
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000034 EventTypeWrapper Wait(unsigned long max_time) override;
35 bool Set() override;
niklase@google.com470e71d2011-07-07 08:21:25 +000036
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000037 bool StartTimer(bool periodic, unsigned long time) override;
38 bool StopTimer() override;
niklase@google.com470e71d2011-07-07 08:21:25 +000039
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000040 private:
41 EventPosix();
niklase@google.com470e71d2011-07-07 08:21:25 +000042
tommi@webrtc.org27c0be92015-03-19 14:35:58 +000043 static bool Run(void* obj);
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000044 bool Process();
pbos@webrtc.orga8463712015-03-17 13:11:15 +000045 EventTypeWrapper Wait(timespec* end_at);
niklase@google.com470e71d2011-07-07 08:21:25 +000046
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000047 private:
48 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
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000052 ThreadWrapper* timer_thread_;
53 EventPosix* timer_event_;
54 timespec created_at_;
niklase@google.com470e71d2011-07-07 08:21:25 +000055
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000056 bool periodic_;
57 unsigned long time_; // In ms
58 unsigned long count_;
niklase@google.com470e71d2011-07-07 08:21:25 +000059};
niklase@google.com470e71d2011-07-07 08:21:25 +000060
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000061} // namespace webrtc
62
63#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_EVENT_POSIX_H_