File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
290292static std::recursive_mutex render_cb_lock;
291293static std::vector<std::pair<std::function<void (void *)>, void *>> render_cb_queue;
292294
293295DFHACK_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
298300DFHACK_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}
You can’t perform that action at this time.
0 commit comments