win_thread.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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. int
  296. os_sem_init(korp_sem *sem)
  297. {
  298. bh_assert(sem);
  299. *sem = CreateSemaphore(NULL, 0, BH_SEM_COUNT_MAX, NULL);
  300. return (*sem != NULL) ? BHT_OK : BHT_ERROR;
  301. }
  302. int
  303. os_sem_destroy(korp_sem *sem)
  304. {
  305. bh_assert(sem);
  306. CloseHandle(*sem);
  307. return BHT_OK;
  308. }
  309. int
  310. os_sem_wait(korp_sem *sem)
  311. {
  312. DWORD ret;
  313. bh_assert(sem);
  314. ret = WaitForSingleObject(*sem, INFINITE);
  315. if (ret == WAIT_OBJECT_0)
  316. return BHT_OK;
  317. else if (ret == WAIT_TIMEOUT)
  318. return (int)WAIT_TIMEOUT;
  319. else /* WAIT_FAILED or others */
  320. return BHT_ERROR;
  321. }
  322. int
  323. os_sem_reltimed_wait(korp_sem *sem, uint64 useconds)
  324. {
  325. uint64 mseconds_64;
  326. DWORD ret, mseconds;
  327. bh_assert(sem);
  328. if (useconds == BHT_WAIT_FOREVER)
  329. mseconds = INFINITE;
  330. else {
  331. mseconds_64 = useconds / 1000;
  332. if (mseconds_64 < (uint64)(UINT32_MAX - 1)) {
  333. mseconds = (uint32)mseconds_64;
  334. }
  335. else {
  336. mseconds = UINT32_MAX - 1;
  337. os_printf("Warning: os_sem_reltimed_wait exceeds limit, "
  338. "set to max timeout instead\n");
  339. }
  340. }
  341. ret = WaitForSingleObject(*sem, mseconds);
  342. if (ret == WAIT_OBJECT_0)
  343. return BHT_OK;
  344. else if (ret == WAIT_TIMEOUT)
  345. return (int)WAIT_TIMEOUT;
  346. else /* WAIT_FAILED or others */
  347. return BHT_ERROR;
  348. }
  349. int
  350. os_sem_signal(korp_sem *sem)
  351. {
  352. bh_assert(sem);
  353. return ReleaseSemaphore(*sem, 1, NULL) != FALSE ? BHT_OK : BHT_ERROR;
  354. }
  355. int
  356. os_mutex_init(korp_mutex *mutex)
  357. {
  358. bh_assert(mutex);
  359. *mutex = CreateMutex(NULL, FALSE, NULL);
  360. return (*mutex != NULL) ? BHT_OK : BHT_ERROR;
  361. }
  362. int
  363. os_recursive_mutex_init(korp_mutex *mutex)
  364. {
  365. bh_assert(mutex);
  366. *mutex = CreateMutex(NULL, FALSE, NULL);
  367. return (*mutex != NULL) ? BHT_OK : BHT_ERROR;
  368. }
  369. int
  370. os_mutex_destroy(korp_mutex *mutex)
  371. {
  372. assert(mutex);
  373. return CloseHandle(*mutex) ? BHT_OK : BHT_ERROR;
  374. }
  375. int
  376. os_mutex_lock(korp_mutex *mutex)
  377. {
  378. int ret;
  379. assert(mutex);
  380. ret = WaitForSingleObject(*mutex, INFINITE);
  381. return ret != WAIT_FAILED ? BHT_OK : BHT_ERROR;
  382. }
  383. int
  384. os_mutex_unlock(korp_mutex *mutex)
  385. {
  386. bh_assert(mutex);
  387. return ReleaseMutex(*mutex) ? BHT_OK : BHT_ERROR;
  388. }
  389. int
  390. os_cond_init(korp_cond *cond)
  391. {
  392. bh_assert(cond);
  393. if (os_mutex_init(&cond->wait_list_lock) != BHT_OK)
  394. return BHT_ERROR;
  395. cond->thread_wait_list = NULL;
  396. return BHT_OK;
  397. }
  398. int
  399. os_cond_destroy(korp_cond *cond)
  400. {
  401. bh_assert(cond);
  402. os_mutex_destroy(&cond->wait_list_lock);
  403. return BHT_OK;
  404. }
  405. static int
  406. os_cond_wait_internal(korp_cond *cond, korp_mutex *mutex, bool timed,
  407. uint64 useconds)
  408. {
  409. os_thread_wait_node *node = &thread_data_current()->wait_node;
  410. node->next = NULL;
  411. bh_assert(cond);
  412. bh_assert(mutex);
  413. os_mutex_lock(&cond->wait_list_lock);
  414. if (!cond->thread_wait_list)
  415. cond->thread_wait_list = node;
  416. else {
  417. /* Add to end of wait list */
  418. os_thread_wait_node *p = cond->thread_wait_list;
  419. while (p->next)
  420. p = p->next;
  421. p->next = node;
  422. }
  423. os_mutex_unlock(&cond->wait_list_lock);
  424. /* Unlock mutex, wait sem and lock mutex again */
  425. os_mutex_unlock(mutex);
  426. int wait_result;
  427. if (timed)
  428. wait_result = os_sem_reltimed_wait(&node->sem, useconds);
  429. else
  430. wait_result = os_sem_wait(&node->sem);
  431. os_mutex_lock(mutex);
  432. /* Remove wait node from wait list */
  433. os_mutex_lock(&cond->wait_list_lock);
  434. if (cond->thread_wait_list == node)
  435. cond->thread_wait_list = node->next;
  436. else {
  437. /* Remove from the wait list */
  438. os_thread_wait_node *p = cond->thread_wait_list;
  439. while (p->next != node)
  440. p = p->next;
  441. p->next = node->next;
  442. }
  443. os_mutex_unlock(&cond->wait_list_lock);
  444. return wait_result;
  445. }
  446. int
  447. os_cond_wait(korp_cond *cond, korp_mutex *mutex)
  448. {
  449. return os_cond_wait_internal(cond, mutex, false, 0);
  450. }
  451. int
  452. os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds)
  453. {
  454. if (useconds == BHT_WAIT_FOREVER) {
  455. return os_cond_wait_internal(cond, mutex, false, 0);
  456. }
  457. else {
  458. return os_cond_wait_internal(cond, mutex, true, useconds);
  459. }
  460. }
  461. int
  462. os_cond_signal(korp_cond *cond)
  463. {
  464. /* Signal the head wait node of wait list */
  465. os_mutex_lock(&cond->wait_list_lock);
  466. if (cond->thread_wait_list)
  467. os_sem_signal(&cond->thread_wait_list->sem);
  468. os_mutex_unlock(&cond->wait_list_lock);
  469. return BHT_OK;
  470. }
  471. int
  472. os_cond_broadcast(korp_cond *cond)
  473. {
  474. /* Signal all of the wait node of wait list */
  475. os_mutex_lock(&cond->wait_list_lock);
  476. if (cond->thread_wait_list) {
  477. os_thread_wait_node *p = cond->thread_wait_list;
  478. while (p) {
  479. os_sem_signal(&p->sem);
  480. p = p->next;
  481. }
  482. }
  483. os_mutex_unlock(&cond->wait_list_lock);
  484. return BHT_OK;
  485. }
  486. static os_thread_local_attribute uint8 *thread_stack_boundary = NULL;
  487. static ULONG
  488. GetCurrentThreadStackLimits_Win7(PULONG_PTR p_low_limit,
  489. PULONG_PTR p_high_limit)
  490. {
  491. MEMORY_BASIC_INFORMATION mbi;
  492. NT_TIB *tib = (NT_TIB *)NtCurrentTeb();
  493. if (!tib) {
  494. os_printf("warning: NtCurrentTeb() failed\n");
  495. return -1;
  496. }
  497. *p_high_limit = (ULONG_PTR)tib->StackBase;
  498. if (VirtualQuery(tib->StackLimit, &mbi, sizeof(mbi))) {
  499. *p_low_limit = (ULONG_PTR)mbi.AllocationBase;
  500. return 0;
  501. }
  502. os_printf("warning: VirtualQuery() failed\n");
  503. return GetLastError();
  504. }
  505. uint8 *
  506. os_thread_get_stack_boundary()
  507. {
  508. ULONG_PTR low_limit = 0, high_limit = 0;
  509. uint32 page_size;
  510. if (thread_stack_boundary)
  511. return thread_stack_boundary;
  512. page_size = os_getpagesize();
  513. if (GetCurrentThreadStackLimits_Kernel32) {
  514. GetCurrentThreadStackLimits_Kernel32(&low_limit, &high_limit);
  515. }
  516. else {
  517. if (0 != GetCurrentThreadStackLimits_Win7(&low_limit, &high_limit))
  518. return NULL;
  519. }
  520. /* 4 pages are set unaccessible by system, we reserved
  521. one more page at least for safety */
  522. thread_stack_boundary = (uint8 *)(uintptr_t)low_limit + page_size * 5;
  523. return thread_stack_boundary;
  524. }
  525. #ifdef OS_ENABLE_HW_BOUND_CHECK
  526. static os_thread_local_attribute bool thread_signal_inited = false;
  527. int
  528. os_thread_signal_init()
  529. {
  530. ULONG StackSizeInBytes = 16 * 1024;
  531. bool ret;
  532. if (thread_signal_inited)
  533. return 0;
  534. ret = SetThreadStackGuarantee(&StackSizeInBytes);
  535. if (ret)
  536. thread_signal_inited = true;
  537. return ret ? 0 : -1;
  538. }
  539. void
  540. os_thread_signal_destroy()
  541. {
  542. /* Do nothing */
  543. }
  544. bool
  545. os_thread_signal_inited()
  546. {
  547. return thread_signal_inited;
  548. }
  549. #endif