MCPcopy Create free account
hub / github.com/DFHack/dfhack / hotkey_thread_fn

Method hotkey_thread_fn

library/modules/Hotkey.cpp:149–173  ·  view source on GitHub ↗

Hotkeys actions are executed from an external thread to avoid deadlocks that may occur if running commands from the render or simulation threads.

Source from the content-addressed store, hash-verified

147// Hotkeys actions are executed from an external thread to avoid deadlocks
148// that may occur if running commands from the render or simulation threads.
149void HotkeyManager::hotkey_thread_fn() {
150 auto& core = DFHack::Core::getInstance();
151
152 std::unique_lock<std::mutex> l(lock);
153 while (true) {
154 cond.wait(l, [this]() { return this->hotkey_sig != HotkeySignal::None; });
155 if (hotkey_sig == HotkeySignal::Shutdown)
156 return;
157 if (hotkey_sig != HotkeySignal::CmdReady)
158 continue;
159
160 // Copy and reset important data, then release the lock
161 this->hotkey_sig = HotkeySignal::None;
162 std::string cmd = this->queued_command;
163 this->queued_command.clear();
164 l.unlock();
165
166 // Attempt execution of command
167 DFHack::color_ostream_proxy out(core.getConsole());
168 auto res = core.runCommand(out, cmd);
169 if (res == DFHack::CR_NOT_IMPLEMENTED)
170 out.printerr("Invalid hotkey command: '%s'\n", cmd.c_str());
171 l.lock();
172 }
173}
174
175
176bool HotkeyManager::addKeybind(KeySpec spec, std::string_view cmd) {

Callers

nothing calls this directly

Calls 6

waitMethod · 0.80
runCommandMethod · 0.80
c_strMethod · 0.80
clearMethod · 0.45
unlockMethod · 0.45
lockMethod · 0.45

Tested by

no test coverage detected