evtloop_main.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright (C) 2019-2020 Alibaba Group Holding Limited
  3. */
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <stdbool.h>
  7. #include <sys/time.h>
  8. #include <aos/yloop.h>
  9. #include <aos/kernel.h>
  10. #include <aos/list.h>
  11. #include <errno.h>
  12. #include "evtloop_main.h"
  13. //static const char *TAG = "eventloop";
  14. #ifndef DEBUG_MALLOC
  15. #define DEBUG_MALLOC malloc
  16. #endif
  17. #ifndef DEBUG_REALLOC
  18. #define DEBUG_REALLOC realloc
  19. #endif
  20. #ifndef DEBUG_CALLOC
  21. #define DEBUG_CALLOC calloc
  22. #endif
  23. #ifndef DEBUG_FREE
  24. #define DEBUG_FREE free
  25. #endif
  26. #define LOOP_WDT_TIMER (1000 * 10)
  27. typedef void (*eventloop_timeout_cb)(void *private_data);
  28. typedef struct eventloop_timeout_s {
  29. dlist_t next;
  30. long long timeout_ms;
  31. void *private_data;
  32. eventloop_timeout_cb cb;
  33. int ms;
  34. } eventloop_timeout_t;
  35. typedef struct {
  36. dlist_t timeouts;
  37. eventloop_sock_t reader;
  38. uint8_t max_sock;
  39. bool pending_terminate;
  40. bool terminate;
  41. } eventloop_ctx_t;
  42. static eventloop_ctx_t g_main_ctx;
  43. static inline eventloop_ctx_t *get_context(void)
  44. {
  45. return &g_main_ctx;
  46. }
  47. extern int event_poll(void *reader, bool setup, void *sem);
  48. extern void event_read_cb(void);
  49. aos_loop_t aos_loop_init(void)
  50. {
  51. memset(&g_main_ctx, 0, sizeof(eventloop_ctx_t));
  52. g_main_ctx.reader.poll = event_poll;
  53. g_main_ctx.reader.cb = event_read_cb;
  54. eventloop_local_event_init();
  55. dlist_init(&g_main_ctx.timeouts);
  56. return &g_main_ctx;
  57. }
  58. aos_loop_t aos_current_loop(void)
  59. {
  60. return (void *)&g_main_ctx;
  61. }
  62. static int eventloop_poll(int timeout)
  63. {
  64. eventloop_ctx_t *ctx = get_context();
  65. aos_sem_t sem;
  66. int ret = 0;
  67. aos_sem_new(&sem, 0);
  68. /* clear reader event flag */
  69. ctx->reader.event = 0;
  70. /* poll readers */
  71. ctx->reader.poll(&ctx->reader, true, &sem);
  72. if (!timeout) {
  73. ret = 0;
  74. goto check_poll;
  75. }
  76. if (timeout == -1) {
  77. ret = aos_sem_wait(&sem, AOS_WAIT_FOREVER);
  78. } else {
  79. ret = aos_sem_wait(&sem, timeout);
  80. }
  81. check_poll:
  82. ctx->reader.poll(&ctx->reader, false, &sem);
  83. if (ctx->reader.event) {
  84. ret++;
  85. }
  86. aos_sem_free(&sem);
  87. return ret < 0 ? 0 : ret;
  88. }
  89. void aos_loop_run(void)
  90. {
  91. eventloop_ctx_t *ctx = get_context();
  92. while (!ctx->terminate) {
  93. int delayed_ms = -1;
  94. if (!dlist_empty(&ctx->timeouts)) {
  95. eventloop_timeout_t *tmo = dlist_first_entry(&ctx->timeouts, eventloop_timeout_t, next);
  96. long long now = aos_now_ms();
  97. if (now < tmo->timeout_ms) {
  98. delayed_ms = tmo->timeout_ms - now;
  99. } else {
  100. delayed_ms = 0;
  101. }
  102. }
  103. int res = eventloop_poll(delayed_ms);
  104. /* check if some registered timeouts have occurred */
  105. if (!dlist_empty(&ctx->timeouts)) {
  106. eventloop_timeout_t *tmo = dlist_first_entry(&ctx->timeouts, eventloop_timeout_t, next);
  107. long long now = aos_now_ms();
  108. if (now >= tmo->timeout_ms) {
  109. dlist_del(&tmo->next);
  110. tmo->cb(tmo->private_data);
  111. DEBUG_FREE(tmo);
  112. }
  113. }
  114. if (res <= 0) {
  115. continue;
  116. }
  117. if (ctx->reader.event) {
  118. ctx->reader.cb();
  119. }
  120. }
  121. ctx->terminate = 0;
  122. }
  123. void aos_loop_exit(void)
  124. {
  125. eventloop_ctx_t *ctx = get_context();
  126. ctx->terminate = 1;
  127. aos_post_event(EV_SYS, CODE_SYS_LOOP_EXIT, VALUE_NULL);
  128. }
  129. void aos_loop_destroy(void)
  130. {
  131. eventloop_ctx_t *ctx = get_context();
  132. eventloop_local_event_deinit();
  133. while (!dlist_empty(&ctx->timeouts)) {
  134. eventloop_timeout_t *timeout = dlist_first_entry(&ctx->timeouts, eventloop_timeout_t,
  135. next);
  136. dlist_del(&timeout->next);
  137. DEBUG_FREE(timeout);
  138. }
  139. }
  140. int aos_post_delayed_action(int ms, aos_call_t action, void *param)
  141. {
  142. //int ret;
  143. eventloop_timeout_t *tmp;
  144. eventloop_timeout_t *timeout;
  145. eventloop_ctx_t *ctx = get_context();
  146. if (action == NULL || ms < 0) {
  147. return -EINVAL;
  148. }
  149. timeout = DEBUG_MALLOC(sizeof(*timeout));
  150. if (timeout == NULL) {
  151. return -ENOMEM;
  152. }
  153. timeout->timeout_ms = aos_now_ms() + ms;
  154. timeout->private_data = param;
  155. timeout->cb = action;
  156. timeout->ms = ms;
  157. dlist_for_each_entry(&ctx->timeouts, tmp, eventloop_timeout_t, next) {
  158. if (timeout->timeout_ms < tmp->timeout_ms) {
  159. break;
  160. }
  161. }
  162. dlist_add_tail(&timeout->next, &tmp->next);
  163. /* fix bug: if no timer, loop will wait forever, post event active loop */
  164. aos_post_event(EV_SYS, CODE_NULL, VALUE_NULL);
  165. return 0;
  166. }
  167. void aos_cancel_delayed_action(int ms, aos_call_t action, void *param)
  168. {
  169. eventloop_ctx_t *ctx = get_context();
  170. eventloop_timeout_t *tmp;
  171. if (action == NULL) {
  172. return;
  173. }
  174. dlist_for_each_entry(&ctx->timeouts, tmp, eventloop_timeout_t, next) {
  175. if (ms >= 0 && tmp->ms != ms) {
  176. continue;
  177. }
  178. if (tmp->cb != action) {
  179. continue;
  180. }
  181. if (tmp->private_data != param) {
  182. continue;
  183. }
  184. dlist_del(&tmp->next);
  185. DEBUG_FREE(tmp);
  186. return;
  187. }
  188. }