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 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 11 | #include "webrtc/system_wrappers/source/event_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 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 17 | EventWindows::EventWindows() |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 18 | : event_(::CreateEvent(NULL, // security attributes |
| 19 | FALSE, // manual reset |
| 20 | FALSE, // initial state |
| 21 | NULL)), // name of event |
| 22 | timerID_(NULL) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | } |
| 24 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 25 | EventWindows::~EventWindows() { |
| 26 | CloseHandle(event_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 27 | } |
| 28 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 29 | bool EventWindows::Set() { |
| 30 | // Note: setting an event that is already set has no effect. |
| 31 | return SetEvent(event_) == 1 ? true : false; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 32 | } |
| 33 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 34 | bool EventWindows::Reset() { |
| 35 | return ResetEvent(event_) == 1 ? true : false; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 36 | } |
| 37 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 38 | EventTypeWrapper EventWindows::Wait(unsigned long max_time) { |
| 39 | unsigned long res = WaitForSingleObject(event_, max_time); |
| 40 | switch (res) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 41 | case WAIT_OBJECT_0: |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 42 | return kEventSignaled; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 43 | case WAIT_TIMEOUT: |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 44 | return kEventTimeout; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 45 | default: |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 46 | return kEventError; |
| 47 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 48 | } |
| 49 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 50 | bool EventWindows::StartTimer(bool periodic, unsigned long time) { |
| 51 | if (timerID_ != NULL) { |
| 52 | timeKillEvent(timerID_); |
| 53 | timerID_ = NULL; |
| 54 | } |
| 55 | if (periodic) { |
| 56 | timerID_ = timeSetEvent(time, 0, (LPTIMECALLBACK)HANDLE(event_), 0, |
| 57 | TIME_PERIODIC | TIME_CALLBACK_EVENT_PULSE); |
| 58 | } else { |
| 59 | timerID_ = timeSetEvent(time, 0, (LPTIMECALLBACK)HANDLE(event_), 0, |
| 60 | TIME_ONESHOT | TIME_CALLBACK_EVENT_SET); |
| 61 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 62 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 63 | if (timerID_ == NULL) { |
| 64 | return false; |
| 65 | } |
| 66 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 67 | } |
| 68 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 69 | bool EventWindows::StopTimer() { |
| 70 | timeKillEvent(timerID_); |
| 71 | timerID_ = NULL; |
| 72 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 73 | } |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 74 | |
| 75 | } // namespace webrtc |