blob: 0ca9518747f5ae7d95a8b4e8110800a3a0c8cd19 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
phoglund@webrtc.org59ad5412012-12-18 15:20:35 +000011#include "webrtc/system_wrappers/source/rw_lock_generic.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
phoglund@webrtc.org59ad5412012-12-18 15:20:35 +000013#include "webrtc/system_wrappers/interface/condition_variable_wrapper.h"
14#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000015
16namespace webrtc {
henrike@webrtc.org9f847232012-09-25 20:27:51 +000017
18RWLockGeneric::RWLockGeneric()
19 : readers_active_(0),
20 writer_active_(false),
21 readers_waiting_(0),
22 writers_waiting_(0) {
23 critical_section_ = CriticalSectionWrapper::CreateCriticalSection();
24 read_condition_ = ConditionVariableWrapper::CreateConditionVariable();
25 write_condition_ = ConditionVariableWrapper::CreateConditionVariable();
niklase@google.com470e71d2011-07-07 08:21:25 +000026}
27
henrike@webrtc.org9f847232012-09-25 20:27:51 +000028RWLockGeneric::~RWLockGeneric() {
29 delete write_condition_;
30 delete read_condition_;
31 delete critical_section_;
niklase@google.com470e71d2011-07-07 08:21:25 +000032}
33
henrike@webrtc.org9f847232012-09-25 20:27:51 +000034void RWLockGeneric::AcquireLockExclusive() {
35 CriticalSectionScoped cs(critical_section_);
36 if (writer_active_ || readers_active_ > 0) {
37 ++writers_waiting_;
38 while (writer_active_ || readers_active_ > 0) {
39 write_condition_->SleepCS(*critical_section_);
niklase@google.com470e71d2011-07-07 08:21:25 +000040 }
henrike@webrtc.org9f847232012-09-25 20:27:51 +000041 --writers_waiting_;
42 }
43 writer_active_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +000044}
45
henrike@webrtc.org9f847232012-09-25 20:27:51 +000046void RWLockGeneric::ReleaseLockExclusive() {
47 CriticalSectionScoped cs(critical_section_);
48 writer_active_ = false;
49 if (writers_waiting_ > 0) {
50 write_condition_->Wake();
51 } else if (readers_waiting_ > 0) {
52 read_condition_->WakeAll();
53 }
54}
niklase@google.com470e71d2011-07-07 08:21:25 +000055
henrike@webrtc.org9f847232012-09-25 20:27:51 +000056void RWLockGeneric::AcquireLockShared() {
57 CriticalSectionScoped cs(critical_section_);
58 if (writer_active_ || writers_waiting_ > 0) {
59 ++readers_waiting_;
niklase@google.com470e71d2011-07-07 08:21:25 +000060
henrike@webrtc.org9f847232012-09-25 20:27:51 +000061 while (writer_active_ || writers_waiting_ > 0) {
62 read_condition_->SleepCS(*critical_section_);
niklase@google.com470e71d2011-07-07 08:21:25 +000063 }
henrike@webrtc.org9f847232012-09-25 20:27:51 +000064 --readers_waiting_;
65 }
66 ++readers_active_;
niklase@google.com470e71d2011-07-07 08:21:25 +000067}
68
henrike@webrtc.org9f847232012-09-25 20:27:51 +000069void RWLockGeneric::ReleaseLockShared() {
70 CriticalSectionScoped cs(critical_section_);
71 --readers_active_;
72 if (readers_active_ == 0 && writers_waiting_ > 0) {
73 write_condition_->Wake();
74 }
niklase@google.com470e71d2011-07-07 08:21:25 +000075}
76
henrike@webrtc.org9f847232012-09-25 20:27:51 +000077} // namespace webrtc