Home | History | Annotate | Download | only in interface
      1 /*
      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 
     11 #ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_RW_LOCK_WRAPPER_H_
     12 #define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_RW_LOCK_WRAPPER_H_
     13 
     14 // Note, Windows pre-Vista version of RW locks are not supported nativly. For
     15 // these OSs regular critical sections have been used to approximate RW lock
     16 // functionality and will therefore have worse performance.
     17 
     18 namespace webrtc {
     19 class RWLockWrapper
     20 {
     21 public:
     22     static RWLockWrapper* CreateRWLock();
     23     virtual ~RWLockWrapper();
     24 
     25     virtual void AcquireLockExclusive() = 0;
     26     virtual void ReleaseLockExclusive() = 0;
     27 
     28     virtual void AcquireLockShared() = 0;
     29     virtual void ReleaseLockShared() = 0;
     30 
     31 protected:
     32     virtual int Init() = 0;
     33 };
     34 
     35 // RAII extensions of the RW lock. Prevents Acquire/Release missmatches and
     36 // provides more compact locking syntax.
     37 class ReadLockScoped
     38 {
     39 public:
     40     ReadLockScoped(RWLockWrapper& rwLock)
     41         :
     42         _rwLock(rwLock)
     43     {
     44         _rwLock.AcquireLockShared();
     45     }
     46 
     47     ~ReadLockScoped()
     48     {
     49         _rwLock.ReleaseLockShared();
     50     }
     51 
     52 private:
     53     RWLockWrapper& _rwLock;
     54 };
     55 
     56 class WriteLockScoped
     57 {
     58 public:
     59     WriteLockScoped(RWLockWrapper& rwLock)
     60         :
     61         _rwLock(rwLock)
     62     {
     63         _rwLock.AcquireLockExclusive();
     64     }
     65 
     66     ~WriteLockScoped()
     67     {
     68         _rwLock.ReleaseLockExclusive();
     69     }
     70 
     71 private:
     72     RWLockWrapper& _rwLock;
     73 };
     74 } // namespace webrtc
     75 
     76 #endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_RW_LOCK_WRAPPER_H_
     77