niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
kjellander | d56d68c | 2015-11-02 02:12:41 -0800 | [diff] [blame] | 11 | #ifndef WEBRTC_SYSTEM_WRAPPERS_INCLUDE_CRITICAL_SECTION_WRAPPER_H_ |
| 12 | #define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_CRITICAL_SECTION_WRAPPER_H_ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | |
tommi | 5447934 | 2016-01-22 05:26:14 -0800 | [diff] [blame] | 14 | #include "webrtc/base/criticalsection.h" |
pbos@webrtc.org | 38344ed | 2014-09-24 06:05:00 +0000 | [diff] [blame] | 15 | #include "webrtc/base/thread_annotations.h" |
phoglund@webrtc.org | 99f7c91 | 2012-11-30 10:44:49 +0000 | [diff] [blame] | 16 | #include "webrtc/common_types.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
Tommi | ee5a309 | 2016-01-19 15:42:45 +0100 | [diff] [blame] | 19 | |
andresp@webrtc.org | 7fb75ec | 2013-12-20 20:20:50 +0000 | [diff] [blame] | 20 | class LOCKABLE CriticalSectionWrapper { |
phoglund@webrtc.org | 99f7c91 | 2012-11-30 10:44:49 +0000 | [diff] [blame] | 21 | public: |
Tommi | ee5a309 | 2016-01-19 15:42:45 +0100 | [diff] [blame] | 22 | // Legacy factory method, being deprecated. Please use the constructor. |
| 23 | // TODO(tommi): Remove the CriticalSectionWrapper class and move users over |
tommi | 5447934 | 2016-01-22 05:26:14 -0800 | [diff] [blame] | 24 | // to using rtc::CriticalSection. |
| 25 | static CriticalSectionWrapper* CreateCriticalSection() { |
| 26 | return new CriticalSectionWrapper(); |
| 27 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 28 | |
tommi | 5447934 | 2016-01-22 05:26:14 -0800 | [diff] [blame] | 29 | CriticalSectionWrapper() {} |
| 30 | ~CriticalSectionWrapper() {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 31 | |
phoglund@webrtc.org | 99f7c91 | 2012-11-30 10:44:49 +0000 | [diff] [blame] | 32 | // Tries to grab lock, beginning of a critical section. Will wait for the |
| 33 | // lock to become available if the grab failed. |
tommi | 5447934 | 2016-01-22 05:26:14 -0800 | [diff] [blame] | 34 | void Enter() EXCLUSIVE_LOCK_FUNCTION() { lock_.Enter(); } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 35 | |
phoglund@webrtc.org | 99f7c91 | 2012-11-30 10:44:49 +0000 | [diff] [blame] | 36 | // Returns a grabbed lock, end of critical section. |
tommi | 5447934 | 2016-01-22 05:26:14 -0800 | [diff] [blame] | 37 | void Leave() UNLOCK_FUNCTION() { lock_.Leave(); } |
Tommi | ee5a309 | 2016-01-19 15:42:45 +0100 | [diff] [blame] | 38 | |
tommi | 5447934 | 2016-01-22 05:26:14 -0800 | [diff] [blame] | 39 | private: |
| 40 | rtc::CriticalSection lock_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
mflodman@webrtc.org | b19582b | 2011-12-09 10:02:16 +0000 | [diff] [blame] | 43 | // RAII extension of the critical section. Prevents Enter/Leave mismatches and |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 44 | // provides more compact critical section syntax. |
andresp@webrtc.org | 7fb75ec | 2013-12-20 20:20:50 +0000 | [diff] [blame] | 45 | class SCOPED_LOCKABLE CriticalSectionScoped { |
phoglund@webrtc.org | 99f7c91 | 2012-11-30 10:44:49 +0000 | [diff] [blame] | 46 | public: |
| 47 | explicit CriticalSectionScoped(CriticalSectionWrapper* critsec) |
andresp@webrtc.org | 7fb75ec | 2013-12-20 20:20:50 +0000 | [diff] [blame] | 48 | EXCLUSIVE_LOCK_FUNCTION(critsec) |
| 49 | : ptr_crit_sec_(critsec) { |
phoglund@webrtc.org | 99f7c91 | 2012-11-30 10:44:49 +0000 | [diff] [blame] | 50 | ptr_crit_sec_->Enter(); |
| 51 | } |
mflodman@webrtc.org | b19582b | 2011-12-09 10:02:16 +0000 | [diff] [blame] | 52 | |
andresp@webrtc.org | 7fb75ec | 2013-12-20 20:20:50 +0000 | [diff] [blame] | 53 | ~CriticalSectionScoped() UNLOCK_FUNCTION() { ptr_crit_sec_->Leave(); } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | |
phoglund@webrtc.org | 99f7c91 | 2012-11-30 10:44:49 +0000 | [diff] [blame] | 55 | private: |
phoglund@webrtc.org | 99f7c91 | 2012-11-30 10:44:49 +0000 | [diff] [blame] | 56 | CriticalSectionWrapper* ptr_crit_sec_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 57 | }; |
phoglund@webrtc.org | 99f7c91 | 2012-11-30 10:44:49 +0000 | [diff] [blame] | 58 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 59 | } // namespace webrtc |
phoglund@webrtc.org | 99f7c91 | 2012-11-30 10:44:49 +0000 | [diff] [blame] | 60 | |
kjellander | d56d68c | 2015-11-02 02:12:41 -0800 | [diff] [blame] | 61 | #endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_CRITICAL_SECTION_WRAPPER_H_ |