OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2011 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_SYSTEM_WRAPPERS_INCLUDE_CRITICAL_SECTION_WRAPPER_H_ | 11 #ifndef WEBRTC_SYSTEM_WRAPPERS_INCLUDE_CRITICAL_SECTION_WRAPPER_H_ |
12 #define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_CRITICAL_SECTION_WRAPPER_H_ | 12 #define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_CRITICAL_SECTION_WRAPPER_H_ |
13 | 13 |
14 // If the critical section is heavily contended it may be beneficial to use | 14 #include "webrtc/base/criticalsection.h" |
15 // read/write locks instead. | |
16 | |
17 #if defined (WEBRTC_WIN) | |
18 #include <windows.h> | |
19 #else | |
20 #include <pthread.h> | |
21 #endif | |
22 | |
23 #include "webrtc/base/thread_annotations.h" | 15 #include "webrtc/base/thread_annotations.h" |
24 #include "webrtc/common_types.h" | 16 #include "webrtc/common_types.h" |
25 | 17 |
26 namespace webrtc { | 18 namespace webrtc { |
27 | 19 |
28 class LOCKABLE CriticalSectionWrapper { | 20 class LOCKABLE CriticalSectionWrapper { |
29 public: | 21 public: |
30 // Legacy factory method, being deprecated. Please use the constructor. | 22 // Legacy factory method, being deprecated. Please use the constructor. |
31 // TODO(tommi): Remove the CriticalSectionWrapper class and move users over | 23 // TODO(tommi): Remove the CriticalSectionWrapper class and move users over |
32 // to using rtc::CriticalSection. Before we can do that though, we need to | 24 // to using rtc::CriticalSection. |
33 // fix the problem with the ConditionVariable* classes (see below). | 25 static CriticalSectionWrapper* CreateCriticalSection() { |
34 static CriticalSectionWrapper* CreateCriticalSection(); | 26 return new CriticalSectionWrapper(); |
| 27 } |
35 | 28 |
36 CriticalSectionWrapper(); | 29 CriticalSectionWrapper() {} |
37 ~CriticalSectionWrapper(); | 30 ~CriticalSectionWrapper() {} |
38 | 31 |
39 // Tries to grab lock, beginning of a critical section. Will wait for the | 32 // Tries to grab lock, beginning of a critical section. Will wait for the |
40 // lock to become available if the grab failed. | 33 // lock to become available if the grab failed. |
41 void Enter() EXCLUSIVE_LOCK_FUNCTION(); | 34 void Enter() EXCLUSIVE_LOCK_FUNCTION() { lock_.Enter(); } |
42 | 35 |
43 // Returns a grabbed lock, end of critical section. | 36 // Returns a grabbed lock, end of critical section. |
44 void Leave() UNLOCK_FUNCTION(); | 37 void Leave() UNLOCK_FUNCTION() { lock_.Leave(); } |
45 | 38 |
46 private: | 39 private: |
47 #if defined (WEBRTC_WIN) | 40 rtc::CriticalSection lock_; |
48 CRITICAL_SECTION crit_; | |
49 #else | |
50 pthread_mutex_t mutex_; | |
51 #endif | |
52 }; | 41 }; |
53 | 42 |
54 // RAII extension of the critical section. Prevents Enter/Leave mismatches and | 43 // RAII extension of the critical section. Prevents Enter/Leave mismatches and |
55 // provides more compact critical section syntax. | 44 // provides more compact critical section syntax. |
56 class SCOPED_LOCKABLE CriticalSectionScoped { | 45 class SCOPED_LOCKABLE CriticalSectionScoped { |
57 public: | 46 public: |
58 explicit CriticalSectionScoped(CriticalSectionWrapper* critsec) | 47 explicit CriticalSectionScoped(CriticalSectionWrapper* critsec) |
59 EXCLUSIVE_LOCK_FUNCTION(critsec) | 48 EXCLUSIVE_LOCK_FUNCTION(critsec) |
60 : ptr_crit_sec_(critsec) { | 49 : ptr_crit_sec_(critsec) { |
61 ptr_crit_sec_->Enter(); | 50 ptr_crit_sec_->Enter(); |
62 } | 51 } |
63 | 52 |
64 ~CriticalSectionScoped() UNLOCK_FUNCTION() { ptr_crit_sec_->Leave(); } | 53 ~CriticalSectionScoped() UNLOCK_FUNCTION() { ptr_crit_sec_->Leave(); } |
65 | 54 |
66 private: | 55 private: |
67 CriticalSectionWrapper* ptr_crit_sec_; | 56 CriticalSectionWrapper* ptr_crit_sec_; |
68 }; | 57 }; |
69 | 58 |
70 } // namespace webrtc | 59 } // namespace webrtc |
71 | 60 |
72 #endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_CRITICAL_SECTION_WRAPPER_H_ | 61 #endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_CRITICAL_SECTION_WRAPPER_H_ |
OLD | NEW |