timer_wrapper.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 * thread_modulers_timer_check(void * arg)
  36. {
  37. uint32 ms_to_expiry;
  38. uint64 us_to_wait;
  39. while (timer_thread_run) {
  40. ms_to_expiry = (uint32)-1;
  41. os_mutex_lock(&g_timer_ctx_list_mutex);
  42. timer_ctx_node_t* elem = (timer_ctx_node_t*)
  43. bh_list_first_elem(&g_timer_ctx_list);
  44. while (elem) {
  45. uint32 next = check_app_timers(elem->timer_ctx);
  46. if (next != (uint32)-1) {
  47. if (ms_to_expiry == (uint32)-1 || ms_to_expiry > next)
  48. ms_to_expiry = next;
  49. }
  50. elem = (timer_ctx_node_t*) bh_list_elem_next(elem);
  51. }
  52. os_mutex_unlock(&g_timer_ctx_list_mutex);
  53. if (ms_to_expiry == (uint32)-1)
  54. us_to_wait = BHT_WAIT_FOREVER;
  55. else
  56. us_to_wait = (uint64)ms_to_expiry * 1000;
  57. os_mutex_lock(&g_timer_ctx_list_mutex);
  58. os_cond_reltimedwait(&g_timer_ctx_list_cond, &g_timer_ctx_list_mutex,
  59. us_to_wait);
  60. os_mutex_unlock(&g_timer_ctx_list_mutex);
  61. }
  62. return NULL;
  63. }
  64. void
  65. wakeup_modules_timer_thread(timer_ctx_t ctx)
  66. {
  67. os_mutex_lock(&g_timer_ctx_list_mutex);
  68. os_cond_signal(&g_timer_ctx_list_cond);
  69. os_mutex_unlock(&g_timer_ctx_list_mutex);
  70. }
  71. void
  72. init_wasm_timer()
  73. {
  74. korp_tid tm_tid;
  75. bh_list_init(&g_timer_ctx_list);
  76. os_cond_init(&g_timer_ctx_list_cond);
  77. /* temp solution for: thread_modulers_timer_check thread
  78. would recursive lock the mutex */
  79. os_recursive_mutex_init(&g_timer_ctx_list_mutex);
  80. os_thread_create(&tm_tid, thread_modulers_timer_check,
  81. NULL, BH_APPLET_PRESERVED_STACK_SIZE);
  82. }
  83. void
  84. exit_wasm_timer()
  85. {
  86. timer_thread_run = false;
  87. }
  88. timer_ctx_t
  89. create_wasm_timer_ctx(unsigned int module_id, int prealloc_num)
  90. {
  91. timer_ctx_t ctx = create_timer_ctx(wasm_timer_callback,
  92. wakeup_modules_timer_thread,
  93. prealloc_num, module_id);
  94. if (ctx == NULL)
  95. return NULL;
  96. timer_ctx_node_t * node = (timer_ctx_node_t*)
  97. wasm_runtime_malloc(sizeof(timer_ctx_node_t));
  98. if (node == NULL) {
  99. destroy_timer_ctx(ctx);
  100. return NULL;
  101. }
  102. memset(node, 0, sizeof(*node));
  103. node->timer_ctx = ctx;
  104. os_mutex_lock(&g_timer_ctx_list_mutex);
  105. bh_list_insert(&g_timer_ctx_list, node);
  106. os_mutex_unlock(&g_timer_ctx_list_mutex);
  107. return ctx;
  108. }
  109. void
  110. destroy_module_timer_ctx(unsigned int module_id)
  111. {
  112. timer_ctx_node_t* elem;
  113. os_mutex_lock(&g_timer_ctx_list_mutex);
  114. elem = (timer_ctx_node_t*)
  115. bh_list_first_elem(&g_timer_ctx_list);
  116. while (elem) {
  117. if (timer_ctx_get_owner(elem->timer_ctx) == module_id) {
  118. bh_list_remove(&g_timer_ctx_list, elem);
  119. destroy_timer_ctx(elem->timer_ctx);
  120. wasm_runtime_free(elem);
  121. break;
  122. }
  123. elem = (timer_ctx_node_t*) bh_list_elem_next(elem);
  124. }
  125. os_mutex_unlock(&g_timer_ctx_list_mutex);
  126. }
  127. timer_ctx_t
  128. get_wasm_timer_ctx(wasm_module_inst_t module_inst)
  129. {
  130. module_data * m = app_manager_get_module_data(Module_WASM_App,
  131. module_inst);
  132. if (m == NULL)
  133. return NULL;
  134. return m->timer_ctx;
  135. }
  136. timer_id_t
  137. wasm_create_timer(wasm_exec_env_t exec_env,
  138. int interval, bool is_period, bool auto_start)
  139. {
  140. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  141. timer_ctx_t timer_ctx = get_wasm_timer_ctx(module_inst);
  142. bh_assert(timer_ctx);
  143. return sys_create_timer(timer_ctx, interval, is_period, auto_start);
  144. }
  145. void
  146. wasm_timer_destroy(wasm_exec_env_t exec_env, timer_id_t timer_id)
  147. {
  148. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  149. timer_ctx_t timer_ctx = get_wasm_timer_ctx(module_inst);
  150. bh_assert(timer_ctx);
  151. sys_timer_destroy(timer_ctx, timer_id);
  152. }
  153. void
  154. wasm_timer_cancel(wasm_exec_env_t exec_env, timer_id_t timer_id)
  155. {
  156. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  157. timer_ctx_t timer_ctx = get_wasm_timer_ctx(module_inst);
  158. bh_assert(timer_ctx);
  159. sys_timer_cancel(timer_ctx, timer_id);
  160. }
  161. void
  162. wasm_timer_restart(wasm_exec_env_t exec_env,
  163. timer_id_t timer_id, int interval)
  164. {
  165. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  166. timer_ctx_t timer_ctx = get_wasm_timer_ctx(module_inst);
  167. bh_assert(timer_ctx);
  168. sys_timer_restart(timer_ctx, timer_id, interval);
  169. }
  170. uint32
  171. wasm_get_sys_tick_ms(wasm_exec_env_t exec_env)
  172. {
  173. return (uint32)bh_get_tick_ms();
  174. }