win_thread.c 21 KB

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