alios_thread.c 9.0 KB

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