timer_wrapper.c 5.6 KB

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