niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
phoglund@webrtc.org | 9d9ad88 | 2012-02-07 16:16:52 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "system_wrappers/source/event_timer_win.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
| 13 | #include "Mmsystem.h" |
| 14 | |
| 15 | namespace webrtc { |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 16 | |
Peter Boström | 64c0366 | 2015-04-08 11:24:19 +0200 | [diff] [blame] | 17 | // static |
| 18 | EventTimerWrapper* EventTimerWrapper::Create() { |
| 19 | return new EventTimerWin(); |
| 20 | } |
| 21 | |
| 22 | EventTimerWin::EventTimerWin() |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 23 | : event_(::CreateEvent(NULL, // security attributes |
| 24 | FALSE, // manual reset |
| 25 | FALSE, // initial state |
| 26 | NULL)), // name of event |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 27 | timerID_(NULL) {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 28 | |
Peter Boström | 64c0366 | 2015-04-08 11:24:19 +0200 | [diff] [blame] | 29 | EventTimerWin::~EventTimerWin() { |
pbos@webrtc.org | c016770 | 2013-10-02 13:11:15 +0000 | [diff] [blame] | 30 | StopTimer(); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 31 | CloseHandle(event_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Peter Boström | 64c0366 | 2015-04-08 11:24:19 +0200 | [diff] [blame] | 34 | bool EventTimerWin::Set() { |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 35 | // Note: setting an event that is already set has no effect. |
pbos@webrtc.org | c016770 | 2013-10-02 13:11:15 +0000 | [diff] [blame] | 36 | return SetEvent(event_) == 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Peter Boström | 64c0366 | 2015-04-08 11:24:19 +0200 | [diff] [blame] | 39 | EventTypeWrapper EventTimerWin::Wait(unsigned long max_time) { |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 40 | unsigned long res = WaitForSingleObject(event_, max_time); |
| 41 | switch (res) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 42 | case WAIT_OBJECT_0: |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 43 | return kEventSignaled; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 44 | case WAIT_TIMEOUT: |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 45 | return kEventTimeout; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 46 | default: |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 47 | return kEventError; |
| 48 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Peter Boström | 64c0366 | 2015-04-08 11:24:19 +0200 | [diff] [blame] | 51 | bool EventTimerWin::StartTimer(bool periodic, unsigned long time) { |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 52 | if (timerID_ != NULL) { |
| 53 | timeKillEvent(timerID_); |
| 54 | timerID_ = NULL; |
| 55 | } |
pbos@webrtc.org | c016770 | 2013-10-02 13:11:15 +0000 | [diff] [blame] | 56 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 57 | 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 64 | |
pbos@webrtc.org | c016770 | 2013-10-02 13:11:15 +0000 | [diff] [blame] | 65 | return timerID_ != NULL; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Peter Boström | 64c0366 | 2015-04-08 11:24:19 +0200 | [diff] [blame] | 68 | bool EventTimerWin::StopTimer() { |
pbos@webrtc.org | c016770 | 2013-10-02 13:11:15 +0000 | [diff] [blame] | 69 | if (timerID_ != NULL) { |
| 70 | timeKillEvent(timerID_); |
| 71 | timerID_ = NULL; |
| 72 | } |
| 73 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 74 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 75 | } |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 76 | |
| 77 | } // namespace webrtc |