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 | |
| 11 | #ifndef WEBRTC_BASE_CRITICALSECTION_H__ |
| 12 | #define WEBRTC_BASE_CRITICALSECTION_H__ |
| 13 | |
| 14 | #include "webrtc/base/constructormagic.h" |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 15 | #include "webrtc/base/thread_annotations.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 16 | |
| 17 | #if defined(WEBRTC_WIN) |
tommi@webrtc.org | 4a4e688 | 2015-03-04 20:09:37 +0000 | [diff] [blame] | 18 | // Include winsock2.h before including <windows.h> to maintain consistency with |
| 19 | // win32.h. We can't include win32.h directly here since it pulls in |
| 20 | // headers such as basictypes.h which causes problems in Chromium where webrtc |
| 21 | // exists as two separate projects, webrtc and libjingle. |
| 22 | #include <winsock2.h> |
| 23 | #include <windows.h> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 24 | #endif |
| 25 | |
| 26 | #if defined(WEBRTC_POSIX) |
| 27 | #include <pthread.h> |
| 28 | #endif |
| 29 | |
Magnus Jedvert | 6bf1084 | 2015-04-23 11:37:55 +0200 | [diff] [blame] | 30 | #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 31 | #define CS_TRACK_OWNER 1 |
Magnus Jedvert | 6bf1084 | 2015-04-23 11:37:55 +0200 | [diff] [blame] | 32 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 33 | |
| 34 | #if CS_TRACK_OWNER |
| 35 | #define TRACK_OWNER(x) x |
| 36 | #else // !CS_TRACK_OWNER |
| 37 | #define TRACK_OWNER(x) |
| 38 | #endif // !CS_TRACK_OWNER |
| 39 | |
| 40 | namespace rtc { |
| 41 | |
| 42 | #if defined(WEBRTC_WIN) |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 43 | class LOCKABLE CriticalSection { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 44 | public: |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 45 | CriticalSection() { InitializeCriticalSection(&crit_); } |
| 46 | ~CriticalSection() { DeleteCriticalSection(&crit_); } |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 47 | void Enter() EXCLUSIVE_LOCK_FUNCTION() { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 48 | EnterCriticalSection(&crit_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 49 | } |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 50 | bool TryEnter() EXCLUSIVE_TRYLOCK_FUNCTION(true) { |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 51 | return TryEnterCriticalSection(&crit_) != FALSE; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 52 | } |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 53 | void Leave() UNLOCK_FUNCTION() { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 54 | LeaveCriticalSection(&crit_); |
| 55 | } |
| 56 | |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 57 | // Used for debugging. |
| 58 | bool CurrentThreadIsOwner() const { |
| 59 | return crit_.OwningThread == reinterpret_cast<HANDLE>(GetCurrentThreadId()); |
| 60 | } |
Magnus Jedvert | 6bf1084 | 2015-04-23 11:37:55 +0200 | [diff] [blame] | 61 | // Use only for DCHECKing. |
| 62 | bool IsLocked() { return crit_.LockCount != -1; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 63 | |
| 64 | private: |
| 65 | CRITICAL_SECTION crit_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 66 | }; |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 67 | #endif // WEBRTC_WIN |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 68 | |
| 69 | #if defined(WEBRTC_POSIX) |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 70 | class LOCKABLE CriticalSection { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 71 | public: |
| 72 | CriticalSection() { |
| 73 | pthread_mutexattr_t mutex_attribute; |
| 74 | pthread_mutexattr_init(&mutex_attribute); |
| 75 | pthread_mutexattr_settype(&mutex_attribute, PTHREAD_MUTEX_RECURSIVE); |
| 76 | pthread_mutex_init(&mutex_, &mutex_attribute); |
| 77 | pthread_mutexattr_destroy(&mutex_attribute); |
| 78 | TRACK_OWNER(thread_ = 0); |
| 79 | } |
| 80 | ~CriticalSection() { |
| 81 | pthread_mutex_destroy(&mutex_); |
| 82 | } |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 83 | void Enter() EXCLUSIVE_LOCK_FUNCTION() { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 84 | pthread_mutex_lock(&mutex_); |
| 85 | TRACK_OWNER(thread_ = pthread_self()); |
| 86 | } |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 87 | bool TryEnter() EXCLUSIVE_TRYLOCK_FUNCTION(true) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 88 | if (pthread_mutex_trylock(&mutex_) == 0) { |
| 89 | TRACK_OWNER(thread_ = pthread_self()); |
| 90 | return true; |
| 91 | } |
| 92 | return false; |
| 93 | } |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 94 | void Leave() UNLOCK_FUNCTION() { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 95 | TRACK_OWNER(thread_ = 0); |
| 96 | pthread_mutex_unlock(&mutex_); |
| 97 | } |
| 98 | |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 99 | // Used for debugging. |
| 100 | bool CurrentThreadIsOwner() const { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 101 | #if CS_TRACK_OWNER |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 102 | return pthread_equal(thread_, pthread_self()); |
| 103 | #else |
| 104 | return true; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 105 | #endif // CS_TRACK_OWNER |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 106 | } |
Magnus Jedvert | 6bf1084 | 2015-04-23 11:37:55 +0200 | [diff] [blame] | 107 | // Use only for DCHECKing. |
Magnus Jedvert | 7f287cc | 2015-04-23 16:06:59 +0200 | [diff] [blame^] | 108 | bool IsLocked() { |
Magnus Jedvert | 6bf1084 | 2015-04-23 11:37:55 +0200 | [diff] [blame] | 109 | #if CS_TRACK_OWNER |
Magnus Jedvert | 7f287cc | 2015-04-23 16:06:59 +0200 | [diff] [blame^] | 110 | return thread_ != 0; |
| 111 | #else |
| 112 | return true; |
Magnus Jedvert | 6bf1084 | 2015-04-23 11:37:55 +0200 | [diff] [blame] | 113 | #endif |
Magnus Jedvert | 7f287cc | 2015-04-23 16:06:59 +0200 | [diff] [blame^] | 114 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 115 | |
| 116 | private: |
tommi@webrtc.org | a32f064 | 2015-03-08 07:38:31 +0000 | [diff] [blame] | 117 | pthread_mutex_t mutex_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 118 | TRACK_OWNER(pthread_t thread_); |
| 119 | }; |
| 120 | #endif // WEBRTC_POSIX |
| 121 | |
| 122 | // CritScope, for serializing execution through a scope. |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 123 | class SCOPED_LOCKABLE CritScope { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 124 | public: |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 125 | explicit CritScope(CriticalSection *pcrit) EXCLUSIVE_LOCK_FUNCTION(pcrit) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 126 | pcrit_ = pcrit; |
| 127 | pcrit_->Enter(); |
| 128 | } |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 129 | ~CritScope() UNLOCK_FUNCTION() { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 130 | pcrit_->Leave(); |
| 131 | } |
| 132 | private: |
| 133 | CriticalSection *pcrit_; |
| 134 | DISALLOW_COPY_AND_ASSIGN(CritScope); |
| 135 | }; |
| 136 | |
| 137 | // Tries to lock a critical section on construction via |
| 138 | // CriticalSection::TryEnter, and unlocks on destruction if the |
| 139 | // lock was taken. Never blocks. |
| 140 | // |
| 141 | // IMPORTANT: Unlike CritScope, the lock may not be owned by this thread in |
| 142 | // subsequent code. Users *must* check locked() to determine if the |
| 143 | // lock was taken. If you're not calling locked(), you're doing it wrong! |
| 144 | class TryCritScope { |
| 145 | public: |
| 146 | explicit TryCritScope(CriticalSection *pcrit) { |
| 147 | pcrit_ = pcrit; |
| 148 | locked_ = pcrit_->TryEnter(); |
| 149 | } |
| 150 | ~TryCritScope() { |
| 151 | if (locked_) { |
| 152 | pcrit_->Leave(); |
| 153 | } |
| 154 | } |
| 155 | bool locked() const { |
| 156 | return locked_; |
| 157 | } |
| 158 | private: |
| 159 | CriticalSection *pcrit_; |
| 160 | bool locked_; |
| 161 | DISALLOW_COPY_AND_ASSIGN(TryCritScope); |
| 162 | }; |
| 163 | |
| 164 | // TODO: Move this to atomicops.h, which can't be done easily because of |
| 165 | // complex compile rules. |
| 166 | class AtomicOps { |
| 167 | public: |
| 168 | #if defined(WEBRTC_WIN) |
| 169 | // Assumes sizeof(int) == sizeof(LONG), which it is on Win32 and Win64. |
magjed@webrtc.org | a43fce6 | 2015-02-21 13:23:27 +0000 | [diff] [blame] | 170 | static int Increment(volatile int* i) { |
| 171 | return ::InterlockedIncrement(reinterpret_cast<volatile LONG*>(i)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 172 | } |
magjed@webrtc.org | a43fce6 | 2015-02-21 13:23:27 +0000 | [diff] [blame] | 173 | static int Decrement(volatile int* i) { |
| 174 | return ::InterlockedDecrement(reinterpret_cast<volatile LONG*>(i)); |
| 175 | } |
| 176 | static int Load(volatile const int* i) { |
| 177 | return *i; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 178 | } |
tommi@webrtc.org | c7157da | 2015-03-19 09:30:29 +0000 | [diff] [blame] | 179 | static void Store(volatile int* i, int value) { |
| 180 | *i = value; |
| 181 | } |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 182 | static int CompareAndSwap(volatile int* i, int old_value, int new_value) { |
| 183 | return ::InterlockedCompareExchange(reinterpret_cast<volatile LONG*>(i), |
| 184 | new_value, |
| 185 | old_value); |
| 186 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 187 | #else |
magjed@webrtc.org | a43fce6 | 2015-02-21 13:23:27 +0000 | [diff] [blame] | 188 | static int Increment(volatile int* i) { |
tommi@webrtc.org | 25819b8 | 2015-03-17 15:35:11 +0000 | [diff] [blame] | 189 | return __sync_add_and_fetch(i, 1); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 190 | } |
magjed@webrtc.org | a43fce6 | 2015-02-21 13:23:27 +0000 | [diff] [blame] | 191 | static int Decrement(volatile int* i) { |
tommi@webrtc.org | 25819b8 | 2015-03-17 15:35:11 +0000 | [diff] [blame] | 192 | return __sync_sub_and_fetch(i, 1); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 193 | } |
magjed@webrtc.org | a43fce6 | 2015-02-21 13:23:27 +0000 | [diff] [blame] | 194 | static int Load(volatile const int* i) { |
| 195 | // Adding 0 is a no-op, so const_cast is fine. |
tommi@webrtc.org | 25819b8 | 2015-03-17 15:35:11 +0000 | [diff] [blame] | 196 | return __sync_add_and_fetch(const_cast<volatile int*>(i), 0); |
magjed@webrtc.org | a43fce6 | 2015-02-21 13:23:27 +0000 | [diff] [blame] | 197 | } |
tommi@webrtc.org | c7157da | 2015-03-19 09:30:29 +0000 | [diff] [blame] | 198 | static void Store(volatile int* i, int value) { |
| 199 | __sync_synchronize(); |
| 200 | *i = value; |
| 201 | } |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 202 | static int CompareAndSwap(volatile int* i, int old_value, int new_value) { |
| 203 | return __sync_val_compare_and_swap(i, old_value, new_value); |
| 204 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 205 | #endif |
| 206 | }; |
| 207 | |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 208 | |
| 209 | // A POD lock used to protect global variables. Do NOT use for other purposes. |
| 210 | // No custom constructor or private data member should be added. |
| 211 | class LOCKABLE GlobalLockPod { |
| 212 | public: |
| 213 | void Lock() EXCLUSIVE_LOCK_FUNCTION(); |
| 214 | |
| 215 | void Unlock() UNLOCK_FUNCTION(); |
| 216 | |
| 217 | volatile int lock_acquired; |
| 218 | }; |
| 219 | |
| 220 | class GlobalLock : public GlobalLockPod { |
| 221 | public: |
| 222 | GlobalLock(); |
| 223 | }; |
| 224 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 225 | } // namespace rtc |
| 226 | |
| 227 | #endif // WEBRTC_BASE_CRITICALSECTION_H__ |