win_thread.c 15 KB

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