Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9483459

Browse files
committed
add thread safety assertions to getLuaState
split `CoreDefs.h` from `Core.h` to keep `Core` from being loaded into `RemoteClient`
1 parent 23c4f32 commit 9483459

6 files changed

Lines changed: 48 additions & 29 deletions

File tree

library/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ set(MAIN_HEADERS
5555
include/ColorText.h
5656
include/Console.h
5757
include/Core.h
58+
include/CoreDefs.h
5859
include/DataDefs.h
5960
include/DataFuncs.h
6061
include/DataIdentity.h

library/Core.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ static command_result runLuaScript(color_ostream &out, std::string name, std::ve
346346
data.pcmd = &name;
347347
data.pargs = &args;
348348

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

351351
return ok ? CR_OK : CR_FAILURE;
352352
}

library/include/Core.h

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ distribution.
2525
#pragma once
2626

2727
#include "Console.h"
28+
#include "CoreDefs.h"
2829
#include "Export.h"
2930
#include "Hooks.h"
3031

@@ -76,31 +77,6 @@ namespace DFHack
7677
struct Hide;
7778
}
7879

79-
enum command_result
80-
{
81-
CR_LINK_FAILURE = -3, // RPC call failed due to I/O or protocol error
82-
CR_NEEDS_CONSOLE = -2, // Attempt to call interactive command without console
83-
CR_NOT_IMPLEMENTED = -1, // Command not implemented, or plugin not loaded
84-
CR_OK = 0, // Success
85-
CR_FAILURE = 1, // Failure
86-
CR_WRONG_USAGE = 2, // Wrong arguments or ui state
87-
CR_NOT_FOUND = 3 // Target object not found (for RPC mainly)
88-
};
89-
90-
enum state_change_event
91-
{
92-
SC_UNKNOWN = -1,
93-
SC_WORLD_LOADED = 0,
94-
SC_WORLD_UNLOADED = 1,
95-
SC_MAP_LOADED = 2,
96-
SC_MAP_UNLOADED = 3,
97-
SC_VIEWSCREEN_CHANGED = 4,
98-
SC_CORE_INITIALIZED = 5,
99-
SC_BEGIN_UNLOAD = 6,
100-
SC_PAUSED = 7,
101-
SC_UNPAUSED = 8
102-
};
103-
10480
class DFHACK_EXPORT PerfCounters
10581
{
10682
public:
@@ -244,7 +220,10 @@ namespace DFHack
244220

245221
PerfCounters perf_counters;
246222

247-
lua_State* getLuaState() { return State; }
223+
lua_State* getLuaState(bool bypass_assertion = false) {
224+
assert(bypass_assertion || isSuspended());
225+
return State;
226+
}
248227

249228
private:
250229
DFHack::Console con;
@@ -400,9 +379,11 @@ namespace DFHack
400379
if (!owns_lock())
401380
return;
402381
/* Restore core owner to previous value */
403-
core.ownerThread.store(tid, std::memory_order_release);
404382
if (tid == std::thread::id{})
405383
Lua::Core::Reset(core.getConsole(), "suspend");
384+
core.ownerThread.store(tid, std::memory_order_release);
385+
//if (tid == std::thread::id{})
386+
// Lua::Core::Reset(core.getConsole(), "suspend");
406387
parent_t::unlock();
407388
}
408389

library/include/CoreDefs.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#pragma once
2+
3+
/**
4+
* Split off from Core.h to keep unnecessary class definitions out of compilation units that do not need them
5+
*
6+
*/
7+
8+
namespace DFHack
9+
{
10+
11+
enum command_result
12+
{
13+
CR_LINK_FAILURE = -3, // RPC call failed due to I/O or protocol error
14+
CR_NEEDS_CONSOLE = -2, // Attempt to call interactive command without console
15+
CR_NOT_IMPLEMENTED = -1, // Command not implemented, or plugin not loaded
16+
CR_OK = 0, // Success
17+
CR_FAILURE = 1, // Failure
18+
CR_WRONG_USAGE = 2, // Wrong arguments or ui state
19+
CR_NOT_FOUND = 3 // Target object not found (for RPC mainly)
20+
};
21+
22+
enum state_change_event
23+
{
24+
SC_UNKNOWN = -1,
25+
SC_WORLD_LOADED = 0,
26+
SC_WORLD_UNLOADED = 1,
27+
SC_MAP_LOADED = 2,
28+
SC_MAP_UNLOADED = 3,
29+
SC_VIEWSCREEN_CHANGED = 4,
30+
SC_CORE_INITIALIZED = 5,
31+
SC_BEGIN_UNLOAD = 6,
32+
SC_PAUSED = 7,
33+
SC_UNPAUSED = 8
34+
};
35+
36+
}

library/include/RemoteClient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ distribution.
2525
#pragma once
2626
#include "Export.h"
2727
#include "ColorText.h"
28-
#include "Core.h"
28+
#include "CoreDefs.h"
2929

3030
class CPassiveSocket;
3131
class CActiveSocket;

plugins/remotefortressreader/item_reader.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include "modules/MapCache.h"
5454
#include "modules/Materials.h"
5555
#include "MiscUtils.h"
56+
#include "Core.h"
5657

5758

5859
using namespace DFHack;

0 commit comments

Comments
 (0)