blob: d2032131d0814f74ac1e498abdc217543cb24082 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
phoglund@webrtc.org9d9ad882012-02-07 16:16:52 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000011#include "webrtc/system_wrappers/source/event_win.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
13#include "Mmsystem.h"
14
15namespace webrtc {
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000016
niklase@google.com470e71d2011-07-07 08:21:25 +000017EventWindows::EventWindows()
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000018 : event_(::CreateEvent(NULL, // security attributes
19 FALSE, // manual reset
20 FALSE, // initial state
21 NULL)), // name of event
22 timerID_(NULL) {
niklase@google.com470e71d2011-07-07 08:21:25 +000023}
24
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000025EventWindows::~EventWindows() {
26 CloseHandle(event_);
niklase@google.com470e71d2011-07-07 08:21:25 +000027}
28
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000029bool EventWindows::Set() {
30 // Note: setting an event that is already set has no effect.
31 return SetEvent(event_) == 1 ? true : false;
niklase@google.com470e71d2011-07-07 08:21:25 +000032}
33
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000034bool EventWindows::Reset() {
35 return ResetEvent(event_) == 1 ? true : false;
niklase@google.com470e71d2011-07-07 08:21:25 +000036}
37
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000038EventTypeWrapper EventWindows::Wait(unsigned long max_time) {
39 unsigned long res = WaitForSingleObject(event_, max_time);
40 switch (res) {
niklase@google.com470e71d2011-07-07 08:21:25 +000041 case WAIT_OBJECT_0:
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000042 return kEventSignaled;
niklase@google.com470e71d2011-07-07 08:21:25 +000043 case WAIT_TIMEOUT:
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000044 return kEventTimeout;
niklase@google.com470e71d2011-07-07 08:21:25 +000045 default:
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000046 return kEventError;
47 }
niklase@google.com470e71d2011-07-07 08:21:25 +000048}
49
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000050bool EventWindows::StartTimer(bool periodic, unsigned long time) {
51 if (timerID_ != NULL) {
52 timeKillEvent(timerID_);
53 timerID_ = NULL;
54 }
55 if (periodic) {
56 timerID_ = timeSetEvent(time, 0, (LPTIMECALLBACK)HANDLE(event_), 0,
57 TIME_PERIODIC | TIME_CALLBACK_EVENT_PULSE);
58 } else {
59 timerID_ = timeSetEvent(time, 0, (LPTIMECALLBACK)HANDLE(event_), 0,
60 TIME_ONESHOT | TIME_CALLBACK_EVENT_SET);
61 }
niklase@google.com470e71d2011-07-07 08:21:25 +000062
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000063 if (timerID_ == NULL) {
64 return false;
65 }
66 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +000067}
68
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000069bool EventWindows::StopTimer() {
70 timeKillEvent(timerID_);
71 timerID_ = NULL;
72 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +000073}
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000074
75} // namespace webrtc