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" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 17 | |
| 18 | #if defined(WEBRTC_WIN) |
tommi@webrtc.org | 4a4e688 | 2015-03-04 20:09:37 +0000 | [diff] [blame] | 19 | // Include winsock2.h before including <windows.h> to maintain consistency with |
| 20 | // win32.h. We can't include win32.h directly here since it pulls in |
| 21 | // headers such as basictypes.h which causes problems in Chromium where webrtc |
| 22 | // exists as two separate projects, webrtc and libjingle. |
| 23 | #include <winsock2.h> |
| 24 | #include <windows.h> |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 25 | #include <sal.h> // must come after windows headers. |
| 26 | #endif // defined(WEBRTC_WIN) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 27 | |
| 28 | #if defined(WEBRTC_POSIX) |
| 29 | #include <pthread.h> |
| 30 | #endif |
| 31 | |
Magnus Jedvert | 6bf1084 | 2015-04-23 11:37:55 +0200 | [diff] [blame] | 32 | #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 33 | #define CS_DEBUG_CHECKS 1 |
Magnus Jedvert | 6bf1084 | 2015-04-23 11:37:55 +0200 | [diff] [blame] | 34 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 35 | |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 36 | #if CS_DEBUG_CHECKS |
| 37 | #define CS_DEBUG_CODE(x) x |
| 38 | #else // !CS_DEBUG_CHECKS |
| 39 | #define CS_DEBUG_CODE(x) |
| 40 | #endif // !CS_DEBUG_CHECKS |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 41 | |
| 42 | namespace rtc { |
| 43 | |
Peter Boström | af9e663 | 2016-01-21 16:56:52 +0100 | [diff] [blame] | 44 | // Locking methods (Enter, TryEnter, Leave)are const to permit protecting |
| 45 | // members inside a const context without requiring mutable CriticalSections |
| 46 | // everywhere. |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 47 | class LOCKABLE CriticalSection { |
| 48 | public: |
| 49 | CriticalSection(); |
| 50 | ~CriticalSection(); |
| 51 | |
Peter Boström | af9e663 | 2016-01-21 16:56:52 +0100 | [diff] [blame] | 52 | void Enter() const EXCLUSIVE_LOCK_FUNCTION(); |
| 53 | bool TryEnter() const EXCLUSIVE_TRYLOCK_FUNCTION(true); |
| 54 | void Leave() const UNLOCK_FUNCTION(); |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 55 | |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 56 | // Use only for RTC_DCHECKing. |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 57 | bool CurrentThreadIsOwner() const; |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 58 | // Use only for RTC_DCHECKing. |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 59 | bool IsLocked() const; |
| 60 | |
| 61 | private: |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 62 | #if defined(WEBRTC_WIN) |
Peter Boström | af9e663 | 2016-01-21 16:56:52 +0100 | [diff] [blame] | 63 | mutable CRITICAL_SECTION crit_; |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 64 | #elif defined(WEBRTC_POSIX) |
Peter Boström | af9e663 | 2016-01-21 16:56:52 +0100 | [diff] [blame] | 65 | mutable pthread_mutex_t mutex_; |
| 66 | CS_DEBUG_CODE(mutable pthread_t thread_); |
| 67 | CS_DEBUG_CODE(mutable int recursion_count_); |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 68 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 69 | }; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 70 | |
| 71 | // CritScope, for serializing execution through a scope. |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 72 | class SCOPED_LOCKABLE CritScope { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 73 | public: |
Peter Boström | af9e663 | 2016-01-21 16:56:52 +0100 | [diff] [blame] | 74 | explicit CritScope(const CriticalSection* cs) EXCLUSIVE_LOCK_FUNCTION(cs); |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 75 | ~CritScope() UNLOCK_FUNCTION(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 76 | private: |
Peter Boström | af9e663 | 2016-01-21 16:56:52 +0100 | [diff] [blame] | 77 | const CriticalSection* const cs_; |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 78 | RTC_DISALLOW_COPY_AND_ASSIGN(CritScope); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | // Tries to lock a critical section on construction via |
| 82 | // CriticalSection::TryEnter, and unlocks on destruction if the |
| 83 | // lock was taken. Never blocks. |
| 84 | // |
| 85 | // IMPORTANT: Unlike CritScope, the lock may not be owned by this thread in |
| 86 | // subsequent code. Users *must* check locked() to determine if the |
| 87 | // lock was taken. If you're not calling locked(), you're doing it wrong! |
| 88 | class TryCritScope { |
| 89 | public: |
Peter Boström | af9e663 | 2016-01-21 16:56:52 +0100 | [diff] [blame] | 90 | explicit TryCritScope(const CriticalSection* cs); |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 91 | ~TryCritScope(); |
| 92 | #if defined(WEBRTC_WIN) |
| 93 | _Check_return_ bool locked() const; |
| 94 | #else |
kjellander | b71b4f0 | 2016-01-08 04:51:38 -0800 | [diff] [blame] | 95 | bool locked() const __attribute__ ((__warn_unused_result__)); |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 96 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 97 | private: |
Peter Boström | af9e663 | 2016-01-21 16:56:52 +0100 | [diff] [blame] | 98 | const CriticalSection* const cs_; |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 99 | const bool locked_; |
| 100 | CS_DEBUG_CODE(mutable bool lock_was_called_); |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 101 | RTC_DISALLOW_COPY_AND_ASSIGN(TryCritScope); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 104 | // A POD lock used to protect global variables. Do NOT use for other purposes. |
| 105 | // No custom constructor or private data member should be added. |
| 106 | class LOCKABLE GlobalLockPod { |
| 107 | public: |
| 108 | void Lock() EXCLUSIVE_LOCK_FUNCTION(); |
| 109 | |
| 110 | void Unlock() UNLOCK_FUNCTION(); |
| 111 | |
pbos | 46ad542 | 2015-12-07 14:29:14 -0800 | [diff] [blame] | 112 | volatile int lock_acquired; |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
pbos | 3c12f4d | 2015-11-17 03:21:02 -0800 | [diff] [blame] | 115 | class GlobalLock : public GlobalLockPod { |
| 116 | public: |
| 117 | GlobalLock(); |
| 118 | }; |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 119 | |
Joachim Bauch | fec2c6d | 2015-05-27 23:41:43 +0200 | [diff] [blame] | 120 | // GlobalLockScope, for serializing execution through a scope. |
| 121 | class SCOPED_LOCKABLE GlobalLockScope { |
| 122 | public: |
| 123 | explicit GlobalLockScope(GlobalLockPod* lock) EXCLUSIVE_LOCK_FUNCTION(lock); |
| 124 | ~GlobalLockScope() UNLOCK_FUNCTION(); |
| 125 | private: |
| 126 | GlobalLockPod* const lock_; |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 127 | RTC_DISALLOW_COPY_AND_ASSIGN(GlobalLockScope); |
Joachim Bauch | fec2c6d | 2015-05-27 23:41:43 +0200 | [diff] [blame] | 128 | }; |
| 129 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 130 | } // namespace rtc |
| 131 | |
Peter Boström | ff019b0 | 2015-04-30 14:16:07 +0200 | [diff] [blame] | 132 | #endif // WEBRTC_BASE_CRITICALSECTION_H_ |