blob: 410c65851f5d77badf097cb11e74c4a1de57415d [file] [log] [blame]
henrike@webrtc.org0e118e72013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004--2008, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/base/event.h"
29
30#if defined(WIN32)
31#include <windows.h>
32#elif defined(POSIX)
33#include <pthread.h>
34#include <sys/time.h>
35#include <time.h>
36#else
37#error "Must define either WIN32 or POSIX."
38#endif
39
40namespace talk_base {
41
42#if defined(WIN32)
43
44Event::Event(bool manual_reset, bool initially_signaled)
45 : is_manual_reset_(manual_reset),
46 is_initially_signaled_(initially_signaled) {
47 event_handle_ = ::CreateEvent(NULL, // Security attributes.
48 is_manual_reset_,
49 is_initially_signaled_,
50 NULL); // Name.
51 ASSERT(event_handle_ != NULL);
52}
53
54Event::~Event() {
55 CloseHandle(event_handle_);
56}
57
58void Event::Set() {
59 SetEvent(event_handle_);
60}
61
62void Event::Reset() {
63 ResetEvent(event_handle_);
64}
65
66bool Event::Wait(int cms) {
67 DWORD ms = (cms == kForever)? INFINITE : cms;
68 return (WaitForSingleObject(event_handle_, ms) == WAIT_OBJECT_0);
69}
70
71#elif defined(POSIX)
72
73Event::Event(bool manual_reset, bool initially_signaled)
74 : is_manual_reset_(manual_reset),
75 event_status_(initially_signaled) {
76 VERIFY(pthread_mutex_init(&event_mutex_, NULL) == 0);
77 VERIFY(pthread_cond_init(&event_cond_, NULL) == 0);
78}
79
80Event::~Event() {
81 pthread_mutex_destroy(&event_mutex_);
82 pthread_cond_destroy(&event_cond_);
83}
84
85void Event::Set() {
86 pthread_mutex_lock(&event_mutex_);
87 event_status_ = true;
88 pthread_cond_broadcast(&event_cond_);
89 pthread_mutex_unlock(&event_mutex_);
90}
91
92void Event::Reset() {
93 pthread_mutex_lock(&event_mutex_);
94 event_status_ = false;
95 pthread_mutex_unlock(&event_mutex_);
96}
97
98bool Event::Wait(int cms) {
99 pthread_mutex_lock(&event_mutex_);
100 int error = 0;
101
102 if (cms != kForever) {
103 // Converting from seconds and microseconds (1e-6) plus
104 // milliseconds (1e-3) to seconds and nanoseconds (1e-9).
105
henrike@webrtc.orgfa87e752014-02-18 22:28:52 +0000106 struct timespec ts;
107#if HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE
108 // Use relative time version, which tends to be more efficient for
109 // pthread implementations where provided (like on Android).
110 ts.tv_sec = cms / 1000;
111 ts.tv_nsec = (cms % 1000) * 1000000;
112#else
henrike@webrtc.org0e118e72013-07-10 00:45:36 +0000113 struct timeval tv;
114 gettimeofday(&tv, NULL);
115
henrike@webrtc.org0e118e72013-07-10 00:45:36 +0000116 ts.tv_sec = tv.tv_sec + (cms / 1000);
117 ts.tv_nsec = tv.tv_usec * 1000 + (cms % 1000) * 1000000;
118
119 // Handle overflow.
120 if (ts.tv_nsec >= 1000000000) {
121 ts.tv_sec++;
122 ts.tv_nsec -= 1000000000;
123 }
henrike@webrtc.orgfa87e752014-02-18 22:28:52 +0000124#endif
henrike@webrtc.org0e118e72013-07-10 00:45:36 +0000125
henrike@webrtc.orgfa87e752014-02-18 22:28:52 +0000126 while (!event_status_ && error == 0) {
127#if HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE
128 error = pthread_cond_timedwait_relative_np(
129 &event_cond_, &event_mutex_, &ts);
130#else
henrike@webrtc.org0e118e72013-07-10 00:45:36 +0000131 error = pthread_cond_timedwait(&event_cond_, &event_mutex_, &ts);
henrike@webrtc.orgfa87e752014-02-18 22:28:52 +0000132#endif
133 }
henrike@webrtc.org0e118e72013-07-10 00:45:36 +0000134 } else {
135 while (!event_status_ && error == 0)
136 error = pthread_cond_wait(&event_cond_, &event_mutex_);
137 }
138
139 // NOTE(liulk): Exactly one thread will auto-reset this event. All
140 // the other threads will think it's unsignaled. This seems to be
141 // consistent with auto-reset events in WIN32.
142 if (error == 0 && !is_manual_reset_)
143 event_status_ = false;
144
145 pthread_mutex_unlock(&event_mutex_);
146
147 return (error == 0);
148}
149
150#endif
151
152} // namespace talk_base