1
// Copyright 2008, Google Inc.
3
// Redistribution and use in source and binary forms, with or without
4
// modification, are permitted provided that the following conditions are met:
6
// 1. Redistributions of source code must retain the above copyright notice,
7
// this list of conditions and the following disclaimer.
8
// 2. Redistributions in binary form must reproduce the above copyright notice,
9
// this list of conditions and the following disclaimer in the documentation
10
// and/or other materials provided with the distribution.
11
// 3. Neither the name of Google Inc. nor the names of its contributors may be
12
// used to endorse or promote products derived from this software without
13
// specific prior written permission.
15
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
#if defined(WIN32) || defined(OS_WINCE)
31
#include "gears/base/common/mutex.h"
32
#include "gears/base/common/stopwatch.h" // For GetCurrentTimeMillis()
35
Sleep(0); // equivalent to 'yield' in Win32
43
InitializeCriticalSection(&crit_sec_);
48
DeleteCriticalSection(&crit_sec_);
53
EnterCriticalSection(&crit_sec_);
55
assert(!is_locked_); // Google frowns upon mutex re-entrancy
61
void Mutex::Unlock() {
66
LeaveCriticalSection(&crit_sec_);
70
// CondVar::Event implementation
72
// This class adds reference counting to windows manual events.
73
// Since Windows XP & earlier do not implement native condition variables,
74
// we're implementing a condition variable using windows events.
77
class CondVar::Event : public RefCounted {
81
HANDLE Handle() const;
87
CondVar::Event::Event() {
88
event_ = CreateEvent(NULL, TRUE, FALSE, NULL);
92
CondVar::Event::~Event() {
94
BOOL result = CloseHandle(event_);
95
assert(result == TRUE);
102
HANDLE CondVar::Event::Handle() const {
107
// CondVar implementation
109
// Each event will correspond to a single call to CondVar::SignalAll.
110
// An event is created when the first interested party calls Wait.
111
// Subsequent calls to Wait will wait on the same event.
112
// When signal is called, the CondVar removes its reference to the event then
113
// sets the event to wake up the current waiters.
114
// While those waiters are being serviced, subsequent calls to Wait will result
115
// in the creation of a new event.
122
CondVar::~CondVar() {
124
// Make sure nobody is waiting.
125
MutexLock locker(¤t_event_mutex_);
126
if (current_event_.get()) {
127
assert(current_event_->GetRef() == 1);
128
current_event_.reset();
134
void CondVar::Wait(Mutex *mutex) {
135
WaitWithTimeout(mutex, INFINITE);
139
bool CondVar::WaitWithTimeout(Mutex *mutex, int milliseconds) {
141
assert(mutex->is_locked_);
143
scoped_refptr<Event> event;
145
MutexLock locker(¤t_event_mutex_);
146
if (current_event_.get() == 0) {
147
current_event_ = new Event;
149
event = current_event_;
152
DWORD result = WaitForSingleObject(event->Handle(), milliseconds);
154
return result == WAIT_TIMEOUT;
158
void CondVar::SignalAll() {
159
scoped_refptr<Event> old_event;
161
MutexLock locker(¤t_event_mutex_);
162
if (current_event_.get() == 0) return;
163
old_event = current_event_;
164
current_event_.reset();
167
BOOL result = SetEvent(old_event->Handle());
168
assert(result == TRUE);
170
SetEvent(old_event->Handle());
174
#endif // defined(WIN32) || defined(OS_WINCE)