Skip to content

Commit 50533b0

Browse files
committed
[NFC][lldb] Introduce LockGuarded type
LockGuarded is a generic wrapper around a resource to ensure exclusive access.
1 parent bb2ca20 commit 50533b0

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===----------------- LockGuarded.h ----------------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef liblldb_SwiftLockGuarded_h_
14+
#define liblldb_SwiftLockGuarded_h_
15+
16+
#include <mutex>
17+
18+
namespace lldb_private {
19+
/// A generic wrapper around a resource which holds a lock to ensure
20+
/// exclusive access.
21+
template <typename Resource> struct LockGuarded {
22+
LockGuarded(Resource *resource, std::recursive_mutex &mutex)
23+
: m_resource(resource), m_lock(mutex, std::adopt_lock) {}
24+
25+
LockGuarded() = default;
26+
27+
Resource *operator->() const { return m_resource; }
28+
29+
Resource *operator*() const { return m_resource; }
30+
31+
operator bool() const { return m_resource != nullptr; }
32+
33+
private:
34+
Resource *m_resource;
35+
std::unique_lock<std::recursive_mutex> m_lock;
36+
};
37+
38+
} // namespace lldb_private
39+
#endif

0 commit comments

Comments
 (0)