timer_wrapper.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "runtime_timer.h"
  17. #include "app_manager_export.h"
  18. #include "module_wasm_app.h"
  19. #include "bh_list.h"
  20. #include "bh_thread.h"
  21. #include "bh_time.h"
  22. bh_list g_timer_ctx_list;
  23. korp_cond g_timer_ctx_list_cond;
  24. korp_mutex g_timer_ctx_list_mutex;
  25. typedef struct {
  26. bh_list_link l;
  27. timer_ctx_t timer_ctx;
  28. } timer_ctx_node_t;
  29. void wasm_timer_callback(timer_id_t id, unsigned int mod_id)
  30. {
  31. module_data* module = module_data_list_lookup_id(mod_id);
  32. if (module == NULL)
  33. return;
  34. // !!! the length parameter must be 0, so the receiver will
  35. // not free the payload pointer.
  36. bh_post_msg(module->queue, TIMER_EVENT_WASM, (char *)(uintptr_t)id, 0);
  37. }
  38. ///
  39. /// why we create a separate link for module timer contexts
  40. /// rather than traverse the module list?
  41. /// It helps to reduce the lock frequency for the module list.
  42. /// Also when we lock the module list and then call the callback for
  43. /// timer expire, the callback is request the list lock again for lookup
  44. /// the module from module id. It is for avoiding that situation.
  45. void * thread_modulers_timer_check(void * arg)
  46. {
  47. int ms_to_expiry;
  48. while (1) {
  49. ms_to_expiry = -1;
  50. vm_mutex_lock(&g_timer_ctx_list_mutex);
  51. timer_ctx_node_t* elem = (timer_ctx_node_t*)
  52. bh_list_first_elem(&g_timer_ctx_list);
  53. while (elem) {
  54. int next = check_app_timers(elem->timer_ctx);
  55. if (next != -1) {
  56. if (ms_to_expiry == -1 || ms_to_expiry > next)
  57. ms_to_expiry = next;
  58. }
  59. elem = (timer_ctx_node_t*) bh_list_elem_next(elem);
  60. }
  61. vm_mutex_unlock(&g_timer_ctx_list_mutex);
  62. if (ms_to_expiry == -1)
  63. ms_to_expiry = 60 * 1000;
  64. vm_mutex_lock(&g_timer_ctx_list_mutex);
  65. vm_cond_reltimedwait(&g_timer_ctx_list_cond, &g_timer_ctx_list_mutex,
  66. ms_to_expiry);
  67. vm_mutex_unlock(&g_timer_ctx_list_mutex);
  68. }
  69. }
  70. void wakeup_modules_timer_thread(timer_ctx_t ctx)
  71. {
  72. vm_mutex_lock(&g_timer_ctx_list_mutex);
  73. vm_cond_signal(&g_timer_ctx_list_cond);
  74. vm_mutex_unlock(&g_timer_ctx_list_mutex);
  75. }
  76. void init_wasm_timer()
  77. {
  78. korp_tid tm_tid;
  79. bh_list_init(&g_timer_ctx_list);
  80. vm_cond_init(&g_timer_ctx_list_cond);
  81. /* temp solution for: thread_modulers_timer_check thread would recursive lock the mutex */
  82. vm_recursive_mutex_init(&g_timer_ctx_list_mutex);
  83. vm_thread_create(&tm_tid, thread_modulers_timer_check,
  84. NULL, BH_APPLET_PRESERVED_STACK_SIZE);
  85. }
  86. timer_ctx_t 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,
  91. module_id);
  92. if (ctx == NULL)
  93. return NULL;
  94. timer_ctx_node_t * node = (timer_ctx_node_t*)
  95. bh_malloc(sizeof(timer_ctx_node_t));
  96. if (node == NULL) {
  97. destroy_timer_ctx(ctx);
  98. return NULL;
  99. }
  100. memset(node, 0, sizeof(*node));
  101. node->timer_ctx = ctx;
  102. vm_mutex_lock(&g_timer_ctx_list_mutex);
  103. bh_list_insert(&g_timer_ctx_list, node);
  104. vm_mutex_unlock(&g_timer_ctx_list_mutex);
  105. return ctx;
  106. }
  107. void destroy_module_timer_ctx(unsigned int module_id)
  108. {
  109. vm_mutex_lock(&g_timer_ctx_list_mutex);
  110. timer_ctx_node_t* elem = (timer_ctx_node_t*)
  111. bh_list_first_elem(&g_timer_ctx_list);
  112. while (elem) {
  113. if (timer_ctx_get_owner(elem->timer_ctx) == module_id) {
  114. bh_list_remove(&g_timer_ctx_list, elem);
  115. destroy_timer_ctx(elem->timer_ctx);
  116. bh_free(elem);
  117. break;
  118. }
  119. elem = (timer_ctx_node_t*) bh_list_elem_next(elem);
  120. }
  121. vm_mutex_unlock(&g_timer_ctx_list_mutex);
  122. }
  123. timer_ctx_t get_wasm_timer_ctx()
  124. {
  125. module_data * m = app_manager_get_module_data(Module_WASM_App);
  126. if (m == NULL)
  127. return NULL;
  128. return m->timer_ctx;
  129. }
  130. timer_id_t
  131. wasm_create_timer(wasm_module_inst_t module_inst,
  132. int interval, bool is_period, bool auto_start)
  133. {
  134. return sys_create_timer(get_wasm_timer_ctx(), interval, is_period,
  135. auto_start);
  136. }
  137. void
  138. wasm_timer_destroy(wasm_module_inst_t module_inst, timer_id_t timer_id)
  139. {
  140. sys_timer_destroy(get_wasm_timer_ctx(), timer_id);
  141. }
  142. void
  143. wasm_timer_cancel(wasm_module_inst_t module_inst, timer_id_t timer_id)
  144. {
  145. sys_timer_cancel(get_wasm_timer_ctx(), timer_id);
  146. }
  147. void
  148. wasm_timer_restart(wasm_module_inst_t module_inst,
  149. timer_id_t timer_id, int interval)
  150. {
  151. sys_timer_restart(get_wasm_timer_ctx(), timer_id, interval);
  152. }
  153. extern uint32 get_sys_tick_ms();
  154. uint32
  155. wasm_get_sys_tick_ms(wasm_module_inst_t module_inst)
  156. {
  157. return (uint32) bh_get_tick_ms();
  158. }