blob: 9facf60eacaea5fefa7ae9b3c15442b20fc39680 [file] [log] [blame]
henrike@webrtc.orgf7795df2014-05-13 18:00:26 +00001/*
2 * Copyright 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
11#include "webrtc/base/sharedexclusivelock.h"
12
13namespace rtc {
14
15SharedExclusiveLock::SharedExclusiveLock()
16 : shared_count_is_zero_(true, true),
17 shared_count_(0) {
18}
19
20void SharedExclusiveLock::LockExclusive() {
21 cs_exclusive_.Enter();
22 shared_count_is_zero_.Wait(rtc::kForever);
23}
24
25void SharedExclusiveLock::UnlockExclusive() {
26 cs_exclusive_.Leave();
27}
28
29void SharedExclusiveLock::LockShared() {
30 CritScope exclusive_scope(&cs_exclusive_);
31 CritScope shared_scope(&cs_shared_);
32 if (++shared_count_ == 1) {
33 shared_count_is_zero_.Reset();
34 }
35}
36
37void SharedExclusiveLock::UnlockShared() {
38 CritScope shared_scope(&cs_shared_);
39 if (--shared_count_ == 0) {
40 shared_count_is_zero_.Set();
41 }
42}
43
44} // namespace rtc