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 | |
| 30 | #ifdef _DEBUG |
| 31 | #define CS_TRACK_OWNER 1 |
| 32 | #endif // _DEBUG |
| 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 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 61 | |
| 62 | private: |
| 63 | CRITICAL_SECTION crit_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 64 | }; |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 65 | #endif // WEBRTC_WIN |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 66 | |
| 67 | #if defined(WEBRTC_POSIX) |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 68 | class LOCKABLE CriticalSection { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 69 | public: |
| 70 | CriticalSection() { |
| 71 | pthread_mutexattr_t mutex_attribute; |
| 72 | pthread_mutexattr_init(&mutex_attribute); |
| 73 | pthread_mutexattr_settype(&mutex_attribute, PTHREAD_MUTEX_RECURSIVE); |
| 74 | pthread_mutex_init(&mutex_, &mutex_attribute); |
| 75 | pthread_mutexattr_destroy(&mutex_attribute); |
| 76 | TRACK_OWNER(thread_ = 0); |
| 77 | } |
| 78 | ~CriticalSection() { |
| 79 | pthread_mutex_destroy(&mutex_); |
| 80 | } |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 81 | void Enter() EXCLUSIVE_LOCK_FUNCTION() { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 82 | pthread_mutex_lock(&mutex_); |
| 83 | TRACK_OWNER(thread_ = pthread_self()); |
| 84 | } |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 85 | bool TryEnter() EXCLUSIVE_TRYLOCK_FUNCTION(true) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 86 | if (pthread_mutex_trylock(&mutex_) == 0) { |
| 87 | TRACK_OWNER(thread_ = pthread_self()); |
| 88 | return true; |
| 89 | } |
| 90 | return false; |
| 91 | } |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 92 | void Leave() UNLOCK_FUNCTION() { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 93 | TRACK_OWNER(thread_ = 0); |
| 94 | pthread_mutex_unlock(&mutex_); |
| 95 | } |
| 96 | |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 97 | // Used for debugging. |
| 98 | bool CurrentThreadIsOwner() const { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 99 | #if CS_TRACK_OWNER |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 100 | return pthread_equal(thread_, pthread_self()); |
| 101 | #else |
| 102 | return true; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 103 | #endif // CS_TRACK_OWNER |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 104 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 105 | |
| 106 | private: |
tommi@webrtc.org | a32f064 | 2015-03-08 07:38:31 +0000 | [diff] [blame] | 107 | pthread_mutex_t mutex_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 108 | TRACK_OWNER(pthread_t thread_); |
| 109 | }; |
| 110 | #endif // WEBRTC_POSIX |
| 111 | |
| 112 | // CritScope, for serializing execution through a scope. |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 113 | class SCOPED_LOCKABLE CritScope { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 114 | public: |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 115 | explicit CritScope(CriticalSection *pcrit) EXCLUSIVE_LOCK_FUNCTION(pcrit) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 116 | pcrit_ = pcrit; |
| 117 | pcrit_->Enter(); |
| 118 | } |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 119 | ~CritScope() UNLOCK_FUNCTION() { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 120 | pcrit_->Leave(); |
| 121 | } |
| 122 | private: |
| 123 | CriticalSection *pcrit_; |
| 124 | DISALLOW_COPY_AND_ASSIGN(CritScope); |
| 125 | }; |
| 126 | |
| 127 | // Tries to lock a critical section on construction via |
| 128 | // CriticalSection::TryEnter, and unlocks on destruction if the |
| 129 | // lock was taken. Never blocks. |
| 130 | // |
| 131 | // IMPORTANT: Unlike CritScope, the lock may not be owned by this thread in |
| 132 | // subsequent code. Users *must* check locked() to determine if the |
| 133 | // lock was taken. If you're not calling locked(), you're doing it wrong! |
| 134 | class TryCritScope { |
| 135 | public: |
| 136 | explicit TryCritScope(CriticalSection *pcrit) { |
| 137 | pcrit_ = pcrit; |
| 138 | locked_ = pcrit_->TryEnter(); |
| 139 | } |
| 140 | ~TryCritScope() { |
| 141 | if (locked_) { |
| 142 | pcrit_->Leave(); |
| 143 | } |
| 144 | } |
| 145 | bool locked() const { |
| 146 | return locked_; |
| 147 | } |
| 148 | private: |
| 149 | CriticalSection *pcrit_; |
| 150 | bool locked_; |
| 151 | DISALLOW_COPY_AND_ASSIGN(TryCritScope); |
| 152 | }; |
| 153 | |
| 154 | // TODO: Move this to atomicops.h, which can't be done easily because of |
| 155 | // complex compile rules. |
| 156 | class AtomicOps { |
| 157 | public: |
| 158 | #if defined(WEBRTC_WIN) |
| 159 | // 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] | 160 | static int Increment(volatile int* i) { |
| 161 | return ::InterlockedIncrement(reinterpret_cast<volatile LONG*>(i)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 162 | } |
magjed@webrtc.org | a43fce6 | 2015-02-21 13:23:27 +0000 | [diff] [blame] | 163 | static int Decrement(volatile int* i) { |
| 164 | return ::InterlockedDecrement(reinterpret_cast<volatile LONG*>(i)); |
| 165 | } |
tommi@webrtc.org | c383c24 | 2015-03-17 13:46:42 +0000 | [diff] [blame^] | 166 | |
| 167 | // No barrier load. |
magjed@webrtc.org | a43fce6 | 2015-02-21 13:23:27 +0000 | [diff] [blame] | 168 | static int Load(volatile const int* i) { |
| 169 | return *i; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 170 | } |
tommi@webrtc.org | c383c24 | 2015-03-17 13:46:42 +0000 | [diff] [blame^] | 171 | |
| 172 | // No barrier store. |
| 173 | static void Store(volatile int* i, int value) { |
| 174 | *i = value; |
| 175 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 176 | #else |
magjed@webrtc.org | a43fce6 | 2015-02-21 13:23:27 +0000 | [diff] [blame] | 177 | static int Increment(volatile int* i) { |
tommi@webrtc.org | c383c24 | 2015-03-17 13:46:42 +0000 | [diff] [blame^] | 178 | return __atomic_add_fetch(i, 1, __ATOMIC_SEQ_CST); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 179 | } |
magjed@webrtc.org | a43fce6 | 2015-02-21 13:23:27 +0000 | [diff] [blame] | 180 | static int Decrement(volatile int* i) { |
tommi@webrtc.org | c383c24 | 2015-03-17 13:46:42 +0000 | [diff] [blame^] | 181 | return __atomic_sub_fetch(i, 1, __ATOMIC_SEQ_CST); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 182 | } |
tommi@webrtc.org | c383c24 | 2015-03-17 13:46:42 +0000 | [diff] [blame^] | 183 | // No barrier load. |
magjed@webrtc.org | a43fce6 | 2015-02-21 13:23:27 +0000 | [diff] [blame] | 184 | static int Load(volatile const int* i) { |
| 185 | // Adding 0 is a no-op, so const_cast is fine. |
tommi@webrtc.org | c383c24 | 2015-03-17 13:46:42 +0000 | [diff] [blame^] | 186 | return __atomic_load_n(const_cast<volatile int*>(i), 0); |
| 187 | } |
| 188 | // No barrier store. |
| 189 | static void Store(volatile int* i, int value) { |
| 190 | __atomic_store_n(i, value, __ATOMIC_RELAXED); |
magjed@webrtc.org | a43fce6 | 2015-02-21 13:23:27 +0000 | [diff] [blame] | 191 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 192 | #endif |
| 193 | }; |
| 194 | |
| 195 | } // namespace rtc |
| 196 | |
| 197 | #endif // WEBRTC_BASE_CRITICALSECTION_H__ |