Skip to content

Commit bfd9bc4

Browse files
committed
Release render_cb_lock earlier
1 parent 04da99d commit bfd9bc4

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

library/modules/DFSDL.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,18 +287,24 @@ DFHACK_EXPORT bool DFHack::setClipboardTextCp437Multiline(string text) {
287287
return 0 == DFHack::DFSDL::DFSDL_SetClipboardText(str.str().c_str());
288288
}
289289

290+
// Queue to run callbacks on the render thread.
291+
// Semantics loosely based on SDL3's SDL_RunOnMainThread
290292
static std::recursive_mutex render_cb_lock;
291293
static std::vector<std::pair<std::function<void(void*)>, void*>> render_cb_queue;
292294

293295
DFHACK_EXPORT void DFHack::runOnRenderThread(std::function<void (void *)> cb, void *userdata) {
294296
std::lock_guard<std::recursive_mutex> l(render_cb_lock);
295-
render_cb_queue.push_back({cb, userdata});
297+
render_cb_queue.push_back({std::move(cb), userdata});
296298
}
297299

298300
DFHACK_EXPORT void DFHack::runRenderThreadCallbacks() {
299-
std::lock_guard<std::recursive_mutex> l(render_cb_lock);
300-
for (auto& cb : render_cb_queue) {
301+
static decltype(render_cb_queue) local_queue;
302+
{
303+
std::lock_guard<std::recursive_mutex> l(render_cb_lock);
304+
std::swap(local_queue, render_cb_queue);
305+
}
306+
for (auto& cb : local_queue) {
301307
std::get<0>(cb)(std::get<1>(cb));
302308
}
303-
render_cb_queue.clear();
309+
local_queue.clear();
304310
}

0 commit comments

Comments
 (0)