Remove ConditionVariableWrapper.

ConditionVariableEventWin remains for now since it's still needed for the rw lock on Windows XP.

BUG=
R=pbos@webrtc.org

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

Cr-Commit-Position: refs/heads/master@{#11317}
This commit is contained in:
Tommi 2016-01-20 13:36:31 +01:00
parent 63cb434691
commit e8493326f2
12 changed files with 67 additions and 306 deletions

View File

@ -15,7 +15,6 @@ static_library("system_wrappers") {
"include/aligned_malloc.h",
"include/atomic32.h",
"include/clock.h",
"include/condition_variable_wrapper.h",
"include/cpu_features_wrapper.h",
"include/cpu_info.h",
"include/critical_section_wrapper.h",
@ -45,11 +44,8 @@ static_library("system_wrappers") {
"source/atomic32_mac.cc",
"source/atomic32_win.cc",
"source/clock.cc",
"source/condition_variable.cc",
"source/condition_variable_event_win.cc",
"source/condition_variable_event_win.h",
"source/condition_variable_native_win.cc",
"source/condition_variable_native_win.h",
"source/cpu_features.cc",
"source/cpu_info.cc",
"source/critical_section.cc",

View File

@ -1,49 +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.
*/
#ifndef WEBRTC_SYSTEM_WRAPPERS_INCLUDE_CONDITION_VARIABLE_WRAPPER_H_
#define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_CONDITION_VARIABLE_WRAPPER_H_
// TODO(tommi): Remove completely. As is there is still some code for Windows
// that relies on ConditionVariableWrapper, but code has been removed on other
// platforms.
#if defined(WEBRTC_WIN)
namespace webrtc {
class CriticalSectionWrapper;
class ConditionVariableWrapper {
public:
// Factory method, constructor disabled.
static ConditionVariableWrapper* CreateConditionVariable();
virtual ~ConditionVariableWrapper() {}
// Calling thread will atomically release crit_sect and wait until next
// some other thread calls Wake() or WakeAll().
virtual void SleepCS(CriticalSectionWrapper& crit_sect) = 0;
// Same as above but with a timeout.
virtual bool SleepCS(CriticalSectionWrapper& crit_sect,
unsigned long max_time_in_ms) = 0;
// Wakes one thread calling SleepCS().
virtual void Wake() = 0;
// Wakes all threads calling SleepCS().
virtual void WakeAll() = 0;
};
} // namespace webrtc
#endif // defined(WEBRTC_WIN)
#endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_CONDITION_VARIABLE_WRAPPER_H_

View File

@ -46,10 +46,6 @@ class LOCKABLE CriticalSectionWrapper {
private:
#if defined (WEBRTC_WIN)
CRITICAL_SECTION crit_;
// TODO(tommi): Remove friendness.
friend class ConditionVariableEventWin;
friend class ConditionVariableNativeWin;
#else
pthread_mutex_t mutex_;
#endif

View File

@ -1,35 +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/include/condition_variable_wrapper.h"
// TODO(tommi): Remove completely. As is there is still some code for Windows
// that relies on ConditionVariableWrapper, but code has been removed on other
// platforms.
#if defined(WEBRTC_WIN)
#include <windows.h>
#include "webrtc/system_wrappers/source/condition_variable_event_win.h"
#include "webrtc/system_wrappers/source/condition_variable_native_win.h"
namespace webrtc {
ConditionVariableWrapper* ConditionVariableWrapper::CreateConditionVariable() {
// Try to create native condition variable implementation.
ConditionVariableWrapper* ret_val = ConditionVariableNativeWin::Create();
if (!ret_val) {
// Native condition variable implementation does not exist. Create generic
// condition variable based on events.
ret_val = new ConditionVariableEventWin();
}
return ret_val;
}
} // namespace webrtc
#endif // defined(WEBRTC_WIN)

View File

@ -118,11 +118,11 @@ ConditionVariableEventWin::~ConditionVariableEventWin() {
DeleteCriticalSection(&num_waiters_crit_sect_);
}
void ConditionVariableEventWin::SleepCS(CriticalSectionWrapper& crit_sect) {
void ConditionVariableEventWin::SleepCS(CRITICAL_SECTION* crit_sect) {
SleepCS(crit_sect, INFINITE);
}
bool ConditionVariableEventWin::SleepCS(CriticalSectionWrapper& crit_sect,
bool ConditionVariableEventWin::SleepCS(CRITICAL_SECTION* crit_sect,
unsigned long max_time_in_ms) {
EnterCriticalSection(&num_waiters_crit_sect_);
@ -134,7 +134,7 @@ bool ConditionVariableEventWin::SleepCS(CriticalSectionWrapper& crit_sect,
++(num_waiters_[eventID]);
LeaveCriticalSection(&num_waiters_crit_sect_);
LeaveCriticalSection(&crit_sect.crit_);
LeaveCriticalSection(crit_sect);
HANDLE events[2];
events[0] = events_[WAKE];
events[1] = events_[eventID];
@ -160,7 +160,7 @@ bool ConditionVariableEventWin::SleepCS(CriticalSectionWrapper& crit_sect,
ResetEvent(events_[eventID]);
}
EnterCriticalSection(&crit_sect.crit_);
EnterCriticalSection(crit_sect);
return ret_val;
}

View File

@ -13,17 +13,15 @@
#include <windows.h>
#include "webrtc/system_wrappers/include/condition_variable_wrapper.h"
namespace webrtc {
class ConditionVariableEventWin : public ConditionVariableWrapper {
class ConditionVariableEventWin {
public:
ConditionVariableEventWin();
virtual ~ConditionVariableEventWin();
~ConditionVariableEventWin();
void SleepCS(CriticalSectionWrapper& crit_sect);
bool SleepCS(CriticalSectionWrapper& crit_sect, unsigned long max_time_inMS);
void SleepCS(CRITICAL_SECTION* crit_sect);
bool SleepCS(CRITICAL_SECTION* crit_sect, unsigned long max_time_inMS);
void Wake();
void WakeAll();

View File

@ -1,102 +0,0 @@
/*
* Copyright (c) 2013 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/include/critical_section_wrapper.h"
#include "webrtc/system_wrappers/include/trace.h"
#include "webrtc/system_wrappers/source/condition_variable_native_win.h"
namespace webrtc {
static HMODULE library = NULL;
static bool win_support_condition_variables_primitive = false;
PInitializeConditionVariable PInitializeConditionVariable_;
PSleepConditionVariableCS PSleepConditionVariableCS_;
PWakeConditionVariable PWakeConditionVariable_;
PWakeAllConditionVariable PWakeAllConditionVariable_;
typedef void (WINAPI *PInitializeConditionVariable)(PCONDITION_VARIABLE);
typedef BOOL (WINAPI *PSleepConditionVariableCS)(PCONDITION_VARIABLE,
PCRITICAL_SECTION, DWORD);
typedef void (WINAPI *PWakeConditionVariable)(PCONDITION_VARIABLE);
typedef void (WINAPI *PWakeAllConditionVariable)(PCONDITION_VARIABLE);
ConditionVariableNativeWin::ConditionVariableNativeWin() {
}
ConditionVariableNativeWin::~ConditionVariableNativeWin() {
}
ConditionVariableWrapper* ConditionVariableNativeWin::Create() {
ConditionVariableNativeWin* ret_val = new ConditionVariableNativeWin();
if (!ret_val->Init()) {
delete ret_val;
return NULL;
}
return ret_val;
}
bool ConditionVariableNativeWin::Init() {
if (!library) {
// Native implementation is supported on Vista+.
library = LoadLibrary(TEXT("Kernel32.dll"));
// TODO(henrike): this code results in an attempt to load the above dll
// every time the previous attempt failed. Only try to load once.
if (library) {
// TODO(henrike): not thread safe as reading and writing to library is not
// serialized. Fix.
WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1, "Loaded Kernel.dll");
PInitializeConditionVariable_ =
(PInitializeConditionVariable) GetProcAddress(
library, "InitializeConditionVariable");
PSleepConditionVariableCS_ = (PSleepConditionVariableCS) GetProcAddress(
library, "SleepConditionVariableCS");
PWakeConditionVariable_ = (PWakeConditionVariable) GetProcAddress(
library, "WakeConditionVariable");
PWakeAllConditionVariable_ = (PWakeAllConditionVariable) GetProcAddress(
library, "WakeAllConditionVariable");
if (PInitializeConditionVariable_ && PSleepConditionVariableCS_
&& PWakeConditionVariable_ && PWakeAllConditionVariable_) {
WEBRTC_TRACE(
kTraceStateInfo, kTraceUtility, -1,
"Loaded native condition variables");
win_support_condition_variables_primitive = true;
}
}
}
if (!win_support_condition_variables_primitive) {
return false;
}
PInitializeConditionVariable_(&condition_variable_);
return true;
}
void ConditionVariableNativeWin::SleepCS(CriticalSectionWrapper& crit_sect) {
SleepCS(crit_sect, INFINITE);
}
bool ConditionVariableNativeWin::SleepCS(CriticalSectionWrapper& crit_sect,
unsigned long max_time_in_ms) {
BOOL ret_val = PSleepConditionVariableCS_(&condition_variable_,
&crit_sect.crit_, max_time_in_ms);
return ret_val != 0;
}
void ConditionVariableNativeWin::Wake() {
PWakeConditionVariable_(&condition_variable_);
}
void ConditionVariableNativeWin::WakeAll() {
PWakeAllConditionVariable_(&condition_variable_);
}
} // namespace webrtc

View File

@ -1,54 +0,0 @@
/*
* Copyright (c) 2013 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.
*/
#ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_CONDITION_VARIABLE_NATIVE_WIN_H_
#define WEBRTC_SYSTEM_WRAPPERS_SOURCE_CONDITION_VARIABLE_NATIVE_WIN_H_
#include <windows.h>
#include "webrtc/system_wrappers/include/condition_variable_wrapper.h"
namespace webrtc {
#if !defined CONDITION_VARIABLE_INIT
typedef struct RTL_CONDITION_VARIABLE_ {
void* Ptr;
} RTL_CONDITION_VARIABLE, *PRTL_CONDITION_VARIABLE;
typedef RTL_CONDITION_VARIABLE CONDITION_VARIABLE, *PCONDITION_VARIABLE;
#endif
typedef void (WINAPI* PInitializeConditionVariable)(PCONDITION_VARIABLE);
typedef BOOL (WINAPI* PSleepConditionVariableCS)(PCONDITION_VARIABLE,
PCRITICAL_SECTION, DWORD);
typedef void (WINAPI* PWakeConditionVariable)(PCONDITION_VARIABLE);
typedef void (WINAPI* PWakeAllConditionVariable)(PCONDITION_VARIABLE);
class ConditionVariableNativeWin : public ConditionVariableWrapper {
public:
static ConditionVariableWrapper* Create();
virtual ~ConditionVariableNativeWin();
void SleepCS(CriticalSectionWrapper& crit_sect);
bool SleepCS(CriticalSectionWrapper& crit_sect, unsigned long max_time_inMS);
void Wake();
void WakeAll();
private:
ConditionVariableNativeWin();
bool Init();
CONDITION_VARIABLE condition_variable_;
};
} // namespace webrtc
#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_CONDITION_VARIABLE_NATIVE_WIN_H_

View File

@ -8,13 +8,13 @@
* be found in the AUTHORS file in the root of the source tree.
*/
#include "webrtc/system_wrappers/include/condition_variable_wrapper.h"
// TODO(tommi): Remove completely. As is there is still some code for Windows
// that relies on ConditionVariableWrapper, but code has been removed on other
// that relies on ConditionVariableEventWin, but code has been removed on other
// platforms.
#if defined(WEBRTC_WIN)
#include "webrtc/system_wrappers/source/condition_variable_event_win.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "webrtc/base/platform_thread.h"
#include "webrtc/base/scoped_ptr.h"
@ -40,37 +40,36 @@ const int kVeryShortWaitMs = 20; // Used when we want a timeout
class Baton {
public:
Baton()
: giver_sect_(CriticalSectionWrapper::CreateCriticalSection()),
crit_sect_(CriticalSectionWrapper::CreateCriticalSection()),
cond_var_(ConditionVariableWrapper::CreateConditionVariable()),
being_passed_(false),
: being_passed_(false),
pass_count_(0) {
InitializeCriticalSection(&crit_sect_);
}
~Baton() {
delete giver_sect_;
delete crit_sect_;
delete cond_var_;
DeleteCriticalSection(&crit_sect_);
}
// Pass the baton. Returns false if baton is not picked up in |max_msecs|.
// Only one process can pass at the same time; this property is
// ensured by the |giver_sect_| lock.
bool Pass(uint32_t max_msecs) {
CriticalSectionScoped cs_giver(giver_sect_);
CriticalSectionScoped cs(crit_sect_);
CriticalSectionScoped cs_giver(&giver_sect_);
EnterCriticalSection(&crit_sect_);
SignalBatonAvailable();
const bool result = TakeBatonIfStillFree(max_msecs);
if (result) {
++pass_count_;
}
LeaveCriticalSection(&crit_sect_);
return result;
}
// Grab the baton. Returns false if baton is not passed.
bool Grab(uint32_t max_msecs) {
CriticalSectionScoped cs(crit_sect_);
return WaitUntilBatonOffered(max_msecs);
EnterCriticalSection(&crit_sect_);
bool ret = WaitUntilBatonOffered(max_msecs);
LeaveCriticalSection(&crit_sect_);
return ret;
}
int PassCount() {
@ -79,7 +78,7 @@ class Baton {
// finishes. I.e. the Grab()-call may finish before |pass_count_| has been
// incremented.
// Thus, this function waits on giver_sect_.
CriticalSectionScoped cs(giver_sect_);
CriticalSectionScoped cs(&giver_sect_);
return pass_count_;
}
@ -88,19 +87,19 @@ class Baton {
// These functions must be called with crit_sect_ held.
bool WaitUntilBatonOffered(int timeout_ms) {
while (!being_passed_) {
if (!cond_var_->SleepCS(*crit_sect_, timeout_ms)) {
if (!cond_var_.SleepCS(&crit_sect_, timeout_ms)) {
return false;
}
}
being_passed_ = false;
cond_var_->Wake();
cond_var_.Wake();
return true;
}
void SignalBatonAvailable() {
assert(!being_passed_);
being_passed_ = true;
cond_var_->Wake();
cond_var_.Wake();
}
// Timeout extension: Wait for a limited time for someone else to
@ -112,27 +111,25 @@ class Baton {
bool TakeBatonIfStillFree(int timeout_ms) {
bool not_timeout = true;
while (being_passed_ && not_timeout) {
not_timeout = cond_var_->SleepCS(*crit_sect_, timeout_ms);
not_timeout = cond_var_.SleepCS(&crit_sect_, timeout_ms);
// If we're woken up while variable is still held, we may have
// gotten a wakeup destined for a grabber thread.
// This situation is not treated specially here.
}
if (!being_passed_) {
if (!being_passed_)
return true;
} else {
assert(!not_timeout);
being_passed_ = false;
return false;
}
assert(!not_timeout);
being_passed_ = false;
return false;
}
// Lock that ensures that there is only one thread in the active
// part of Pass() at a time.
// |giver_sect_| must always be acquired before |cond_var_|.
CriticalSectionWrapper* giver_sect_;
CriticalSectionWrapper giver_sect_;
// Lock that protects |being_passed_|.
CriticalSectionWrapper* crit_sect_;
ConditionVariableWrapper* cond_var_;
CRITICAL_SECTION crit_sect_;
ConditionVariableEventWin cond_var_;
bool being_passed_;
// Statistics information: Number of successfull passes.
int pass_count_;
@ -191,16 +188,17 @@ TEST_F(CondVarTest, DISABLED_PassBatonMultipleTimes) {
}
TEST(CondVarWaitTest, WaitingWaits) {
rtc::scoped_ptr<CriticalSectionWrapper> crit_sect(
CriticalSectionWrapper::CreateCriticalSection());
rtc::scoped_ptr<ConditionVariableWrapper> cond_var(
ConditionVariableWrapper::CreateConditionVariable());
CriticalSectionScoped cs(crit_sect.get());
CRITICAL_SECTION crit_sect;
InitializeCriticalSection(&crit_sect);
ConditionVariableEventWin cond_var;
EnterCriticalSection(&crit_sect);
int64_t start_ms = TickTime::MillisecondTimestamp();
EXPECT_FALSE(cond_var->SleepCS(*(crit_sect), kVeryShortWaitMs));
EXPECT_FALSE(cond_var.SleepCS(&crit_sect, kVeryShortWaitMs));
int64_t end_ms = TickTime::MillisecondTimestamp();
EXPECT_LE(start_ms + kVeryShortWaitMs, end_ms)
<< "actual elapsed:" << end_ms - start_ms;
LeaveCriticalSection(&crit_sect);
DeleteCriticalSection(&crit_sect);
}
} // anonymous namespace

View File

@ -11,16 +11,34 @@
#include "webrtc/system_wrappers/source/rw_lock_winxp_win.h"
namespace webrtc {
namespace {
class ScopedLock {
public:
ScopedLock(CRITICAL_SECTION* lock) : lock_(lock) {
EnterCriticalSection(lock_);
}
~ScopedLock() {
LeaveCriticalSection(lock_);
}
private:
CRITICAL_SECTION* const lock_;
};
}
RWLockWinXP::RWLockWinXP() {}
RWLockWinXP::~RWLockWinXP() {}
RWLockWinXP::RWLockWinXP() {
InitializeCriticalSection(&critical_section_);
}
RWLockWinXP::~RWLockWinXP() {
DeleteCriticalSection(&critical_section_);
}
void RWLockWinXP::AcquireLockExclusive() {
CriticalSectionScoped cs(&critical_section_);
ScopedLock cs(&critical_section_);
if (writer_active_ || readers_active_ > 0) {
++writers_waiting_;
while (writer_active_ || readers_active_ > 0) {
write_condition_.SleepCS(critical_section_);
write_condition_.SleepCS(&critical_section_);
}
--writers_waiting_;
}
@ -28,7 +46,7 @@ void RWLockWinXP::AcquireLockExclusive() {
}
void RWLockWinXP::ReleaseLockExclusive() {
CriticalSectionScoped cs(&critical_section_);
ScopedLock cs(&critical_section_);
writer_active_ = false;
if (writers_waiting_ > 0) {
write_condition_.Wake();
@ -38,12 +56,12 @@ void RWLockWinXP::ReleaseLockExclusive() {
}
void RWLockWinXP::AcquireLockShared() {
CriticalSectionScoped cs(&critical_section_);
ScopedLock cs(&critical_section_);
if (writer_active_ || writers_waiting_ > 0) {
++readers_waiting_;
while (writer_active_ || writers_waiting_ > 0) {
read_condition_.SleepCS(critical_section_);
read_condition_.SleepCS(&critical_section_);
}
--readers_waiting_;
}
@ -51,7 +69,7 @@ void RWLockWinXP::AcquireLockShared() {
}
void RWLockWinXP::ReleaseLockShared() {
CriticalSectionScoped cs(&critical_section_);
ScopedLock cs(&critical_section_);
--readers_active_;
if (readers_active_ == 0 && writers_waiting_ > 0) {
write_condition_.Wake();

View File

@ -13,7 +13,6 @@
#include "webrtc/system_wrappers/include/rw_lock_wrapper.h"
#include "webrtc/system_wrappers/source/condition_variable_event_win.h"
#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
#include "webrtc/typedefs.h"
namespace webrtc {
@ -30,7 +29,7 @@ class RWLockWinXP : public RWLockWrapper {
void ReleaseLockShared() override;
private:
CriticalSectionWrapper critical_section_;
CRITICAL_SECTION critical_section_;
ConditionVariableEventWin read_condition_;
ConditionVariableEventWin write_condition_;

View File

@ -21,7 +21,6 @@
'include/aligned_malloc.h',
'include/atomic32.h',
'include/clock.h',
'include/condition_variable_wrapper.h',
'include/cpu_info.h',
'include/cpu_features_wrapper.h',
'include/critical_section_wrapper.h',
@ -54,11 +53,8 @@
'source/atomic32_posix.cc',
'source/atomic32_win.cc',
'source/clock.cc',
'source/condition_variable.cc',
'source/condition_variable_event_win.cc',
'source/condition_variable_event_win.h',
'source/condition_variable_native_win.cc',
'source/condition_variable_native_win.h',
'source/cpu_info.cc',
'source/cpu_features.cc',
'source/critical_section.cc',