blob: 4979b5c7dd566f29690dea794a535c14039ca893 [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
11#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_CRITICAL_SECTION_WRAPPER_H_
12#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_CRITICAL_SECTION_WRAPPER_H_
13
14// If the critical section is heavily contended it may be beneficial to use
15// read/write locks instead.
16
phoglund@webrtc.org99f7c912012-11-30 10:44:49 +000017#include "webrtc/common_types.h"
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000018#include "webrtc/system_wrappers/interface/thread_annotations.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000019
20namespace webrtc {
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000021class LOCKABLE CriticalSectionWrapper {
phoglund@webrtc.org99f7c912012-11-30 10:44:49 +000022 public:
23 // Factory method, constructor disabled
24 static CriticalSectionWrapper* CreateCriticalSection();
niklase@google.com470e71d2011-07-07 08:21:25 +000025
phoglund@webrtc.org99f7c912012-11-30 10:44:49 +000026 virtual ~CriticalSectionWrapper() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000027
phoglund@webrtc.org99f7c912012-11-30 10:44:49 +000028 // Tries to grab lock, beginning of a critical section. Will wait for the
29 // lock to become available if the grab failed.
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000030 virtual void Enter() EXCLUSIVE_LOCK_FUNCTION() = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000031
phoglund@webrtc.org99f7c912012-11-30 10:44:49 +000032 // Returns a grabbed lock, end of critical section.
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000033 virtual void Leave() UNLOCK_FUNCTION() = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000034};
35
mflodman@webrtc.orgb19582b2011-12-09 10:02:16 +000036// RAII extension of the critical section. Prevents Enter/Leave mismatches and
niklase@google.com470e71d2011-07-07 08:21:25 +000037// provides more compact critical section syntax.
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000038class SCOPED_LOCKABLE CriticalSectionScoped {
phoglund@webrtc.org99f7c912012-11-30 10:44:49 +000039 public:
40 explicit CriticalSectionScoped(CriticalSectionWrapper* critsec)
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000041 EXCLUSIVE_LOCK_FUNCTION(critsec)
42 : ptr_crit_sec_(critsec) {
phoglund@webrtc.org99f7c912012-11-30 10:44:49 +000043 ptr_crit_sec_->Enter();
44 }
mflodman@webrtc.orgb19582b2011-12-09 10:02:16 +000045
andresp@webrtc.org7fb75ec2013-12-20 20:20:50 +000046 ~CriticalSectionScoped() UNLOCK_FUNCTION() { ptr_crit_sec_->Leave(); }
niklase@google.com470e71d2011-07-07 08:21:25 +000047
phoglund@webrtc.org99f7c912012-11-30 10:44:49 +000048 private:
phoglund@webrtc.org99f7c912012-11-30 10:44:49 +000049 CriticalSectionWrapper* ptr_crit_sec_;
niklase@google.com470e71d2011-07-07 08:21:25 +000050};
phoglund@webrtc.org99f7c912012-11-30 10:44:49 +000051
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000052} // namespace webrtc
phoglund@webrtc.org99f7c912012-11-30 10:44:49 +000053
54#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_CRITICAL_SECTION_WRAPPER_H_