win_thread.c 19 KB

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