henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 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 | |
Peter Boström | ff019b0 | 2015-04-30 14:16:07 +0200 | [diff] [blame] | 11 | #ifndef WEBRTC_BASE_CRITICALSECTION_H_ |
| 12 | #define WEBRTC_BASE_CRITICALSECTION_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame^] | 14 | #include "webrtc/base/atomicops.h" |
| 15 | #include "webrtc/base/checks.h" |
| 16 | #include "webrtc/base/constructormagic.h" |
| 17 | #include "webrtc/base/platform_thread_types.h" |
| 18 | #include "webrtc/base/thread_annotations.h" |
| 19 | #include "webrtc/typedefs.h" |
| 20 | |
| 21 | #if defined(WEBRTC_WIN) |
| 22 | // Include winsock2.h before including <windows.h> to maintain consistency with |
| 23 | // win32.h. We can't include win32.h directly here since it pulls in |
| 24 | // headers such as basictypes.h which causes problems in Chromium where webrtc |
| 25 | // exists as two separate projects, webrtc and libjingle. |
| 26 | #include <winsock2.h> |
| 27 | #include <windows.h> |
| 28 | #include <sal.h> // must come after windows headers. |
| 29 | #endif // defined(WEBRTC_WIN) |
| 30 | |
| 31 | #if defined(WEBRTC_POSIX) |
| 32 | #include <pthread.h> |
| 33 | #endif |
| 34 | |
| 35 | // See notes in the 'Performance' unit test for the effects of this flag. |
| 36 | #define USE_NATIVE_MUTEX_ON_MAC 0 |
| 37 | |
| 38 | #if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
| 39 | #include <dispatch/dispatch.h> |
| 40 | #endif |
| 41 | |
| 42 | #define CS_DEBUG_CHECKS RTC_DCHECK_IS_ON |
| 43 | |
| 44 | #if CS_DEBUG_CHECKS |
| 45 | #define CS_DEBUG_CODE(x) x |
| 46 | #else // !CS_DEBUG_CHECKS |
| 47 | #define CS_DEBUG_CODE(x) |
| 48 | #endif // !CS_DEBUG_CHECKS |
| 49 | |
| 50 | namespace rtc { |
| 51 | |
| 52 | // Locking methods (Enter, TryEnter, Leave)are const to permit protecting |
| 53 | // members inside a const context without requiring mutable CriticalSections |
| 54 | // everywhere. |
| 55 | class LOCKABLE CriticalSection { |
| 56 | public: |
| 57 | CriticalSection(); |
| 58 | ~CriticalSection(); |
| 59 | |
| 60 | void Enter() const EXCLUSIVE_LOCK_FUNCTION(); |
| 61 | bool TryEnter() const EXCLUSIVE_TRYLOCK_FUNCTION(true); |
| 62 | void Leave() const UNLOCK_FUNCTION(); |
| 63 | |
| 64 | private: |
| 65 | // Use only for RTC_DCHECKing. |
| 66 | bool CurrentThreadIsOwner() const; |
| 67 | |
| 68 | #if defined(WEBRTC_WIN) |
| 69 | mutable CRITICAL_SECTION crit_; |
| 70 | #elif defined(WEBRTC_POSIX) |
| 71 | # if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
| 72 | // Number of times the lock has been locked + number of threads waiting. |
| 73 | // TODO(tommi): We could use this number and subtract the recursion count |
| 74 | // to find places where we have multiple threads contending on the same lock. |
| 75 | mutable volatile int lock_queue_; |
| 76 | // |recursion_| represents the recursion count + 1 for the thread that owns |
| 77 | // the lock. Only modified by the thread that owns the lock. |
| 78 | mutable int recursion_; |
| 79 | // Used to signal a single waiting thread when the lock becomes available. |
| 80 | mutable dispatch_semaphore_t semaphore_; |
| 81 | // The thread that currently holds the lock. Required to handle recursion. |
| 82 | mutable PlatformThreadRef owning_thread_; |
| 83 | # else |
| 84 | mutable pthread_mutex_t mutex_; |
| 85 | # endif |
| 86 | mutable PlatformThreadRef thread_; // Only used by RTC_DCHECKs. |
| 87 | mutable int recursion_count_; // Only used by RTC_DCHECKs. |
| 88 | #else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX) |
| 89 | # error Unsupported platform. |
| 90 | #endif |
| 91 | }; |
| 92 | |
| 93 | // CritScope, for serializing execution through a scope. |
| 94 | class SCOPED_LOCKABLE CritScope { |
| 95 | public: |
| 96 | explicit CritScope(const CriticalSection* cs) EXCLUSIVE_LOCK_FUNCTION(cs); |
| 97 | ~CritScope() UNLOCK_FUNCTION(); |
| 98 | private: |
| 99 | const CriticalSection* const cs_; |
| 100 | RTC_DISALLOW_COPY_AND_ASSIGN(CritScope); |
| 101 | }; |
| 102 | |
| 103 | // Tries to lock a critical section on construction via |
| 104 | // CriticalSection::TryEnter, and unlocks on destruction if the |
| 105 | // lock was taken. Never blocks. |
| 106 | // |
| 107 | // IMPORTANT: Unlike CritScope, the lock may not be owned by this thread in |
| 108 | // subsequent code. Users *must* check locked() to determine if the |
| 109 | // lock was taken. If you're not calling locked(), you're doing it wrong! |
| 110 | class TryCritScope { |
| 111 | public: |
| 112 | explicit TryCritScope(const CriticalSection* cs); |
| 113 | ~TryCritScope(); |
| 114 | #if defined(WEBRTC_WIN) |
| 115 | _Check_return_ bool locked() const; |
| 116 | #elif defined(WEBRTC_POSIX) |
| 117 | bool locked() const __attribute__ ((__warn_unused_result__)); |
| 118 | #else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX) |
| 119 | # error Unsupported platform. |
| 120 | #endif |
| 121 | private: |
| 122 | const CriticalSection* const cs_; |
| 123 | const bool locked_; |
| 124 | mutable bool lock_was_called_; // Only used by RTC_DCHECKs. |
| 125 | RTC_DISALLOW_COPY_AND_ASSIGN(TryCritScope); |
| 126 | }; |
| 127 | |
| 128 | // A POD lock used to protect global variables. Do NOT use for other purposes. |
| 129 | // No custom constructor or private data member should be added. |
| 130 | class LOCKABLE GlobalLockPod { |
| 131 | public: |
| 132 | void Lock() EXCLUSIVE_LOCK_FUNCTION(); |
| 133 | |
| 134 | void Unlock() UNLOCK_FUNCTION(); |
| 135 | |
| 136 | volatile int lock_acquired; |
| 137 | }; |
| 138 | |
| 139 | class GlobalLock : public GlobalLockPod { |
| 140 | public: |
| 141 | GlobalLock(); |
| 142 | }; |
| 143 | |
| 144 | // GlobalLockScope, for serializing execution through a scope. |
| 145 | class SCOPED_LOCKABLE GlobalLockScope { |
| 146 | public: |
| 147 | explicit GlobalLockScope(GlobalLockPod* lock) EXCLUSIVE_LOCK_FUNCTION(lock); |
| 148 | ~GlobalLockScope() UNLOCK_FUNCTION(); |
| 149 | private: |
| 150 | GlobalLockPod* const lock_; |
| 151 | RTC_DISALLOW_COPY_AND_ASSIGN(GlobalLockScope); |
| 152 | }; |
| 153 | |
| 154 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 155 | |
Peter Boström | ff019b0 | 2015-04-30 14:16:07 +0200 | [diff] [blame] | 156 | #endif // WEBRTC_BASE_CRITICALSECTION_H_ |