win_thread.c 18 KB

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