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 | |
Peter Boström | ff019b0 | 2015-04-30 14:16:07 +0200 | [diff] [blame] | 14 | #include "webrtc/base/atomicops.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 15 | #include "webrtc/base/constructormagic.h" |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 16 | #include "webrtc/base/thread_annotations.h" |
tommi | 7406b96 | 2016-01-22 05:13:33 -0800 | [diff] [blame] | 17 | #include "webrtc/base/platform_thread_types.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 18 | |
| 19 | #if defined(WEBRTC_WIN) |
tommi@webrtc.org | 4a4e688 | 2015-03-04 20:09:37 +0000 | [diff] [blame] | 20 | // Include winsock2.h before including <windows.h> to maintain consistency with |
| 21 | // win32.h. We can't include win32.h directly here since it pulls in |
| 22 | // headers such as basictypes.h which causes problems in Chromium where webrtc |
| 23 | // exists as two separate projects, webrtc and libjingle. |
| 24 | #include <winsock2.h> |
| 25 | #include <windows.h> |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 26 | #include <sal.h> // must come after windows headers. |
| 27 | #endif // defined(WEBRTC_WIN) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 28 | |
| 29 | #if defined(WEBRTC_POSIX) |
| 30 | #include <pthread.h> |
| 31 | #endif |
| 32 | |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 33 | // See notes in the 'Performance' unit test for the effects of this flag. |
| 34 | #define USE_NATIVE_MUTEX_ON_MAC 0 |
| 35 | |
| 36 | #if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
| 37 | #include <dispatch/dispatch.h> |
| 38 | #endif |
| 39 | |
Magnus Jedvert | 6bf1084 | 2015-04-23 11:37:55 +0200 | [diff] [blame] | 40 | #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 41 | #define CS_DEBUG_CHECKS 1 |
Magnus Jedvert | 6bf1084 | 2015-04-23 11:37:55 +0200 | [diff] [blame] | 42 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 43 | |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 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 |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 49 | |
| 50 | namespace rtc { |
| 51 | |
Peter Boström | af9e663 | 2016-01-21 16:56:52 +0100 | [diff] [blame] | 52 | // Locking methods (Enter, TryEnter, Leave)are const to permit protecting |
| 53 | // members inside a const context without requiring mutable CriticalSections |
| 54 | // everywhere. |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 55 | class LOCKABLE CriticalSection { |
| 56 | public: |
| 57 | CriticalSection(); |
| 58 | ~CriticalSection(); |
| 59 | |
Peter Boström | af9e663 | 2016-01-21 16:56:52 +0100 | [diff] [blame] | 60 | void Enter() const EXCLUSIVE_LOCK_FUNCTION(); |
| 61 | bool TryEnter() const EXCLUSIVE_TRYLOCK_FUNCTION(true); |
| 62 | void Leave() const UNLOCK_FUNCTION(); |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 63 | |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 64 | // Use only for RTC_DCHECKing. |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 65 | bool CurrentThreadIsOwner() const; |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 66 | // Use only for RTC_DCHECKing. |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 67 | bool IsLocked() const; |
| 68 | |
| 69 | private: |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 70 | #if defined(WEBRTC_WIN) |
Peter Boström | af9e663 | 2016-01-21 16:56:52 +0100 | [diff] [blame] | 71 | mutable CRITICAL_SECTION crit_; |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 72 | #elif defined(WEBRTC_POSIX) |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 73 | #if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
| 74 | // Number of times the lock has been locked + number of threads waiting. |
| 75 | // TODO(tommi): We could use this number and subtract the recursion count |
| 76 | // to find places where we have multiple threads contending on the same lock. |
| 77 | mutable volatile int lock_queue_; |
| 78 | // |recursion_| represents the recursion count + 1 for the thread that owns |
| 79 | // the lock. Only modified by the thread that owns the lock. |
| 80 | mutable int recursion_; |
| 81 | // Used to signal a single waiting thread when the lock becomes available. |
| 82 | mutable dispatch_semaphore_t semaphore_; |
| 83 | // The thread that currently holds the lock. Required to handle recursion. |
tommi | 7406b96 | 2016-01-22 05:13:33 -0800 | [diff] [blame] | 84 | mutable PlatformThreadRef owning_thread_; |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 85 | #else |
Peter Boström | af9e663 | 2016-01-21 16:56:52 +0100 | [diff] [blame] | 86 | mutable pthread_mutex_t mutex_; |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 87 | #endif |
tommi | 7406b96 | 2016-01-22 05:13:33 -0800 | [diff] [blame] | 88 | CS_DEBUG_CODE(mutable PlatformThreadRef thread_); |
Peter Boström | af9e663 | 2016-01-21 16:56:52 +0100 | [diff] [blame] | 89 | CS_DEBUG_CODE(mutable int recursion_count_); |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 90 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 91 | }; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 92 | |
| 93 | // CritScope, for serializing execution through a scope. |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 94 | class SCOPED_LOCKABLE CritScope { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 95 | public: |
Peter Boström | af9e663 | 2016-01-21 16:56:52 +0100 | [diff] [blame] | 96 | explicit CritScope(const CriticalSection* cs) EXCLUSIVE_LOCK_FUNCTION(cs); |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 97 | ~CritScope() UNLOCK_FUNCTION(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 98 | private: |
Peter Boström | af9e663 | 2016-01-21 16:56:52 +0100 | [diff] [blame] | 99 | const CriticalSection* const cs_; |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 100 | RTC_DISALLOW_COPY_AND_ASSIGN(CritScope); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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: |
Peter Boström | af9e663 | 2016-01-21 16:56:52 +0100 | [diff] [blame] | 112 | explicit TryCritScope(const CriticalSection* cs); |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 113 | ~TryCritScope(); |
| 114 | #if defined(WEBRTC_WIN) |
| 115 | _Check_return_ bool locked() const; |
| 116 | #else |
kjellander | b71b4f0 | 2016-01-08 04:51:38 -0800 | [diff] [blame] | 117 | bool locked() const __attribute__ ((__warn_unused_result__)); |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 118 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 119 | private: |
Peter Boström | af9e663 | 2016-01-21 16:56:52 +0100 | [diff] [blame] | 120 | const CriticalSection* const cs_; |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 121 | const bool locked_; |
| 122 | CS_DEBUG_CODE(mutable bool lock_was_called_); |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 123 | RTC_DISALLOW_COPY_AND_ASSIGN(TryCritScope); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 124 | }; |
| 125 | |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 126 | // A POD lock used to protect global variables. Do NOT use for other purposes. |
| 127 | // No custom constructor or private data member should be added. |
| 128 | class LOCKABLE GlobalLockPod { |
| 129 | public: |
| 130 | void Lock() EXCLUSIVE_LOCK_FUNCTION(); |
| 131 | |
| 132 | void Unlock() UNLOCK_FUNCTION(); |
| 133 | |
pbos | 46ad542 | 2015-12-07 14:29:14 -0800 | [diff] [blame] | 134 | volatile int lock_acquired; |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
pbos | 3c12f4d | 2015-11-17 03:21:02 -0800 | [diff] [blame] | 137 | class GlobalLock : public GlobalLockPod { |
| 138 | public: |
| 139 | GlobalLock(); |
| 140 | }; |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 141 | |
Joachim Bauch | fec2c6d | 2015-05-27 23:41:43 +0200 | [diff] [blame] | 142 | // GlobalLockScope, for serializing execution through a scope. |
| 143 | class SCOPED_LOCKABLE GlobalLockScope { |
| 144 | public: |
| 145 | explicit GlobalLockScope(GlobalLockPod* lock) EXCLUSIVE_LOCK_FUNCTION(lock); |
| 146 | ~GlobalLockScope() UNLOCK_FUNCTION(); |
| 147 | private: |
| 148 | GlobalLockPod* const lock_; |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 149 | RTC_DISALLOW_COPY_AND_ASSIGN(GlobalLockScope); |
Joachim Bauch | fec2c6d | 2015-05-27 23:41:43 +0200 | [diff] [blame] | 150 | }; |
| 151 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 152 | } // namespace rtc |
| 153 | |
Peter Boström | ff019b0 | 2015-04-30 14:16:07 +0200 | [diff] [blame] | 154 | #endif // WEBRTC_BASE_CRITICALSECTION_H_ |