| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 #endif | 34 #endif |
| 35 | 35 |
| 36 #if CS_DEBUG_CHECKS | 36 #if CS_DEBUG_CHECKS |
| 37 #define CS_DEBUG_CODE(x) x | 37 #define CS_DEBUG_CODE(x) x |
| 38 #else // !CS_DEBUG_CHECKS | 38 #else // !CS_DEBUG_CHECKS |
| 39 #define CS_DEBUG_CODE(x) | 39 #define CS_DEBUG_CODE(x) |
| 40 #endif // !CS_DEBUG_CHECKS | 40 #endif // !CS_DEBUG_CHECKS |
| 41 | 41 |
| 42 namespace rtc { | 42 namespace rtc { |
| 43 | 43 |
| 44 // Locking methods (Enter, TryEnter, Leave)are const to permit protecting |
| 45 // members inside a const context without requiring mutable CriticalSections |
| 46 // everywhere. |
| 44 class LOCKABLE CriticalSection { | 47 class LOCKABLE CriticalSection { |
| 45 public: | 48 public: |
| 46 CriticalSection(); | 49 CriticalSection(); |
| 47 ~CriticalSection(); | 50 ~CriticalSection(); |
| 48 | 51 |
| 49 void Enter() EXCLUSIVE_LOCK_FUNCTION(); | 52 void Enter() const EXCLUSIVE_LOCK_FUNCTION(); |
| 50 bool TryEnter() EXCLUSIVE_TRYLOCK_FUNCTION(true); | 53 bool TryEnter() const EXCLUSIVE_TRYLOCK_FUNCTION(true); |
| 51 void Leave() UNLOCK_FUNCTION(); | 54 void Leave() const UNLOCK_FUNCTION(); |
| 52 | 55 |
| 53 // Use only for RTC_DCHECKing. | 56 // Use only for RTC_DCHECKing. |
| 54 bool CurrentThreadIsOwner() const; | 57 bool CurrentThreadIsOwner() const; |
| 55 // Use only for RTC_DCHECKing. | 58 // Use only for RTC_DCHECKing. |
| 56 bool IsLocked() const; | 59 bool IsLocked() const; |
| 57 | 60 |
| 58 private: | 61 private: |
| 59 #if defined(WEBRTC_WIN) | 62 #if defined(WEBRTC_WIN) |
| 60 CRITICAL_SECTION crit_; | 63 mutable CRITICAL_SECTION crit_; |
| 61 #elif defined(WEBRTC_POSIX) | 64 #elif defined(WEBRTC_POSIX) |
| 62 pthread_mutex_t mutex_; | 65 mutable pthread_mutex_t mutex_; |
| 63 CS_DEBUG_CODE(pthread_t thread_); | 66 CS_DEBUG_CODE(mutable pthread_t thread_); |
| 64 CS_DEBUG_CODE(int recursion_count_); | 67 CS_DEBUG_CODE(mutable int recursion_count_); |
| 65 #endif | 68 #endif |
| 66 }; | 69 }; |
| 67 | 70 |
| 68 // CritScope, for serializing execution through a scope. | 71 // CritScope, for serializing execution through a scope. |
| 69 class SCOPED_LOCKABLE CritScope { | 72 class SCOPED_LOCKABLE CritScope { |
| 70 public: | 73 public: |
| 71 explicit CritScope(CriticalSection* cs) EXCLUSIVE_LOCK_FUNCTION(cs); | 74 explicit CritScope(const CriticalSection* cs) EXCLUSIVE_LOCK_FUNCTION(cs); |
| 72 ~CritScope() UNLOCK_FUNCTION(); | 75 ~CritScope() UNLOCK_FUNCTION(); |
| 73 private: | 76 private: |
| 74 CriticalSection* const cs_; | 77 const CriticalSection* const cs_; |
| 75 RTC_DISALLOW_COPY_AND_ASSIGN(CritScope); | 78 RTC_DISALLOW_COPY_AND_ASSIGN(CritScope); |
| 76 }; | 79 }; |
| 77 | 80 |
| 78 // Tries to lock a critical section on construction via | 81 // Tries to lock a critical section on construction via |
| 79 // CriticalSection::TryEnter, and unlocks on destruction if the | 82 // CriticalSection::TryEnter, and unlocks on destruction if the |
| 80 // lock was taken. Never blocks. | 83 // lock was taken. Never blocks. |
| 81 // | 84 // |
| 82 // IMPORTANT: Unlike CritScope, the lock may not be owned by this thread in | 85 // IMPORTANT: Unlike CritScope, the lock may not be owned by this thread in |
| 83 // subsequent code. Users *must* check locked() to determine if the | 86 // subsequent code. Users *must* check locked() to determine if the |
| 84 // lock was taken. If you're not calling locked(), you're doing it wrong! | 87 // lock was taken. If you're not calling locked(), you're doing it wrong! |
| 85 class TryCritScope { | 88 class TryCritScope { |
| 86 public: | 89 public: |
| 87 explicit TryCritScope(CriticalSection* cs); | 90 explicit TryCritScope(const CriticalSection* cs); |
| 88 ~TryCritScope(); | 91 ~TryCritScope(); |
| 89 #if defined(WEBRTC_WIN) | 92 #if defined(WEBRTC_WIN) |
| 90 _Check_return_ bool locked() const; | 93 _Check_return_ bool locked() const; |
| 91 #else | 94 #else |
| 92 bool locked() const __attribute__ ((__warn_unused_result__)); | 95 bool locked() const __attribute__ ((__warn_unused_result__)); |
| 93 #endif | 96 #endif |
| 94 private: | 97 private: |
| 95 CriticalSection* const cs_; | 98 const CriticalSection* const cs_; |
| 96 const bool locked_; | 99 const bool locked_; |
| 97 CS_DEBUG_CODE(mutable bool lock_was_called_); | 100 CS_DEBUG_CODE(mutable bool lock_was_called_); |
| 98 RTC_DISALLOW_COPY_AND_ASSIGN(TryCritScope); | 101 RTC_DISALLOW_COPY_AND_ASSIGN(TryCritScope); |
| 99 }; | 102 }; |
| 100 | 103 |
| 101 // A POD lock used to protect global variables. Do NOT use for other purposes. | 104 // A POD lock used to protect global variables. Do NOT use for other purposes. |
| 102 // No custom constructor or private data member should be added. | 105 // No custom constructor or private data member should be added. |
| 103 class LOCKABLE GlobalLockPod { | 106 class LOCKABLE GlobalLockPod { |
| 104 public: | 107 public: |
| 105 void Lock() EXCLUSIVE_LOCK_FUNCTION(); | 108 void Lock() EXCLUSIVE_LOCK_FUNCTION(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 120 explicit GlobalLockScope(GlobalLockPod* lock) EXCLUSIVE_LOCK_FUNCTION(lock); | 123 explicit GlobalLockScope(GlobalLockPod* lock) EXCLUSIVE_LOCK_FUNCTION(lock); |
| 121 ~GlobalLockScope() UNLOCK_FUNCTION(); | 124 ~GlobalLockScope() UNLOCK_FUNCTION(); |
| 122 private: | 125 private: |
| 123 GlobalLockPod* const lock_; | 126 GlobalLockPod* const lock_; |
| 124 RTC_DISALLOW_COPY_AND_ASSIGN(GlobalLockScope); | 127 RTC_DISALLOW_COPY_AND_ASSIGN(GlobalLockScope); |
| 125 }; | 128 }; |
| 126 | 129 |
| 127 } // namespace rtc | 130 } // namespace rtc |
| 128 | 131 |
| 129 #endif // WEBRTC_BASE_CRITICALSECTION_H_ | 132 #endif // WEBRTC_BASE_CRITICALSECTION_H_ |
| OLD | NEW |