blob: 398e714a75962002c794440aa45d99d8b9d34369 [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 +000035bool EventWindows::Reset() {
pbos@webrtc.orgc0167702013-10-02 13:11:15 +000036 return ResetEvent(event_) == 1;
niklase@google.com470e71d2011-07-07 08:21:25 +000037}
38
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000039EventTypeWrapper EventWindows::Wait(unsigned long max_time) {
40 unsigned long res = WaitForSingleObject(event_, max_time);
41 switch (res) {
niklase@google.com470e71d2011-07-07 08:21:25 +000042 case WAIT_OBJECT_0:
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000043 return kEventSignaled;
niklase@google.com470e71d2011-07-07 08:21:25 +000044 case WAIT_TIMEOUT:
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000045 return kEventTimeout;
niklase@google.com470e71d2011-07-07 08:21:25 +000046 default:
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000047 return kEventError;
48 }
niklase@google.com470e71d2011-07-07 08:21:25 +000049}
50
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000051bool EventWindows::StartTimer(bool periodic, unsigned long time) {
52 if (timerID_ != NULL) {
53 timeKillEvent(timerID_);
54 timerID_ = NULL;
55 }
pbos@webrtc.orgc0167702013-10-02 13:11:15 +000056
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000057 if (periodic) {
58 timerID_ = timeSetEvent(time, 0, (LPTIMECALLBACK)HANDLE(event_), 0,
59 TIME_PERIODIC | TIME_CALLBACK_EVENT_PULSE);
60 } else {
61 timerID_ = timeSetEvent(time, 0, (LPTIMECALLBACK)HANDLE(event_), 0,
62 TIME_ONESHOT | TIME_CALLBACK_EVENT_SET);
63 }
niklase@google.com470e71d2011-07-07 08:21:25 +000064
pbos@webrtc.orgc0167702013-10-02 13:11:15 +000065 return timerID_ != NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +000066}
67
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000068bool EventWindows::StopTimer() {
pbos@webrtc.orgc0167702013-10-02 13:11:15 +000069 if (timerID_ != NULL) {
70 timeKillEvent(timerID_);
71 timerID_ = NULL;
72 }
73
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000074 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +000075}
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000076
77} // namespace webrtc