timer_wrapper.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 *) 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*) bh_list_first_elem(
  52. &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,
  85. BH_APPLET_PRESERVED_STACK_SIZE);
  86. }
  87. timer_ctx_t create_wasm_timer_ctx(unsigned int module_id, int prealloc_num)
  88. {
  89. timer_ctx_t ctx = create_timer_ctx(wasm_timer_callback,
  90. wakeup_modules_timer_thread, prealloc_num, module_id);
  91. if (ctx == NULL)
  92. return NULL;
  93. timer_ctx_node_t * node = (timer_ctx_node_t*) bh_malloc(
  94. 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. vm_mutex_lock(&g_timer_ctx_list_mutex);
  102. bh_list_insert(&g_timer_ctx_list, node);
  103. vm_mutex_unlock(&g_timer_ctx_list_mutex);
  104. return ctx;
  105. }
  106. void destory_module_timer_ctx(unsigned int module_id)
  107. {
  108. vm_mutex_lock(&g_timer_ctx_list_mutex);
  109. timer_ctx_node_t* elem = (timer_ctx_node_t*) bh_list_first_elem(
  110. &g_timer_ctx_list);
  111. while (elem) {
  112. if (timer_ctx_get_owner(elem->timer_ctx) == module_id) {
  113. bh_list_remove(&g_timer_ctx_list, elem);
  114. destroy_timer_ctx(elem->timer_ctx);
  115. bh_free(elem);
  116. break;
  117. }
  118. elem = (timer_ctx_node_t*) bh_list_elem_next(elem);
  119. }
  120. vm_mutex_unlock(&g_timer_ctx_list_mutex);
  121. }
  122. timer_ctx_t get_wasm_timer_ctx()
  123. {
  124. module_data * m = app_manager_get_module_data(Module_WASM_App);
  125. if (m == NULL)
  126. return NULL;
  127. return m->timer_ctx;
  128. }
  129. timer_id_t wasm_create_timer(int interval, bool is_period, bool auto_start)
  130. {
  131. return sys_create_timer(get_wasm_timer_ctx(), interval, is_period,
  132. auto_start);
  133. }
  134. void wasm_timer_destory(timer_id_t timer_id)
  135. {
  136. sys_timer_destory(get_wasm_timer_ctx(), timer_id);
  137. }
  138. void wasm_timer_cancel(timer_id_t timer_id)
  139. {
  140. sys_timer_cancel(get_wasm_timer_ctx(), timer_id);
  141. }
  142. void wasm_timer_restart(timer_id_t timer_id, int interval)
  143. {
  144. sys_timer_restart(get_wasm_timer_ctx(), timer_id, interval);
  145. }
  146. extern uint32 get_sys_tick_ms();
  147. uint32 wasm_get_sys_tick_ms(void)
  148. {
  149. return (uint32) bh_get_tick_ms();
  150. }