Make fire_event_callback take a std::function instead of function pointer

This commit is contained in:
ridiculousfish 2017-01-21 17:15:45 -08:00
parent a91dad35db
commit 439f233ccc
4 changed files with 13 additions and 29 deletions

View file

@ -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<event_t *>(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;
}

View file

@ -37,8 +37,7 @@ static int wait_on_escape_ms = WAIT_ON_ESCAPE_DEFAULT;
static std::deque<wchar_t> lookahead_list;
// Queue of pairs of (function pointer, argument) to be invoked. Expected to be mostly empty.
typedef std::pair<void (*)(void *), void *> callback_info_t;
typedef std::queue<callback_info_t, std::list<callback_info_t> > callback_queue_t;
typedef std::list<std::function<void(void)>> 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<void(void)> 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();
}
}

View file

@ -2,6 +2,7 @@
#ifndef INPUT_COMMON_H
#define INPUT_COMMON_H
#include <functional>
#include <stddef.h>
#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<void(void)>);
#endif

View file

@ -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);
}
}