2016-01-19 02:59:56 -08:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "webrtc/system_wrappers/source/rw_lock_winxp_win.h"
|
|
|
|
|
|
|
|
|
|
namespace webrtc {
|
2016-01-20 13:36:31 +01:00
|
|
|
namespace {
|
|
|
|
|
class ScopedLock {
|
|
|
|
|
public:
|
|
|
|
|
ScopedLock(CRITICAL_SECTION* lock) : lock_(lock) {
|
|
|
|
|
EnterCriticalSection(lock_);
|
|
|
|
|
}
|
|
|
|
|
~ScopedLock() {
|
|
|
|
|
LeaveCriticalSection(lock_);
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
CRITICAL_SECTION* const lock_;
|
|
|
|
|
};
|
|
|
|
|
}
|
2016-01-19 02:59:56 -08:00
|
|
|
|
2016-01-20 13:36:31 +01:00
|
|
|
RWLockWinXP::RWLockWinXP() {
|
|
|
|
|
InitializeCriticalSection(&critical_section_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RWLockWinXP::~RWLockWinXP() {
|
|
|
|
|
DeleteCriticalSection(&critical_section_);
|
|
|
|
|
}
|
2016-01-19 02:59:56 -08:00
|
|
|
|
|
|
|
|
void RWLockWinXP::AcquireLockExclusive() {
|
2016-01-20 13:36:31 +01:00
|
|
|
ScopedLock cs(&critical_section_);
|
2016-01-19 02:59:56 -08:00
|
|
|
if (writer_active_ || readers_active_ > 0) {
|
|
|
|
|
++writers_waiting_;
|
|
|
|
|
while (writer_active_ || readers_active_ > 0) {
|
2016-01-20 13:36:31 +01:00
|
|
|
write_condition_.SleepCS(&critical_section_);
|
2016-01-19 02:59:56 -08:00
|
|
|
}
|
|
|
|
|
--writers_waiting_;
|
|
|
|
|
}
|
|
|
|
|
writer_active_ = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RWLockWinXP::ReleaseLockExclusive() {
|
2016-01-20 13:36:31 +01:00
|
|
|
ScopedLock cs(&critical_section_);
|
2016-01-19 02:59:56 -08:00
|
|
|
writer_active_ = false;
|
|
|
|
|
if (writers_waiting_ > 0) {
|
|
|
|
|
write_condition_.Wake();
|
|
|
|
|
} else if (readers_waiting_ > 0) {
|
|
|
|
|
read_condition_.WakeAll();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RWLockWinXP::AcquireLockShared() {
|
2016-01-20 13:36:31 +01:00
|
|
|
ScopedLock cs(&critical_section_);
|
2016-01-19 02:59:56 -08:00
|
|
|
if (writer_active_ || writers_waiting_ > 0) {
|
|
|
|
|
++readers_waiting_;
|
|
|
|
|
|
|
|
|
|
while (writer_active_ || writers_waiting_ > 0) {
|
2016-01-20 13:36:31 +01:00
|
|
|
read_condition_.SleepCS(&critical_section_);
|
2016-01-19 02:59:56 -08:00
|
|
|
}
|
|
|
|
|
--readers_waiting_;
|
|
|
|
|
}
|
|
|
|
|
++readers_active_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RWLockWinXP::ReleaseLockShared() {
|
2016-01-20 13:36:31 +01:00
|
|
|
ScopedLock cs(&critical_section_);
|
2016-01-19 02:59:56 -08:00
|
|
|
--readers_active_;
|
|
|
|
|
if (readers_active_ == 0 && writers_waiting_ > 0) {
|
|
|
|
|
write_condition_.Wake();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace webrtc
|