ev.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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/sched.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. ev->id = ev_id;
  37. ev->data = data;
  38. ev->periodic = FAUX_SCHED_ONCE; // Not periodic by default
  39. ev->cycles_num = 0;
  40. faux_nsec_to_timespec(&ev->interval, 0l);
  41. faux_ev_reschedule(ev, time);
  42. return ev;
  43. }
  44. void faux_ev_free(void *ptr)
  45. {
  46. faux_ev_t *ev = (faux_ev_t *)ptr;
  47. if (!ev)
  48. return;
  49. faux_free(ev);
  50. }
  51. int faux_ev_periodic(faux_ev_t *ev,
  52. const struct timespec *interval, int cycles_num)
  53. {
  54. assert(ev);
  55. assert(interval);
  56. // When cycles_num == 0 then periodic has no meaning
  57. if (!ev || !interval || cycles_num == 0)
  58. return -1;
  59. ev->periodic = FAUX_SCHED_PERIODIC;
  60. ev->cycles_num = cycles_num;
  61. ev->interval = *interval;
  62. return 0;
  63. }
  64. faux_sched_periodic_t faux_ev_is_periodic(faux_ev_t *ev)
  65. {
  66. assert(ev);
  67. if (!ev)
  68. return FAUX_SCHED_ONCE;
  69. return ev->periodic;
  70. }
  71. int faux_ev_dec_cycles(faux_ev_t *ev, int *new_cycles_num)
  72. {
  73. assert(ev);
  74. if (!ev)
  75. return -1;
  76. if (ev->periodic != FAUX_SCHED_PERIODIC)
  77. return -1; // Non-periodic event
  78. if ((ev->cycles_num != FAUX_SCHED_CYCLES_INFINITE) &&
  79. (ev->cycles_num > 0))
  80. ev->cycles_num--;
  81. if (new_cycles_num)
  82. *new_cycles_num = ev->cycles_num;
  83. return 0;
  84. }
  85. /**
  86. *
  87. * Note: faux_ev_new() use it. Be carefull.
  88. */
  89. int faux_ev_reschedule(faux_ev_t *ev, const struct timespec *new_time)
  90. {
  91. assert(ev);
  92. if (!ev)
  93. return -1;
  94. if (new_time) {
  95. ev->time = *new_time;
  96. } else { // Time isn't given so use "NOW"
  97. struct timespec t = {};
  98. clock_gettime(FAUX_SCHED_CLOCK_SOURCE, &t);
  99. ev->time = t;
  100. }
  101. return 0;
  102. }
  103. int faux_ev_reschedule_interval(faux_ev_t *ev)
  104. {
  105. struct timespec new_time = {};
  106. assert(ev);
  107. if (!ev)
  108. return -1;
  109. if (ev->periodic != FAUX_SCHED_PERIODIC)
  110. return -1;
  111. if (0 == ev->cycles_num)
  112. return -1;
  113. faux_timespec_sum(&new_time, &ev->time, &ev->interval);
  114. faux_ev_reschedule(ev, &new_time);
  115. if (ev->cycles_num != FAUX_SCHED_CYCLES_INFINITE)
  116. faux_ev_dec_cycles(ev, NULL);
  117. return 0;
  118. }
  119. int faux_ev_time_left(faux_ev_t *ev, struct timespec *left)
  120. {
  121. struct timespec now = {};
  122. assert(ev);
  123. assert(left);
  124. if (!ev || !left)
  125. return -1;
  126. clock_gettime(FAUX_SCHED_CLOCK_SOURCE, &now);
  127. if (faux_timespec_cmp(&now, &ev->time) > 0) { // Already happend
  128. faux_nsec_to_timespec(left, 0l);
  129. return 0;
  130. }
  131. faux_timespec_diff(left, &ev->time, &now);
  132. return 0;
  133. }
  134. int faux_ev_id(const faux_ev_t *ev)
  135. {
  136. assert(ev);
  137. if (!ev)
  138. return -1;
  139. return ev->id;
  140. }
  141. void *faux_ev_data(const faux_ev_t *ev)
  142. {
  143. assert(ev);
  144. if (!ev)
  145. return NULL;
  146. return ev->data;
  147. }
  148. const struct timespec *faux_ev_time(const faux_ev_t *ev)
  149. {
  150. assert(ev);
  151. if (!ev)
  152. return NULL;
  153. return &ev->time;
  154. }