Line data Source code
1 : /*
2 : * Copyright (c) 2011 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_SYSTEM_WRAPPERS_INCLUDE_CRITICAL_SECTION_WRAPPER_H_
12 : #define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_CRITICAL_SECTION_WRAPPER_H_
13 :
14 : #include "webrtc/base/criticalsection.h"
15 : #include "webrtc/base/thread_annotations.h"
16 : #include "webrtc/common_types.h"
17 :
18 : namespace webrtc {
19 :
20 : class LOCKABLE CriticalSectionWrapper {
21 : public:
22 : // Legacy factory method, being deprecated. Please use the constructor.
23 : // TODO(tommi): Remove the CriticalSectionWrapper class and move users over
24 : // to using rtc::CriticalSection.
25 0 : static CriticalSectionWrapper* CreateCriticalSection() {
26 0 : return new CriticalSectionWrapper();
27 : }
28 :
29 0 : CriticalSectionWrapper() {}
30 0 : ~CriticalSectionWrapper() {}
31 :
32 : // Tries to grab lock, beginning of a critical section. Will wait for the
33 : // lock to become available if the grab failed.
34 0 : void Enter() EXCLUSIVE_LOCK_FUNCTION() { lock_.Enter(); }
35 :
36 : // Returns a grabbed lock, end of critical section.
37 0 : void Leave() UNLOCK_FUNCTION() { lock_.Leave(); }
38 :
39 : private:
40 : rtc::CriticalSection lock_;
41 : };
42 :
43 : // RAII extension of the critical section. Prevents Enter/Leave mismatches and
44 : // provides more compact critical section syntax.
45 : class SCOPED_LOCKABLE CriticalSectionScoped {
46 : public:
47 0 : explicit CriticalSectionScoped(CriticalSectionWrapper* critsec)
48 : EXCLUSIVE_LOCK_FUNCTION(critsec)
49 0 : : ptr_crit_sec_(critsec) {
50 0 : ptr_crit_sec_->Enter();
51 0 : }
52 :
53 0 : ~CriticalSectionScoped() UNLOCK_FUNCTION() { ptr_crit_sec_->Leave(); }
54 :
55 : private:
56 : CriticalSectionWrapper* ptr_crit_sec_;
57 : };
58 :
59 : } // namespace webrtc
60 :
61 : #endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_CRITICAL_SECTION_WRAPPER_H_
|