blob: b6a93feb8229ea9e693b47515aeedaa7886c7cc1 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "system_wrappers/source/event_timer_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
Peter Boström64c03662015-04-08 11:24:19 +020017// static
18EventTimerWrapper* EventTimerWrapper::Create() {
19 return new EventTimerWin();
20}
21
22EventTimerWin::EventTimerWin()
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000023 : event_(::CreateEvent(NULL, // security attributes
24 FALSE, // manual reset
25 FALSE, // initial state
26 NULL)), // name of event
Karl Wiberg79eb1d92017-11-08 12:26:07 +010027 timerID_(NULL) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000028
Peter Boström64c03662015-04-08 11:24:19 +020029EventTimerWin::~EventTimerWin() {
pbos@webrtc.orgc0167702013-10-02 13:11:15 +000030 StopTimer();
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000031 CloseHandle(event_);
niklase@google.com470e71d2011-07-07 08:21:25 +000032}
33
Peter Boström64c03662015-04-08 11:24:19 +020034bool EventTimerWin::Set() {
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000035 // Note: setting an event that is already set has no effect.
pbos@webrtc.orgc0167702013-10-02 13:11:15 +000036 return SetEvent(event_) == 1;
niklase@google.com470e71d2011-07-07 08:21:25 +000037}
38
Peter Boström64c03662015-04-08 11:24:19 +020039EventTypeWrapper EventTimerWin::Wait(unsigned long max_time) {
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000040 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
Peter Boström64c03662015-04-08 11:24:19 +020051bool EventTimerWin::StartTimer(bool periodic, unsigned long time) {
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000052 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
Peter Boström64c03662015-04-08 11:24:19 +020068bool EventTimerWin::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