event.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. event.c -- event queue
  3. Copyright (C) 2002-2009 Guus Sliepen <guus@tinc-vpn.org>,
  4. 2002-2005 Ivo Timmermans
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. #include "system.h"
  18. #include "avl_tree.h"
  19. #include "event.h"
  20. #include "utils.h"
  21. #include "xalloc.h"
  22. avl_tree_t *event_tree;
  23. extern time_t now;
  24. static int id;
  25. static int event_compare(const event_t *a, const event_t *b) {
  26. if(a->time > b->time) {
  27. return 1;
  28. }
  29. if(a->time < b->time) {
  30. return -1;
  31. }
  32. return a->id - b->id;
  33. }
  34. void init_events(void) {
  35. event_tree = avl_alloc_tree((avl_compare_t) event_compare, (avl_action_t) free_event);
  36. }
  37. void exit_events(void) {
  38. avl_delete_tree(event_tree);
  39. }
  40. void expire_events(void) {
  41. avl_node_t *node;
  42. event_t *event;
  43. time_t diff;
  44. /*
  45. * Make all events appear expired by subtracting the difference between
  46. * the expiration time of the last event and the current time.
  47. */
  48. if(!event_tree->tail) {
  49. return;
  50. }
  51. event = event_tree->tail->data;
  52. if(event->time <= now) {
  53. return;
  54. }
  55. diff = event->time - now;
  56. for(node = event_tree->head; node; node = node->next) {
  57. event = node->data;
  58. event->time -= diff;
  59. }
  60. }
  61. event_t *new_event(void) {
  62. return xmalloc_and_zero(sizeof(event_t));
  63. }
  64. void free_event(event_t *event) {
  65. free(event);
  66. }
  67. void event_add(event_t *event) {
  68. event->id = ++id;
  69. avl_insert(event_tree, event);
  70. }
  71. void event_del(event_t *event) {
  72. avl_delete(event_tree, event);
  73. }
  74. event_t *get_expired_event(void) {
  75. event_t *event;
  76. if(event_tree->head) {
  77. event = event_tree->head->data;
  78. if(event->time <= now) {
  79. avl_node_t *node = event_tree->head;
  80. avl_unlink_node(event_tree, node);
  81. free(node);
  82. return event;
  83. }
  84. }
  85. return NULL;
  86. }
  87. event_t *peek_next_event(void) {
  88. if(event_tree->head) {
  89. return event_tree->head->data;
  90. }
  91. return NULL;
  92. }