event.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #ifndef CX_ZIVUOS_EVENT_H_INCLUDED
  2. #define CX_ZIVUOS_EVENT_H_INCLUDED
  3. #include "zv_types.h"
  4. #include "event_type.h"
  5. #include "zv_time.h"
  6. /**
  7. * @brief List of event types.
  8. *
  9. * That enum is used to store type of the event. Default event is empty,
  10. * which mean that no event occur.
  11. */
  12. typedef enum zv_event_type_e {
  13. /* That mean no event occur. */
  14. ZV_EVENT_EMPTY,
  15. /* That mean event is timer, whose waiting time has already expired. */
  16. ZV_EVENT_TIMER,
  17. /*
  18. * That mean timer event is waiting in first phrase, it is used by
  19. * scheduler for timer processing.
  20. */
  21. ZV_EVENT_TIMER_PHRASE_FIRST,
  22. /*
  23. * That mean timer event is waiting in second phrase, it is used by
  24. * scheduler for timer processing.
  25. */
  26. ZV_EVENT_TIMER_PHRASE_SECOND,
  27. /* That mean event with data occured. */
  28. ZV_EVENT_DATA,
  29. /* That mean event with unsigned data occured. */
  30. ZV_EVENT_UDATA,
  31. /* That mean event with object pointer occured. */
  32. ZV_EVENT_OBJECT,
  33. } zv_event_type_t;
  34. /**
  35. * @brief That store content of the event.
  36. *
  37. * That union store content of event. Event could store pointer to any
  38. * object, number or time. How to interpret content of that union depends
  39. * on type of the event.
  40. */
  41. typedef union zv_event_content_u {
  42. /* Pointer to any object. */
  43. void *object;
  44. /* Time after which task would be ready. */
  45. zv_time_t time;
  46. /* Unsigned number. */
  47. uintptr_t unumber;
  48. /* Signed number. */
  49. intptr_t number;
  50. } zv_event_content_t;
  51. /**
  52. * @brief That is event container. Do not modify without API function.
  53. *
  54. * That is event container. Store type of the event and its content.
  55. * Type could also being empty, which mean that event did not occur.
  56. */
  57. typedef struct zv_event_s {
  58. /* Type of the event. */
  59. zv_event_type_t type;
  60. /* Content of the event. */
  61. zv_event_content_t content;
  62. } zv_event_t;
  63. /**
  64. * That would return true, when event is timer in any phrase. Software
  65. * timers are supported by scheduler, and it is used to determinate which
  66. * event must being updated by scheduler every task selection loop.
  67. */
  68. #define __zv_event_is_working_timer(X) (\
  69. X.type == ZV_EVENT_TIMER_PHRASE_FIRST \
  70. || X.type == ZV_EVENT_TIMER_PHRASE_SECOND \
  71. )
  72. /**
  73. * That return true, when any event occur. It is used to determinate
  74. * chech if the process should be updated to ZV_TASK_READY state.
  75. */
  76. #define __zv_event_is_set(X) ( \
  77. X.type != ZV_EVENT_EMPTY \
  78. && !__zv_event_is_working_timer(X) \
  79. )
  80. /**
  81. * That function clean event. After call it, target event would return to
  82. * default empty state with empty content.
  83. *
  84. * @param target Pointer to target event.
  85. */
  86. static inline void __zv_event_clear(zv_event_t *target) {
  87. target->type = ZV_EVENT_EMPTY;
  88. target->content = (zv_event_content_t) { .unumber = 0x00 };
  89. }
  90. /**
  91. * @brief That function send event with number.
  92. *
  93. * That function would trigger event with number content for choosen task.
  94. *
  95. * @param self Kernel object to work on.
  96. * @param target Target task TID.
  97. * @param content Content of event to push.
  98. * @return True when operation end with success. False when operation
  99. * could not being finalized, for example task not exists or
  100. * event already in use.
  101. */
  102. bool zv_event_push_number(
  103. struct zv_kernel_s *self,
  104. zv_tid_t target,
  105. int64_t content
  106. );
  107. /**
  108. * @brief That function send event with unsigned number.
  109. *
  110. * That function would trigger event with unsigned number content
  111. * for choosen task.
  112. *
  113. * @param self Kernel object to work on.
  114. * @param target Target task TID.
  115. * @param content Content of event to push.
  116. * @return True when operation end with success. False when operation
  117. * could not being finalized, for example task not exists or
  118. * event already in use.
  119. */
  120. bool zv_event_push_unumber(
  121. struct zv_kernel_s *self,
  122. zv_tid_t target,
  123. uint64_t content
  124. );
  125. /**
  126. * @brief That function set software timer for task.
  127. *
  128. * That function would use event to set software timer. It use event
  129. * to store timestamp after which time event would be triggered. Timestamp
  130. * would be calculated automatically by delay time.
  131. *
  132. * @param self Kernel object to work on.
  133. * @param target Target task TID.
  134. * @param delay_time Time after which timer event would be triggered
  135. * is ms. For example 500 for 500ms.
  136. * @return True when operation end with success. False when operation
  137. * could not being finalized, for example task not exists or
  138. * event already in use.
  139. */
  140. bool zv_event_set_timer(
  141. struct zv_kernel_s *self,
  142. zv_tid_t target,
  143. zv_time_t delay_time
  144. );
  145. /**
  146. * @brief That function send event with pointer to any data type.
  147. *
  148. * That function would trigger event with pointer to object as content.
  149. * Pointer is void type, which mean that data type must be known by
  150. * event receiver.
  151. *
  152. * @param self Kernel object to work on.
  153. * @param target Target task TID.
  154. * @param content Pointer to target data.
  155. * @return True when operation end with success. False when operation
  156. * could not being finalized, for example task not exists or
  157. * event already in use.
  158. */
  159. bool zv_event_push_object(
  160. struct zv_kernel_s *self,
  161. zv_tid_t target,
  162. void *content
  163. );
  164. #endif