alios_thread.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "platform_api_vmcore.h"
  6. #include "platform_api_extension.h"
  7. /* clang-format off */
  8. #define bh_assert(v) do { \
  9. if (!(v)) { \
  10. printf("\nASSERTION FAILED: %s, at %s, line %d\n", \
  11. #v, __FILE__, __LINE__); \
  12. aos_reboot(); \
  13. while (1); \
  14. } \
  15. } while (0)
  16. /* clang-format on */
  17. struct os_thread_data;
  18. typedef struct os_thread_wait_node {
  19. aos_sem_t sem;
  20. os_thread_wait_list next;
  21. } os_thread_wait_node;
  22. typedef struct os_thread_data {
  23. /* Thread body */
  24. aos_task_t thread;
  25. /* Thread start routine */
  26. thread_start_routine_t start_routine;
  27. /* Thread start routine argument */
  28. void *arg;
  29. /* Thread local root */
  30. void *tlr;
  31. /* Wait node of current thread */
  32. os_thread_wait_node wait_node;
  33. /* Lock for waiting list */
  34. aos_mutex_t wait_list_lock;
  35. /* Waiting list of other threads who are joining this thread */
  36. os_thread_wait_list thread_wait_list;
  37. } os_thread_data;
  38. static bool is_thread_sys_inited = false;
  39. /* Thread data of supervisor thread */
  40. static os_thread_data supervisor_thread_data;
  41. /* Thread data key */
  42. static aos_task_key_t thread_data_key;
  43. /* Thread name index */
  44. static int thread_name_index;
  45. int
  46. os_thread_sys_init()
  47. {
  48. if (is_thread_sys_inited)
  49. return BHT_OK;
  50. if (aos_task_key_create(&thread_data_key) != 0)
  51. return BHT_ERROR;
  52. /* Initialize supervisor thread data */
  53. memset(&supervisor_thread_data, 0, sizeof(supervisor_thread_data));
  54. if (aos_sem_new(&supervisor_thread_data.wait_node.sem, 1) != 0) {
  55. aos_task_key_delete(thread_data_key);
  56. return BHT_ERROR;
  57. }
  58. if (aos_task_setspecific(thread_data_key, &supervisor_thread_data)) {
  59. aos_sem_free(&supervisor_thread_data.wait_node.sem);
  60. aos_task_key_delete(thread_data_key);
  61. return BHT_ERROR;
  62. }
  63. is_thread_sys_inited = true;
  64. return BHT_OK;
  65. }
  66. void
  67. os_thread_sys_destroy()
  68. {
  69. if (is_thread_sys_inited) {
  70. aos_task_key_delete(thread_data_key);
  71. aos_sem_free(&supervisor_thread_data.wait_node.sem);
  72. is_thread_sys_inited = false;
  73. }
  74. }
  75. static os_thread_data *
  76. thread_data_current()
  77. {
  78. return aos_task_getspecific(thread_data_key);
  79. }
  80. static void
  81. os_thread_cleanup(void)
  82. {
  83. os_thread_data *thread_data = thread_data_current();
  84. os_thread_wait_list thread_wait_list;
  85. aos_mutex_t *wait_list_lock;
  86. aos_sem_t *wait_node_sem;
  87. bh_assert(thread_data != NULL);
  88. wait_list_lock = &thread_data->wait_list_lock;
  89. thread_wait_list = thread_data->thread_wait_list;
  90. wait_node_sem = &thread_data->wait_node.sem;
  91. /* Free thread data firstly */
  92. BH_FREE(thread_data);
  93. aos_mutex_lock(wait_list_lock, AOS_WAIT_FOREVER);
  94. if (thread_wait_list) {
  95. /* Signal each joining thread */
  96. os_thread_wait_list head = thread_wait_list;
  97. while (head) {
  98. os_thread_wait_list next = head->next;
  99. aos_sem_signal(&head->sem);
  100. head = next;
  101. }
  102. }
  103. aos_mutex_unlock(wait_list_lock);
  104. /* Free sem and lock */
  105. aos_sem_free(wait_node_sem);
  106. aos_mutex_free(wait_list_lock);
  107. }
  108. static void
  109. os_thread_wrapper(void *arg)
  110. {
  111. os_thread_data *thread_data = arg;
  112. /* Set thread custom data */
  113. if (!aos_task_setspecific(thread_data_key, thread_data))
  114. thread_data->start_routine(thread_data->arg);
  115. os_thread_cleanup();
  116. }
  117. int
  118. os_thread_create(korp_tid *p_tid, thread_start_routine_t start, void *arg,
  119. unsigned int stack_size)
  120. {
  121. return os_thread_create_with_prio(p_tid, start, arg, stack_size,
  122. BH_THREAD_DEFAULT_PRIORITY);
  123. }
  124. int
  125. os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
  126. void *arg, unsigned int stack_size, int prio)
  127. {
  128. os_thread_data *thread_data;
  129. char thread_name[32];
  130. if (!p_tid || !stack_size)
  131. return BHT_ERROR;
  132. /* Create and initialize thread data */
  133. if (!(thread_data = BH_MALLOC(sizeof(os_thread_data))))
  134. return BHT_ERROR;
  135. memset(thread_data, 0, sizeof(os_thread_data));
  136. thread_data->start_routine = start;
  137. thread_data->arg = arg;
  138. if (aos_sem_new(&thread_data->wait_node.sem, 1) != 0)
  139. goto fail1;
  140. if (aos_mutex_new(&thread_data->wait_list_lock))
  141. goto fail2;
  142. snprintf(thread_name, sizeof(thread_name), "%s%d", "wasm-thread-",
  143. ++thread_name_index);
  144. /* Create the thread */
  145. if (aos_task_new_ext((aos_task_t *)thread_data, thread_name,
  146. os_thread_wrapper, thread_data, stack_size, prio))
  147. goto fail3;
  148. aos_msleep(10);
  149. *p_tid = (korp_tid)thread_data;
  150. return BHT_OK;
  151. fail3:
  152. aos_mutex_free(&thread_data->wait_list_lock);
  153. fail2:
  154. aos_sem_free(&thread_data->wait_node.sem);
  155. fail1:
  156. BH_FREE(thread_data);
  157. return BHT_ERROR;
  158. }
  159. korp_tid
  160. os_self_thread()
  161. {
  162. return (korp_tid)aos_task_getspecific(thread_data_key);
  163. }
  164. int
  165. os_thread_join(korp_tid thread, void **value_ptr)
  166. {
  167. (void)value_ptr;
  168. os_thread_data *thread_data, *curr_thread_data;
  169. /* Get thread data of current thread */
  170. curr_thread_data = thread_data_current();
  171. curr_thread_data->wait_node.next = NULL;
  172. /* Get thread data */
  173. thread_data = (os_thread_data *)thread;
  174. aos_mutex_lock(&thread_data->wait_list_lock, AOS_WAIT_FOREVER);
  175. if (!thread_data->thread_wait_list)
  176. thread_data->thread_wait_list = &curr_thread_data->wait_node;
  177. else {
  178. /* Add to end of waiting list */
  179. os_thread_wait_node *p = thread_data->thread_wait_list;
  180. while (p->next)
  181. p = p->next;
  182. p->next = &curr_thread_data->wait_node;
  183. }
  184. aos_mutex_unlock(&thread_data->wait_list_lock);
  185. /* Wait the sem */
  186. aos_sem_wait(&curr_thread_data->wait_node.sem, AOS_WAIT_FOREVER);
  187. return BHT_OK;
  188. }
  189. int
  190. os_mutex_init(korp_mutex *mutex)
  191. {
  192. return aos_mutex_new(mutex) == 0 ? BHT_OK : BHT_ERROR;
  193. }
  194. int
  195. os_mutex_destroy(korp_mutex *mutex)
  196. {
  197. aos_mutex_free(mutex);
  198. return BHT_OK;
  199. }
  200. int
  201. os_mutex_lock(korp_mutex *mutex)
  202. {
  203. return aos_mutex_lock(mutex, AOS_WAIT_FOREVER);
  204. }
  205. int
  206. os_mutex_unlock(korp_mutex *mutex)
  207. {
  208. return aos_mutex_unlock(mutex);
  209. }
  210. int
  211. os_cond_init(korp_cond *cond)
  212. {
  213. if (aos_mutex_new(&cond->wait_list_lock) != 0)
  214. return BHT_ERROR;
  215. cond->thread_wait_list = NULL;
  216. return BHT_OK;
  217. }
  218. int
  219. os_cond_destroy(korp_cond *cond)
  220. {
  221. aos_mutex_free(&cond->wait_list_lock);
  222. return BHT_OK;
  223. }
  224. static int
  225. os_cond_wait_internal(korp_cond *cond, korp_mutex *mutex, bool timed,
  226. uint32 mills)
  227. {
  228. os_thread_wait_node *node = &thread_data_current()->wait_node;
  229. node->next = NULL;
  230. aos_mutex_lock(&cond->wait_list_lock, AOS_WAIT_FOREVER);
  231. if (!cond->thread_wait_list)
  232. cond->thread_wait_list = node;
  233. else {
  234. /* Add to end of wait list */
  235. os_thread_wait_node *p = cond->thread_wait_list;
  236. while (p->next)
  237. p = p->next;
  238. p->next = node;
  239. }
  240. aos_mutex_unlock(&cond->wait_list_lock);
  241. /* Unlock mutex, wait sem and lock mutex again */
  242. aos_mutex_unlock(mutex);
  243. aos_sem_wait(&node->sem, timed ? mills : AOS_WAIT_FOREVER);
  244. aos_mutex_lock(mutex, AOS_WAIT_FOREVER);
  245. /* Remove wait node from wait list */
  246. aos_mutex_lock(&cond->wait_list_lock, AOS_WAIT_FOREVER);
  247. if (cond->thread_wait_list == node)
  248. cond->thread_wait_list = node->next;
  249. else {
  250. /* Remove from the wait list */
  251. os_thread_wait_node *p = cond->thread_wait_list;
  252. while (p->next != node)
  253. p = p->next;
  254. p->next = node->next;
  255. }
  256. aos_mutex_unlock(&cond->wait_list_lock);
  257. return BHT_OK;
  258. }
  259. int
  260. os_cond_wait(korp_cond *cond, korp_mutex *mutex)
  261. {
  262. return os_cond_wait_internal(cond, mutex, false, 0);
  263. }
  264. int
  265. os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds)
  266. {
  267. if (useconds == BHT_WAIT_FOREVER) {
  268. return os_cond_wait_internal(cond, mutex, false, 0);
  269. }
  270. else {
  271. uint64 mills_64 = useconds / 1000;
  272. uint32 mills;
  273. if (mills_64 < (uint64)(UINT32_MAX - 1)) {
  274. mills = (uint64)mills_64;
  275. }
  276. else {
  277. mills = UINT32_MAX - 1;
  278. os_printf("Warning: os_cond_reltimedwait exceeds limit, "
  279. "set to max timeout instead\n");
  280. }
  281. return os_cond_wait_internal(cond, mutex, true, mills);
  282. }
  283. }
  284. int
  285. os_cond_signal(korp_cond *cond)
  286. {
  287. /* Signal the head wait node of wait list */
  288. aos_mutex_lock(&cond->wait_list_lock, AOS_WAIT_FOREVER);
  289. if (cond->thread_wait_list)
  290. aos_sem_signal(&cond->thread_wait_list->sem);
  291. aos_mutex_unlock(&cond->wait_list_lock);
  292. return BHT_OK;
  293. }
  294. uint8 *
  295. os_thread_get_stack_boundary()
  296. {
  297. /* TODO: get alios stack boundary */
  298. return NULL;
  299. }
  300. void
  301. os_thread_jit_write_protect_np(bool enabled)
  302. {}