Skip to content

Commit

Permalink
add thread safety assertions to getLuaState
Browse files Browse the repository at this point in the history
split `CoreDefs.h` from `Core.h` to keep `Core` from being loaded into `RemoteClient`
  • Loading branch information
ab9rf committed Dec 31, 2024
1 parent 23c4f32 commit 9483459
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 29 deletions.
1 change: 1 addition & 0 deletions library/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ set(MAIN_HEADERS
include/ColorText.h
include/Console.h
include/Core.h
include/CoreDefs.h
include/DataDefs.h
include/DataFuncs.h
include/DataIdentity.h
Expand Down
2 changes: 1 addition & 1 deletion library/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ static command_result runLuaScript(color_ostream &out, std::string name, std::ve
data.pcmd = &name;
data.pargs = &args;

bool ok = Lua::RunCoreQueryLoop(out, DFHack::Core::getInstance().getLuaState(), init_run_script, &data);
bool ok = Lua::RunCoreQueryLoop(out, DFHack::Core::getInstance().getLuaState(true), init_run_script, &data);

return ok ? CR_OK : CR_FAILURE;
}
Expand Down
35 changes: 8 additions & 27 deletions library/include/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ distribution.
#pragma once

#include "Console.h"
#include "CoreDefs.h"
#include "Export.h"
#include "Hooks.h"

Expand Down Expand Up @@ -76,31 +77,6 @@ namespace DFHack
struct Hide;
}

enum command_result
{
CR_LINK_FAILURE = -3, // RPC call failed due to I/O or protocol error
CR_NEEDS_CONSOLE = -2, // Attempt to call interactive command without console
CR_NOT_IMPLEMENTED = -1, // Command not implemented, or plugin not loaded
CR_OK = 0, // Success
CR_FAILURE = 1, // Failure
CR_WRONG_USAGE = 2, // Wrong arguments or ui state
CR_NOT_FOUND = 3 // Target object not found (for RPC mainly)
};

enum state_change_event
{
SC_UNKNOWN = -1,
SC_WORLD_LOADED = 0,
SC_WORLD_UNLOADED = 1,
SC_MAP_LOADED = 2,
SC_MAP_UNLOADED = 3,
SC_VIEWSCREEN_CHANGED = 4,
SC_CORE_INITIALIZED = 5,
SC_BEGIN_UNLOAD = 6,
SC_PAUSED = 7,
SC_UNPAUSED = 8
};

class DFHACK_EXPORT PerfCounters
{
public:
Expand Down Expand Up @@ -244,7 +220,10 @@ namespace DFHack

PerfCounters perf_counters;

lua_State* getLuaState() { return State; }
lua_State* getLuaState(bool bypass_assertion = false) {
assert(bypass_assertion || isSuspended());
return State;
}

private:
DFHack::Console con;
Expand Down Expand Up @@ -400,9 +379,11 @@ namespace DFHack
if (!owns_lock())
return;
/* Restore core owner to previous value */
core.ownerThread.store(tid, std::memory_order_release);
if (tid == std::thread::id{})
Lua::Core::Reset(core.getConsole(), "suspend");
core.ownerThread.store(tid, std::memory_order_release);
//if (tid == std::thread::id{})
// Lua::Core::Reset(core.getConsole(), "suspend");
parent_t::unlock();
}

Expand Down
36 changes: 36 additions & 0 deletions library/include/CoreDefs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

/**
* Split off from Core.h to keep unnecessary class definitions out of compilation units that do not need them
*
*/

namespace DFHack
{

enum command_result
{
CR_LINK_FAILURE = -3, // RPC call failed due to I/O or protocol error
CR_NEEDS_CONSOLE = -2, // Attempt to call interactive command without console
CR_NOT_IMPLEMENTED = -1, // Command not implemented, or plugin not loaded
CR_OK = 0, // Success
CR_FAILURE = 1, // Failure
CR_WRONG_USAGE = 2, // Wrong arguments or ui state
CR_NOT_FOUND = 3 // Target object not found (for RPC mainly)
};

enum state_change_event
{
SC_UNKNOWN = -1,
SC_WORLD_LOADED = 0,
SC_WORLD_UNLOADED = 1,
SC_MAP_LOADED = 2,
SC_MAP_UNLOADED = 3,
SC_VIEWSCREEN_CHANGED = 4,
SC_CORE_INITIALIZED = 5,
SC_BEGIN_UNLOAD = 6,
SC_PAUSED = 7,
SC_UNPAUSED = 8
};

}
2 changes: 1 addition & 1 deletion library/include/RemoteClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ distribution.
#pragma once
#include "Export.h"
#include "ColorText.h"
#include "Core.h"
#include "CoreDefs.h"

class CPassiveSocket;
class CActiveSocket;
Expand Down
1 change: 1 addition & 0 deletions plugins/remotefortressreader/item_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#include "modules/MapCache.h"
#include "modules/Materials.h"
#include "MiscUtils.h"
#include "Core.h"


using namespace DFHack;
Expand Down

0 comments on commit 9483459

Please sign in to comment.