mirror of
https://github.com/siemens-mobile-hacks/llvm-c166.git
synced 2026-11-26 06:28:13 +00:00
While implementing #208242, we realized that we needed a Lockable wrapper for the Target's API Mutex that could skip the locking on re-entrant threads (when a command (i.e `bt`) triggers scripted extension (i.e `ScriptedFrameProvider`) that uses SBAPI (i.e. `SBFrame`), and still behave as a normal mutex otherwise. However, since `Target::GetAPIMutex()` returns a `std::recursive_mutex&`, that can't represent "no synchronization at all". This is why this PR introduces `TargetAPILock`, a small Lockable type that behaves exactly like `std::recursive_mutex`, with no RAII of its own, so callers can wrap it in `std::lock_guard`/`std::unique_lock` like they would any other Lockable. A `TargetAPILock` is bound to a `Target` rather than to a specific mutex, so `lock()`/`try_lock()` resolve which real mutex to use fresh on every call instead of caching one resolution for the handle's lifetime. `unlock()` replays whatever the matching `lock()`/`try_lock()` resolved rather than re-resolving, so a policy change between the two calls can't release the wrong mutex. That's also what lets a handle be constructed on one thread and locked/unlocked on another, which the deferred-lock `SBMutex` needs. `GetAPIMutex()` now returns `TargetAPILock` by value, so every caller that took its old `std::recursive_mutex&` materializes the handle into a local and wraps that in a `lock_guard`/`unique_lock` instead. A few places where the lock outlives its constructing statement (`CommandObject`'s `m_api_locker`, `ValueLocker`, `StoppedExecutionContext`) keep the resolved handle as a member and release it explicitly instead. `SBMutex`'s opaque member becomes a `std::variant<std::recursive_mutex, TargetAPILock>` rather than two separate members, so the no-target case still owns a real mutex directly without growing `SBMutex`'s size across the ABI boundary. Signed-off-by: Med Ismail Bennani <ismail@bennani.ma>
92 lines
2.4 KiB
C++
92 lines
2.4 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/TargetAPIMutex.h"
|
|
#include "lldb/Utility/Instrumentation.h"
|
|
#include "lldb/lldb-forward.h"
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <variant>
|
|
|
|
using namespace lldb;
|
|
using namespace lldb_private;
|
|
|
|
/// Holds either a standalone std::recursive_mutex (default-constructed
|
|
/// SBMutex, no Target to resolve through) or a TargetAPIMutex (constructed
|
|
/// from a Target). Kept out of SBMutex.h since std::variant is a C++17
|
|
/// feature and the public SB headers must stay usable from a C++11 client.
|
|
class SBMutex::MutexVariant {
|
|
public:
|
|
MutexVariant() : m_variant(std::in_place_type<std::recursive_mutex>) {}
|
|
explicit MutexVariant(lldb::TargetSP target_sp)
|
|
: m_variant(std::in_place_type<TargetAPIMutex>, std::move(target_sp)) {}
|
|
|
|
void lock() {
|
|
std::visit([](auto &mutex) { mutex.lock(); }, m_variant);
|
|
}
|
|
void unlock() {
|
|
std::visit([](auto &mutex) { mutex.unlock(); }, m_variant);
|
|
}
|
|
bool try_lock() {
|
|
return std::visit([](auto &mutex) { return mutex.try_lock(); }, m_variant);
|
|
}
|
|
|
|
private:
|
|
std::variant<std::recursive_mutex, TargetAPIMutex> m_variant;
|
|
};
|
|
|
|
SBMutex::SBMutex() : m_opaque_sp(std::make_shared<MutexVariant>()) {
|
|
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::make_shared<MutexVariant>(target_sp)) {
|
|
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();
|
|
}
|
|
|
|
bool SBMutex::try_lock() const {
|
|
LLDB_INSTRUMENT_VA(this);
|
|
|
|
if (!m_opaque_sp)
|
|
return false;
|
|
return m_opaque_sp->try_lock();
|
|
}
|