timer_wrapper.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "bh_platform.h"
  6. #include "app_manager_export.h"
  7. #include "../app-manager/module_wasm_app.h"
  8. #include "timer_native_api.h"
  9. typedef struct {
  10. bh_list_link l;
  11. timer_ctx_t timer_ctx;
  12. } timer_ctx_node_t;
  13. static bool timer_thread_run = true;
  14. static bh_list g_timer_ctx_list;
  15. static korp_cond g_timer_ctx_list_cond;
  16. static korp_mutex g_timer_ctx_list_mutex;
  17. void
  18. wasm_timer_callback(timer_id_t id, unsigned int mod_id)
  19. {
  20. module_data *module = module_data_list_lookup_id(mod_id);
  21. if (module == NULL)
  22. return;
  23. // !!! the length parameter must be 0, so the receiver will
  24. // not free the payload pointer.
  25. bh_post_msg(module->queue, TIMER_EVENT_WASM, (char *)(uintptr_t)id, 0);
  26. }
  27. /**
  28. * why we create a separate link for module timer contexts
  29. * rather than traverse the module list?
  30. * It helps to reduce the lock frequency for the module list.
  31. * Also when we lock the module list and then call the callback for
  32. * timer expire, the callback is request the list lock again for lookup
  33. * the module from module id. It is for avoiding that situation.
  34. */
  35. void *
  36. thread_modulers_timer_check(void *arg)
  37. {
  38. uint32 ms_to_expiry;
  39. uint64 us_to_wait;
  40. while (timer_thread_run) {
  41. ms_to_expiry = (uint32)-1;
  42. os_mutex_lock(&g_timer_ctx_list_mutex);
  43. timer_ctx_node_t *elem =
  44. (timer_ctx_node_t *)bh_list_first_elem(&g_timer_ctx_list);
  45. while (elem) {
  46. uint32 next = check_app_timers(elem->timer_ctx);
  47. if (next != (uint32)-1) {
  48. if (ms_to_expiry == (uint32)-1 || ms_to_expiry > next)
  49. ms_to_expiry = next;
  50. }
  51. elem = (timer_ctx_node_t *)bh_list_elem_next(elem);
  52. }
  53. os_mutex_unlock(&g_timer_ctx_list_mutex);
  54. if (ms_to_expiry == (uint32)-1)
  55. us_to_wait = BHT_WAIT_FOREVER;
  56. else
  57. us_to_wait = (uint64)ms_to_expiry * 1000;
  58. os_mutex_lock(&g_timer_ctx_list_mutex);
  59. os_cond_reltimedwait(&g_timer_ctx_list_cond, &g_timer_ctx_list_mutex,
  60. us_to_wait);
  61. os_mutex_unlock(&g_timer_ctx_list_mutex);
  62. }
  63. return NULL;
  64. }
  65. void
  66. wakeup_modules_timer_thread(timer_ctx_t ctx)
  67. {
  68. os_mutex_lock(&g_timer_ctx_list_mutex);
  69. os_cond_signal(&g_timer_ctx_list_cond);
  70. os_mutex_unlock(&g_timer_ctx_list_mutex);
  71. }
  72. bool
  73. init_wasm_timer()
  74. {
  75. korp_tid tm_tid;
  76. bh_list_init(&g_timer_ctx_list);
  77. if (os_cond_init(&g_timer_ctx_list_cond) != 0) {
  78. return false;
  79. }
  80. /* temp solution for: thread_modulers_timer_check thread
  81. would recursive lock the mutex */
  82. if (os_recursive_mutex_init(&g_timer_ctx_list_mutex) != 0) {
  83. goto fail1;
  84. }
  85. if (0
  86. != os_thread_create(&tm_tid, thread_modulers_timer_check, NULL,
  87. BH_APPLET_PRESERVED_STACK_SIZE)) {
  88. goto fail2;
  89. }
  90. return true;
  91. fail2:
  92. os_mutex_destroy(&g_timer_ctx_list_mutex);
  93. fail1:
  94. os_cond_destroy(&g_timer_ctx_list_cond);
  95. return false;
  96. }
  97. void
  98. exit_wasm_timer()
  99. {
  100. timer_thread_run = false;
  101. }
  102. timer_ctx_t
  103. create_wasm_timer_ctx(unsigned int module_id, int prealloc_num)
  104. {
  105. timer_ctx_t ctx =
  106. create_timer_ctx(wasm_timer_callback, wakeup_modules_timer_thread,
  107. prealloc_num, module_id);
  108. if (ctx == NULL)
  109. return NULL;
  110. timer_ctx_node_t *node =
  111. (timer_ctx_node_t *)wasm_runtime_malloc(sizeof(timer_ctx_node_t));
  112. if (node == NULL) {
  113. destroy_timer_ctx(ctx);
  114. return NULL;
  115. }
  116. memset(node, 0, sizeof(*node));
  117. node->timer_ctx = ctx;
  118. os_mutex_lock(&g_timer_ctx_list_mutex);
  119. bh_list_insert(&g_timer_ctx_list, node);
  120. os_mutex_unlock(&g_timer_ctx_list_mutex);
  121. return ctx;
  122. }
  123. void
  124. destroy_module_timer_ctx(unsigned int module_id)
  125. {
  126. timer_ctx_node_t *elem;
  127. os_mutex_lock(&g_timer_ctx_list_mutex);
  128. elem = (timer_ctx_node_t *)bh_list_first_elem(&g_timer_ctx_list);
  129. while (elem) {
  130. if (timer_ctx_get_owner(elem->timer_ctx) == module_id) {
  131. bh_list_remove(&g_timer_ctx_list, elem);
  132. destroy_timer_ctx(elem->timer_ctx);
  133. wasm_runtime_free(elem);
  134. break;
  135. }
  136. elem = (timer_ctx_node_t *)bh_list_elem_next(elem);
  137. }
  138. os_mutex_unlock(&g_timer_ctx_list_mutex);
  139. }
  140. timer_ctx_t
  141. get_wasm_timer_ctx(wasm_module_inst_t module_inst)
  142. {
  143. module_data *m = app_manager_get_module_data(Module_WASM_App, module_inst);
  144. if (m == NULL)
  145. return NULL;
  146. return m->timer_ctx;
  147. }
  148. timer_id_t
  149. wasm_create_timer(wasm_exec_env_t exec_env, int interval, bool is_period,
  150. bool auto_start)
  151. {
  152. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  153. timer_ctx_t timer_ctx = get_wasm_timer_ctx(module_inst);
  154. bh_assert(timer_ctx);
  155. return sys_create_timer(timer_ctx, interval, is_period, auto_start);
  156. }
  157. void
  158. wasm_timer_destroy(wasm_exec_env_t exec_env, timer_id_t timer_id)
  159. {
  160. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  161. timer_ctx_t timer_ctx = get_wasm_timer_ctx(module_inst);
  162. bh_assert(timer_ctx);
  163. sys_timer_destroy(timer_ctx, timer_id);
  164. }
  165. void
  166. wasm_timer_cancel(wasm_exec_env_t exec_env, timer_id_t timer_id)
  167. {
  168. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  169. timer_ctx_t timer_ctx = get_wasm_timer_ctx(module_inst);
  170. bh_assert(timer_ctx);
  171. sys_timer_cancel(timer_ctx, timer_id);
  172. }
  173. void
  174. wasm_timer_restart(wasm_exec_env_t exec_env, timer_id_t timer_id, int interval)
  175. {
  176. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  177. timer_ctx_t timer_ctx = get_wasm_timer_ctx(module_inst);
  178. bh_assert(timer_ctx);
  179. sys_timer_restart(timer_ctx, timer_id, interval);
  180. }
  181. uint32
  182. wasm_get_sys_tick_ms(wasm_exec_env_t exec_env)
  183. {
  184. return (uint32)bh_get_tick_ms();
  185. }