Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

event-loop: poll a signalfd to know when to quit #79

Merged
merged 1 commit into from
Oct 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 40 additions & 12 deletions event-loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,46 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/timerfd.h>
#include <signal.h>
#include <sys/signalfd.h>
#include <time.h>
#include <unistd.h>

#include "event-loop.h"

void init_event_loop(struct mako_event_loop *loop, sd_bus *bus,
static int init_signalfd() {
sigset_t mask;
int sfd;

sigemptyset(&mask);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGTERM);
sigaddset(&mask, SIGQUIT);

if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1) {
fprintf(stderr, "sigprocmask: %s", strerror(errno));
return -1;
}

if ((sfd = signalfd(-1, &mask, SFD_NONBLOCK)) == -1) {
fprintf(stderr, "signalfd: %s", strerror(errno));
return -1;
}

return sfd;
}

bool init_event_loop(struct mako_event_loop *loop, sd_bus *bus,
struct wl_display *display) {
if ((loop->sfd = init_signalfd()) == -1) {
return false;
}

loop->fds[MAKO_EVENT_SIGNAL] = (struct pollfd){
.fd = loop->sfd,
.events = POLLIN,
};

loop->fds[MAKO_EVENT_DBUS] = (struct pollfd){
.fd = sd_bus_get_fd(bus),
.events = POLLIN,
Expand All @@ -29,6 +62,8 @@ void init_event_loop(struct mako_event_loop *loop, sd_bus *bus,
loop->bus = bus;
loop->display = display;
wl_list_init(&loop->timers);

return true;
}

void finish_event_loop(struct mako_event_loop *loop) {
Expand Down Expand Up @@ -172,6 +207,10 @@ int run_event_loop(struct mako_event_loop *loop) {
break;
}

if (loop->fds[MAKO_EVENT_SIGNAL].revents & POLLIN) {
break;
}

if (!(loop->fds[MAKO_EVENT_WAYLAND].revents & POLLIN)) {
wl_display_cancel_read(loop->display);
}
Expand Down Expand Up @@ -211,14 +250,3 @@ int run_event_loop(struct mako_event_loop *loop) {
}
return ret;
}

static void handle_stop_event_loop_timer(void *data) {
// No-op
}

void stop_event_loop(struct mako_event_loop *loop) {
loop->running = false;

// Wake up the event loop
add_event_loop_timer(loop, 0, handle_stop_event_loop_timer, NULL);
}
5 changes: 3 additions & 2 deletions include/event-loop.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ enum mako_event {
MAKO_EVENT_DBUS,
MAKO_EVENT_WAYLAND,
MAKO_EVENT_TIMER,
MAKO_EVENT_SIGNAL,
MAKO_EVENT_COUNT, // keep last
};

struct mako_event_loop {
struct pollfd fds[MAKO_EVENT_COUNT];
sd_bus *bus;
struct wl_display *display;
int sfd;

bool running;
struct wl_list timers; // mako_timer::link
Expand All @@ -34,11 +36,10 @@ struct mako_timer {
struct wl_list link; // mako_event_loop::timers
};

void init_event_loop(struct mako_event_loop *loop, sd_bus *bus,
bool init_event_loop(struct mako_event_loop *loop, sd_bus *bus,
struct wl_display *display);
void finish_event_loop(struct mako_event_loop *loop);
int run_event_loop(struct mako_event_loop *loop);
void stop_event_loop(struct mako_event_loop *loop);
struct mako_timer *add_event_loop_timer(struct mako_event_loop *loop,
int delay_ms, mako_event_loop_timer_func_t func, void *data);

Expand Down
14 changes: 5 additions & 9 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -45,7 +44,11 @@ static bool init(struct mako_state *state) {
finish_dbus(state);
return false;
}
init_event_loop(&state->event_loop, state->bus, state->display);
if (!init_event_loop(&state->event_loop, state->bus, state->display)) {
finish_dbus(state);
finish_wayland(state);
return false;
}
wl_list_init(&state->notifications);
return true;
}
Expand All @@ -62,10 +65,6 @@ static void finish(struct mako_state *state) {

static struct mako_event_loop *event_loop = NULL;

static void handle_signal(int signum) {
stop_event_loop(event_loop);
}

int main(int argc, char *argv[]) {
struct mako_state state = {0};

Expand All @@ -89,9 +88,6 @@ int main(int argc, char *argv[]) {
}

event_loop = &state.event_loop;
struct sigaction sa = { .sa_handler = handle_signal };
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);

ret = run_event_loop(&state.event_loop);

Expand Down