tool_root_lock.cpp

src/mcp/tool_root_lock.cpp

@file tool_root_lock.cpp @brief ToolRootLock implementation (gh#158, gh#160).

Owners: @tvanfossen

ToolRootShared

entropic::ToolRootShared::ToolRootShared(ToolRootLock &lock, const std::string &what)

Take the shared side (or pass through as the owner).

src/mcp/tool_root_lock.cpp:104

Show on the map →

Called by none

Calls lock_shared

Source · 2 lines
ToolRootShared::ToolRootShared(ToolRootLock& lock, const std::string& what)
    : lock_(lock), held_(lock.lock_shared(what)) {}

Requirements

REQ-LOOP-009

lock

void entropic::ToolRootLock::lock()

Acquire exclusively; re-entrant for the owning thread.

src/mcp/tool_root_lock.cpp:26

Show on the map →

Called by TEST_CASE, enter_working_dir

Calls none

Source · 10 lines
void ToolRootLock::lock() {
    const auto me = std::this_thread::get_id();
    if (owner_.load(std::memory_order_acquire) == me) {
        ++depth_;
        return;
    }
    mutex_.lock();
    owner_.store(me, std::memory_order_release);
    depth_ = 1;
}
Activity · 7 steps
lines 27–27const auto me = std::this_thread::get_id();lines 28–28◇ if (owner_.load(std::memory_order_acquire) == me)lines 29–29++depth_;lines 30–30return;lines 32–32mutex_.lock();lines 33–33owner_.store(me, std::memory_order_release);lines 34–34depth_ = 1;

Requirements

REQ-DELEG-005 REQ-LOOP-009

lock_shared

bool entropic::ToolRootLock::lock_shared(const std::string &what)

Acquire shared, unless this thread owns the exclusive side.

src/mcp/tool_root_lock.cpp:64

Source · 12 lines
bool ToolRootLock::lock_shared(const std::string& what) {
    if (owned_by_this_thread()) {
        return false;
    }
    if (!mutex_.try_lock_shared()) {
        logger->info("[{}] waiting: a sandboxed delegation has this server "
                     "set re-rooted; dispatching once it restores", what);
        mutex_.lock_shared();
        logger->info("[{}] working directory restored — dispatching", what);
    }
    return true;
}
Activity · 7 steps
lines 65–65◇ if (owned_by_this_thread())lines 66–66return false;lines 68–68◇ if (!mutex_.try_lock_shared())lines 69–70logger->info("[{}] waiting: a sandboxed delegation has this server " "set re-rooted; dispatching once it restores", what);lines 71–71mutex_.lock_shared();lines 72–72logger->info("[{}] working directory restored — dispatching", what);lines 74–74return true;

Requirements

REQ-DELEG-005 REQ-LOOP-009

owned_by_this_thread

bool entropic::ToolRootLock::owned_by_this_thread() const

Whether the calling thread holds the exclusive side.

src/mcp/tool_root_lock.cpp:92

Show on the map →

Called by TEST_CASE, leave_working_dir, lock_shared

Calls none

Source · 4 lines
bool ToolRootLock::owned_by_this_thread() const {
    return owner_.load(std::memory_order_acquire)
        == std::this_thread::get_id();
}
Activity · 1 steps
lines 93–94return owner_.load(std::memory_order_acquire) == std::this_thread::get_id();

Requirements

REQ-LOOP-009

unlock

void entropic::ToolRootLock::unlock()

Release one exclusive level; the last one frees the lock.

src/mcp/tool_root_lock.cpp:43

Show on the map →

Called by TEST_CASE, leave_working_dir

Calls none

Source · 6 lines
void ToolRootLock::unlock() {
    if (--depth_ == 0) {
        owner_.store(std::thread::id{}, std::memory_order_release);
        mutex_.unlock();
    }
}
Activity · 3 steps
lines 44–44◇ if (--depth_ == 0)lines 45–45owner_.store(std::thread::id{}, std::memory_order_release);lines 46–46mutex_.unlock();

Requirements

REQ-DELEG-005 REQ-LOOP-009

unlock_shared

void entropic::ToolRootLock::unlock_shared()

Release a shared hold taken by lock_shared() == true.

src/mcp/tool_root_lock.cpp:82

Source · 3 lines
void ToolRootLock::unlock_shared() {
    mutex_.unlock_shared();
}
Activity · 1 steps
lines 83–83mutex_.unlock_shared();

Requirements

REQ-LOOP-009

~ToolRootShared

entropic::ToolRootShared::~ToolRootShared()

Release the shared hold if one was taken.

src/mcp/tool_root_lock.cpp:112

The index is older than this file: the lines it records no longer hold this function. Refresh the index to bundle its body.

Requirements

REQ-LOOP-009