147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org/*
247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  Copyright 2011 The WebRTC Project Authors. All rights reserved.
347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *
447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  Use of this source code is governed by a BSD-style license
547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  that can be found in the LICENSE file in the root of the source
647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  tree. An additional intellectual property rights grant can be found
747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  in the file PATENTS.  All contributing project authors may
847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org */
1047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
1147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#ifndef WEBRTC_BASE_SHAREDEXCLUSIVELOCK_H_
1247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#define WEBRTC_BASE_SHAREDEXCLUSIVELOCK_H_
1347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
1447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/constructormagic.h"
1547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/criticalsection.h"
1647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/event.h"
1747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
1847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgnamespace rtc {
1947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
2047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// This class provides shared-exclusive lock. It can be used in cases like
2147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// multiple-readers/single-writer model.
2246c93aa23855b429a7bd3c622842539bf5937d3dpbos@webrtc.orgclass LOCKABLE SharedExclusiveLock {
2347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org public:
2447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  SharedExclusiveLock();
2547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
2647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Locking/unlocking methods. It is encouraged to use SharedScope or
2747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // ExclusiveScope for protection.
2846c93aa23855b429a7bd3c622842539bf5937d3dpbos@webrtc.org  void LockExclusive() EXCLUSIVE_LOCK_FUNCTION();
2946c93aa23855b429a7bd3c622842539bf5937d3dpbos@webrtc.org  void UnlockExclusive() UNLOCK_FUNCTION();
3047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  void LockShared();
3147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  void UnlockShared();
3247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
3347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org private:
3447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  rtc::CriticalSection cs_exclusive_;
3547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  rtc::CriticalSection cs_shared_;
3647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  rtc::Event shared_count_is_zero_;
3747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  int shared_count_;
3847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
3947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  DISALLOW_COPY_AND_ASSIGN(SharedExclusiveLock);
4047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org};
4147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
4246c93aa23855b429a7bd3c622842539bf5937d3dpbos@webrtc.orgclass SCOPED_LOCKABLE SharedScope {
4347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org public:
4446c93aa23855b429a7bd3c622842539bf5937d3dpbos@webrtc.org  explicit SharedScope(SharedExclusiveLock* lock) SHARED_LOCK_FUNCTION(lock)
4546c93aa23855b429a7bd3c622842539bf5937d3dpbos@webrtc.org      : lock_(lock) {
4647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    lock_->LockShared();
4747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
4847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
4946c93aa23855b429a7bd3c622842539bf5937d3dpbos@webrtc.org  ~SharedScope() UNLOCK_FUNCTION() { lock_->UnlockShared(); }
5047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
5147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org private:
5247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  SharedExclusiveLock* lock_;
5347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
5447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  DISALLOW_COPY_AND_ASSIGN(SharedScope);
5547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org};
5647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
5746c93aa23855b429a7bd3c622842539bf5937d3dpbos@webrtc.orgclass SCOPED_LOCKABLE ExclusiveScope {
5847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org public:
5946c93aa23855b429a7bd3c622842539bf5937d3dpbos@webrtc.org  explicit ExclusiveScope(SharedExclusiveLock* lock)
6046c93aa23855b429a7bd3c622842539bf5937d3dpbos@webrtc.org      EXCLUSIVE_LOCK_FUNCTION(lock)
6146c93aa23855b429a7bd3c622842539bf5937d3dpbos@webrtc.org      : lock_(lock) {
6247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    lock_->LockExclusive();
6347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
6447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
6546c93aa23855b429a7bd3c622842539bf5937d3dpbos@webrtc.org  ~ExclusiveScope() UNLOCK_FUNCTION() { lock_->UnlockExclusive(); }
6647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
6747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org private:
6847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  SharedExclusiveLock* lock_;
6947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
7047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  DISALLOW_COPY_AND_ASSIGN(ExclusiveScope);
7147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org};
7247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
7347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}  // namespace rtc
7447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
7547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#endif  // WEBRTC_BASE_SHAREDEXCLUSIVELOCK_H_
76