LLDB mainline
SBMutex.cpp
Go to the documentation of this file.
1//===-- SBMutex.cpp -------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/API/SBMutex.h"
12#include "lldb/lldb-forward.h"
13#include <memory>
14#include <mutex>
15#include <variant>
16
17using namespace lldb;
18using namespace lldb_private;
19
20/// Holds either a standalone std::recursive_mutex (default-constructed
21/// SBMutex, no Target to resolve through) or a TargetAPIMutex (constructed
22/// from a Target). Kept out of SBMutex.h since std::variant is a C++17
23/// feature and the public SB headers must stay usable from a C++11 client.
25public:
26 MutexVariant() : m_variant(std::in_place_type<std::recursive_mutex>) {}
27 explicit MutexVariant(lldb::TargetSP target_sp)
28 : m_variant(std::in_place_type<TargetAPIMutex>, std::move(target_sp)) {}
29
30 void lock() {
31 std::visit([](auto &mutex) { mutex.lock(); }, m_variant);
32 }
33 void unlock() {
34 std::visit([](auto &mutex) { mutex.unlock(); }, m_variant);
35 }
36 bool try_lock() {
37 return std::visit([](auto &mutex) { return mutex.try_lock(); }, m_variant);
38 }
39
40private:
41 std::variant<std::recursive_mutex, TargetAPIMutex> m_variant;
42};
43
44SBMutex::SBMutex() : m_opaque_sp(std::make_shared<MutexVariant>()) {
46}
47
51
54
56 return *this;
57}
58
60 : m_opaque_sp(std::make_shared<MutexVariant>(target_sp)) {
61 LLDB_INSTRUMENT_VA(this, target_sp);
62}
63
65
66bool SBMutex::IsValid() const {
68
69 return static_cast<bool>(m_opaque_sp);
70}
71
72void SBMutex::lock() const {
74
75 if (m_opaque_sp)
76 m_opaque_sp->lock();
77}
78
79void SBMutex::unlock() const {
81
82 if (m_opaque_sp)
83 m_opaque_sp->unlock();
84}
85
86bool SBMutex::try_lock() const {
88
89 if (!m_opaque_sp)
90 return false;
91 return m_opaque_sp->try_lock();
92}
#define LLDB_INSTRUMENT_VA(...)
Holds either a standalone std::recursive_mutex (default-constructed SBMutex, no Target to resolve thr...
Definition SBMutex.cpp:24
std::variant< std::recursive_mutex, TargetAPIMutex > m_variant
Definition SBMutex.cpp:41
MutexVariant(lldb::TargetSP target_sp)
Definition SBMutex.cpp:27
bool IsValid() const
Returns true if this lock has ownership of the underlying mutex.
Definition SBMutex.cpp:66
void unlock() const
Releases ownership of this lock.
Definition SBMutex.cpp:79
bool try_lock() const
Tries to lock the mutex.
Definition SBMutex.cpp:86
std::shared_ptr< MutexVariant > m_opaque_sp
Definition SBMutex.h:45
const SBMutex & operator=(const SBMutex &rhs)
Definition SBMutex.cpp:52
void lock() const
Blocking operation that takes ownership of this lock.
Definition SBMutex.cpp:72
A Lockable handle over a Target's API mutex, returned by Target::GetAPIMutex() and backing the public...
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::Target > TargetSP