-event_t *get_expired_event(void)
-{
- event_t *event;
- cp();
- if(event_tree->head)
- {
- event = (event_t *)event_tree->head->data;
- if(event->time < now)
- {
- avl_delete(event_tree, event);
- return event;
- }
- }
- cp();
- return NULL;
+struct timeval *timeout_execute(struct timeval *diff) {
+ gettimeofday(&now, NULL);
+ struct timeval *tv = NULL;
+
+ while(timeout_tree.head) {
+ timeout_t *timeout = timeout_tree.head->data;
+ timersub(&timeout->tv, &now, diff);
+
+ if(diff->tv_sec < 0) {
+ timeout->cb(timeout->data);
+
+ if(timercmp(&timeout->tv, &now, <)) {
+ timeout_del(timeout);
+ }
+ } else {
+ tv = diff;
+ break;
+ }
+ }
+
+ return tv;