
Expose u target API mutex through the SB API. This is motivated by lldb-dap, which is built on top of the SB API and needs a way to execute a series of SB API calls in an atomic manner (see #131242). We can solve this problem by either introducing an additional layer of locking at the DAP level or by exposing the existing locking at the SB API level. This patch implements the second approach. This was discussed in an RFC on Discourse [0]. The original implementation exposed a move-only lock rather than a mutex [1] which doesn't work well with SWIG 4.0 [2]. This implement the alternative solution of exposing the mutex rather than the lock. The SBMutex conforms to the BasicLockable requirement [3] (which is why the methods are called `lock` and `unlock` rather than Lock and Unlock) so it can be used as `std::lock_guard<lldb::SBMutex>` and `std::unique_lock<lldb::SBMutex>`. [0]: https://discourse.llvm.org/t/rfc-exposing-the-target-api-lock-through-the-sb-api/85215/6 [1]: https://github.com/llvm/llvm-project/pull/131404 [2]: https://discourse.llvm.org/t/rfc-bumping-the-minimum-swig-version-to-4-1-0/85377/9 [3]: https://en.cppreference.com/w/cpp/named_req/BasicLockable
61 lines
1.5 KiB
C++
61 lines
1.5 KiB
C++
//===-- SBMutex.cpp -------------------------------------------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "lldb/API/SBMutex.h"
|
|
#include "lldb/Target/Target.h"
|
|
#include "lldb/Utility/Instrumentation.h"
|
|
#include "lldb/lldb-forward.h"
|
|
#include <memory>
|
|
#include <mutex>
|
|
|
|
using namespace lldb;
|
|
using namespace lldb_private;
|
|
|
|
SBMutex::SBMutex() : m_opaque_sp(std::make_shared<std::recursive_mutex>()) {
|
|
LLDB_INSTRUMENT_VA(this);
|
|
}
|
|
|
|
SBMutex::SBMutex(const SBMutex &rhs) : m_opaque_sp(rhs.m_opaque_sp) {
|
|
LLDB_INSTRUMENT_VA(this);
|
|
}
|
|
|
|
const SBMutex &SBMutex::operator=(const SBMutex &rhs) {
|
|
LLDB_INSTRUMENT_VA(this);
|
|
|
|
m_opaque_sp = rhs.m_opaque_sp;
|
|
return *this;
|
|
}
|
|
|
|
SBMutex::SBMutex(lldb::TargetSP target_sp)
|
|
: m_opaque_sp(std::shared_ptr<std::recursive_mutex>(
|
|
target_sp, &target_sp->GetAPIMutex())) {
|
|
LLDB_INSTRUMENT_VA(this, target_sp);
|
|
}
|
|
|
|
SBMutex::~SBMutex() { LLDB_INSTRUMENT_VA(this); }
|
|
|
|
bool SBMutex::IsValid() const {
|
|
LLDB_INSTRUMENT_VA(this);
|
|
|
|
return static_cast<bool>(m_opaque_sp);
|
|
}
|
|
|
|
void SBMutex::lock() const {
|
|
LLDB_INSTRUMENT_VA(this);
|
|
|
|
if (m_opaque_sp)
|
|
m_opaque_sp->lock();
|
|
}
|
|
|
|
void SBMutex::unlock() const {
|
|
LLDB_INSTRUMENT_VA(this);
|
|
|
|
if (m_opaque_sp)
|
|
m_opaque_sp->unlock();
|
|
}
|