diff --git a/src/event.cpp b/src/event.cpp index 3bfda8cc8..071177aee 100644 --- a/src/event.cpp +++ b/src/event.cpp @@ -302,15 +302,6 @@ bool event_is_signal_observed(int sig) { return result; } -/// Callback for firing (and then deleting) an event. -static void fire_event_callback(void *arg) { - ASSERT_IS_MAIN_THREAD(); - assert(arg != NULL); - event_t *event = static_cast(arg); - event_fire(event); - delete event; -} - /// Perform the specified event. Since almost all event firings will not be matched by even a single /// event handler, we make sure to optimize the 'no matches' path. This means that nothing is /// allocated/initialized unless needed. @@ -332,8 +323,10 @@ static void event_fire_internal(const event_t &event) { if (signal_is_blocked()) { // Fix for https://github.com/fish-shell/fish-shell/issues/608. Don't run event handlers // while signals are blocked. - event_t *heap_event = new event_t(event); - input_common_add_callback(fire_event_callback, heap_event); + input_common_add_callback([event](){ + ASSERT_IS_MAIN_THREAD(); + event_fire(&event); + }); return; } diff --git a/src/input_common.cpp b/src/input_common.cpp index f5590e184..3d44a7953 100644 --- a/src/input_common.cpp +++ b/src/input_common.cpp @@ -37,8 +37,7 @@ static int wait_on_escape_ms = WAIT_ON_ESCAPE_DEFAULT; static std::deque lookahead_list; // Queue of pairs of (function pointer, argument) to be invoked. Expected to be mostly empty. -typedef std::pair callback_info_t; -typedef std::queue > callback_queue_t; +typedef std::list> callback_queue_t; static callback_queue_t callback_queue; static void input_flush_callbacks(void); @@ -237,22 +236,18 @@ void input_common_queue_ch(wint_t ch) { lookahead_push_back(ch); } void input_common_next_ch(wint_t ch) { lookahead_push_front(ch); } -void input_common_add_callback(void (*callback)(void *), void *arg) { +void input_common_add_callback(std::function callback) { ASSERT_IS_MAIN_THREAD(); - callback_queue.push(callback_info_t(callback, arg)); + callback_queue.push_back(std::move(callback)); } static void input_flush_callbacks(void) { - // Nothing to do if nothing to do. - if (callback_queue.empty()) return; - // We move the queue into a local variable, so that events queued up during a callback don't get // fired until next round. + ASSERT_IS_MAIN_THREAD(); callback_queue_t local_queue; std::swap(local_queue, callback_queue); - while (!local_queue.empty()) { - const callback_info_t &callback = local_queue.front(); - callback.first(callback.second); // cute - local_queue.pop(); + for (auto &f : local_queue) { + f(); } } diff --git a/src/input_common.h b/src/input_common.h index 88e75c0fd..bc4f93225 100644 --- a/src/input_common.h +++ b/src/input_common.h @@ -2,6 +2,7 @@ #ifndef INPUT_COMMON_H #define INPUT_COMMON_H +#include #include #include "common.h" @@ -104,6 +105,6 @@ void input_common_next_ch(wint_t ch); /// Adds a callback to be invoked at the next turn of the "event loop." The callback function will /// be invoked and passed arg. -void input_common_add_callback(void (*callback)(void *), void *arg); +void input_common_add_callback(std::function); #endif diff --git a/src/reader.cpp b/src/reader.cpp index b53037093..41aec0c3d 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -833,18 +833,13 @@ void reader_repaint_if_needed() { } } -static void reader_repaint_if_needed_one_arg(void *unused) { - UNUSED(unused); - reader_repaint_if_needed(); -} - void reader_react_to_color_change() { if (!data) return; if (!data->repaint_needed || !data->screen_reset_needed) { data->repaint_needed = true; data->screen_reset_needed = true; - input_common_add_callback(reader_repaint_if_needed_one_arg, NULL); + input_common_add_callback(reader_repaint_if_needed); } }