Skip to content

Commit

Permalink
adjustments to core suspender
Browse files Browse the repository at this point in the history
this is an attempted step to improve semantics around core suspenders. this does not actually resolve DFHack#5074 but it is intended as a step toward doing so
  • Loading branch information
ab9rf committed Dec 14, 2024
1 parent fcc83fe commit ca31a28
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
18 changes: 16 additions & 2 deletions library/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,15 @@ bool is_builtin(color_ostream &con, const std::string &command) {
}

void get_commands(color_ostream &con, std::vector<std::string> &commands) {
CoreSuspender suspend;
// FIXME: this should be a conditional lock but that functionality doesn't work yet
CoreSuspender suspend{};

if (!suspend.owns_lock()) {
con.printerr("Cannot acquire core lock in helpdb.get_commands\n");
commands.clear();
return;
}

auto L = Lua::Core::State;
Lua::StackUnwinder top(L);

Expand Down Expand Up @@ -641,7 +649,13 @@ static std::string sc_event_name (state_change_event id) {
}

void help_helper(color_ostream &con, const std::string &entry_name) {
CoreSuspender suspend;
// FIXME: this should be a conditional lock but that functionality doesn't work yet
CoreSuspender suspend{ };

if (!suspend.owns_lock()) {
con.printerr("Failed Lua call to helpdb.help (could not acquire core lock).\n");
return;
}
auto L = Lua::Core::State;
Lua::StackUnwinder top(L);

Expand Down
41 changes: 35 additions & 6 deletions library/include/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ distribution.
#include "modules/Graphic.h"

#include <atomic>
#include <chrono>
#include <condition_variable>
#include <map>
#include <memory>
Expand Down Expand Up @@ -353,15 +354,17 @@ namespace DFHack
friend struct CoreSuspendReleaseMain;
};

class CoreSuspenderBase : protected std::unique_lock<std::recursive_mutex> {
class CoreSuspenderBase : protected std::unique_lock< decltype(Core::CoreSuspendMutex) > {
protected:
using parent_t = std::unique_lock<std::recursive_mutex>;
size_t lock_count = 0;
using mutex_type = decltype(Core::CoreSuspendMutex);
using parent_t = std::unique_lock< mutex_type >;
std::thread::id tid;

CoreSuspenderBase(std::defer_lock_t d) : CoreSuspenderBase{&Core::getInstance(), d} {}

CoreSuspenderBase(Core* core, std::defer_lock_t) :
/* Lock the core */
/* Lock the core (jk not really) */
parent_t{core->CoreSuspendMutex,std::defer_lock},
/* Mark this thread to be the core owner */
tid{}
Expand All @@ -373,6 +376,20 @@ namespace DFHack
parent_t::lock();
tid = core.ownerThread.exchange(std::this_thread::get_id(),
std::memory_order_acquire);
lock_count++;
}

bool try_lock()
{
auto& core = Core::getInstance();
bool locked = parent_t::try_lock();
if (locked)
{
tid = core.ownerThread.exchange(std::this_thread::get_id(),
std::memory_order_acquire);
lock_count++;
}
return locked;
}

void unlock()
Expand All @@ -383,15 +400,17 @@ namespace DFHack
if (tid == std::thread::id{})
Lua::Core::Reset(core.getConsole(), "suspend");
parent_t::unlock();
lock_count--;
}

bool owns_lock() const noexcept
{
return parent_t::owns_lock();
return lock_count > 0;
// return parent_t::owns_lock();
}

~CoreSuspenderBase() {
if (owns_lock())
while (lock_count > 0)
unlock();
}
friend class MainThread;
Expand Down Expand Up @@ -443,6 +462,15 @@ namespace DFHack
parent_t::lock();
}

bool try_lock()
{
auto& core = Core::getInstance();
bool locked = parent_t::try_lock();
if (locked)
core.toolCount.fetch_add(1, std::memory_order_relaxed);
return locked;
}

void unlock()
{
auto& core = Core::getInstance();
Expand All @@ -457,7 +485,7 @@ namespace DFHack
}

~CoreSuspender() {
if (owns_lock())
while (lock_count > 0)
unlock();
}
};
Expand All @@ -483,4 +511,5 @@ namespace DFHack
};

using CoreSuspendClaimer = CoreSuspender;

}

0 comments on commit ca31a28

Please sign in to comment.