win_thread.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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. /* The GetCurrentThreadStackLimits API from "kernel32" */
  41. static void(WINAPI *GetCurrentThreadStackLimits_Kernel32)(PULONG_PTR,
  42. PULONG_PTR) = NULL;
  43. int
  44. os_sem_init(korp_sem *sem);
  45. int
  46. os_sem_destroy(korp_sem *sem);
  47. int
  48. os_sem_wait(korp_sem *sem);
  49. int
  50. os_sem_reltimed_wait(korp_sem *sem, uint64 useconds);
  51. int
  52. os_sem_signal(korp_sem *sem);
  53. int
  54. os_thread_sys_init()
  55. {
  56. HMODULE module;
  57. if (is_thread_sys_inited)
  58. return BHT_OK;
  59. if ((thread_data_key = TlsAlloc()) == TLS_OUT_OF_INDEXES)
  60. return BHT_ERROR;
  61. /* Initialize supervisor thread data */
  62. memset(&supervisor_thread_data, 0, sizeof(os_thread_data));
  63. supervisor_thread_data.thread_id = GetCurrentThreadId();
  64. if (os_sem_init(&supervisor_thread_data.wait_node.sem) != BHT_OK)
  65. goto fail1;
  66. if (os_mutex_init(&supervisor_thread_data.wait_lock) != BHT_OK)
  67. goto fail2;
  68. if (os_cond_init(&supervisor_thread_data.wait_cond) != BHT_OK)
  69. goto fail3;
  70. if (!TlsSetValue(thread_data_key, &supervisor_thread_data))
  71. goto fail4;
  72. if ((module = GetModuleHandle((LPSTR) "kernel32"))) {
  73. *(void **)&GetCurrentThreadStackLimits_Kernel32 =
  74. GetProcAddress(module, "GetCurrentThreadStackLimits");
  75. }
  76. is_thread_sys_inited = true;
  77. return BHT_OK;
  78. fail4:
  79. os_cond_destroy(&supervisor_thread_data.wait_cond);
  80. fail3:
  81. os_mutex_destroy(&supervisor_thread_data.wait_lock);
  82. fail2:
  83. os_sem_destroy(&supervisor_thread_data.wait_node.sem);
  84. fail1:
  85. TlsFree(thread_data_key);
  86. return BHT_ERROR;
  87. }
  88. void
  89. os_thread_sys_destroy()
  90. {
  91. if (is_thread_sys_inited) {
  92. os_cond_destroy(&supervisor_thread_data.wait_cond);
  93. os_mutex_destroy(&supervisor_thread_data.wait_lock);
  94. os_sem_destroy(&supervisor_thread_data.wait_node.sem);
  95. memset(&supervisor_thread_data, 0, sizeof(os_thread_data));
  96. TlsFree(thread_data_key);
  97. thread_data_key = 0;
  98. is_thread_sys_inited = false;
  99. }
  100. }
  101. static os_thread_data *
  102. thread_data_current()
  103. {
  104. return (os_thread_data *)TlsGetValue(thread_data_key);
  105. }
  106. static void
  107. os_thread_cleanup(void *retval)
  108. {
  109. os_thread_data *thread_data = thread_data_current();
  110. bh_assert(thread_data != NULL);
  111. os_mutex_lock(&thread_data->wait_lock);
  112. if (thread_data->thread_wait_list) {
  113. /* Signal each joining thread */
  114. os_thread_wait_list head = thread_data->thread_wait_list;
  115. while (head) {
  116. os_thread_wait_list next = head->next;
  117. head->retval = retval;
  118. os_sem_signal(&head->sem);
  119. head = next;
  120. }
  121. thread_data->thread_wait_list = NULL;
  122. }
  123. os_mutex_unlock(&thread_data->wait_lock);
  124. /* Destroy resources */
  125. os_cond_destroy(&thread_data->wait_cond);
  126. os_sem_destroy(&thread_data->wait_node.sem);
  127. os_mutex_destroy(&thread_data->wait_lock);
  128. BH_FREE(thread_data);
  129. }
  130. static unsigned __stdcall os_thread_wrapper(void *arg)
  131. {
  132. os_thread_data *thread_data = arg;
  133. os_thread_data *parent = thread_data->parent;
  134. void *retval;
  135. bool result;
  136. #if 0
  137. os_printf("THREAD CREATED %p\n", thread_data);
  138. #endif
  139. os_mutex_lock(&parent->wait_lock);
  140. thread_data->thread_id = GetCurrentThreadId();
  141. result = TlsSetValue(thread_data_key, thread_data);
  142. #ifdef OS_ENABLE_HW_BOUND_CHECK
  143. if (result)
  144. result = os_thread_signal_init() == 0 ? true : false;
  145. #endif
  146. /* Notify parent thread */
  147. os_cond_signal(&parent->wait_cond);
  148. os_mutex_unlock(&parent->wait_lock);
  149. if (!result)
  150. return -1;
  151. retval = thread_data->start_routine(thread_data->arg);
  152. os_thread_cleanup(retval);
  153. return 0;
  154. }
  155. int
  156. os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
  157. void *arg, unsigned int stack_size, int prio)
  158. {
  159. os_thread_data *parent = thread_data_current();
  160. os_thread_data *thread_data;
  161. if (!p_tid || !start)
  162. return BHT_ERROR;
  163. if (stack_size < BH_APPLET_PRESERVED_STACK_SIZE)
  164. stack_size = BH_APPLET_PRESERVED_STACK_SIZE;
  165. if (!(thread_data = BH_MALLOC(sizeof(os_thread_data))))
  166. return BHT_ERROR;
  167. memset(thread_data, 0, sizeof(os_thread_data));
  168. thread_data->parent = parent;
  169. thread_data->start_routine = start;
  170. thread_data->arg = arg;
  171. if (os_sem_init(&thread_data->wait_node.sem) != BHT_OK)
  172. goto fail1;
  173. if (os_mutex_init(&thread_data->wait_lock) != BHT_OK)
  174. goto fail2;
  175. if (os_cond_init(&thread_data->wait_cond) != BHT_OK)
  176. goto fail3;
  177. os_mutex_lock(&parent->wait_lock);
  178. if (!_beginthreadex(NULL, stack_size, os_thread_wrapper, thread_data, 0,
  179. NULL)) {
  180. os_mutex_unlock(&parent->wait_lock);
  181. goto fail4;
  182. }
  183. /* Wait for the thread routine to set thread_data's tid
  184. and add thread_data to thread data list */
  185. os_cond_wait(&parent->wait_cond, &parent->wait_lock);
  186. os_mutex_unlock(&parent->wait_lock);
  187. *p_tid = (korp_tid)thread_data;
  188. return BHT_OK;
  189. fail4:
  190. os_cond_destroy(&thread_data->wait_cond);
  191. fail3:
  192. os_mutex_destroy(&thread_data->wait_lock);
  193. fail2:
  194. os_sem_destroy(&thread_data->wait_node.sem);
  195. fail1:
  196. BH_FREE(thread_data);
  197. return BHT_ERROR;
  198. }
  199. int
  200. os_thread_create(korp_tid *tid, thread_start_routine_t start, void *arg,
  201. unsigned int stack_size)
  202. {
  203. return os_thread_create_with_prio(tid, start, arg, stack_size,
  204. BH_THREAD_DEFAULT_PRIORITY);
  205. }
  206. korp_tid
  207. os_self_thread()
  208. {
  209. return (korp_tid)TlsGetValue(thread_data_key);
  210. }
  211. int
  212. os_thread_join(korp_tid thread, void **p_retval)
  213. {
  214. os_thread_data *thread_data, *curr_thread_data;
  215. /* Get thread data of current thread */
  216. curr_thread_data = thread_data_current();
  217. curr_thread_data->wait_node.next = NULL;
  218. /* Get thread data of thread to join */
  219. thread_data = (os_thread_data *)thread;
  220. bh_assert(thread_data);
  221. os_mutex_lock(&thread_data->wait_lock);
  222. if (!thread_data->thread_wait_list)
  223. thread_data->thread_wait_list = &curr_thread_data->wait_node;
  224. else {
  225. /* Add to end of waiting list */
  226. os_thread_wait_node *p = thread_data->thread_wait_list;
  227. while (p->next)
  228. p = p->next;
  229. p->next = &curr_thread_data->wait_node;
  230. }
  231. os_mutex_unlock(&thread_data->wait_lock);
  232. /* Wait the sem */
  233. os_sem_wait(&curr_thread_data->wait_node.sem);
  234. if (p_retval)
  235. *p_retval = curr_thread_data->wait_node.retval;
  236. return BHT_OK;
  237. }
  238. int
  239. os_thread_detach(korp_tid thread)
  240. {
  241. /* Do nothing */
  242. return BHT_OK;
  243. (void)thread;
  244. }
  245. void
  246. os_thread_exit(void *retval)
  247. {
  248. os_thread_cleanup(retval);
  249. _endthreadex(0);
  250. }
  251. int
  252. os_thread_env_init()
  253. {
  254. os_thread_data *thread_data = TlsGetValue(thread_data_key);
  255. if (thread_data)
  256. /* Already created */
  257. return BHT_OK;
  258. if (!(thread_data = BH_MALLOC(sizeof(os_thread_data))))
  259. return BHT_ERROR;
  260. memset(thread_data, 0, sizeof(os_thread_data));
  261. thread_data->thread_id = GetCurrentThreadId();
  262. if (os_sem_init(&thread_data->wait_node.sem) != BHT_OK)
  263. goto fail1;
  264. if (os_mutex_init(&thread_data->wait_lock) != BHT_OK)
  265. goto fail2;
  266. if (os_cond_init(&thread_data->wait_cond) != BHT_OK)
  267. goto fail3;
  268. if (!TlsSetValue(thread_data_key, thread_data))
  269. goto fail4;
  270. return BHT_OK;
  271. fail4:
  272. os_cond_destroy(&thread_data->wait_cond);
  273. fail3:
  274. os_mutex_destroy(&thread_data->wait_lock);
  275. fail2:
  276. os_sem_destroy(&thread_data->wait_node.sem);
  277. fail1:
  278. BH_FREE(thread_data);
  279. return BHT_ERROR;
  280. }
  281. void
  282. os_thread_env_destroy()
  283. {
  284. os_thread_data *thread_data = TlsGetValue(thread_data_key);
  285. /* Note that supervisor_thread_data's resources will be destroyed
  286. by os_thread_sys_destroy() */
  287. if (thread_data && thread_data != &supervisor_thread_data) {
  288. TlsSetValue(thread_data_key, NULL);
  289. os_cond_destroy(&thread_data->wait_cond);
  290. os_mutex_destroy(&thread_data->wait_lock);
  291. os_sem_destroy(&thread_data->wait_node.sem);
  292. BH_FREE(thread_data);
  293. }
  294. }
  295. bool
  296. os_thread_env_inited()
  297. {
  298. os_thread_data *thread_data = TlsGetValue(thread_data_key);
  299. return thread_data ? true : false;
  300. }
  301. int
  302. os_sem_init(korp_sem *sem)
  303. {
  304. bh_assert(sem);
  305. *sem = CreateSemaphore(NULL, 0, BH_SEM_COUNT_MAX, NULL);
  306. return (*sem != NULL) ? BHT_OK : BHT_ERROR;
  307. }
  308. int
  309. os_sem_destroy(korp_sem *sem)
  310. {
  311. bh_assert(sem);
  312. CloseHandle(*sem);
  313. return BHT_OK;
  314. }
  315. int
  316. os_sem_wait(korp_sem *sem)
  317. {
  318. DWORD ret;
  319. bh_assert(sem);
  320. ret = WaitForSingleObject(*sem, INFINITE);
  321. if (ret == WAIT_OBJECT_0)
  322. return BHT_OK;
  323. else if (ret == WAIT_TIMEOUT)
  324. return (int)WAIT_TIMEOUT;
  325. else /* WAIT_FAILED or others */
  326. return BHT_ERROR;
  327. }
  328. int
  329. os_sem_reltimed_wait(korp_sem *sem, uint64 useconds)
  330. {
  331. uint64 mseconds_64;
  332. DWORD ret, mseconds;
  333. bh_assert(sem);
  334. if (useconds == BHT_WAIT_FOREVER)
  335. mseconds = INFINITE;
  336. else {
  337. mseconds_64 = useconds / 1000;
  338. if (mseconds_64 < (uint64)(UINT32_MAX - 1)) {
  339. mseconds = (uint32)mseconds_64;
  340. }
  341. else {
  342. mseconds = UINT32_MAX - 1;
  343. os_printf("Warning: os_sem_reltimed_wait exceeds limit, "
  344. "set to max timeout instead\n");
  345. }
  346. }
  347. ret = WaitForSingleObject(*sem, mseconds);
  348. if (ret == WAIT_OBJECT_0)
  349. return BHT_OK;
  350. else if (ret == WAIT_TIMEOUT)
  351. return (int)WAIT_TIMEOUT;
  352. else /* WAIT_FAILED or others */
  353. return BHT_ERROR;
  354. }
  355. int
  356. os_sem_signal(korp_sem *sem)
  357. {
  358. bh_assert(sem);
  359. return ReleaseSemaphore(*sem, 1, NULL) != FALSE ? BHT_OK : BHT_ERROR;
  360. }
  361. int
  362. os_mutex_init(korp_mutex *mutex)
  363. {
  364. bh_assert(mutex);
  365. *mutex = CreateMutex(NULL, FALSE, NULL);
  366. return (*mutex != NULL) ? BHT_OK : BHT_ERROR;
  367. }
  368. int
  369. os_recursive_mutex_init(korp_mutex *mutex)
  370. {
  371. bh_assert(mutex);
  372. *mutex = CreateMutex(NULL, FALSE, NULL);
  373. return (*mutex != NULL) ? BHT_OK : BHT_ERROR;
  374. }
  375. int
  376. os_mutex_destroy(korp_mutex *mutex)
  377. {
  378. assert(mutex);
  379. return CloseHandle(*mutex) ? BHT_OK : BHT_ERROR;
  380. }
  381. int
  382. os_mutex_lock(korp_mutex *mutex)
  383. {
  384. int ret;
  385. assert(mutex);
  386. ret = WaitForSingleObject(*mutex, INFINITE);
  387. return ret != WAIT_FAILED ? BHT_OK : BHT_ERROR;
  388. }
  389. int
  390. os_mutex_unlock(korp_mutex *mutex)
  391. {
  392. bh_assert(mutex);
  393. return ReleaseMutex(*mutex) ? BHT_OK : BHT_ERROR;
  394. }
  395. int
  396. os_cond_init(korp_cond *cond)
  397. {
  398. bh_assert(cond);
  399. if (os_mutex_init(&cond->wait_list_lock) != BHT_OK)
  400. return BHT_ERROR;
  401. cond->thread_wait_list = NULL;
  402. return BHT_OK;
  403. }
  404. int
  405. os_cond_destroy(korp_cond *cond)
  406. {
  407. bh_assert(cond);
  408. os_mutex_destroy(&cond->wait_list_lock);
  409. return BHT_OK;
  410. }
  411. static int
  412. os_cond_wait_internal(korp_cond *cond, korp_mutex *mutex, bool timed,
  413. uint64 useconds)
  414. {
  415. os_thread_wait_node *node = &thread_data_current()->wait_node;
  416. node->next = NULL;
  417. bh_assert(cond);
  418. bh_assert(mutex);
  419. os_mutex_lock(&cond->wait_list_lock);
  420. if (!cond->thread_wait_list)
  421. cond->thread_wait_list = node;
  422. else {
  423. /* Add to end of wait list */
  424. os_thread_wait_node *p = cond->thread_wait_list;
  425. while (p->next)
  426. p = p->next;
  427. p->next = node;
  428. }
  429. os_mutex_unlock(&cond->wait_list_lock);
  430. /* Unlock mutex, wait sem and lock mutex again */
  431. os_mutex_unlock(mutex);
  432. int wait_result;
  433. if (timed)
  434. wait_result = os_sem_reltimed_wait(&node->sem, useconds);
  435. else
  436. wait_result = os_sem_wait(&node->sem);
  437. os_mutex_lock(mutex);
  438. /* Remove wait node from wait list */
  439. os_mutex_lock(&cond->wait_list_lock);
  440. if (cond->thread_wait_list == node)
  441. cond->thread_wait_list = node->next;
  442. else {
  443. /* Remove from the wait list */
  444. os_thread_wait_node *p = cond->thread_wait_list;
  445. while (p->next != node)
  446. p = p->next;
  447. p->next = node->next;
  448. }
  449. os_mutex_unlock(&cond->wait_list_lock);
  450. return wait_result;
  451. }
  452. int
  453. os_cond_wait(korp_cond *cond, korp_mutex *mutex)
  454. {
  455. return os_cond_wait_internal(cond, mutex, false, 0);
  456. }
  457. int
  458. os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds)
  459. {
  460. if (useconds == BHT_WAIT_FOREVER) {
  461. return os_cond_wait_internal(cond, mutex, false, 0);
  462. }
  463. else {
  464. return os_cond_wait_internal(cond, mutex, true, useconds);
  465. }
  466. }
  467. int
  468. os_cond_signal(korp_cond *cond)
  469. {
  470. /* Signal the head wait node of wait list */
  471. os_mutex_lock(&cond->wait_list_lock);
  472. if (cond->thread_wait_list)
  473. os_sem_signal(&cond->thread_wait_list->sem);
  474. os_mutex_unlock(&cond->wait_list_lock);
  475. return BHT_OK;
  476. }
  477. int
  478. os_cond_broadcast(korp_cond *cond)
  479. {
  480. /* Signal all of the wait node of wait list */
  481. os_mutex_lock(&cond->wait_list_lock);
  482. if (cond->thread_wait_list) {
  483. os_thread_wait_node *p = cond->thread_wait_list;
  484. while (p) {
  485. os_sem_signal(&p->sem);
  486. p = p->next;
  487. }
  488. }
  489. os_mutex_unlock(&cond->wait_list_lock);
  490. return BHT_OK;
  491. }
  492. static os_thread_local_attribute uint8 *thread_stack_boundary = NULL;
  493. static ULONG
  494. GetCurrentThreadStackLimits_Win7(PULONG_PTR p_low_limit,
  495. PULONG_PTR p_high_limit)
  496. {
  497. MEMORY_BASIC_INFORMATION mbi;
  498. NT_TIB *tib = (NT_TIB *)NtCurrentTeb();
  499. if (!tib) {
  500. os_printf("warning: NtCurrentTeb() failed\n");
  501. return -1;
  502. }
  503. *p_high_limit = (ULONG_PTR)tib->StackBase;
  504. if (VirtualQuery(tib->StackLimit, &mbi, sizeof(mbi))) {
  505. *p_low_limit = (ULONG_PTR)mbi.AllocationBase;
  506. return 0;
  507. }
  508. os_printf("warning: VirtualQuery() failed\n");
  509. return GetLastError();
  510. }
  511. uint8 *
  512. os_thread_get_stack_boundary()
  513. {
  514. ULONG_PTR low_limit = 0, high_limit = 0;
  515. uint32 page_size;
  516. if (thread_stack_boundary)
  517. return thread_stack_boundary;
  518. page_size = os_getpagesize();
  519. if (GetCurrentThreadStackLimits_Kernel32) {
  520. GetCurrentThreadStackLimits_Kernel32(&low_limit, &high_limit);
  521. }
  522. else {
  523. if (0 != GetCurrentThreadStackLimits_Win7(&low_limit, &high_limit))
  524. return NULL;
  525. }
  526. /* 4 pages are set unaccessible by system, we reserved
  527. one more page at least for safety */
  528. thread_stack_boundary = (uint8 *)(uintptr_t)low_limit + page_size * 5;
  529. return thread_stack_boundary;
  530. }
  531. #ifdef OS_ENABLE_HW_BOUND_CHECK
  532. static os_thread_local_attribute bool thread_signal_inited = false;
  533. int
  534. os_thread_signal_init()
  535. {
  536. ULONG StackSizeInBytes = 16 * 1024;
  537. bool ret;
  538. if (thread_signal_inited)
  539. return 0;
  540. ret = SetThreadStackGuarantee(&StackSizeInBytes);
  541. if (ret)
  542. thread_signal_inited = true;
  543. return ret ? 0 : -1;
  544. }
  545. void
  546. os_thread_signal_destroy()
  547. {
  548. /* Do nothing */
  549. }
  550. bool
  551. os_thread_signal_inited()
  552. {
  553. return thread_signal_inited;
  554. }
  555. #endif