/*
- event.c -- event queue
- Copyright (C) 2002-2006 Guus Sliepen <guus@tinc-vpn.org>,
- 2002-2005 Ivo Timmermans
+ event.c -- I/O, timeout, and event handling
+ Copyright (C) 2012-2022 Guus Sliepen <guus@tinc-vpn.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-
- $Id$
+ You should have received a copy of the GNU General Public License along
+ with this program; if not, write to the Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "system.h"
-#include "avl_tree.h"
#include "event.h"
-#include "utils.h"
-#include "xalloc.h"
-
-avl_tree_t *event_tree;
-extern time_t now;
-int id;
+struct timeval now;
-static int event_compare(const event_t *a, const event_t *b)
-{
- if(a->time > b->time)
- return 1;
+static int io_compare(const io_t *a, const io_t *b) {
+#ifndef HAVE_WINDOWS
+ return a->fd - b->fd;
+#else
- if(a->time < b->time)
+ if(a->event < b->event) {
return -1;
+ }
+
+ if(a->event > b->event) {
+ return 1;
+ }
- return a->id - b->id;
+ return 0;
+#endif
}
-void init_events(void)
-{
- cp();
+static int timeout_compare(const timeout_t *a, const timeout_t *b) {
+ struct timeval diff;
+ timersub(&a->tv, &b->tv, &diff);
- event_tree = avl_alloc_tree((avl_compare_t) event_compare, NULL);
-}
+ if(diff.tv_sec < 0) {
+ return -1;
+ }
-void exit_events(void)
-{
- cp();
+ if(diff.tv_sec > 0) {
+ return 1;
+ }
- avl_delete_tree(event_tree);
-}
+ if(diff.tv_usec < 0) {
+ return -1;
+ }
-void flush_events(void)
-{
- avl_tree_t *to_flush;
- event_t *event;
+ if(diff.tv_usec > 0) {
+ return 1;
+ }
- /*
- * Events can be inserted from event handlers, so only flush events
- * already in the priority queue.
- */
+ uintptr_t ap = (uintptr_t)a;
+ uintptr_t bp = (uintptr_t)b;
- cp();
+ if(ap < bp) {
+ return -1;
+ }
- to_flush = event_tree;
- init_events();
- while (to_flush->head) {
- event = to_flush->head->data;
- event->handler(event->data);
- avl_delete(to_flush, event);
+ if(ap > bp) {
+ return 1;
}
- avl_delete_tree(to_flush);
+
+ return 0;
}
-event_t *new_event(void)
-{
- cp();
+splay_tree_t io_tree = {.compare = (splay_compare_t)io_compare};
+splay_tree_t timeout_tree = {.compare = (splay_compare_t)timeout_compare};
- return xmalloc_and_zero(sizeof(event_t));
+void timeout_add(timeout_t *timeout, timeout_cb_t cb, void *data, const struct timeval *tv) {
+ timeout->cb = cb;
+ timeout->data = data;
+ timeout->node.data = timeout;
+
+ timeout_set(timeout, tv);
}
-void free_event(event_t *event)
-{
- cp();
+void timeout_set(timeout_t *timeout, const struct timeval *tv) {
+ if(timerisset(&timeout->tv)) {
+ splay_unlink_node(&timeout_tree, &timeout->node);
+ }
- free(event);
-}
+ if(!now.tv_sec) {
+ gettimeofday(&now, NULL);
+ }
-void event_add(event_t *event)
-{
- cp();
+ timeradd(&now, tv, &timeout->tv);
- event->id = ++id;
- avl_insert(event_tree, event);
+ if(!splay_insert_node(&timeout_tree, &timeout->node)) {
+ abort();
+ }
}
-void event_del(event_t *event)
-{
- cp();
+void timeout_del(timeout_t *timeout) {
+ if(!timeout->cb) {
+ return;
+ }
- avl_delete(event_tree, event);
+ splay_unlink_node(&timeout_tree, &timeout->node);
+ timeout->cb = 0;
+ timeout->tv = (struct timeval) {
+ 0, 0
+ };
}
-event_t *get_expired_event(void)
-{
- event_t *event;
+struct timeval *timeout_execute(struct timeval *diff) {
+ gettimeofday(&now, NULL);
+ struct timeval *tv = NULL;
- cp();
+ while(timeout_tree.head) {
+ timeout_t *timeout = timeout_tree.head->data;
+ timersub(&timeout->tv, &now, diff);
- if(event_tree->head) {
- event = event_tree->head->data;
+ if(diff->tv_sec < 0) {
+ timeout->cb(timeout->data);
- if(event->time < now) {
- event_del(event);
- return event;
+ if(timercmp(&timeout->tv, &now, <)) {
+ timeout_del(timeout);
+ }
+ } else {
+ tv = diff;
+ break;
}
}
- return NULL;
+ return tv;
}