2012-01-27 08:47:14 -07:00
|
|
|
/*
|
2011-06-16 15:53:39 -06:00
|
|
|
https://github.com/peterix/dfhack
|
2012-09-29 20:03:37 -06:00
|
|
|
Copyright (c) 2009-2012 Petr Mrázek (peterix@gmail.com)
|
2011-06-16 15:53:39 -06:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any
|
|
|
|
damages arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any
|
|
|
|
purpose, including commercial applications, and to alter it and
|
|
|
|
redistribute it freely, subject to the following restrictions:
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must
|
|
|
|
not claim that you wrote the original software. If you use this
|
|
|
|
software in a product, an acknowledgment in the product documentation
|
|
|
|
would be appreciated but is not required.
|
|
|
|
|
|
|
|
2. Altered source versions must be plainly marked as such, and
|
|
|
|
must not be misrepresented as being the original software.
|
|
|
|
|
|
|
|
3. This notice may not be removed or altered from any source
|
|
|
|
distribution.
|
|
|
|
*/
|
|
|
|
|
2011-06-14 08:13:28 -06:00
|
|
|
#pragma once
|
|
|
|
|
2011-12-31 04:48:42 -07:00
|
|
|
#include "Pragma.h"
|
|
|
|
#include "Export.h"
|
|
|
|
#include "Hooks.h"
|
2011-06-19 20:29:38 -06:00
|
|
|
#include <vector>
|
2011-07-11 14:23:13 -06:00
|
|
|
#include <stack>
|
2011-06-26 18:13:01 -06:00
|
|
|
#include <map>
|
2018-07-08 13:04:53 -06:00
|
|
|
#include <memory>
|
2011-06-26 18:13:01 -06:00
|
|
|
#include <stdint.h>
|
2011-12-31 04:48:42 -07:00
|
|
|
#include "Console.h"
|
|
|
|
#include "modules/Graphic.h"
|
2011-06-19 20:29:38 -06:00
|
|
|
|
2018-06-21 09:58:16 -06:00
|
|
|
#include <atomic>
|
2018-06-12 08:33:12 -06:00
|
|
|
#include <condition_variable>
|
2018-06-21 09:58:16 -06:00
|
|
|
#include <mutex>
|
|
|
|
#include <thread>
|
2018-06-12 08:33:12 -06:00
|
|
|
|
2012-05-04 09:47:18 -06:00
|
|
|
#include "RemoteClient.h"
|
|
|
|
|
2015-01-17 06:56:49 -07:00
|
|
|
#define DFH_MOD_SHIFT 1
|
|
|
|
#define DFH_MOD_CTRL 2
|
|
|
|
#define DFH_MOD_ALT 4
|
2015-01-11 10:48:52 -07:00
|
|
|
|
2011-07-31 20:40:23 -06:00
|
|
|
struct WINDOW;
|
|
|
|
|
2011-12-30 12:25:50 -07:00
|
|
|
namespace df
|
|
|
|
{
|
|
|
|
struct viewscreen;
|
|
|
|
}
|
|
|
|
|
2011-06-14 08:13:28 -06:00
|
|
|
namespace DFHack
|
|
|
|
{
|
|
|
|
class Process;
|
2011-06-17 07:02:43 -06:00
|
|
|
class Module;
|
|
|
|
class Materials;
|
2012-02-09 18:35:51 -07:00
|
|
|
struct VersionInfo;
|
2011-06-17 07:02:43 -06:00
|
|
|
class VersionInfoFactory;
|
2011-06-24 21:35:29 -06:00
|
|
|
class PluginManager;
|
2011-06-26 18:13:01 -06:00
|
|
|
class Core;
|
2012-03-14 09:57:29 -06:00
|
|
|
class ServerMain;
|
2018-06-21 09:58:16 -06:00
|
|
|
class CoreSuspender;
|
|
|
|
|
|
|
|
namespace Lua { namespace Core {
|
|
|
|
DFHACK_EXPORT void Reset(color_ostream &out, const char *where);
|
|
|
|
} }
|
2011-06-17 07:02:43 -06:00
|
|
|
|
2018-06-20 11:08:35 -06:00
|
|
|
namespace Screen
|
|
|
|
{
|
|
|
|
struct Hide;
|
|
|
|
}
|
|
|
|
|
2012-05-17 10:04:09 -06:00
|
|
|
enum state_change_event
|
|
|
|
{
|
2015-02-07 17:33:12 -07:00
|
|
|
SC_UNKNOWN = -1,
|
2012-05-17 10:04:09 -06:00
|
|
|
SC_WORLD_LOADED = 0,
|
|
|
|
SC_WORLD_UNLOADED = 1,
|
|
|
|
SC_MAP_LOADED = 2,
|
|
|
|
SC_MAP_UNLOADED = 3,
|
|
|
|
SC_VIEWSCREEN_CHANGED = 4,
|
|
|
|
SC_CORE_INITIALIZED = 5,
|
2012-07-05 10:03:02 -06:00
|
|
|
SC_BEGIN_UNLOAD = 6,
|
|
|
|
SC_PAUSED = 7,
|
|
|
|
SC_UNPAUSED = 8
|
2012-05-17 10:04:09 -06:00
|
|
|
};
|
|
|
|
|
2015-02-07 17:33:12 -07:00
|
|
|
class DFHACK_EXPORT StateChangeScript
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
state_change_event event;
|
|
|
|
std::string path;
|
|
|
|
bool save_specific;
|
|
|
|
StateChangeScript(state_change_event event, std::string path, bool save_specific = false)
|
|
|
|
:event(event), path(path), save_specific(save_specific)
|
|
|
|
{ }
|
|
|
|
bool operator==(const StateChangeScript& other)
|
|
|
|
{
|
|
|
|
return event == other.event && path == other.path && save_specific == other.save_specific;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-06-14 08:13:28 -06:00
|
|
|
// Core is a singleton. Why? Because it is closely tied to SDL calls. It tracks the global state of DF.
|
|
|
|
// There should never be more than one instance
|
|
|
|
// Better than tracking some weird variables all over the place.
|
|
|
|
class DFHACK_EXPORT Core
|
|
|
|
{
|
2022-12-23 20:05:00 -07:00
|
|
|
friend void ::dfhooks_init();
|
|
|
|
friend void ::dfhooks_shutdown();
|
|
|
|
friend void ::dfhooks_update();
|
|
|
|
friend void ::dfhooks_prerender();
|
|
|
|
friend bool ::dfhooks_sdl_event(SDL::Event* event);
|
|
|
|
friend bool ::dfhooks_ncurses_key(int key);
|
2011-06-14 08:13:28 -06:00
|
|
|
public:
|
2011-06-17 07:02:43 -06:00
|
|
|
/// Get the single Core instance or make one.
|
2011-06-14 08:13:28 -06:00
|
|
|
static Core& getInstance()
|
|
|
|
{
|
|
|
|
static Core instance;
|
|
|
|
return instance;
|
|
|
|
}
|
2012-04-15 09:09:25 -06:00
|
|
|
/// check if the activity lock is owned by this thread
|
|
|
|
bool isSuspended(void);
|
2011-06-17 07:02:43 -06:00
|
|
|
/// Is everything OK?
|
|
|
|
bool isValid(void) { return !errorstate; }
|
|
|
|
|
|
|
|
/// get the materials module
|
|
|
|
Materials * getMaterials();
|
2011-12-07 12:37:09 -07:00
|
|
|
/// get the graphic module
|
|
|
|
Graphic * getGraphic();
|
2011-07-09 03:33:58 -06:00
|
|
|
/// sets the current hotkey command
|
|
|
|
bool setHotkeyCmd( std::string cmd );
|
|
|
|
/// removes the hotkey command and gives it to the caller thread
|
2018-06-21 08:13:22 -06:00
|
|
|
std::string getHotkeyCmd( bool &keep_going );
|
2011-06-24 21:35:29 -06:00
|
|
|
|
2011-11-04 02:08:29 -06:00
|
|
|
/// adds a named pointer (for later or between plugins)
|
|
|
|
void RegisterData(void *p,std::string key);
|
|
|
|
/// returns a named pointer.
|
|
|
|
void *GetData(std::string key);
|
2011-08-04 13:00:21 -06:00
|
|
|
|
2012-05-04 09:47:18 -06:00
|
|
|
command_result runCommand(color_ostream &out, const std::string &command, std::vector <std::string> ¶meters);
|
|
|
|
command_result runCommand(color_ostream &out, const std::string &command);
|
|
|
|
bool loadScriptFile(color_ostream &out, std::string fname, bool silent = false);
|
|
|
|
|
2015-09-06 14:23:02 -06:00
|
|
|
bool addScriptPath(std::string path, bool search_before = false);
|
2023-03-11 03:07:59 -07:00
|
|
|
bool setModScriptPaths(const std::vector<std::string> &mod_script_paths);
|
2015-09-06 14:23:02 -06:00
|
|
|
bool removeScriptPath(std::string path);
|
|
|
|
std::string findScript(std::string name);
|
|
|
|
void getScriptPaths(std::vector<std::string> *dest);
|
|
|
|
|
2011-12-30 12:25:50 -07:00
|
|
|
bool ClearKeyBindings(std::string keyspec);
|
|
|
|
bool AddKeyBinding(std::string keyspec, std::string cmdline);
|
2011-12-31 02:25:46 -07:00
|
|
|
std::vector<std::string> ListKeyBindings(std::string keyspec);
|
2015-01-11 10:48:52 -07:00
|
|
|
int8_t getModstate() { return modstate; }
|
2011-12-30 12:25:50 -07:00
|
|
|
|
2017-06-18 15:39:54 -06:00
|
|
|
bool AddAlias(const std::string &name, const std::vector<std::string> &command, bool replace = false);
|
|
|
|
bool RemoveAlias(const std::string &name);
|
|
|
|
bool IsAlias(const std::string &name);
|
|
|
|
bool RunAlias(color_ostream &out, const std::string &name,
|
|
|
|
const std::vector<std::string> ¶meters, command_result &result);
|
|
|
|
std::map<std::string, std::vector<std::string>> ListAliases();
|
2023-03-25 00:25:24 -06:00
|
|
|
std::string GetAliasCommand(const std::string &name, bool ignore_params = false);
|
2017-06-18 15:39:54 -06:00
|
|
|
|
2012-05-04 09:47:18 -06:00
|
|
|
std::string getHackPath();
|
|
|
|
|
2011-12-30 07:12:15 -07:00
|
|
|
bool isWorldLoaded() { return (last_world_data_ptr != NULL); }
|
2012-03-31 18:56:54 -06:00
|
|
|
bool isMapLoaded() { return (last_local_map_ptr != NULL && last_world_data_ptr != NULL); }
|
2012-03-10 04:55:42 -07:00
|
|
|
|
|
|
|
static df::viewscreen *getTopViewscreen() { return getInstance().top_viewscreen; }
|
|
|
|
|
|
|
|
DFHack::Console &getConsole() { return con; }
|
2011-12-30 07:12:15 -07:00
|
|
|
|
2018-07-08 13:04:53 -06:00
|
|
|
std::unique_ptr<DFHack::Process> p;
|
2018-07-08 16:50:14 -06:00
|
|
|
std::shared_ptr<DFHack::VersionInfo> vinfo;
|
2012-03-10 04:55:42 -07:00
|
|
|
|
2018-06-11 10:20:51 -06:00
|
|
|
static void print(const char *format, ...) Wformat(printf,1,2);
|
|
|
|
static void printerr(const char *format, ...) Wformat(printf,1,2);
|
2012-03-10 04:55:42 -07:00
|
|
|
|
Allow plugins to export functions to lua with safe reload support.
- To ensure reload safety functions have to be wrapped. Every call
checks the loaded state and locks a mutex in Plugin. If the plugin
is unloaded, calling its functions throws a lua error. Therefore,
plugins may not create closures or export yieldable functions.
- The set of function argument and return types supported by
LuaWrapper is severely limited when compared to being compiled
inside the main library.
Currently supported types: numbers, bool, std::string, df::foo,
df::foo*, std::vector<bool>, std::vector<df::foo*>.
- To facilitate postponing initialization until after all plugins
have been loaded, the core sends a SC_CORE_INITIALIZED event.
- As an example, the burrows plugin now exports its functions.
2012-04-14 09:44:07 -06:00
|
|
|
PluginManager *getPluginManager() { return plug_mgr; }
|
|
|
|
|
2012-04-15 08:40:19 -06:00
|
|
|
static void cheap_tokenise(std::string const& input, std::vector<std::string> &output);
|
|
|
|
|
2011-06-14 08:13:28 -06:00
|
|
|
private:
|
2012-03-10 04:55:42 -07:00
|
|
|
DFHack::Console con;
|
|
|
|
|
2011-06-14 08:13:28 -06:00
|
|
|
Core();
|
2018-06-12 08:53:43 -06:00
|
|
|
~Core();
|
2012-03-15 05:33:19 -06:00
|
|
|
|
2012-03-17 02:09:30 -06:00
|
|
|
struct Private;
|
2018-07-08 13:04:53 -06:00
|
|
|
std::unique_ptr<Private> d;
|
2012-03-15 05:33:19 -06:00
|
|
|
|
2023-05-15 18:33:57 -06:00
|
|
|
bool InitMainThread();
|
|
|
|
bool InitSimulationThread();
|
2012-02-28 04:59:02 -07:00
|
|
|
int Update (void);
|
2011-06-16 15:53:39 -06:00
|
|
|
int Shutdown (void);
|
2023-01-04 10:57:06 -07:00
|
|
|
bool DFH_SDL_Event(SDL::Event* event);
|
2011-07-31 20:40:23 -06:00
|
|
|
bool ncurses_wgetch(int in, int & out);
|
2022-12-23 20:05:00 -07:00
|
|
|
bool DFH_ncurses_key(int key);
|
2012-03-15 05:33:19 -06:00
|
|
|
|
2022-12-23 20:05:00 -07:00
|
|
|
void doUpdate(color_ostream &out);
|
2012-05-17 10:04:09 -06:00
|
|
|
void onUpdate(color_ostream &out);
|
|
|
|
void onStateChange(color_ostream &out, state_change_event event);
|
2015-02-07 17:33:12 -07:00
|
|
|
void handleLoadAndUnloadScripts(color_ostream &out, state_change_event event);
|
2019-05-29 17:52:03 -06:00
|
|
|
void doSaveData(color_ostream &out);
|
|
|
|
void doLoadData(color_ostream &out);
|
2012-05-17 10:04:09 -06:00
|
|
|
|
2011-06-14 08:13:28 -06:00
|
|
|
Core(Core const&); // Don't Implement
|
|
|
|
void operator=(Core const&); // Don't implement
|
2012-03-15 05:33:19 -06:00
|
|
|
|
2011-11-04 02:08:29 -06:00
|
|
|
// report error to user while failing
|
2015-10-17 19:18:04 -06:00
|
|
|
void fatal (std::string output);
|
2012-03-15 05:33:19 -06:00
|
|
|
|
2011-11-04 02:08:29 -06:00
|
|
|
// 1 = fatal failure
|
2011-06-14 08:13:28 -06:00
|
|
|
bool errorstate;
|
2011-07-11 14:23:13 -06:00
|
|
|
// regulate access to DF
|
|
|
|
struct Cond;
|
2012-03-15 05:33:19 -06:00
|
|
|
|
2011-06-17 07:02:43 -06:00
|
|
|
// FIXME: shouldn't be kept around like this
|
2018-07-08 16:50:14 -06:00
|
|
|
std::unique_ptr<DFHack::VersionInfoFactory> vif;
|
2011-06-17 07:02:43 -06:00
|
|
|
// Module storage
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
Materials * pMaterials;
|
2011-12-07 12:37:09 -07:00
|
|
|
Graphic * pGraphic;
|
2011-06-17 07:02:43 -06:00
|
|
|
} s_mods;
|
2018-07-08 21:48:39 -06:00
|
|
|
std::vector<std::unique_ptr<Module>> allModules;
|
2011-06-24 21:35:29 -06:00
|
|
|
DFHack::PluginManager * plug_mgr;
|
2012-05-04 09:47:18 -06:00
|
|
|
|
2023-03-11 03:07:59 -07:00
|
|
|
std::vector<std::string> script_paths[3];
|
2018-06-12 08:33:12 -06:00
|
|
|
std::mutex script_path_mutex;
|
2015-09-06 14:23:02 -06:00
|
|
|
|
2011-07-09 03:33:58 -06:00
|
|
|
// hotkey-related stuff
|
2011-12-30 12:25:50 -07:00
|
|
|
struct KeyBinding {
|
|
|
|
int modifiers;
|
|
|
|
std::vector<std::string> command;
|
|
|
|
std::string cmdline;
|
2012-05-19 11:31:42 -06:00
|
|
|
std::string focus;
|
2011-12-30 12:25:50 -07:00
|
|
|
};
|
2015-01-11 10:48:52 -07:00
|
|
|
int8_t modstate;
|
2011-12-30 12:25:50 -07:00
|
|
|
|
|
|
|
std::map<int, std::vector<KeyBinding> > key_bindings;
|
|
|
|
std::map<int, bool> hotkey_states;
|
2011-07-09 03:33:58 -06:00
|
|
|
std::string hotkey_cmd;
|
2018-06-21 08:13:22 -06:00
|
|
|
enum hotkey_set_t {
|
|
|
|
NO,
|
|
|
|
SET,
|
|
|
|
SHUTDOWN,
|
|
|
|
};
|
|
|
|
hotkey_set_t hotkey_set;
|
2018-06-12 08:33:12 -06:00
|
|
|
std::mutex HotkeyMutex;
|
|
|
|
std::condition_variable HotkeyCond;
|
2011-12-30 12:25:50 -07:00
|
|
|
|
2017-06-18 15:39:54 -06:00
|
|
|
std::map<std::string, std::vector<std::string>> aliases;
|
2018-06-12 08:33:12 -06:00
|
|
|
std::recursive_mutex alias_mutex;
|
2017-06-18 15:39:54 -06:00
|
|
|
|
2011-12-30 12:25:50 -07:00
|
|
|
bool SelectHotkey(int key, int modifiers);
|
|
|
|
|
2012-03-31 18:56:54 -06:00
|
|
|
// for state change tracking
|
|
|
|
void *last_world_data_ptr;
|
|
|
|
// for state change tracking
|
|
|
|
void *last_local_map_ptr;
|
2018-06-20 11:08:35 -06:00
|
|
|
friend struct Screen::Hide;
|
2011-12-30 12:25:50 -07:00
|
|
|
df::viewscreen *top_viewscreen;
|
2012-07-05 10:03:02 -06:00
|
|
|
bool last_pause_state;
|
2011-07-09 03:33:58 -06:00
|
|
|
// Very important!
|
2018-07-07 04:13:55 -06:00
|
|
|
std::atomic<bool> started;
|
2015-02-07 17:33:12 -07:00
|
|
|
// Additional state change scripts
|
|
|
|
std::vector<StateChangeScript> state_change_scripts;
|
2011-08-04 13:00:21 -06:00
|
|
|
|
2018-06-12 08:33:12 -06:00
|
|
|
std::mutex misc_data_mutex;
|
2012-03-04 17:34:04 -07:00
|
|
|
std::map<std::string,void*> misc_data_map;
|
2012-03-14 09:57:29 -06:00
|
|
|
|
2018-06-21 09:58:16 -06:00
|
|
|
/*!
|
|
|
|
* \defgroup core_suspend CoreSuspender state handling serialization to
|
|
|
|
* DF memory.
|
|
|
|
* \sa DFHack::CoreSuspender
|
|
|
|
* \{
|
|
|
|
*/
|
|
|
|
std::recursive_mutex CoreSuspendMutex;
|
|
|
|
std::condition_variable_any CoreWakeup;
|
|
|
|
std::atomic<std::thread::id> ownerThread;
|
|
|
|
std::atomic<size_t> toolCount;
|
|
|
|
//! \}
|
|
|
|
|
2012-03-14 09:57:29 -06:00
|
|
|
friend class CoreService;
|
2012-03-15 03:01:23 -06:00
|
|
|
friend class ServerConnection;
|
2018-06-21 09:58:16 -06:00
|
|
|
friend class CoreSuspender;
|
2018-07-03 13:19:19 -06:00
|
|
|
friend class CoreSuspenderBase;
|
2018-07-03 12:15:12 -06:00
|
|
|
friend struct CoreSuspendClaimMain;
|
|
|
|
friend struct CoreSuspendReleaseMain;
|
2011-06-14 08:13:28 -06:00
|
|
|
};
|
2011-12-24 03:51:58 -07:00
|
|
|
|
2018-07-03 13:19:19 -06:00
|
|
|
class CoreSuspenderBase : protected std::unique_lock<std::recursive_mutex> {
|
|
|
|
protected:
|
|
|
|
using parent_t = std::unique_lock<std::recursive_mutex>;
|
|
|
|
std::thread::id tid;
|
|
|
|
|
|
|
|
CoreSuspenderBase(std::defer_lock_t d) : CoreSuspenderBase{&Core::getInstance(), d} {}
|
|
|
|
|
|
|
|
CoreSuspenderBase(Core* core, std::defer_lock_t) :
|
|
|
|
/* Lock the core */
|
|
|
|
parent_t{core->CoreSuspendMutex,std::defer_lock},
|
|
|
|
/* Mark this thread to be the core owner */
|
|
|
|
tid{}
|
|
|
|
{}
|
|
|
|
public:
|
|
|
|
void lock()
|
|
|
|
{
|
|
|
|
auto& core = Core::getInstance();
|
|
|
|
parent_t::lock();
|
|
|
|
tid = core.ownerThread.exchange(std::this_thread::get_id(),
|
|
|
|
std::memory_order_acquire);
|
|
|
|
}
|
|
|
|
|
|
|
|
void unlock()
|
|
|
|
{
|
|
|
|
auto& core = Core::getInstance();
|
|
|
|
/* 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");
|
|
|
|
parent_t::unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool owns_lock() const noexcept
|
|
|
|
{
|
|
|
|
return parent_t::owns_lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
~CoreSuspenderBase() {
|
|
|
|
if (owns_lock())
|
|
|
|
unlock();
|
2018-06-21 09:58:16 -06:00
|
|
|
}
|
2018-07-03 13:19:19 -06:00
|
|
|
friend class MainThread;
|
2011-12-24 03:51:58 -07:00
|
|
|
};
|
2012-08-18 01:52:38 -06:00
|
|
|
|
2018-06-21 09:58:16 -06:00
|
|
|
/*!
|
|
|
|
* CoreSuspender allows serialization to DF data with std::unique_lock like
|
|
|
|
* interface. It includes handling for recursive CoreSuspender calls and
|
|
|
|
* notification to main thread after all queue tools have been handled.
|
|
|
|
*
|
|
|
|
* State transitions are:
|
|
|
|
* - Startup setups Core::SuspendMutex to unlocked states
|
|
|
|
* - Core::Init locks Core::SuspendMutex until the thread exits or that thread
|
|
|
|
* calls Core::Shutdown or Core::~Core.
|
|
|
|
* - Other thread request core suspend by atomic incrementation of Core::toolCount
|
|
|
|
* and then locking Core::CoreSuspendMutex. After locking CoreSuspendMutex
|
|
|
|
* success callers exchange their std::thread::id to Core::ownerThread.
|
|
|
|
* - Core::Update() makes sure that queued tools are run when it calls
|
|
|
|
* Core::CoreWakup::wait. The wait keeps Core::CoreSuspendMutex unlocked
|
|
|
|
* and waits until Core::toolCount is reduced back to zero.
|
|
|
|
* - CoreSuspender::~CoreSuspender() first stores the previous Core::ownerThread
|
|
|
|
* back. In case of recursive call Core::ownerThread equals tid. If tis is
|
|
|
|
* zero then we are releasing the recursive_mutex which means suspend
|
|
|
|
* context is over. It is time to reset lua.
|
|
|
|
* The last step is to decrement Core::toolCount and wakeup main thread if
|
|
|
|
* no more tools are queued trying to acquire the
|
|
|
|
* Core::CoreSuspenderMutex.
|
2012-08-18 01:52:38 -06:00
|
|
|
*/
|
2018-07-03 13:19:19 -06:00
|
|
|
class CoreSuspender : public CoreSuspenderBase {
|
|
|
|
using parent_t = CoreSuspenderBase;
|
2012-08-18 01:52:38 -06:00
|
|
|
public:
|
2018-07-03 13:19:19 -06:00
|
|
|
CoreSuspender() : CoreSuspender{&Core::getInstance()} { }
|
|
|
|
CoreSuspender(std::defer_lock_t d) : CoreSuspender{&Core::getInstance(),d} { }
|
|
|
|
CoreSuspender(bool) : CoreSuspender{&Core::getInstance()} { }
|
|
|
|
CoreSuspender(Core* core, bool) : CoreSuspender{core} { }
|
2018-06-21 09:58:16 -06:00
|
|
|
CoreSuspender(Core* core) :
|
2018-07-03 13:19:19 -06:00
|
|
|
CoreSuspenderBase{core, std::defer_lock}
|
|
|
|
{
|
|
|
|
lock();
|
|
|
|
}
|
|
|
|
CoreSuspender(Core* core, std::defer_lock_t) :
|
|
|
|
CoreSuspenderBase{core, std::defer_lock}
|
|
|
|
{}
|
|
|
|
|
|
|
|
void lock()
|
|
|
|
{
|
|
|
|
auto& core = Core::getInstance();
|
|
|
|
core.toolCount.fetch_add(1, std::memory_order_relaxed);
|
|
|
|
parent_t::lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void unlock()
|
|
|
|
{
|
|
|
|
auto& core = Core::getInstance();
|
|
|
|
parent_t::unlock();
|
2018-06-21 09:58:16 -06:00
|
|
|
/* Notify core to continue when all queued tools have completed
|
|
|
|
* 0 = None wants to own the core
|
|
|
|
* 1+ = There are tools waiting core access
|
|
|
|
* fetch_add returns old value before subtraction
|
|
|
|
*/
|
2018-07-03 13:19:19 -06:00
|
|
|
if (core.toolCount.fetch_add(-1, std::memory_order_relaxed) == 1)
|
|
|
|
core.CoreWakeup.notify_one();
|
|
|
|
}
|
|
|
|
|
|
|
|
~CoreSuspender() {
|
|
|
|
if (owns_lock())
|
|
|
|
unlock();
|
2012-08-18 01:52:38 -06:00
|
|
|
}
|
|
|
|
};
|
2018-06-21 09:58:16 -06:00
|
|
|
|
2018-07-03 12:15:12 -06:00
|
|
|
/*!
|
|
|
|
* Temporary release main thread ownership to allow alternative thread
|
|
|
|
* implement DF logic thread loop
|
|
|
|
*/
|
|
|
|
struct DFHACK_EXPORT CoreSuspendReleaseMain {
|
|
|
|
CoreSuspendReleaseMain();
|
|
|
|
~CoreSuspendReleaseMain();
|
|
|
|
};
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* Temporary claim main thread ownership. This allows caller to call
|
|
|
|
* Core::Update from a different thread than original DF logic thread if
|
|
|
|
* logic thread has released main thread ownership with
|
|
|
|
* CoreSuspendReleaseMain
|
|
|
|
*/
|
|
|
|
struct DFHACK_EXPORT CoreSuspendClaimMain {
|
|
|
|
CoreSuspendClaimMain();
|
|
|
|
~CoreSuspendClaimMain();
|
|
|
|
};
|
|
|
|
|
2018-06-21 09:58:16 -06:00
|
|
|
using CoreSuspendClaimer = CoreSuspender;
|
2011-07-13 00:17:51 -06:00
|
|
|
}
|