ev.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /** @file ev.c
  2. * Single event for scheduling.
  3. */
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <assert.h>
  7. #include "private.h"
  8. #include "faux/str.h"
  9. #include "faux/schev.h"
  10. int faux_ev_compare(const void *first, const void *second)
  11. {
  12. const faux_ev_t *f = (const faux_ev_t *)first;
  13. const faux_ev_t *s = (const faux_ev_t *)second;
  14. return faux_timespec_cmp(&f->time, &s->time);
  15. }
  16. int faux_ev_compare_id(const void *key, const void *list_item)
  17. {
  18. int *f = (int *)key;
  19. const faux_ev_t *s = (const faux_ev_t *)list_item;
  20. return ((*f == s->id) ? 0 : 1);
  21. }
  22. int faux_ev_compare_data(const void *key, const void *list_item)
  23. {
  24. void *f = (void *)key;
  25. const faux_ev_t *s = (const faux_ev_t *)list_item;
  26. return ((f == s->data) ? 0 : 1);
  27. }
  28. faux_ev_t *faux_ev_new(const struct timespec *time, int ev_id, void *data)
  29. {
  30. faux_ev_t *ev = NULL;
  31. ev = faux_zmalloc(sizeof(*ev));
  32. assert(ev);
  33. if (!ev)
  34. return NULL;
  35. // Initialize
  36. if (time) {
  37. ev->time = *time;
  38. } else {
  39. struct timespec t = {};
  40. clock_gettime(FAUX_SCHEV_CLOCK_SOURCE, &t);
  41. ev->time = t;
  42. }
  43. ev->id = ev_id;
  44. ev->data = data;
  45. ev->periodic = FAUX_SCHEV_ONCE; // Not periodic by default
  46. ev->cycles_num = 0;
  47. faux_nsec_to_timespec(&ev->interval, 0l);
  48. return ev;
  49. }
  50. void faux_ev_free(void *ptr)
  51. {
  52. faux_ev_t *ev = (faux_ev_t *)ptr;
  53. if (!ev)
  54. return;
  55. faux_free(ev);
  56. }
  57. int faux_ev_periodic(faux_ev_t *schev,
  58. struct timespec *interval, int cycles_num)
  59. {
  60. assert(schev);
  61. assert(interval);
  62. // When cycles_num == 0 then periodic has no meaning
  63. if (!schev || !interval || cycles_num == 0)
  64. return -1;
  65. schev->periodic = FAUX_SCHEV_PERIODIC;
  66. schev->cycles_num = cycles_num;
  67. schev->interval = *interval;
  68. return 0;
  69. }
  70. faux_schev_periodic_t faux_ev_is_periodic(faux_ev_t *schev)
  71. {
  72. assert(schev);
  73. if (!schev)
  74. return FAUX_SCHEV_ONCE;
  75. return schev->periodic;
  76. }
  77. int faux_ev_dec_cycles(faux_ev_t *schev, int *new_cycles_num)
  78. {
  79. assert(schev);
  80. if (!schev)
  81. return -1;
  82. if (schev->periodic != FAUX_SCHEV_PERIODIC)
  83. return -1; // Non-periodic event
  84. if ((schev->cycles_num != FAUX_SCHEV_CYCLES_INFINITE) &&
  85. (schev->cycles_num > 0))
  86. schev->cycles_num--;
  87. if (new_cycles_num)
  88. *new_cycles_num = schev->cycles_num;
  89. return 0;
  90. }
  91. int faux_ev_id(const faux_ev_t *ev)
  92. {
  93. assert(ev);
  94. if (!ev)
  95. return -1;
  96. return ev->id;
  97. }
  98. void *faux_ev_data(const faux_ev_t *ev)
  99. {
  100. assert(ev);
  101. if (!ev)
  102. return NULL;
  103. return ev->data;
  104. }
  105. const struct timespec *faux_ev_time(const faux_ev_t *ev)
  106. {
  107. assert(ev);
  108. if (!ev)
  109. return NULL;
  110. return &ev->time;
  111. }