blob: 5fbe061367116d7f1e15b6832e5cab589a820a04 [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
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000032 virtual ~EventPosix();
niklase@google.com470e71d2011-07-07 08:21:25 +000033
pbos@webrtc.orga2a27182013-08-01 17:26:15 +000034 virtual EventTypeWrapper Wait(unsigned long max_time) OVERRIDE;
35 virtual bool Set() OVERRIDE;
36 virtual bool Reset() OVERRIDE;
niklase@google.com470e71d2011-07-07 08:21:25 +000037
pbos@webrtc.orga2a27182013-08-01 17:26:15 +000038 virtual bool StartTimer(bool periodic, unsigned long time) OVERRIDE;
39 virtual bool StopTimer() OVERRIDE;
niklase@google.com470e71d2011-07-07 08:21:25 +000040
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000041 private:
42 EventPosix();
43 int Construct();
niklase@google.com470e71d2011-07-07 08:21:25 +000044
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000045 static bool Run(ThreadObj obj);
46 bool Process();
47 EventTypeWrapper Wait(timespec& wake_at);
niklase@google.com470e71d2011-07-07 08:21:25 +000048
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000049 private:
50 pthread_cond_t cond_;
51 pthread_mutex_t mutex_;
niklase@google.com470e71d2011-07-07 08:21:25 +000052
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000053 ThreadWrapper* timer_thread_;
54 EventPosix* timer_event_;
55 timespec created_at_;
niklase@google.com470e71d2011-07-07 08:21:25 +000056
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000057 bool periodic_;
58 unsigned long time_; // In ms
59 unsigned long count_;
60 State state_;
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_