win_thread.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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 __stdcall
  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_thread_env_init()
  240. {
  241. os_thread_data *thread_data = TlsGetValue(thread_data_key);
  242. if (thread_data)
  243. /* Already created */
  244. return BHT_OK;
  245. if (!(thread_data = BH_MALLOC(sizeof(os_thread_data))))
  246. return BHT_ERROR;
  247. memset(thread_data, 0, sizeof(os_thread_data));
  248. thread_data->thread_id = GetCurrentThreadId();
  249. if (os_sem_init(&thread_data->wait_node.sem) != BHT_OK)
  250. goto fail1;
  251. if (os_mutex_init(&thread_data->wait_lock) != BHT_OK)
  252. goto fail2;
  253. if (os_cond_init(&thread_data->wait_cond) != BHT_OK)
  254. goto fail3;
  255. if (!TlsSetValue(thread_data_key, thread_data))
  256. goto fail4;
  257. return BHT_OK;
  258. fail4:
  259. os_cond_destroy(&thread_data->wait_cond);
  260. fail3:
  261. os_mutex_destroy(&thread_data->wait_lock);
  262. fail2:
  263. os_sem_destroy(&thread_data->wait_node.sem);
  264. fail1:
  265. BH_FREE(thread_data);
  266. return BHT_ERROR;
  267. }
  268. void
  269. os_thread_env_destroy()
  270. {
  271. os_thread_data *thread_data = TlsGetValue(thread_data_key);
  272. /* Note that supervisor_thread_data's resources will be destroyed
  273. by os_thread_sys_destroy() */
  274. if (thread_data && thread_data != &supervisor_thread_data) {
  275. TlsSetValue(thread_data_key, NULL);
  276. os_cond_destroy(&thread_data->wait_cond);
  277. os_mutex_destroy(&thread_data->wait_lock);
  278. os_sem_destroy(&thread_data->wait_node.sem);
  279. BH_FREE(thread_data);
  280. }
  281. }
  282. int
  283. os_sem_init(korp_sem *sem)
  284. {
  285. bh_assert(sem);
  286. *sem = CreateSemaphore(NULL, 0, BH_SEM_COUNT_MAX, NULL);
  287. return (*sem != NULL) ? BHT_OK : BHT_ERROR;
  288. }
  289. int
  290. os_sem_destroy(korp_sem *sem)
  291. {
  292. bh_assert(sem);
  293. CloseHandle(*sem);
  294. return BHT_OK;
  295. }
  296. int
  297. os_sem_wait(korp_sem *sem)
  298. {
  299. DWORD ret;
  300. bh_assert(sem);
  301. ret = WaitForSingleObject(*sem, INFINITE);
  302. if (ret == WAIT_OBJECT_0)
  303. return BHT_OK;
  304. else if(ret == WAIT_TIMEOUT)
  305. return (int)WAIT_TIMEOUT;
  306. else /* WAIT_FAILED or others */
  307. return BHT_ERROR;
  308. }
  309. int
  310. os_sem_reltimed_wait(korp_sem *sem, uint64 useconds)
  311. {
  312. uint64 mseconds_64;
  313. DWORD ret, mseconds;
  314. bh_assert(sem);
  315. if (useconds == BHT_WAIT_FOREVER)
  316. mseconds = INFINITE;
  317. else {
  318. mseconds_64 = useconds / 1000;
  319. if (mseconds_64 < (uint64)(UINT32_MAX - 1)) {
  320. mseconds = (uint32)mseconds_64;
  321. }
  322. else {
  323. mseconds = UINT32_MAX - 1;
  324. os_printf("Warning: os_sem_reltimed_wait exceeds limit, "
  325. "set to max timeout instead\n");
  326. }
  327. }
  328. ret = WaitForSingleObject(*sem, mseconds);
  329. if (ret == WAIT_OBJECT_0)
  330. return BHT_OK;
  331. else if(ret == WAIT_TIMEOUT)
  332. return (int)WAIT_TIMEOUT;
  333. else /* WAIT_FAILED or others */
  334. return BHT_ERROR;
  335. }
  336. int
  337. os_sem_signal(korp_sem *sem)
  338. {
  339. bh_assert(sem);
  340. return ReleaseSemaphore(*sem, 1, NULL) != FALSE
  341. ? BHT_OK: BHT_ERROR;
  342. }
  343. int
  344. os_mutex_init(korp_mutex *mutex)
  345. {
  346. bh_assert(mutex);
  347. *mutex = CreateMutex(NULL, FALSE, NULL);
  348. return (*mutex != NULL) ? BHT_OK : BHT_ERROR;
  349. }
  350. int
  351. os_recursive_mutex_init(korp_mutex *mutex)
  352. {
  353. bh_assert(mutex);
  354. *mutex = CreateMutex(NULL, FALSE, NULL);
  355. return (*mutex != NULL) ? BHT_OK : BHT_ERROR;
  356. }
  357. int
  358. os_mutex_destroy(korp_mutex *mutex)
  359. {
  360. assert(mutex);
  361. return CloseHandle(*mutex) ? BHT_OK : BHT_ERROR;
  362. }
  363. int
  364. os_mutex_lock(korp_mutex *mutex)
  365. {
  366. int ret;
  367. assert(mutex);
  368. ret = WaitForSingleObject(*mutex, INFINITE);
  369. return ret != WAIT_FAILED ? BHT_OK : BHT_ERROR;
  370. }
  371. int
  372. os_mutex_unlock(korp_mutex *mutex)
  373. {
  374. bh_assert(mutex);
  375. return ReleaseMutex(*mutex) ? BHT_OK : BHT_ERROR;
  376. }
  377. int
  378. os_cond_init(korp_cond *cond)
  379. {
  380. bh_assert(cond);
  381. if (os_mutex_init(&cond->wait_list_lock) != BHT_OK)
  382. return BHT_ERROR;
  383. cond->thread_wait_list = NULL;
  384. return BHT_OK;
  385. }
  386. int
  387. os_cond_destroy(korp_cond *cond)
  388. {
  389. bh_assert(cond);
  390. os_mutex_destroy(&cond->wait_list_lock);
  391. return BHT_OK;
  392. }
  393. static int
  394. os_cond_wait_internal(korp_cond *cond, korp_mutex *mutex,
  395. bool timed, uint64 useconds)
  396. {
  397. os_thread_wait_node *node = &thread_data_current()->wait_node;
  398. node->next = NULL;
  399. bh_assert(cond);
  400. bh_assert(mutex);
  401. os_mutex_lock(&cond->wait_list_lock);
  402. if (!cond->thread_wait_list)
  403. cond->thread_wait_list = node;
  404. else {
  405. /* Add to end of wait list */
  406. os_thread_wait_node *p = cond->thread_wait_list;
  407. while (p->next)
  408. p = p->next;
  409. p->next = node;
  410. }
  411. os_mutex_unlock(&cond->wait_list_lock);
  412. /* Unlock mutex, wait sem and lock mutex again */
  413. os_mutex_unlock(mutex);
  414. if (timed)
  415. os_sem_reltimed_wait(&node->sem, useconds);
  416. else
  417. os_sem_wait(&node->sem);
  418. os_mutex_lock(mutex);
  419. /* Remove wait node from wait list */
  420. os_mutex_lock(&cond->wait_list_lock);
  421. if (cond->thread_wait_list == node)
  422. cond->thread_wait_list = node->next;
  423. else {
  424. /* Remove from the wait list */
  425. os_thread_wait_node *p = cond->thread_wait_list;
  426. while (p->next != node)
  427. p = p->next;
  428. p->next = node->next;
  429. }
  430. os_mutex_unlock(&cond->wait_list_lock);
  431. return BHT_OK;
  432. }
  433. int
  434. os_cond_wait(korp_cond *cond, korp_mutex *mutex)
  435. {
  436. return os_cond_wait_internal(cond, mutex, false, 0);
  437. }
  438. int
  439. os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds)
  440. {
  441. if (useconds == BHT_WAIT_FOREVER) {
  442. return os_cond_wait_internal(cond, mutex, false, 0);
  443. }
  444. else {
  445. return os_cond_wait_internal(cond, mutex, true, useconds);
  446. }
  447. }
  448. int
  449. os_cond_signal(korp_cond *cond)
  450. {
  451. /* Signal the head wait node of wait list */
  452. os_mutex_lock(&cond->wait_list_lock);
  453. if (cond->thread_wait_list)
  454. os_sem_signal(&cond->thread_wait_list->sem);
  455. os_mutex_unlock(&cond->wait_list_lock);
  456. return BHT_OK;
  457. }
  458. static os_thread_local_attribute uint8 *thread_stack_boundary = NULL;
  459. uint8 *
  460. os_thread_get_stack_boundary()
  461. {
  462. ULONG_PTR low_limit = 0, high_limit = 0;
  463. uint32 page_size;
  464. if (thread_stack_boundary)
  465. return thread_stack_boundary;
  466. page_size = os_getpagesize();
  467. GetCurrentThreadStackLimits(&low_limit, &high_limit);
  468. /* 4 pages are set unaccessible by system, we reserved
  469. one more page at least for safety */
  470. thread_stack_boundary = (uint8*)(uintptr_t)low_limit + page_size * 5;
  471. return thread_stack_boundary;
  472. }
  473. #ifdef OS_ENABLE_HW_BOUND_CHECK
  474. static os_thread_local_attribute bool thread_signal_inited = false;
  475. int
  476. os_thread_signal_init()
  477. {
  478. ULONG StackSizeInBytes = 16 * 1024;
  479. bool ret;
  480. if (thread_signal_inited)
  481. return true;
  482. ret = SetThreadStackGuarantee(&StackSizeInBytes);
  483. if (ret)
  484. thread_signal_inited = true;
  485. return ret ? 0 : -1;
  486. }
  487. void
  488. os_thread_signal_destroy()
  489. {
  490. /* Do nothing */
  491. }
  492. bool
  493. os_thread_signal_inited()
  494. {
  495. return thread_signal_inited;
  496. }
  497. #endif