Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: webrtc/base/criticalsection.h

Issue 2877023002: Move webrtc/{base => rtc_base} (Closed)
Patch Set: update presubmit.py and DEPS include rules Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/base/crc32_unittest.cc ('k') | webrtc/base/criticalsection.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #ifndef WEBRTC_BASE_CRITICALSECTION_H_ 11 #ifndef WEBRTC_BASE_CRITICALSECTION_H_
12 #define WEBRTC_BASE_CRITICALSECTION_H_ 12 #define WEBRTC_BASE_CRITICALSECTION_H_
13 13
14 #include "webrtc/base/atomicops.h" 14 // This header is deprecated and is just left here temporarily during
15 #include "webrtc/base/checks.h" 15 // refactoring. See https://bugs.webrtc.org/7634 for more details.
16 #include "webrtc/base/constructormagic.h" 16 #include "webrtc/rtc_base/criticalsection.h"
17 #include "webrtc/base/platform_thread_types.h"
18 #include "webrtc/base/thread_annotations.h"
19 #include "webrtc/typedefs.h"
20
21 #if defined(WEBRTC_WIN)
22 // Include winsock2.h before including <windows.h> to maintain consistency with
23 // win32.h. We can't include win32.h directly here since it pulls in
24 // headers such as basictypes.h which causes problems in Chromium where webrtc
25 // exists as two separate projects, webrtc and libjingle.
26 #include <winsock2.h>
27 #include <windows.h>
28 #include <sal.h> // must come after windows headers.
29 #endif // defined(WEBRTC_WIN)
30
31 #if defined(WEBRTC_POSIX)
32 #include <pthread.h>
33 #endif
34
35 // See notes in the 'Performance' unit test for the effects of this flag.
36 #define USE_NATIVE_MUTEX_ON_MAC 0
37
38 #if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC
39 #include <dispatch/dispatch.h>
40 #endif
41
42 #define CS_DEBUG_CHECKS RTC_DCHECK_IS_ON
43
44 #if CS_DEBUG_CHECKS
45 #define CS_DEBUG_CODE(x) x
46 #else // !CS_DEBUG_CHECKS
47 #define CS_DEBUG_CODE(x)
48 #endif // !CS_DEBUG_CHECKS
49
50 namespace rtc {
51
52 // Locking methods (Enter, TryEnter, Leave)are const to permit protecting
53 // members inside a const context without requiring mutable CriticalSections
54 // everywhere.
55 class LOCKABLE CriticalSection {
56 public:
57 CriticalSection();
58 ~CriticalSection();
59
60 void Enter() const EXCLUSIVE_LOCK_FUNCTION();
61 bool TryEnter() const EXCLUSIVE_TRYLOCK_FUNCTION(true);
62 void Leave() const UNLOCK_FUNCTION();
63
64 private:
65 // Use only for RTC_DCHECKing.
66 bool CurrentThreadIsOwner() const;
67
68 #if defined(WEBRTC_WIN)
69 mutable CRITICAL_SECTION crit_;
70 #elif defined(WEBRTC_POSIX)
71 # if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC
72 // Number of times the lock has been locked + number of threads waiting.
73 // TODO(tommi): We could use this number and subtract the recursion count
74 // to find places where we have multiple threads contending on the same lock.
75 mutable volatile int lock_queue_;
76 // |recursion_| represents the recursion count + 1 for the thread that owns
77 // the lock. Only modified by the thread that owns the lock.
78 mutable int recursion_;
79 // Used to signal a single waiting thread when the lock becomes available.
80 mutable dispatch_semaphore_t semaphore_;
81 // The thread that currently holds the lock. Required to handle recursion.
82 mutable PlatformThreadRef owning_thread_;
83 # else
84 mutable pthread_mutex_t mutex_;
85 # endif
86 mutable PlatformThreadRef thread_; // Only used by RTC_DCHECKs.
87 mutable int recursion_count_; // Only used by RTC_DCHECKs.
88 #else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX)
89 # error Unsupported platform.
90 #endif
91 };
92
93 // CritScope, for serializing execution through a scope.
94 class SCOPED_LOCKABLE CritScope {
95 public:
96 explicit CritScope(const CriticalSection* cs) EXCLUSIVE_LOCK_FUNCTION(cs);
97 ~CritScope() UNLOCK_FUNCTION();
98 private:
99 const CriticalSection* const cs_;
100 RTC_DISALLOW_COPY_AND_ASSIGN(CritScope);
101 };
102
103 // Tries to lock a critical section on construction via
104 // CriticalSection::TryEnter, and unlocks on destruction if the
105 // lock was taken. Never blocks.
106 //
107 // IMPORTANT: Unlike CritScope, the lock may not be owned by this thread in
108 // subsequent code. Users *must* check locked() to determine if the
109 // lock was taken. If you're not calling locked(), you're doing it wrong!
110 class TryCritScope {
111 public:
112 explicit TryCritScope(const CriticalSection* cs);
113 ~TryCritScope();
114 #if defined(WEBRTC_WIN)
115 _Check_return_ bool locked() const;
116 #elif defined(WEBRTC_POSIX)
117 bool locked() const __attribute__ ((__warn_unused_result__));
118 #else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX)
119 # error Unsupported platform.
120 #endif
121 private:
122 const CriticalSection* const cs_;
123 const bool locked_;
124 mutable bool lock_was_called_; // Only used by RTC_DCHECKs.
125 RTC_DISALLOW_COPY_AND_ASSIGN(TryCritScope);
126 };
127
128 // A POD lock used to protect global variables. Do NOT use for other purposes.
129 // No custom constructor or private data member should be added.
130 class LOCKABLE GlobalLockPod {
131 public:
132 void Lock() EXCLUSIVE_LOCK_FUNCTION();
133
134 void Unlock() UNLOCK_FUNCTION();
135
136 volatile int lock_acquired;
137 };
138
139 class GlobalLock : public GlobalLockPod {
140 public:
141 GlobalLock();
142 };
143
144 // GlobalLockScope, for serializing execution through a scope.
145 class SCOPED_LOCKABLE GlobalLockScope {
146 public:
147 explicit GlobalLockScope(GlobalLockPod* lock) EXCLUSIVE_LOCK_FUNCTION(lock);
148 ~GlobalLockScope() UNLOCK_FUNCTION();
149 private:
150 GlobalLockPod* const lock_;
151 RTC_DISALLOW_COPY_AND_ASSIGN(GlobalLockScope);
152 };
153
154 } // namespace rtc
155 17
156 #endif // WEBRTC_BASE_CRITICALSECTION_H_ 18 #endif // WEBRTC_BASE_CRITICALSECTION_H_
OLDNEW
« no previous file with comments | « webrtc/base/crc32_unittest.cc ('k') | webrtc/base/criticalsection.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698