3 * Copyright 2004--2008, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #include "talk/base/event.h"
37 #error "Must define either WIN32 or POSIX."
44 Event::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.
49 is_initially_signaled_,
51 ASSERT(event_handle_ != NULL);
55 CloseHandle(event_handle_);
59 SetEvent(event_handle_);
63 ResetEvent(event_handle_);
66 bool Event::Wait(int cms) {
67 DWORD ms = (cms == kForever)? INFINITE : cms;
68 return (WaitForSingleObject(event_handle_, ms) == WAIT_OBJECT_0);
73 Event::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);
81 pthread_mutex_destroy(&event_mutex_);
82 pthread_cond_destroy(&event_cond_);
86 pthread_mutex_lock(&event_mutex_);
88 pthread_cond_broadcast(&event_cond_);
89 pthread_mutex_unlock(&event_mutex_);
93 pthread_mutex_lock(&event_mutex_);
94 event_status_ = false;
95 pthread_mutex_unlock(&event_mutex_);
98 bool Event::Wait(int cms) {
99 pthread_mutex_lock(&event_mutex_);
102 if (cms != kForever) {
103 // Converting from seconds and microseconds (1e-6) plus
104 // milliseconds (1e-3) to seconds and nanoseconds (1e-9).
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;
114 gettimeofday(&tv, NULL);
116 ts.tv_sec = tv.tv_sec + (cms / 1000);
117 ts.tv_nsec = tv.tv_usec * 1000 + (cms % 1000) * 1000000;
120 if (ts.tv_nsec >= 1000000000) {
122 ts.tv_nsec -= 1000000000;
126 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);
131 error = pthread_cond_timedwait(&event_cond_, &event_mutex_, &ts);
135 while (!event_status_ && error == 0)
136 error = pthread_cond_wait(&event_cond_, &event_mutex_);
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;
145 pthread_mutex_unlock(&event_mutex_);
152 } // namespace talk_base