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> |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame^] | 24 | #include <sal.h> // must come after windows headers. |
| 25 | #endif // defined(WEBRTC_WIN) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 26 | |
| 27 | #if defined(WEBRTC_POSIX) |
| 28 | #include <pthread.h> |
| 29 | #endif |
| 30 | |
Magnus Jedvert | 6bf1084 | 2015-04-23 11:37:55 +0200 | [diff] [blame] | 31 | #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame^] | 32 | #define CS_DEBUG_CHECKS 1 |
Magnus Jedvert | 6bf1084 | 2015-04-23 11:37:55 +0200 | [diff] [blame] | 33 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 34 | |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame^] | 35 | #if CS_DEBUG_CHECKS |
| 36 | #define CS_DEBUG_CODE(x) x |
| 37 | #else // !CS_DEBUG_CHECKS |
| 38 | #define CS_DEBUG_CODE(x) |
| 39 | #endif // !CS_DEBUG_CHECKS |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 40 | |
| 41 | namespace rtc { |
| 42 | |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame^] | 43 | class LOCKABLE CriticalSection { |
| 44 | public: |
| 45 | CriticalSection(); |
| 46 | ~CriticalSection(); |
| 47 | |
| 48 | void Enter() EXCLUSIVE_LOCK_FUNCTION(); |
| 49 | bool TryEnter() EXCLUSIVE_TRYLOCK_FUNCTION(true); |
| 50 | void Leave() UNLOCK_FUNCTION(); |
| 51 | |
| 52 | // Use only for DCHECKing. |
| 53 | bool CurrentThreadIsOwner() const; |
| 54 | // Use only for DCHECKing. |
| 55 | bool IsLocked() const; |
| 56 | |
| 57 | private: |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 58 | #if defined(WEBRTC_WIN) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 59 | CRITICAL_SECTION crit_; |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame^] | 60 | #elif defined(WEBRTC_POSIX) |
tommi@webrtc.org | a32f064 | 2015-03-08 07:38:31 +0000 | [diff] [blame] | 61 | pthread_mutex_t mutex_; |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame^] | 62 | CS_DEBUG_CODE(pthread_t thread_); |
| 63 | CS_DEBUG_CODE(int recursion_count_); |
| 64 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 65 | }; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 66 | |
| 67 | // CritScope, for serializing execution through a scope. |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 68 | class SCOPED_LOCKABLE CritScope { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 69 | public: |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame^] | 70 | explicit CritScope(CriticalSection* cs) EXCLUSIVE_LOCK_FUNCTION(cs); |
| 71 | ~CritScope() UNLOCK_FUNCTION(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 72 | private: |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame^] | 73 | CriticalSection* const cs_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 74 | DISALLOW_COPY_AND_ASSIGN(CritScope); |
| 75 | }; |
| 76 | |
| 77 | // Tries to lock a critical section on construction via |
| 78 | // CriticalSection::TryEnter, and unlocks on destruction if the |
| 79 | // lock was taken. Never blocks. |
| 80 | // |
| 81 | // IMPORTANT: Unlike CritScope, the lock may not be owned by this thread in |
| 82 | // subsequent code. Users *must* check locked() to determine if the |
| 83 | // lock was taken. If you're not calling locked(), you're doing it wrong! |
| 84 | class TryCritScope { |
| 85 | public: |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame^] | 86 | explicit TryCritScope(CriticalSection* cs); |
| 87 | ~TryCritScope(); |
| 88 | #if defined(WEBRTC_WIN) |
| 89 | _Check_return_ bool locked() const; |
| 90 | #else |
| 91 | bool locked() const __attribute__((warn_unused_result)); |
| 92 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 93 | private: |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame^] | 94 | CriticalSection* const cs_; |
| 95 | const bool locked_; |
| 96 | CS_DEBUG_CODE(mutable bool lock_was_called_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 97 | DISALLOW_COPY_AND_ASSIGN(TryCritScope); |
| 98 | }; |
| 99 | |
| 100 | // TODO: Move this to atomicops.h, which can't be done easily because of |
| 101 | // complex compile rules. |
| 102 | class AtomicOps { |
| 103 | public: |
| 104 | #if defined(WEBRTC_WIN) |
| 105 | // 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] | 106 | static int Increment(volatile int* i) { |
| 107 | return ::InterlockedIncrement(reinterpret_cast<volatile LONG*>(i)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 108 | } |
magjed@webrtc.org | a43fce6 | 2015-02-21 13:23:27 +0000 | [diff] [blame] | 109 | static int Decrement(volatile int* i) { |
| 110 | return ::InterlockedDecrement(reinterpret_cast<volatile LONG*>(i)); |
| 111 | } |
| 112 | static int Load(volatile const int* i) { |
| 113 | return *i; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 114 | } |
tommi@webrtc.org | c7157da | 2015-03-19 09:30:29 +0000 | [diff] [blame] | 115 | static void Store(volatile int* i, int value) { |
| 116 | *i = value; |
| 117 | } |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 118 | static int CompareAndSwap(volatile int* i, int old_value, int new_value) { |
| 119 | return ::InterlockedCompareExchange(reinterpret_cast<volatile LONG*>(i), |
| 120 | new_value, |
| 121 | old_value); |
| 122 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 123 | #else |
magjed@webrtc.org | a43fce6 | 2015-02-21 13:23:27 +0000 | [diff] [blame] | 124 | static int Increment(volatile int* i) { |
tommi@webrtc.org | 25819b8 | 2015-03-17 15:35:11 +0000 | [diff] [blame] | 125 | return __sync_add_and_fetch(i, 1); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 126 | } |
magjed@webrtc.org | a43fce6 | 2015-02-21 13:23:27 +0000 | [diff] [blame] | 127 | static int Decrement(volatile int* i) { |
tommi@webrtc.org | 25819b8 | 2015-03-17 15:35:11 +0000 | [diff] [blame] | 128 | return __sync_sub_and_fetch(i, 1); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 129 | } |
magjed@webrtc.org | a43fce6 | 2015-02-21 13:23:27 +0000 | [diff] [blame] | 130 | static int Load(volatile const int* i) { |
| 131 | // Adding 0 is a no-op, so const_cast is fine. |
tommi@webrtc.org | 25819b8 | 2015-03-17 15:35:11 +0000 | [diff] [blame] | 132 | return __sync_add_and_fetch(const_cast<volatile int*>(i), 0); |
magjed@webrtc.org | a43fce6 | 2015-02-21 13:23:27 +0000 | [diff] [blame] | 133 | } |
tommi@webrtc.org | c7157da | 2015-03-19 09:30:29 +0000 | [diff] [blame] | 134 | static void Store(volatile int* i, int value) { |
| 135 | __sync_synchronize(); |
| 136 | *i = value; |
| 137 | } |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 138 | static int CompareAndSwap(volatile int* i, int old_value, int new_value) { |
| 139 | return __sync_val_compare_and_swap(i, old_value, new_value); |
| 140 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 141 | #endif |
| 142 | }; |
| 143 | |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 144 | |
| 145 | // A POD lock used to protect global variables. Do NOT use for other purposes. |
| 146 | // No custom constructor or private data member should be added. |
| 147 | class LOCKABLE GlobalLockPod { |
| 148 | public: |
| 149 | void Lock() EXCLUSIVE_LOCK_FUNCTION(); |
| 150 | |
| 151 | void Unlock() UNLOCK_FUNCTION(); |
| 152 | |
| 153 | volatile int lock_acquired; |
| 154 | }; |
| 155 | |
| 156 | class GlobalLock : public GlobalLockPod { |
| 157 | public: |
| 158 | GlobalLock(); |
| 159 | }; |
| 160 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 161 | } // namespace rtc |
| 162 | |
| 163 | #endif // WEBRTC_BASE_CRITICALSECTION_H__ |