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 | |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 44 | class LOCKABLE CriticalSection { |
| 45 | public: |
| 46 | CriticalSection(); |
| 47 | ~CriticalSection(); |
| 48 | |
| 49 | void Enter() EXCLUSIVE_LOCK_FUNCTION(); |
| 50 | bool TryEnter() EXCLUSIVE_TRYLOCK_FUNCTION(true); |
| 51 | void Leave() UNLOCK_FUNCTION(); |
| 52 | |
| 53 | // Use only for DCHECKing. |
| 54 | bool CurrentThreadIsOwner() const; |
| 55 | // Use only for DCHECKing. |
| 56 | bool IsLocked() const; |
| 57 | |
| 58 | private: |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 59 | #if defined(WEBRTC_WIN) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 60 | CRITICAL_SECTION crit_; |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 61 | #elif defined(WEBRTC_POSIX) |
tommi@webrtc.org | a32f064 | 2015-03-08 07:38:31 +0000 | [diff] [blame] | 62 | pthread_mutex_t mutex_; |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 63 | CS_DEBUG_CODE(pthread_t thread_); |
| 64 | CS_DEBUG_CODE(int recursion_count_); |
| 65 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 66 | }; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 67 | |
| 68 | // CritScope, for serializing execution through a scope. |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 69 | class SCOPED_LOCKABLE CritScope { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 70 | public: |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 71 | explicit CritScope(CriticalSection* cs) EXCLUSIVE_LOCK_FUNCTION(cs); |
| 72 | ~CritScope() UNLOCK_FUNCTION(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 73 | private: |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 74 | CriticalSection* const cs_; |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame^] | 75 | RTC_DISALLOW_COPY_AND_ASSIGN(CritScope); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | // Tries to lock a critical section on construction via |
| 79 | // CriticalSection::TryEnter, and unlocks on destruction if the |
| 80 | // lock was taken. Never blocks. |
| 81 | // |
| 82 | // IMPORTANT: Unlike CritScope, the lock may not be owned by this thread in |
| 83 | // subsequent code. Users *must* check locked() to determine if the |
| 84 | // lock was taken. If you're not calling locked(), you're doing it wrong! |
| 85 | class TryCritScope { |
| 86 | public: |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 87 | explicit TryCritScope(CriticalSection* cs); |
| 88 | ~TryCritScope(); |
| 89 | #if defined(WEBRTC_WIN) |
| 90 | _Check_return_ bool locked() const; |
| 91 | #else |
| 92 | bool locked() const __attribute__((warn_unused_result)); |
| 93 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 94 | private: |
Tommi | 494f209 | 2015-04-27 17:39:23 +0200 | [diff] [blame] | 95 | CriticalSection* const cs_; |
| 96 | const bool locked_; |
| 97 | CS_DEBUG_CODE(mutable bool lock_was_called_); |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame^] | 98 | RTC_DISALLOW_COPY_AND_ASSIGN(TryCritScope); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 101 | // A POD lock used to protect global variables. Do NOT use for other purposes. |
| 102 | // No custom constructor or private data member should be added. |
| 103 | class LOCKABLE GlobalLockPod { |
| 104 | public: |
| 105 | void Lock() EXCLUSIVE_LOCK_FUNCTION(); |
| 106 | |
| 107 | void Unlock() UNLOCK_FUNCTION(); |
| 108 | |
| 109 | volatile int lock_acquired; |
| 110 | }; |
| 111 | |
| 112 | class GlobalLock : public GlobalLockPod { |
| 113 | public: |
| 114 | GlobalLock(); |
| 115 | }; |
| 116 | |
Joachim Bauch | fec2c6d | 2015-05-27 23:41:43 +0200 | [diff] [blame] | 117 | // GlobalLockScope, for serializing execution through a scope. |
| 118 | class SCOPED_LOCKABLE GlobalLockScope { |
| 119 | public: |
| 120 | explicit GlobalLockScope(GlobalLockPod* lock) EXCLUSIVE_LOCK_FUNCTION(lock); |
| 121 | ~GlobalLockScope() UNLOCK_FUNCTION(); |
| 122 | private: |
| 123 | GlobalLockPod* const lock_; |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame^] | 124 | RTC_DISALLOW_COPY_AND_ASSIGN(GlobalLockScope); |
Joachim Bauch | fec2c6d | 2015-05-27 23:41:43 +0200 | [diff] [blame] | 125 | }; |
| 126 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 127 | } // namespace rtc |
| 128 | |
Peter Boström | ff019b0 | 2015-04-30 14:16:07 +0200 | [diff] [blame] | 129 | #endif // WEBRTC_BASE_CRITICALSECTION_H_ |