win_thread.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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) assert(v)
  8. #define BH_SEM_COUNT_MAX 0xFFFF
  9. struct os_thread_data;
  10. typedef struct os_thread_wait_node {
  11. korp_sem sem;
  12. void *retval;
  13. os_thread_wait_list next;
  14. } os_thread_wait_node;
  15. typedef struct os_thread_data {
  16. /* Next thread data */
  17. struct os_thread_data *next;
  18. /* Thread data of parent thread */
  19. struct os_thread_data *parent;
  20. /* Thread Id */
  21. DWORD thread_id;
  22. /* Thread start routine */
  23. thread_start_routine_t start_routine;
  24. /* Thread start routine argument */
  25. void *arg;
  26. /* Wait node of current thread */
  27. os_thread_wait_node wait_node;
  28. /* Wait cond */
  29. korp_cond wait_cond;
  30. /* Wait lock */
  31. korp_mutex wait_lock;
  32. /* Waiting list of other threads who are joining this thread */
  33. os_thread_wait_list thread_wait_list;
  34. } os_thread_data;
  35. static bool is_thread_sys_inited = false;
  36. /* Thread data of supervisor thread */
  37. static os_thread_data supervisor_thread_data;
  38. /* Thread data key */
  39. static DWORD thread_data_key;
  40. int os_sem_init(korp_sem* sem);
  41. int os_sem_destroy(korp_sem* sem);
  42. int os_sem_wait(korp_sem* sem);
  43. int os_sem_reltimed_wait(korp_sem* sem, uint64 useconds);
  44. int os_sem_signal(korp_sem* sem);
  45. int
  46. os_thread_sys_init()
  47. {
  48. if (is_thread_sys_inited)
  49. return BHT_OK;
  50. if ((thread_data_key = TlsAlloc()) == TLS_OUT_OF_INDEXES)
  51. return BHT_ERROR;
  52. /* Initialize supervisor thread data */
  53. memset(&supervisor_thread_data, 0, sizeof(os_thread_data));
  54. supervisor_thread_data.thread_id = GetCurrentThreadId();
  55. if (os_sem_init(&supervisor_thread_data.wait_node.sem) != BHT_OK)
  56. goto fail1;
  57. if (os_mutex_init(&supervisor_thread_data.wait_lock) != BHT_OK)
  58. goto fail2;
  59. if (os_cond_init(&supervisor_thread_data.wait_cond) != BHT_OK)
  60. goto fail3;
  61. if (!TlsSetValue(thread_data_key, &supervisor_thread_data))
  62. goto fail4;
  63. is_thread_sys_inited = true;
  64. return BHT_OK;
  65. fail4:
  66. os_cond_destroy(&supervisor_thread_data.wait_cond);
  67. fail3:
  68. os_mutex_destroy(&supervisor_thread_data.wait_lock);
  69. fail2:
  70. os_sem_destroy(&supervisor_thread_data.wait_node.sem);
  71. fail1:
  72. TlsFree(thread_data_key);
  73. return BHT_ERROR;
  74. }
  75. void
  76. os_thread_sys_destroy()
  77. {
  78. if (is_thread_sys_inited) {
  79. os_cond_destroy(&supervisor_thread_data.wait_cond);
  80. os_mutex_destroy(&supervisor_thread_data.wait_lock);
  81. os_sem_destroy(&supervisor_thread_data.wait_node.sem);
  82. memset(&supervisor_thread_data, 0, sizeof(os_thread_data));
  83. TlsFree(thread_data_key);
  84. thread_data_key = 0;
  85. is_thread_sys_inited = false;
  86. }
  87. }
  88. static os_thread_data *
  89. thread_data_current()
  90. {
  91. return (os_thread_data *)TlsGetValue(thread_data_key);
  92. }
  93. static void
  94. os_thread_cleanup(void *retval)
  95. {
  96. os_thread_data *thread_data = thread_data_current();
  97. bh_assert(thread_data != NULL);
  98. os_mutex_lock(&thread_data->wait_lock);
  99. if (thread_data->thread_wait_list) {
  100. /* Signal each joining thread */
  101. os_thread_wait_list head = thread_data->thread_wait_list;
  102. while (head) {
  103. os_thread_wait_list next = head->next;
  104. head->retval = retval;
  105. os_sem_signal(&head->sem);
  106. head = next;
  107. }
  108. thread_data->thread_wait_list = NULL;
  109. }
  110. os_mutex_unlock(&thread_data->wait_lock);
  111. /* Destroy resources */
  112. os_cond_destroy(&thread_data->wait_cond);
  113. os_sem_destroy(&thread_data->wait_node.sem);
  114. os_mutex_destroy(&thread_data->wait_lock);
  115. BH_FREE(thread_data);
  116. }
  117. static unsigned
  118. os_thread_wrapper(void *arg)
  119. {
  120. os_thread_data *thread_data = arg;
  121. os_thread_data *parent = thread_data->parent;
  122. void *retval;
  123. bool result;
  124. os_printf("THREAD CREATED %p\n", thread_data);
  125. os_mutex_lock(&parent->wait_lock);
  126. thread_data->thread_id = GetCurrentThreadId();
  127. result = TlsSetValue(thread_data_key, thread_data);
  128. #ifdef OS_ENABLE_HW_BOUND_CHECK
  129. if (result)
  130. result = os_thread_signal_init() == 0 ? true : false;
  131. #endif
  132. /* Notify parent thread */
  133. os_cond_signal(&parent->wait_cond);
  134. os_mutex_unlock(&parent->wait_lock);
  135. if (!result)
  136. return -1;
  137. retval = thread_data->start_routine(thread_data->arg);
  138. os_thread_cleanup(retval);
  139. return 0;
  140. }
  141. int
  142. os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
  143. void *arg, unsigned int stack_size, int prio)
  144. {
  145. os_thread_data *parent = thread_data_current();
  146. os_thread_data *thread_data;
  147. if (!p_tid || !start)
  148. return BHT_ERROR;
  149. if (stack_size < BH_APPLET_PRESERVED_STACK_SIZE)
  150. stack_size = BH_APPLET_PRESERVED_STACK_SIZE;
  151. if (!(thread_data = BH_MALLOC(sizeof(os_thread_data))))
  152. return BHT_ERROR;
  153. memset(thread_data, 0, sizeof(os_thread_data));
  154. thread_data->parent = parent;
  155. thread_data->start_routine = start;
  156. thread_data->arg = arg;
  157. if (os_sem_init(&thread_data->wait_node.sem) != BHT_OK)
  158. goto fail1;
  159. if (os_mutex_init(&thread_data->wait_lock) != BHT_OK)
  160. goto fail2;
  161. if (os_cond_init(&thread_data->wait_cond) != BHT_OK)
  162. goto fail3;
  163. os_mutex_lock(&parent->wait_lock);
  164. if (!_beginthreadex(NULL, stack_size,
  165. os_thread_wrapper, thread_data,
  166. 0, NULL)) {
  167. os_mutex_unlock(&parent->wait_lock);
  168. goto fail4;
  169. }
  170. /* Wait for the thread routine to set thread_data's tid
  171. and add thread_data to thread data list */
  172. os_cond_wait(&parent->wait_cond, &parent->wait_lock);
  173. os_mutex_unlock(&parent->wait_lock);
  174. *p_tid = (korp_tid)thread_data;
  175. return BHT_OK;
  176. fail4:
  177. os_cond_destroy(&thread_data->wait_cond);
  178. fail3:
  179. os_mutex_destroy(&thread_data->wait_lock);
  180. fail2:
  181. os_sem_destroy(&thread_data->wait_node.sem);
  182. fail1:
  183. BH_FREE(thread_data);
  184. return BHT_ERROR;
  185. }
  186. int
  187. os_thread_create(korp_tid *tid, thread_start_routine_t start, void *arg,
  188. unsigned int stack_size)
  189. {
  190. return os_thread_create_with_prio(tid, start, arg, stack_size,
  191. BH_THREAD_DEFAULT_PRIORITY);
  192. }
  193. korp_tid
  194. os_self_thread()
  195. {
  196. return (korp_tid)TlsGetValue(thread_data_key);
  197. }
  198. int
  199. os_thread_join(korp_tid thread, void **p_retval)
  200. {
  201. os_thread_data *thread_data, *curr_thread_data;
  202. /* Get thread data of current thread */
  203. curr_thread_data = thread_data_current();
  204. curr_thread_data->wait_node.next = NULL;
  205. /* Get thread data of thread to join */
  206. thread_data = (os_thread_data *)thread;
  207. bh_assert(thread_data);
  208. os_mutex_lock(&thread_data->wait_lock);
  209. if (!thread_data->thread_wait_list)
  210. thread_data->thread_wait_list = &curr_thread_data->wait_node;
  211. else {
  212. /* Add to end of waiting list */
  213. os_thread_wait_node *p = thread_data->thread_wait_list;
  214. while (p->next)
  215. p = p->next;
  216. p->next = &curr_thread_data->wait_node;
  217. }
  218. os_mutex_unlock(&thread_data->wait_lock);
  219. /* Wait the sem */
  220. os_sem_wait(&curr_thread_data->wait_node.sem);
  221. if (p_retval)
  222. *p_retval = curr_thread_data->wait_node.retval;
  223. return BHT_OK;
  224. }
  225. int
  226. os_thread_detach(korp_tid thread)
  227. {
  228. /* Do nothing */
  229. return BHT_OK;
  230. (void)thread;
  231. }
  232. void
  233. os_thread_exit(void *retval)
  234. {
  235. os_thread_cleanup(retval);
  236. _endthreadex(0);
  237. }
  238. int
  239. os_sem_init(korp_sem *sem)
  240. {
  241. bh_assert(sem);
  242. *sem = CreateSemaphore(NULL, 0, BH_SEM_COUNT_MAX, NULL);
  243. return (*sem != NULL) ? BHT_OK : BHT_ERROR;
  244. }
  245. int
  246. os_sem_destroy(korp_sem *sem)
  247. {
  248. bh_assert(sem);
  249. CloseHandle(*sem);
  250. return BHT_OK;
  251. }
  252. int
  253. os_sem_wait(korp_sem *sem)
  254. {
  255. DWORD ret;
  256. bh_assert(sem);
  257. ret = WaitForSingleObject(*sem, INFINITE);
  258. if (ret == WAIT_OBJECT_0)
  259. return BHT_OK;
  260. else if(ret == WAIT_TIMEOUT)
  261. return (int)WAIT_TIMEOUT;
  262. else /* WAIT_FAILED or others */
  263. return BHT_ERROR;
  264. }
  265. int
  266. os_sem_reltimed_wait(korp_sem *sem, uint64 useconds)
  267. {
  268. uint64 mseconds_64;
  269. DWORD ret, mseconds;
  270. bh_assert(sem);
  271. if (useconds == BHT_WAIT_FOREVER)
  272. mseconds = INFINITE;
  273. else {
  274. mseconds_64 = useconds / 1000;
  275. if (mseconds_64 < (uint64)(UINT32_MAX - 1)) {
  276. mseconds = (uint32)mseconds_64;
  277. }
  278. else {
  279. mseconds = UINT32_MAX - 1;
  280. os_printf("Warning: os_sem_reltimed_wait exceeds limit, "
  281. "set to max timeout instead\n");
  282. }
  283. }
  284. ret = WaitForSingleObject(*sem, mseconds);
  285. if (ret == WAIT_OBJECT_0)
  286. return BHT_OK;
  287. else if(ret == WAIT_TIMEOUT)
  288. return (int)WAIT_TIMEOUT;
  289. else /* WAIT_FAILED or others */
  290. return BHT_ERROR;
  291. }
  292. int
  293. os_sem_signal(korp_sem *sem)
  294. {
  295. bh_assert(sem);
  296. return ReleaseSemaphore(*sem, 1, NULL) != FALSE
  297. ? BHT_OK: BHT_ERROR;
  298. }
  299. int
  300. os_mutex_init(korp_mutex *mutex)
  301. {
  302. bh_assert(mutex);
  303. *mutex = CreateMutex(NULL, FALSE, NULL);
  304. return (*mutex != NULL) ? BHT_OK : BHT_ERROR;
  305. }
  306. int
  307. os_recursive_mutex_init(korp_mutex *mutex)
  308. {
  309. bh_assert(mutex);
  310. *mutex = CreateMutex(NULL, FALSE, NULL);
  311. return (*mutex != NULL) ? BHT_OK : BHT_ERROR;
  312. }
  313. int
  314. os_mutex_destroy(korp_mutex *mutex)
  315. {
  316. assert(mutex);
  317. return CloseHandle(*mutex) ? BHT_OK : BHT_ERROR;
  318. }
  319. int
  320. os_mutex_lock(korp_mutex *mutex)
  321. {
  322. int ret;
  323. assert(mutex);
  324. ret = WaitForSingleObject(*mutex, INFINITE);
  325. return ret != WAIT_FAILED ? BHT_OK : BHT_ERROR;
  326. }
  327. int
  328. os_mutex_unlock(korp_mutex *mutex)
  329. {
  330. bh_assert(mutex);
  331. return ReleaseMutex(*mutex) ? BHT_OK : BHT_ERROR;
  332. }
  333. int
  334. os_cond_init(korp_cond *cond)
  335. {
  336. bh_assert(cond);
  337. if (os_mutex_init(&cond->wait_list_lock) != BHT_OK)
  338. return BHT_ERROR;
  339. cond->thread_wait_list = NULL;
  340. return BHT_OK;
  341. }
  342. int
  343. os_cond_destroy(korp_cond *cond)
  344. {
  345. bh_assert(cond);
  346. os_mutex_destroy(&cond->wait_list_lock);
  347. return BHT_OK;
  348. }
  349. static int
  350. os_cond_wait_internal(korp_cond *cond, korp_mutex *mutex,
  351. bool timed, uint64 useconds)
  352. {
  353. os_thread_wait_node *node = &thread_data_current()->wait_node;
  354. node->next = NULL;
  355. bh_assert(cond);
  356. bh_assert(mutex);
  357. os_mutex_lock(&cond->wait_list_lock);
  358. if (!cond->thread_wait_list)
  359. cond->thread_wait_list = node;
  360. else {
  361. /* Add to end of wait list */
  362. os_thread_wait_node *p = cond->thread_wait_list;
  363. while (p->next)
  364. p = p->next;
  365. p->next = node;
  366. }
  367. os_mutex_unlock(&cond->wait_list_lock);
  368. /* Unlock mutex, wait sem and lock mutex again */
  369. os_mutex_unlock(mutex);
  370. if (timed)
  371. os_sem_reltimed_wait(&node->sem, useconds);
  372. else
  373. os_sem_wait(&node->sem);
  374. os_mutex_lock(mutex);
  375. /* Remove wait node from wait list */
  376. os_mutex_lock(&cond->wait_list_lock);
  377. if (cond->thread_wait_list == node)
  378. cond->thread_wait_list = node->next;
  379. else {
  380. /* Remove from the wait list */
  381. os_thread_wait_node *p = cond->thread_wait_list;
  382. while (p->next != node)
  383. p = p->next;
  384. p->next = node->next;
  385. }
  386. os_mutex_unlock(&cond->wait_list_lock);
  387. return BHT_OK;
  388. }
  389. int
  390. os_cond_wait(korp_cond *cond, korp_mutex *mutex)
  391. {
  392. return os_cond_wait_internal(cond, mutex, false, 0);
  393. }
  394. int
  395. os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds)
  396. {
  397. if (useconds == BHT_WAIT_FOREVER) {
  398. return os_cond_wait_internal(cond, mutex, false, 0);
  399. }
  400. else {
  401. return os_cond_wait_internal(cond, mutex, true, useconds);
  402. }
  403. }
  404. int
  405. os_cond_signal(korp_cond *cond)
  406. {
  407. /* Signal the head wait node of wait list */
  408. os_mutex_lock(&cond->wait_list_lock);
  409. if (cond->thread_wait_list)
  410. os_sem_signal(&cond->thread_wait_list->sem);
  411. os_mutex_unlock(&cond->wait_list_lock);
  412. return BHT_OK;
  413. }
  414. static os_thread_local_attribute uint8 *thread_stack_boundary = NULL;
  415. uint8 *
  416. os_thread_get_stack_boundary()
  417. {
  418. ULONG_PTR low_limit = 0, high_limit = 0;
  419. uint32 page_size;
  420. if (thread_stack_boundary)
  421. return thread_stack_boundary;
  422. page_size = os_getpagesize();
  423. GetCurrentThreadStackLimits(&low_limit, &high_limit);
  424. /* 4 pages are set unaccessible by system, we reserved
  425. one more page at least for safety */
  426. thread_stack_boundary = (uint8*)(uintptr_t)low_limit + page_size * 5;
  427. return thread_stack_boundary;
  428. }
  429. #ifdef OS_ENABLE_HW_BOUND_CHECK
  430. static os_thread_local_attribute bool thread_signal_inited = false;
  431. int
  432. os_thread_signal_init()
  433. {
  434. ULONG StackSizeInBytes = 16 * 1024;
  435. bool ret;
  436. if (thread_signal_inited)
  437. return true;
  438. ret = SetThreadStackGuarantee(&StackSizeInBytes);
  439. if (ret)
  440. thread_signal_inited = true;
  441. return ret ? 0 : -1;
  442. }
  443. void
  444. os_thread_signal_destroy()
  445. {
  446. /* Do nothing */
  447. }
  448. bool
  449. os_thread_signal_inited()
  450. {
  451. return thread_signal_inited;
  452. }
  453. #endif