blob: 0858b4f6d1a58ad9f3d6c7dceccfa3fec4fe22c5 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
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
kjellanderd56d68c2015-11-02 02:12:41 -080011#ifndef WEBRTC_SYSTEM_WRAPPERS_INCLUDE_CRITICAL_SECTION_WRAPPER_H_
12#define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_CRITICAL_SECTION_WRAPPER_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
tommi54479342016-01-22 05:26:14 -080014#include "webrtc/base/criticalsection.h"
pbos@webrtc.org38344ed2014-09-24 06:05:00 +000015#include "webrtc/base/thread_annotations.h"
phoglund@webrtc.org99f7c912012-11-30 10:44:49 +000016#include "webrtc/common_types.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000017
18namespace webrtc {
Tommiee5a3092016-01-19 15:42:45 +010019
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000020class LOCKABLE CriticalSectionWrapper {
phoglund@webrtc.org99f7c912012-11-30 10:44:49 +000021 public:
Tommiee5a3092016-01-19 15:42:45 +010022 // Legacy factory method, being deprecated. Please use the constructor.
23 // TODO(tommi): Remove the CriticalSectionWrapper class and move users over
tommi54479342016-01-22 05:26:14 -080024 // to using rtc::CriticalSection.
25 static CriticalSectionWrapper* CreateCriticalSection() {
26 return new CriticalSectionWrapper();
27 }
niklase@google.com470e71d2011-07-07 08:21:25 +000028
tommi54479342016-01-22 05:26:14 -080029 CriticalSectionWrapper() {}
30 ~CriticalSectionWrapper() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000031
phoglund@webrtc.org99f7c912012-11-30 10:44:49 +000032 // Tries to grab lock, beginning of a critical section. Will wait for the
33 // lock to become available if the grab failed.
tommi54479342016-01-22 05:26:14 -080034 void Enter() EXCLUSIVE_LOCK_FUNCTION() { lock_.Enter(); }
niklase@google.com470e71d2011-07-07 08:21:25 +000035
phoglund@webrtc.org99f7c912012-11-30 10:44:49 +000036 // Returns a grabbed lock, end of critical section.
tommi54479342016-01-22 05:26:14 -080037 void Leave() UNLOCK_FUNCTION() { lock_.Leave(); }
Tommiee5a3092016-01-19 15:42:45 +010038
tommi54479342016-01-22 05:26:14 -080039 private:
40 rtc::CriticalSection lock_;
niklase@google.com470e71d2011-07-07 08:21:25 +000041};
42
mflodman@webrtc.orgb19582b2011-12-09 10:02:16 +000043// RAII extension of the critical section. Prevents Enter/Leave mismatches and
niklase@google.com470e71d2011-07-07 08:21:25 +000044// provides more compact critical section syntax.
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000045class SCOPED_LOCKABLE CriticalSectionScoped {
phoglund@webrtc.org99f7c912012-11-30 10:44:49 +000046 public:
47 explicit CriticalSectionScoped(CriticalSectionWrapper* critsec)
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000048 EXCLUSIVE_LOCK_FUNCTION(critsec)
49 : ptr_crit_sec_(critsec) {
phoglund@webrtc.org99f7c912012-11-30 10:44:49 +000050 ptr_crit_sec_->Enter();
51 }
mflodman@webrtc.orgb19582b2011-12-09 10:02:16 +000052
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000053 ~CriticalSectionScoped() UNLOCK_FUNCTION() { ptr_crit_sec_->Leave(); }
niklase@google.com470e71d2011-07-07 08:21:25 +000054
phoglund@webrtc.org99f7c912012-11-30 10:44:49 +000055 private:
phoglund@webrtc.org99f7c912012-11-30 10:44:49 +000056 CriticalSectionWrapper* ptr_crit_sec_;
niklase@google.com470e71d2011-07-07 08:21:25 +000057};
phoglund@webrtc.org99f7c912012-11-30 10:44:49 +000058
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000059} // namespace webrtc
phoglund@webrtc.org99f7c912012-11-30 10:44:49 +000060
kjellanderd56d68c2015-11-02 02:12:41 -080061#endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_CRITICAL_SECTION_WRAPPER_H_