blob: 90104495030be18bf36725c8b53c606c1e73296a [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() {
pbos@webrtc.orgc0167702013-10-02 13:11:15 +000026 StopTimer();
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000027 CloseHandle(event_);
niklase@google.com470e71d2011-07-07 08:21:25 +000028}
29
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000030bool EventWindows::Set() {
31 // Note: setting an event that is already set has no effect.
pbos@webrtc.orgc0167702013-10-02 13:11:15 +000032 return SetEvent(event_) == 1;
niklase@google.com470e71d2011-07-07 08:21:25 +000033}
34
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000035EventTypeWrapper EventWindows::Wait(unsigned long max_time) {
36 unsigned long res = WaitForSingleObject(event_, max_time);
37 switch (res) {
niklase@google.com470e71d2011-07-07 08:21:25 +000038 case WAIT_OBJECT_0:
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000039 return kEventSignaled;
niklase@google.com470e71d2011-07-07 08:21:25 +000040 case WAIT_TIMEOUT:
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000041 return kEventTimeout;
niklase@google.com470e71d2011-07-07 08:21:25 +000042 default:
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000043 return kEventError;
44 }
niklase@google.com470e71d2011-07-07 08:21:25 +000045}
46
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000047bool EventWindows::StartTimer(bool periodic, unsigned long time) {
48 if (timerID_ != NULL) {
49 timeKillEvent(timerID_);
50 timerID_ = NULL;
51 }
pbos@webrtc.orgc0167702013-10-02 13:11:15 +000052
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000053 if (periodic) {
54 timerID_ = timeSetEvent(time, 0, (LPTIMECALLBACK)HANDLE(event_), 0,
55 TIME_PERIODIC | TIME_CALLBACK_EVENT_PULSE);
56 } else {
57 timerID_ = timeSetEvent(time, 0, (LPTIMECALLBACK)HANDLE(event_), 0,
58 TIME_ONESHOT | TIME_CALLBACK_EVENT_SET);
59 }
niklase@google.com470e71d2011-07-07 08:21:25 +000060
pbos@webrtc.orgc0167702013-10-02 13:11:15 +000061 return timerID_ != NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +000062}
63
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000064bool EventWindows::StopTimer() {
pbos@webrtc.orgc0167702013-10-02 13:11:15 +000065 if (timerID_ != NULL) {
66 timeKillEvent(timerID_);
67 timerID_ = NULL;
68 }
69
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000070 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +000071}
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000072
73} // namespace webrtc