Rename RWLockGeneric to RWLockWinXP to more accurately reflect when it's used.

Since this is on Windows only, I'm also using the CriticalSectionWrapper and ConditionVariableWrapper Windows types directly which allows us to skip 3 extra heap allocations. It also helps with the removal of the 'friend' relationship between ConditionVariableWrapper and CriticalSectionWrapper, which is causing headaches on Mac.

BUG=

Review URL: https://codereview.webrtc.org/1595983002

Cr-Commit-Position: refs/heads/master@{#11300}
This commit is contained in:
tommi 2016-01-19 02:59:56 -08:00 committed by Commit bot
parent 3860c7f873
commit 61046eb38d
6 changed files with 82 additions and 99 deletions

View File

@ -70,12 +70,12 @@ static_library("system_wrappers") {
"source/logging.cc",
"source/rtp_to_ntp.cc",
"source/rw_lock.cc",
"source/rw_lock_generic.cc",
"source/rw_lock_generic.h",
"source/rw_lock_posix.cc",
"source/rw_lock_posix.h",
"source/rw_lock_win.cc",
"source/rw_lock_win.h",
"source/rw_lock_winxp_win.cc",
"source/rw_lock_winxp_win.h",
"source/sleep.cc",
"source/sort.cc",
"source/tick_util.cc",

View File

@ -13,8 +13,8 @@
#include <assert.h>
#if defined(_WIN32)
#include "webrtc/system_wrappers/source/rw_lock_generic.h"
#include "webrtc/system_wrappers/source/rw_lock_win.h"
#include "webrtc/system_wrappers/source/rw_lock_winxp_win.h"
#else
#include "webrtc/system_wrappers/source/rw_lock_posix.h"
#endif
@ -28,7 +28,7 @@ RWLockWrapper* RWLockWrapper::CreateRWLock() {
if (lock) {
return lock;
}
return new RWLockGeneric();
return new RWLockWinXP();
#else
return RWLockPosix::Create();
#endif

View File

@ -1,77 +0,0 @@
/*
* 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_generic.h"
#include "webrtc/system_wrappers/include/condition_variable_wrapper.h"
#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
namespace webrtc {
RWLockGeneric::RWLockGeneric()
: readers_active_(0),
writer_active_(false),
readers_waiting_(0),
writers_waiting_(0) {
critical_section_ = CriticalSectionWrapper::CreateCriticalSection();
read_condition_ = ConditionVariableWrapper::CreateConditionVariable();
write_condition_ = ConditionVariableWrapper::CreateConditionVariable();
}
RWLockGeneric::~RWLockGeneric() {
delete write_condition_;
delete read_condition_;
delete critical_section_;
}
void RWLockGeneric::AcquireLockExclusive() {
CriticalSectionScoped cs(critical_section_);
if (writer_active_ || readers_active_ > 0) {
++writers_waiting_;
while (writer_active_ || readers_active_ > 0) {
write_condition_->SleepCS(*critical_section_);
}
--writers_waiting_;
}
writer_active_ = true;
}
void RWLockGeneric::ReleaseLockExclusive() {
CriticalSectionScoped cs(critical_section_);
writer_active_ = false;
if (writers_waiting_ > 0) {
write_condition_->Wake();
} else if (readers_waiting_ > 0) {
read_condition_->WakeAll();
}
}
void RWLockGeneric::AcquireLockShared() {
CriticalSectionScoped cs(critical_section_);
if (writer_active_ || writers_waiting_ > 0) {
++readers_waiting_;
while (writer_active_ || writers_waiting_ > 0) {
read_condition_->SleepCS(*critical_section_);
}
--readers_waiting_;
}
++readers_active_;
}
void RWLockGeneric::ReleaseLockShared() {
CriticalSectionScoped cs(critical_section_);
--readers_active_;
if (readers_active_ == 0 && writers_waiting_ > 0) {
write_condition_->Wake();
}
}
} // namespace webrtc

View File

@ -0,0 +1,61 @@
/*
* 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 {
RWLockWinXP::RWLockWinXP() {}
RWLockWinXP::~RWLockWinXP() {}
void RWLockWinXP::AcquireLockExclusive() {
CriticalSectionScoped cs(&critical_section_);
if (writer_active_ || readers_active_ > 0) {
++writers_waiting_;
while (writer_active_ || readers_active_ > 0) {
write_condition_.SleepCS(critical_section_);
}
--writers_waiting_;
}
writer_active_ = true;
}
void RWLockWinXP::ReleaseLockExclusive() {
CriticalSectionScoped cs(&critical_section_);
writer_active_ = false;
if (writers_waiting_ > 0) {
write_condition_.Wake();
} else if (readers_waiting_ > 0) {
read_condition_.WakeAll();
}
}
void RWLockWinXP::AcquireLockShared() {
CriticalSectionScoped cs(&critical_section_);
if (writer_active_ || writers_waiting_ > 0) {
++readers_waiting_;
while (writer_active_ || writers_waiting_ > 0) {
read_condition_.SleepCS(critical_section_);
}
--readers_waiting_;
}
++readers_active_;
}
void RWLockWinXP::ReleaseLockShared() {
CriticalSectionScoped cs(&critical_section_);
--readers_active_;
if (readers_active_ == 0 && writers_waiting_ > 0) {
write_condition_.Wake();
}
}
} // namespace webrtc

View File

@ -8,21 +8,20 @@
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_RW_LOCK_GENERIC_H_
#define WEBRTC_SYSTEM_WRAPPERS_SOURCE_RW_LOCK_GENERIC_H_
#ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_RW_LOCK_WINXP_WIN_H_
#define WEBRTC_SYSTEM_WRAPPERS_SOURCE_RW_LOCK_WINXP_WIN_H_
#include "webrtc/system_wrappers/include/rw_lock_wrapper.h"
#include "webrtc/system_wrappers/source/condition_variable_event_win.h"
#include "webrtc/system_wrappers/source/critical_section_win.h"
#include "webrtc/typedefs.h"
namespace webrtc {
class CriticalSectionWrapper;
class ConditionVariableWrapper;
class RWLockGeneric : public RWLockWrapper {
class RWLockWinXP : public RWLockWrapper {
public:
RWLockGeneric();
~RWLockGeneric() override;
RWLockWinXP();
~RWLockWinXP() override;
void AcquireLockExclusive() override;
void ReleaseLockExclusive() override;
@ -31,16 +30,16 @@ class RWLockGeneric : public RWLockWrapper {
void ReleaseLockShared() override;
private:
CriticalSectionWrapper* critical_section_;
ConditionVariableWrapper* read_condition_;
ConditionVariableWrapper* write_condition_;
CriticalSectionWindows critical_section_;
ConditionVariableEventWin read_condition_;
ConditionVariableEventWin write_condition_;
int readers_active_;
bool writer_active_;
int readers_waiting_;
int writers_waiting_;
int readers_active_ = 0;
bool writer_active_ = false;
int readers_waiting_ = 0;
int writers_waiting_ = 0;
};
} // namespace webrtc
#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_RW_LOCK_GENERIC_H_
#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_RW_LOCK_WINXP_WIN_H_

View File

@ -82,12 +82,12 @@
'source/logging.cc',
'source/rtp_to_ntp.cc',
'source/rw_lock.cc',
'source/rw_lock_generic.cc',
'source/rw_lock_generic.h',
'source/rw_lock_posix.cc',
'source/rw_lock_posix.h',
'source/rw_lock_win.cc',
'source/rw_lock_win.h',
'source/rw_lock_winxp_win.cc',
'source/rw_lock_winxp_win.h',
'source/sleep.cc',
'source/sort.cc',
'source/tick_util.cc',