From 61046eb38d188c759478c84fb536ec8dd941e146 Mon Sep 17 00:00:00 2001 From: tommi Date: Tue, 19 Jan 2016 02:59:56 -0800 Subject: [PATCH] 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} --- webrtc/system_wrappers/BUILD.gn | 4 +- webrtc/system_wrappers/source/rw_lock.cc | 4 +- .../system_wrappers/source/rw_lock_generic.cc | 77 ------------------- .../source/rw_lock_winxp_win.cc | 61 +++++++++++++++ ...{rw_lock_generic.h => rw_lock_winxp_win.h} | 31 ++++---- webrtc/system_wrappers/system_wrappers.gyp | 4 +- 6 files changed, 82 insertions(+), 99 deletions(-) delete mode 100644 webrtc/system_wrappers/source/rw_lock_generic.cc create mode 100644 webrtc/system_wrappers/source/rw_lock_winxp_win.cc rename webrtc/system_wrappers/source/{rw_lock_generic.h => rw_lock_winxp_win.h} (52%) diff --git a/webrtc/system_wrappers/BUILD.gn b/webrtc/system_wrappers/BUILD.gn index 5e0e41e832..a73a32fa10 100644 --- a/webrtc/system_wrappers/BUILD.gn +++ b/webrtc/system_wrappers/BUILD.gn @@ -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", diff --git a/webrtc/system_wrappers/source/rw_lock.cc b/webrtc/system_wrappers/source/rw_lock.cc index ff44896dbe..7c77123a0b 100644 --- a/webrtc/system_wrappers/source/rw_lock.cc +++ b/webrtc/system_wrappers/source/rw_lock.cc @@ -13,8 +13,8 @@ #include #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 diff --git a/webrtc/system_wrappers/source/rw_lock_generic.cc b/webrtc/system_wrappers/source/rw_lock_generic.cc deleted file mode 100644 index 9786155a63..0000000000 --- a/webrtc/system_wrappers/source/rw_lock_generic.cc +++ /dev/null @@ -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 diff --git a/webrtc/system_wrappers/source/rw_lock_winxp_win.cc b/webrtc/system_wrappers/source/rw_lock_winxp_win.cc new file mode 100644 index 0000000000..0fb57d9b9f --- /dev/null +++ b/webrtc/system_wrappers/source/rw_lock_winxp_win.cc @@ -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 diff --git a/webrtc/system_wrappers/source/rw_lock_generic.h b/webrtc/system_wrappers/source/rw_lock_winxp_win.h similarity index 52% rename from webrtc/system_wrappers/source/rw_lock_generic.h rename to webrtc/system_wrappers/source/rw_lock_winxp_win.h index f0d445692e..20e538250a 100644 --- a/webrtc/system_wrappers/source/rw_lock_generic.h +++ b/webrtc/system_wrappers/source/rw_lock_winxp_win.h @@ -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_ diff --git a/webrtc/system_wrappers/system_wrappers.gyp b/webrtc/system_wrappers/system_wrappers.gyp index 507fc45311..f294c9e326 100644 --- a/webrtc/system_wrappers/system_wrappers.gyp +++ b/webrtc/system_wrappers/system_wrappers.gyp @@ -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',