1 #ifndef MESHLINK_EVENT_H
2 #define MESHLINK_EVENT_H
5 event.h -- I/O, timeout and signal event handling
6 Copyright (C) 2014, 2017 Guus Sliepen <guus@meshlink.io>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "splay_tree.h"
30 typedef struct event_loop_t event_loop_t;
32 typedef void (*io_cb_t)(event_loop_t *loop, void *data, int flags);
33 typedef void (*timeout_cb_t)(event_loop_t *loop, void *data);
34 typedef void (*signal_cb_t)(event_loop_t *loop, void *data);
35 typedef struct timespec(*idle_cb_t)(event_loop_t *loop, void *data);
38 struct splay_node_t node;
45 typedef struct timeout_t {
46 struct splay_node_t node;
52 typedef struct signal_t {
53 struct splay_node_t node;
55 #ifdef HAVE_STDATOMIC_H
56 volatile atomic_flag set;
65 volatile bool running;
70 splay_tree_t timeouts;
83 void io_add(event_loop_t *loop, io_t *io, io_cb_t cb, void *data, int fd, int flags);
84 void io_del(event_loop_t *loop, io_t *io);
85 void io_set(event_loop_t *loop, io_t *io, int flags);
87 void timeout_add(event_loop_t *loop, timeout_t *timeout, timeout_cb_t cb, void *data, struct timespec *tv);
88 void timeout_del(event_loop_t *loop, timeout_t *timeout);
89 void timeout_set(event_loop_t *loop, timeout_t *timeout, struct timespec *tv);
91 void signal_add(event_loop_t *loop, signal_t *sig, signal_cb_t cb, void *data, uint8_t signum);
92 void signal_trigger(event_loop_t *loop, signal_t *sig);
93 void signal_del(event_loop_t *loop, signal_t *sig);
95 void idle_set(event_loop_t *loop, idle_cb_t cb, void *data);
97 void event_loop_init(event_loop_t *loop);
98 void event_loop_exit(event_loop_t *loop);
99 bool event_loop_run(event_loop_t *loop, pthread_mutex_t *mutex) __attribute__((__warn_unused_result__));
100 void event_loop_flush_output(event_loop_t *loop);
101 void event_loop_start(event_loop_t *loop);
102 void event_loop_stop(event_loop_t *loop);