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 | |
Peter Boström | 64c0366 | 2015-04-08 11:24:19 +0200 | [diff] [blame] | 11 | #include "webrtc/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 |
| 27 | timerID_(NULL) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Peter Boström | 64c0366 | 2015-04-08 11:24:19 +0200 | [diff] [blame] | 30 | EventTimerWin::~EventTimerWin() { |
pbos@webrtc.org | c016770 | 2013-10-02 13:11:15 +0000 | [diff] [blame] | 31 | StopTimer(); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 32 | CloseHandle(event_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Peter Boström | 64c0366 | 2015-04-08 11:24:19 +0200 | [diff] [blame] | 35 | bool EventTimerWin::Set() { |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 36 | // Note: setting an event that is already set has no effect. |
pbos@webrtc.org | c016770 | 2013-10-02 13:11:15 +0000 | [diff] [blame] | 37 | return SetEvent(event_) == 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Peter Boström | 64c0366 | 2015-04-08 11:24:19 +0200 | [diff] [blame] | 40 | EventTypeWrapper EventTimerWin::Wait(unsigned long max_time) { |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 41 | unsigned long res = WaitForSingleObject(event_, max_time); |
| 42 | switch (res) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 43 | case WAIT_OBJECT_0: |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 44 | return kEventSignaled; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 45 | case WAIT_TIMEOUT: |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 46 | return kEventTimeout; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 47 | default: |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 48 | return kEventError; |
| 49 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Peter Boström | 64c0366 | 2015-04-08 11:24:19 +0200 | [diff] [blame] | 52 | bool EventTimerWin::StartTimer(bool periodic, unsigned long time) { |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 53 | if (timerID_ != NULL) { |
| 54 | timeKillEvent(timerID_); |
| 55 | timerID_ = NULL; |
| 56 | } |
pbos@webrtc.org | c016770 | 2013-10-02 13:11:15 +0000 | [diff] [blame] | 57 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 58 | if (periodic) { |
| 59 | timerID_ = timeSetEvent(time, 0, (LPTIMECALLBACK)HANDLE(event_), 0, |
| 60 | TIME_PERIODIC | TIME_CALLBACK_EVENT_PULSE); |
| 61 | } else { |
| 62 | timerID_ = timeSetEvent(time, 0, (LPTIMECALLBACK)HANDLE(event_), 0, |
| 63 | TIME_ONESHOT | TIME_CALLBACK_EVENT_SET); |
| 64 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 65 | |
pbos@webrtc.org | c016770 | 2013-10-02 13:11:15 +0000 | [diff] [blame] | 66 | return timerID_ != NULL; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Peter Boström | 64c0366 | 2015-04-08 11:24:19 +0200 | [diff] [blame] | 69 | bool EventTimerWin::StopTimer() { |
pbos@webrtc.org | c016770 | 2013-10-02 13:11:15 +0000 | [diff] [blame] | 70 | if (timerID_ != NULL) { |
| 71 | timeKillEvent(timerID_); |
| 72 | timerID_ = NULL; |
| 73 | } |
| 74 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 75 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 76 | } |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 77 | |
| 78 | } // namespace webrtc |