timer_wrapper.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "runtime_timer.h"
  6. #include "app_manager_export.h"
  7. #include "module_wasm_app.h"
  8. #include "bh_list.h"
  9. #include "bh_thread.h"
  10. #include "bh_time.h"
  11. bh_list g_timer_ctx_list;
  12. korp_cond g_timer_ctx_list_cond;
  13. korp_mutex g_timer_ctx_list_mutex;
  14. typedef struct {
  15. bh_list_link l;
  16. timer_ctx_t timer_ctx;
  17. } timer_ctx_node_t;
  18. void 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. void * thread_modulers_timer_check(void * arg)
  35. {
  36. int ms_to_expiry;
  37. while (1) {
  38. ms_to_expiry = -1;
  39. vm_mutex_lock(&g_timer_ctx_list_mutex);
  40. timer_ctx_node_t* elem = (timer_ctx_node_t*)
  41. bh_list_first_elem(&g_timer_ctx_list);
  42. while (elem) {
  43. int next = check_app_timers(elem->timer_ctx);
  44. if (next != -1) {
  45. if (ms_to_expiry == -1 || ms_to_expiry > next)
  46. ms_to_expiry = next;
  47. }
  48. elem = (timer_ctx_node_t*) bh_list_elem_next(elem);
  49. }
  50. vm_mutex_unlock(&g_timer_ctx_list_mutex);
  51. if (ms_to_expiry == -1)
  52. ms_to_expiry = 60 * 1000;
  53. vm_mutex_lock(&g_timer_ctx_list_mutex);
  54. vm_cond_reltimedwait(&g_timer_ctx_list_cond, &g_timer_ctx_list_mutex,
  55. ms_to_expiry);
  56. vm_mutex_unlock(&g_timer_ctx_list_mutex);
  57. }
  58. }
  59. void wakeup_modules_timer_thread(timer_ctx_t ctx)
  60. {
  61. vm_mutex_lock(&g_timer_ctx_list_mutex);
  62. vm_cond_signal(&g_timer_ctx_list_cond);
  63. vm_mutex_unlock(&g_timer_ctx_list_mutex);
  64. }
  65. void init_wasm_timer()
  66. {
  67. korp_tid tm_tid;
  68. bh_list_init(&g_timer_ctx_list);
  69. vm_cond_init(&g_timer_ctx_list_cond);
  70. /* temp solution for: thread_modulers_timer_check thread would recursive lock the mutex */
  71. vm_recursive_mutex_init(&g_timer_ctx_list_mutex);
  72. vm_thread_create(&tm_tid, thread_modulers_timer_check,
  73. NULL, BH_APPLET_PRESERVED_STACK_SIZE);
  74. }
  75. timer_ctx_t create_wasm_timer_ctx(unsigned int module_id, int prealloc_num)
  76. {
  77. timer_ctx_t ctx = create_timer_ctx(wasm_timer_callback,
  78. wakeup_modules_timer_thread,
  79. prealloc_num,
  80. module_id);
  81. if (ctx == NULL)
  82. return NULL;
  83. timer_ctx_node_t * node = (timer_ctx_node_t*)
  84. bh_malloc(sizeof(timer_ctx_node_t));
  85. if (node == NULL) {
  86. destroy_timer_ctx(ctx);
  87. return NULL;
  88. }
  89. memset(node, 0, sizeof(*node));
  90. node->timer_ctx = ctx;
  91. vm_mutex_lock(&g_timer_ctx_list_mutex);
  92. bh_list_insert(&g_timer_ctx_list, node);
  93. vm_mutex_unlock(&g_timer_ctx_list_mutex);
  94. return ctx;
  95. }
  96. void destroy_module_timer_ctx(unsigned int module_id)
  97. {
  98. vm_mutex_lock(&g_timer_ctx_list_mutex);
  99. timer_ctx_node_t* elem = (timer_ctx_node_t*)
  100. bh_list_first_elem(&g_timer_ctx_list);
  101. while (elem) {
  102. if (timer_ctx_get_owner(elem->timer_ctx) == module_id) {
  103. bh_list_remove(&g_timer_ctx_list, elem);
  104. destroy_timer_ctx(elem->timer_ctx);
  105. bh_free(elem);
  106. break;
  107. }
  108. elem = (timer_ctx_node_t*) bh_list_elem_next(elem);
  109. }
  110. vm_mutex_unlock(&g_timer_ctx_list_mutex);
  111. }
  112. timer_ctx_t get_wasm_timer_ctx(wasm_module_inst_t module_inst)
  113. {
  114. module_data * m = app_manager_get_module_data(Module_WASM_App,
  115. module_inst);
  116. if (m == NULL)
  117. return NULL;
  118. return m->timer_ctx;
  119. }
  120. timer_id_t
  121. wasm_create_timer(wasm_module_inst_t module_inst,
  122. int interval, bool is_period, bool auto_start)
  123. {
  124. return sys_create_timer(get_wasm_timer_ctx(module_inst), interval, is_period,
  125. auto_start);
  126. }
  127. void
  128. wasm_timer_destroy(wasm_module_inst_t module_inst, timer_id_t timer_id)
  129. {
  130. sys_timer_destroy(get_wasm_timer_ctx(module_inst), timer_id);
  131. }
  132. void
  133. wasm_timer_cancel(wasm_module_inst_t module_inst, timer_id_t timer_id)
  134. {
  135. sys_timer_cancel(get_wasm_timer_ctx(module_inst), timer_id);
  136. }
  137. void
  138. wasm_timer_restart(wasm_module_inst_t module_inst,
  139. timer_id_t timer_id, int interval)
  140. {
  141. sys_timer_restart(get_wasm_timer_ctx(module_inst), timer_id, interval);
  142. }
  143. extern uint32 get_sys_tick_ms();
  144. uint32
  145. wasm_get_sys_tick_ms(wasm_module_inst_t module_inst)
  146. {
  147. return (uint32) bh_get_tick_ms();
  148. }