Delete unused class SharedExclusiveLock.
Unused since cl https://codereview.webrtc.org/2828223002/ BUG=webrtc:7501 Review-Url: https://codereview.webrtc.org/2865083002 Cr-Commit-Position: refs/heads/master@{#18050}
This commit is contained in:
parent
e61103dc6c
commit
3f580e24cc
@ -473,8 +473,6 @@ rtc_static_library("rtc_base") {
|
||||
"sha1.h",
|
||||
"sha1digest.cc",
|
||||
"sha1digest.h",
|
||||
"sharedexclusivelock.cc",
|
||||
"sharedexclusivelock.h",
|
||||
"signalthread.cc",
|
||||
"signalthread.h",
|
||||
"sigslot.cc",
|
||||
@ -935,7 +933,6 @@ if (rtc_include_tests) {
|
||||
"rtccertificate_unittest.cc",
|
||||
"rtccertificategenerator_unittest.cc",
|
||||
"sha1digest_unittest.cc",
|
||||
"sharedexclusivelock_unittest.cc",
|
||||
"signalthread_unittest.cc",
|
||||
"sigslot_unittest.cc",
|
||||
"sigslottester_unittest.cc",
|
||||
|
||||
@ -1,44 +0,0 @@
|
||||
/*
|
||||
* Copyright 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/base/sharedexclusivelock.h"
|
||||
|
||||
namespace rtc {
|
||||
|
||||
SharedExclusiveLock::SharedExclusiveLock()
|
||||
: shared_count_is_zero_(true, true),
|
||||
shared_count_(0) {
|
||||
}
|
||||
|
||||
void SharedExclusiveLock::LockExclusive() {
|
||||
cs_exclusive_.Enter();
|
||||
shared_count_is_zero_.Wait(Event::kForever);
|
||||
}
|
||||
|
||||
void SharedExclusiveLock::UnlockExclusive() {
|
||||
cs_exclusive_.Leave();
|
||||
}
|
||||
|
||||
void SharedExclusiveLock::LockShared() {
|
||||
CritScope exclusive_scope(&cs_exclusive_);
|
||||
CritScope shared_scope(&cs_shared_);
|
||||
if (++shared_count_ == 1) {
|
||||
shared_count_is_zero_.Reset();
|
||||
}
|
||||
}
|
||||
|
||||
void SharedExclusiveLock::UnlockShared() {
|
||||
CritScope shared_scope(&cs_shared_);
|
||||
if (--shared_count_ == 0) {
|
||||
shared_count_is_zero_.Set();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace rtc
|
||||
@ -1,75 +0,0 @@
|
||||
/*
|
||||
* Copyright 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_BASE_SHAREDEXCLUSIVELOCK_H_
|
||||
#define WEBRTC_BASE_SHAREDEXCLUSIVELOCK_H_
|
||||
|
||||
#include "webrtc/base/constructormagic.h"
|
||||
#include "webrtc/base/criticalsection.h"
|
||||
#include "webrtc/base/event.h"
|
||||
|
||||
namespace rtc {
|
||||
|
||||
// This class provides shared-exclusive lock. It can be used in cases like
|
||||
// multiple-readers/single-writer model.
|
||||
class LOCKABLE SharedExclusiveLock {
|
||||
public:
|
||||
SharedExclusiveLock();
|
||||
|
||||
// Locking/unlocking methods. It is encouraged to use SharedScope or
|
||||
// ExclusiveScope for protection.
|
||||
void LockExclusive() EXCLUSIVE_LOCK_FUNCTION();
|
||||
void UnlockExclusive() UNLOCK_FUNCTION();
|
||||
void LockShared();
|
||||
void UnlockShared();
|
||||
|
||||
private:
|
||||
rtc::CriticalSection cs_exclusive_;
|
||||
rtc::CriticalSection cs_shared_;
|
||||
rtc::Event shared_count_is_zero_;
|
||||
int shared_count_;
|
||||
|
||||
RTC_DISALLOW_COPY_AND_ASSIGN(SharedExclusiveLock);
|
||||
};
|
||||
|
||||
class SCOPED_LOCKABLE SharedScope {
|
||||
public:
|
||||
explicit SharedScope(SharedExclusiveLock* lock) SHARED_LOCK_FUNCTION(lock)
|
||||
: lock_(lock) {
|
||||
lock_->LockShared();
|
||||
}
|
||||
|
||||
~SharedScope() UNLOCK_FUNCTION() { lock_->UnlockShared(); }
|
||||
|
||||
private:
|
||||
SharedExclusiveLock* lock_;
|
||||
|
||||
RTC_DISALLOW_COPY_AND_ASSIGN(SharedScope);
|
||||
};
|
||||
|
||||
class SCOPED_LOCKABLE ExclusiveScope {
|
||||
public:
|
||||
explicit ExclusiveScope(SharedExclusiveLock* lock)
|
||||
EXCLUSIVE_LOCK_FUNCTION(lock)
|
||||
: lock_(lock) {
|
||||
lock_->LockExclusive();
|
||||
}
|
||||
|
||||
~ExclusiveScope() UNLOCK_FUNCTION() { lock_->UnlockExclusive(); }
|
||||
|
||||
private:
|
||||
SharedExclusiveLock* lock_;
|
||||
|
||||
RTC_DISALLOW_COPY_AND_ASSIGN(ExclusiveScope);
|
||||
};
|
||||
|
||||
} // namespace rtc
|
||||
|
||||
#endif // WEBRTC_BASE_SHAREDEXCLUSIVELOCK_H_
|
||||
@ -1,197 +0,0 @@
|
||||
/*
|
||||
* Copyright 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 <memory>
|
||||
|
||||
#include "webrtc/base/checks.h"
|
||||
#include "webrtc/base/gunit.h"
|
||||
#include "webrtc/base/event.h"
|
||||
#include "webrtc/base/messagehandler.h"
|
||||
#include "webrtc/base/messagequeue.h"
|
||||
#include "webrtc/base/sharedexclusivelock.h"
|
||||
#include "webrtc/base/thread.h"
|
||||
#include "webrtc/base/timeutils.h"
|
||||
|
||||
namespace rtc {
|
||||
|
||||
static const uint32_t kMsgRead = 0;
|
||||
static const uint32_t kMsgWrite = 0;
|
||||
static const int kNoWaitThresholdInMs = 10;
|
||||
static const int kWaitThresholdInMs = 80;
|
||||
static const int kProcessTimeInMs = 100;
|
||||
static const int kProcessTimeoutInMs = 5000;
|
||||
|
||||
class SharedExclusiveTask : public MessageHandler {
|
||||
public:
|
||||
SharedExclusiveTask(SharedExclusiveLock* shared_exclusive_lock,
|
||||
int* value,
|
||||
Event* done)
|
||||
: shared_exclusive_lock_(shared_exclusive_lock),
|
||||
value_(value),
|
||||
done_(done) {
|
||||
worker_thread_.reset(new Thread());
|
||||
worker_thread_->Start();
|
||||
}
|
||||
|
||||
protected:
|
||||
std::unique_ptr<Thread> worker_thread_;
|
||||
SharedExclusiveLock* shared_exclusive_lock_;
|
||||
int* value_;
|
||||
Event* done_;
|
||||
};
|
||||
|
||||
class ReadTask : public SharedExclusiveTask {
|
||||
public:
|
||||
ReadTask(SharedExclusiveLock* shared_exclusive_lock, int* value, Event* done)
|
||||
: SharedExclusiveTask(shared_exclusive_lock, value, done) {
|
||||
}
|
||||
|
||||
void PostRead(int* value) {
|
||||
worker_thread_->Post(RTC_FROM_HERE, this, kMsgRead,
|
||||
new TypedMessageData<int*>(value));
|
||||
}
|
||||
|
||||
private:
|
||||
virtual void OnMessage(Message* message) {
|
||||
RTC_CHECK(rtc::Thread::Current() == worker_thread_.get());
|
||||
RTC_CHECK(message != nullptr);
|
||||
RTC_CHECK(message->message_id == kMsgRead);
|
||||
|
||||
TypedMessageData<int*>* message_data =
|
||||
static_cast<TypedMessageData<int*>*>(message->pdata);
|
||||
|
||||
{
|
||||
SharedScope ss(shared_exclusive_lock_);
|
||||
*message_data->data() = *value_;
|
||||
done_->Set();
|
||||
}
|
||||
delete message->pdata;
|
||||
message->pdata = nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
class WriteTask : public SharedExclusiveTask {
|
||||
public:
|
||||
WriteTask(SharedExclusiveLock* shared_exclusive_lock, int* value, Event* done)
|
||||
: SharedExclusiveTask(shared_exclusive_lock, value, done) {
|
||||
}
|
||||
|
||||
void PostWrite(int value) {
|
||||
worker_thread_->Post(RTC_FROM_HERE, this, kMsgWrite,
|
||||
new TypedMessageData<int>(value));
|
||||
}
|
||||
|
||||
private:
|
||||
virtual void OnMessage(Message* message) {
|
||||
RTC_CHECK(rtc::Thread::Current() == worker_thread_.get());
|
||||
RTC_CHECK(message != nullptr);
|
||||
RTC_CHECK(message->message_id == kMsgWrite);
|
||||
|
||||
TypedMessageData<int>* message_data =
|
||||
static_cast<TypedMessageData<int>*>(message->pdata);
|
||||
|
||||
{
|
||||
ExclusiveScope es(shared_exclusive_lock_);
|
||||
*value_ = message_data->data();
|
||||
done_->Set();
|
||||
}
|
||||
delete message->pdata;
|
||||
message->pdata = nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
// Unit test for SharedExclusiveLock.
|
||||
class SharedExclusiveLockTest
|
||||
: public testing::Test {
|
||||
public:
|
||||
SharedExclusiveLockTest() : value_(0) {
|
||||
}
|
||||
|
||||
virtual void SetUp() {
|
||||
shared_exclusive_lock_.reset(new SharedExclusiveLock());
|
||||
}
|
||||
|
||||
protected:
|
||||
std::unique_ptr<SharedExclusiveLock> shared_exclusive_lock_;
|
||||
int value_;
|
||||
};
|
||||
|
||||
TEST_F(SharedExclusiveLockTest, TestSharedShared) {
|
||||
int value0, value1;
|
||||
Event done0(false, false), done1(false, false);
|
||||
ReadTask reader0(shared_exclusive_lock_.get(), &value_, &done0);
|
||||
ReadTask reader1(shared_exclusive_lock_.get(), &value_, &done1);
|
||||
|
||||
// Test shared locks can be shared without waiting.
|
||||
{
|
||||
SharedScope ss(shared_exclusive_lock_.get());
|
||||
value_ = 1;
|
||||
reader0.PostRead(&value0);
|
||||
reader1.PostRead(&value1);
|
||||
|
||||
EXPECT_TRUE(done0.Wait(kProcessTimeoutInMs));
|
||||
EXPECT_TRUE(done1.Wait(kProcessTimeoutInMs));
|
||||
EXPECT_EQ(1, value0);
|
||||
EXPECT_EQ(1, value1);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(SharedExclusiveLockTest, TestSharedExclusive) {
|
||||
Event done(false, false);
|
||||
WriteTask writer(shared_exclusive_lock_.get(), &value_, &done);
|
||||
|
||||
// Test exclusive lock needs to wait for shared lock.
|
||||
{
|
||||
SharedScope ss(shared_exclusive_lock_.get());
|
||||
value_ = 1;
|
||||
writer.PostWrite(2);
|
||||
EXPECT_FALSE(done.Wait(kProcessTimeInMs));
|
||||
}
|
||||
EXPECT_TRUE(done.Wait(kProcessTimeoutInMs));
|
||||
EXPECT_EQ(2, value_);
|
||||
}
|
||||
|
||||
TEST_F(SharedExclusiveLockTest, TestExclusiveShared) {
|
||||
int value;
|
||||
Event done(false, false);
|
||||
ReadTask reader(shared_exclusive_lock_.get(), &value_, &done);
|
||||
|
||||
// Test shared lock needs to wait for exclusive lock.
|
||||
{
|
||||
ExclusiveScope es(shared_exclusive_lock_.get());
|
||||
value_ = 1;
|
||||
reader.PostRead(&value);
|
||||
EXPECT_FALSE(done.Wait(kProcessTimeInMs));
|
||||
value_ = 2;
|
||||
}
|
||||
|
||||
EXPECT_TRUE(done.Wait(kProcessTimeoutInMs));
|
||||
EXPECT_EQ(2, value);
|
||||
}
|
||||
|
||||
TEST_F(SharedExclusiveLockTest, TestExclusiveExclusive) {
|
||||
Event done(false, false);
|
||||
WriteTask writer(shared_exclusive_lock_.get(), &value_, &done);
|
||||
|
||||
// Test exclusive lock needs to wait for exclusive lock.
|
||||
{
|
||||
ExclusiveScope es(shared_exclusive_lock_.get());
|
||||
// Start the writer task only after holding the lock, to ensure it need
|
||||
value_ = 1;
|
||||
writer.PostWrite(2);
|
||||
EXPECT_FALSE(done.Wait(kProcessTimeInMs));
|
||||
EXPECT_EQ(1, value_);
|
||||
}
|
||||
|
||||
EXPECT_TRUE(done.Wait(kProcessTimeoutInMs));
|
||||
EXPECT_EQ(2, value_);
|
||||
}
|
||||
|
||||
} // namespace rtc
|
||||
Loading…
x
Reference in New Issue
Block a user