blob: efcb5af870f8aea9199d79dccf87064ff9931276 [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
andrew@webrtc.org59ccd5c2011-12-15 00:17:43 +000011#include "event_win.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
13#include "Mmsystem.h"
14
15namespace webrtc {
16EventWindows::EventWindows()
17 : _event(::CreateEvent(NULL /* security attributes */,
18 FALSE /* manual reset */,
19 FALSE /* initial state */,
20 NULL /* name of event */)),
21 _timerID(NULL)
22{
23}
24
25EventWindows::~EventWindows()
26{
27 CloseHandle(_event);
28}
29
30bool EventWindows::Set()
31{
32 // Note: setting an event that is already set has no effect.
phoglund@webrtc.org9d9ad882012-02-07 16:16:52 +000033 return SetEvent(_event) == 1 ? true : false;
niklase@google.com470e71d2011-07-07 08:21:25 +000034}
35
36bool EventWindows::Reset()
37{
phoglund@webrtc.org9d9ad882012-02-07 16:16:52 +000038 return ResetEvent(_event) == 1 ? true : false;
niklase@google.com470e71d2011-07-07 08:21:25 +000039}
40
41EventTypeWrapper EventWindows::Wait(unsigned long maxTime)
42{
43 unsigned long res = WaitForSingleObject(_event, maxTime);
44 switch(res)
45 {
46 case WAIT_OBJECT_0:
47 return kEventSignaled;
48 case WAIT_TIMEOUT:
49 return kEventTimeout;
50 default:
51 return kEventError;
52 }
53}
54
55bool EventWindows::StartTimer(bool periodic, unsigned long time)
56{
57 if (_timerID != NULL)
58 {
59 timeKillEvent(_timerID);
60 _timerID=NULL;
61 }
62 if (periodic)
63 {
64 _timerID=timeSetEvent(time, 0,(LPTIMECALLBACK)HANDLE(_event),0,
65 TIME_PERIODIC|TIME_CALLBACK_EVENT_PULSE);
66 } else {
67 _timerID=timeSetEvent(time, 0,(LPTIMECALLBACK)HANDLE(_event),0,
68 TIME_ONESHOT|TIME_CALLBACK_EVENT_SET);
69 }
70
71 if (_timerID == NULL)
72 {
73 return false;
74 }
75 return true;
76}
77
78bool EventWindows::StopTimer()
79{
80 timeKillEvent(_timerID);
81 _timerID = NULL;
82 return true;
83}
84} // namespace webrtc