rw_lock_wrapper.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #ifndef RTC_BASE_SYNCHRONIZATION_RW_LOCK_WRAPPER_H_
  11. #define RTC_BASE_SYNCHRONIZATION_RW_LOCK_WRAPPER_H_
  12. #include "rtc_base/thread_annotations.h"
  13. // Note, Windows pre-Vista version of RW locks are not supported natively. For
  14. // these OSs regular critical sections have been used to approximate RW lock
  15. // functionality and will therefore have worse performance.
  16. namespace webrtc {
  17. class RTC_LOCKABLE RWLockWrapper {
  18. public:
  19. static RWLockWrapper* CreateRWLock();
  20. virtual ~RWLockWrapper() {}
  21. virtual void AcquireLockExclusive() RTC_EXCLUSIVE_LOCK_FUNCTION() = 0;
  22. virtual void ReleaseLockExclusive() RTC_UNLOCK_FUNCTION() = 0;
  23. virtual void AcquireLockShared() RTC_SHARED_LOCK_FUNCTION() = 0;
  24. virtual void ReleaseLockShared() RTC_UNLOCK_FUNCTION() = 0;
  25. };
  26. // RAII extensions of the RW lock. Prevents Acquire/Release missmatches and
  27. // provides more compact locking syntax.
  28. class RTC_SCOPED_LOCKABLE ReadLockScoped {
  29. public:
  30. explicit ReadLockScoped(RWLockWrapper& rw_lock)
  31. RTC_SHARED_LOCK_FUNCTION(rw_lock)
  32. : rw_lock_(rw_lock) {
  33. rw_lock_.AcquireLockShared();
  34. }
  35. ~ReadLockScoped() RTC_UNLOCK_FUNCTION() { rw_lock_.ReleaseLockShared(); }
  36. private:
  37. RWLockWrapper& rw_lock_;
  38. };
  39. class RTC_SCOPED_LOCKABLE WriteLockScoped {
  40. public:
  41. explicit WriteLockScoped(RWLockWrapper& rw_lock)
  42. RTC_EXCLUSIVE_LOCK_FUNCTION(rw_lock)
  43. : rw_lock_(rw_lock) {
  44. rw_lock_.AcquireLockExclusive();
  45. }
  46. ~WriteLockScoped() RTC_UNLOCK_FUNCTION() { rw_lock_.ReleaseLockExclusive(); }
  47. private:
  48. RWLockWrapper& rw_lock_;
  49. };
  50. } // namespace webrtc
  51. #endif // RTC_BASE_SYNCHRONIZATION_RW_LOCK_WRAPPER_H_