win_thread.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. int os_sem_init(korp_sem* sem);
  41. int os_sem_destroy(korp_sem* sem);
  42. int os_sem_wait(korp_sem* sem);
  43. int os_sem_reltimed_wait(korp_sem* sem, uint64 useconds);
  44. int os_sem_signal(korp_sem* sem);
  45. int
  46. os_thread_sys_init()
  47. {
  48. if (is_thread_sys_inited)
  49. return BHT_OK;
  50. if ((thread_data_key = TlsAlloc()) == TLS_OUT_OF_INDEXES)
  51. return BHT_ERROR;
  52. /* Initialize supervisor thread data */
  53. memset(&supervisor_thread_data, 0, sizeof(os_thread_data));
  54. supervisor_thread_data.thread_id = GetCurrentThreadId();
  55. if (os_sem_init(&supervisor_thread_data.wait_node.sem) != BHT_OK)
  56. goto fail1;
  57. if (os_mutex_init(&supervisor_thread_data.wait_lock) != BHT_OK)
  58. goto fail2;
  59. if (os_cond_init(&supervisor_thread_data.wait_cond) != BHT_OK)
  60. goto fail3;
  61. if (!TlsSetValue(thread_data_key, &supervisor_thread_data))
  62. goto fail4;
  63. is_thread_sys_inited = true;
  64. return BHT_OK;
  65. fail4:
  66. os_cond_destroy(&supervisor_thread_data.wait_cond);
  67. fail3:
  68. os_mutex_destroy(&supervisor_thread_data.wait_lock);
  69. fail2:
  70. os_sem_destroy(&supervisor_thread_data.wait_node.sem);
  71. fail1:
  72. TlsFree(thread_data_key);
  73. return BHT_ERROR;
  74. }
  75. void
  76. os_thread_sys_destroy()
  77. {
  78. if (is_thread_sys_inited) {
  79. os_cond_destroy(&supervisor_thread_data.wait_cond);
  80. os_mutex_destroy(&supervisor_thread_data.wait_lock);
  81. os_sem_destroy(&supervisor_thread_data.wait_node.sem);
  82. memset(&supervisor_thread_data, 0, sizeof(os_thread_data));
  83. TlsFree(thread_data_key);
  84. thread_data_key = 0;
  85. is_thread_sys_inited = false;
  86. }
  87. }
  88. static os_thread_data *
  89. thread_data_current()
  90. {
  91. return (os_thread_data *)TlsGetValue(thread_data_key);
  92. }
  93. static void
  94. os_thread_cleanup(void *retval)
  95. {
  96. os_thread_data *thread_data = thread_data_current();
  97. bh_assert(thread_data != NULL);
  98. os_mutex_lock(&thread_data->wait_lock);
  99. if (thread_data->thread_wait_list) {
  100. /* Signal each joining thread */
  101. os_thread_wait_list head = thread_data->thread_wait_list;
  102. while (head) {
  103. os_thread_wait_list next = head->next;
  104. head->retval = retval;
  105. os_sem_signal(&head->sem);
  106. head = next;
  107. }
  108. thread_data->thread_wait_list = NULL;
  109. }
  110. os_mutex_unlock(&thread_data->wait_lock);
  111. /* Destroy resources */
  112. os_cond_destroy(&thread_data->wait_cond);
  113. os_sem_destroy(&thread_data->wait_node.sem);
  114. os_mutex_destroy(&thread_data->wait_lock);
  115. BH_FREE(thread_data);
  116. }
  117. static unsigned
  118. os_thread_wrapper(void *arg)
  119. {
  120. os_thread_data *thread_data = arg;
  121. os_thread_data *parent = thread_data->parent;
  122. void *retval;
  123. bool result;
  124. os_printf("THREAD CREATED %p\n", thread_data);
  125. os_mutex_lock(&parent->wait_lock);
  126. thread_data->thread_id = GetCurrentThreadId();
  127. result = TlsSetValue(thread_data_key, thread_data);
  128. /* Notify parent thread */
  129. os_cond_signal(&parent->wait_cond);
  130. os_mutex_unlock(&parent->wait_lock);
  131. if (!result)
  132. return -1;
  133. retval = thread_data->start_routine(thread_data->arg);
  134. os_thread_cleanup(retval);
  135. return 0;
  136. }
  137. int
  138. os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
  139. void *arg, unsigned int stack_size, int prio)
  140. {
  141. os_thread_data *parent = thread_data_current();
  142. os_thread_data *thread_data;
  143. if (!p_tid || !start)
  144. return BHT_ERROR;
  145. if (stack_size < BH_APPLET_PRESERVED_STACK_SIZE)
  146. stack_size = BH_APPLET_PRESERVED_STACK_SIZE;
  147. if (!(thread_data = BH_MALLOC(sizeof(os_thread_data))))
  148. return BHT_ERROR;
  149. memset(thread_data, 0, sizeof(os_thread_data));
  150. thread_data->parent = parent;
  151. thread_data->start_routine = start;
  152. thread_data->arg = arg;
  153. if (os_sem_init(&thread_data->wait_node.sem) != BHT_OK)
  154. goto fail1;
  155. if (os_mutex_init(&thread_data->wait_lock) != BHT_OK)
  156. goto fail2;
  157. if (os_cond_init(&thread_data->wait_cond) != BHT_OK)
  158. goto fail3;
  159. os_mutex_lock(&parent->wait_lock);
  160. if (!_beginthreadex(NULL, stack_size,
  161. os_thread_wrapper, thread_data,
  162. 0, NULL)) {
  163. os_mutex_unlock(&parent->wait_lock);
  164. goto fail4;
  165. }
  166. /* Wait for the thread routine to set thread_data's tid
  167. and add thread_data to thread data list */
  168. os_cond_wait(&parent->wait_cond, &parent->wait_lock);
  169. os_mutex_unlock(&parent->wait_lock);
  170. *p_tid = (korp_tid)thread_data;
  171. return BHT_OK;
  172. fail4:
  173. os_cond_destroy(&thread_data->wait_cond);
  174. fail3:
  175. os_mutex_destroy(&thread_data->wait_lock);
  176. fail2:
  177. os_sem_destroy(&thread_data->wait_node.sem);
  178. fail1:
  179. BH_FREE(thread_data);
  180. return BHT_ERROR;
  181. }
  182. int
  183. os_thread_create(korp_tid *tid, thread_start_routine_t start, void *arg,
  184. unsigned int stack_size)
  185. {
  186. return os_thread_create_with_prio(tid, start, arg, stack_size,
  187. BH_THREAD_DEFAULT_PRIORITY);
  188. }
  189. korp_tid
  190. os_self_thread()
  191. {
  192. return (korp_tid)TlsGetValue(thread_data_key);
  193. }
  194. int
  195. os_thread_join(korp_tid thread, void **p_retval)
  196. {
  197. os_thread_data *thread_data, *curr_thread_data;
  198. /* Get thread data of current thread */
  199. curr_thread_data = thread_data_current();
  200. curr_thread_data->wait_node.next = NULL;
  201. /* Get thread data of thread to join */
  202. thread_data = (os_thread_data *)thread;
  203. bh_assert(thread_data);
  204. os_mutex_lock(&thread_data->wait_lock);
  205. if (!thread_data->thread_wait_list)
  206. thread_data->thread_wait_list = &curr_thread_data->wait_node;
  207. else {
  208. /* Add to end of waiting list */
  209. os_thread_wait_node *p = thread_data->thread_wait_list;
  210. while (p->next)
  211. p = p->next;
  212. p->next = &curr_thread_data->wait_node;
  213. }
  214. os_mutex_unlock(&thread_data->wait_lock);
  215. /* Wait the sem */
  216. os_sem_wait(&curr_thread_data->wait_node.sem);
  217. if (p_retval)
  218. *p_retval = curr_thread_data->wait_node.retval;
  219. return BHT_OK;
  220. }
  221. int
  222. os_thread_detach(korp_tid thread)
  223. {
  224. /* Do nothing */
  225. return BHT_OK;
  226. (void)thread;
  227. }
  228. void
  229. os_thread_exit(void *retval)
  230. {
  231. os_thread_cleanup(retval);
  232. _endthreadex(0);
  233. }
  234. int
  235. os_sem_init(korp_sem *sem)
  236. {
  237. bh_assert(sem);
  238. *sem = CreateSemaphore(NULL, 0, BH_SEM_COUNT_MAX, NULL);
  239. return (*sem != NULL) ? BHT_OK : BHT_ERROR;
  240. }
  241. int
  242. os_sem_destroy(korp_sem *sem)
  243. {
  244. bh_assert(sem);
  245. CloseHandle(*sem);
  246. return BHT_OK;
  247. }
  248. int
  249. os_sem_wait(korp_sem *sem)
  250. {
  251. DWORD ret;
  252. bh_assert(sem);
  253. ret = WaitForSingleObject(*sem, INFINITE);
  254. if (ret == WAIT_OBJECT_0)
  255. return BHT_OK;
  256. else if(ret == WAIT_TIMEOUT)
  257. return (int)WAIT_TIMEOUT;
  258. else /* WAIT_FAILED or others */
  259. return BHT_ERROR;
  260. }
  261. int
  262. os_sem_reltimed_wait(korp_sem *sem, uint64 useconds)
  263. {
  264. uint64 mseconds_64;
  265. DWORD ret, mseconds;
  266. bh_assert(sem);
  267. if (useconds == BHT_WAIT_FOREVER)
  268. mseconds = INFINITE;
  269. else {
  270. mseconds_64 = useconds / 1000;
  271. if (mseconds_64 < (uint64)(UINT32_MAX - 1)) {
  272. mseconds = (uint32)mseconds_64;
  273. }
  274. else {
  275. mseconds = UINT32_MAX - 1;
  276. os_printf("Warning: os_sem_reltimed_wait exceeds limit, "
  277. "set to max timeout instead\n");
  278. }
  279. }
  280. ret = WaitForSingleObject(*sem, mseconds);
  281. if (ret == WAIT_OBJECT_0)
  282. return BHT_OK;
  283. else if(ret == WAIT_TIMEOUT)
  284. return (int)WAIT_TIMEOUT;
  285. else /* WAIT_FAILED or others */
  286. return BHT_ERROR;
  287. }
  288. int
  289. os_sem_signal(korp_sem *sem)
  290. {
  291. bh_assert(sem);
  292. return ReleaseSemaphore(*sem, 1, NULL) != FALSE
  293. ? BHT_OK: BHT_ERROR;
  294. }
  295. int
  296. os_mutex_init(korp_mutex *mutex)
  297. {
  298. bh_assert(mutex);
  299. *mutex = CreateMutex(NULL, FALSE, NULL);
  300. return (*mutex != NULL) ? BHT_OK : BHT_ERROR;
  301. }
  302. int
  303. os_recursive_mutex_init(korp_mutex *mutex)
  304. {
  305. bh_assert(mutex);
  306. *mutex = CreateMutex(NULL, FALSE, NULL);
  307. return (*mutex != NULL) ? BHT_OK : BHT_ERROR;
  308. }
  309. int
  310. os_mutex_destroy(korp_mutex *mutex)
  311. {
  312. assert(mutex);
  313. return CloseHandle(*mutex) ? BHT_OK : BHT_ERROR;
  314. }
  315. int
  316. os_mutex_lock(korp_mutex *mutex)
  317. {
  318. int ret;
  319. assert(mutex);
  320. ret = WaitForSingleObject(*mutex, INFINITE);
  321. return ret != WAIT_FAILED ? BHT_OK : BHT_ERROR;
  322. }
  323. int
  324. os_mutex_unlock(korp_mutex *mutex)
  325. {
  326. bh_assert(mutex);
  327. return ReleaseMutex(*mutex) ? BHT_OK : BHT_ERROR;
  328. }
  329. int
  330. os_cond_init(korp_cond *cond)
  331. {
  332. bh_assert(cond);
  333. if (os_mutex_init(&cond->wait_list_lock) != BHT_OK)
  334. return BHT_ERROR;
  335. cond->thread_wait_list = NULL;
  336. return BHT_OK;
  337. }
  338. int
  339. os_cond_destroy(korp_cond *cond)
  340. {
  341. bh_assert(cond);
  342. os_mutex_destroy(&cond->wait_list_lock);
  343. return BHT_OK;
  344. }
  345. static int
  346. os_cond_wait_internal(korp_cond *cond, korp_mutex *mutex,
  347. bool timed, uint64 useconds)
  348. {
  349. os_thread_wait_node *node = &thread_data_current()->wait_node;
  350. node->next = NULL;
  351. bh_assert(cond);
  352. bh_assert(mutex);
  353. os_mutex_lock(&cond->wait_list_lock);
  354. if (!cond->thread_wait_list)
  355. cond->thread_wait_list = node;
  356. else {
  357. /* Add to end of wait list */
  358. os_thread_wait_node *p = cond->thread_wait_list;
  359. while (p->next)
  360. p = p->next;
  361. p->next = node;
  362. }
  363. os_mutex_unlock(&cond->wait_list_lock);
  364. /* Unlock mutex, wait sem and lock mutex again */
  365. os_mutex_unlock(mutex);
  366. if (timed)
  367. os_sem_reltimed_wait(&node->sem, useconds);
  368. else
  369. os_sem_wait(&node->sem);
  370. os_mutex_lock(mutex);
  371. /* Remove wait node from wait list */
  372. os_mutex_lock(&cond->wait_list_lock);
  373. if (cond->thread_wait_list == node)
  374. cond->thread_wait_list = node->next;
  375. else {
  376. /* Remove from the wait list */
  377. os_thread_wait_node *p = cond->thread_wait_list;
  378. while (p->next != node)
  379. p = p->next;
  380. p->next = node->next;
  381. }
  382. os_mutex_unlock(&cond->wait_list_lock);
  383. return BHT_OK;
  384. }
  385. int
  386. os_cond_wait(korp_cond *cond, korp_mutex *mutex)
  387. {
  388. return os_cond_wait_internal(cond, mutex, false, 0);
  389. }
  390. int
  391. os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds)
  392. {
  393. if (useconds == BHT_WAIT_FOREVER) {
  394. return os_cond_wait_internal(cond, mutex, false, 0);
  395. }
  396. else {
  397. return os_cond_wait_internal(cond, mutex, true, useconds);
  398. }
  399. }
  400. int
  401. os_cond_signal(korp_cond *cond)
  402. {
  403. /* Signal the head wait node of wait list */
  404. os_mutex_lock(&cond->wait_list_lock);
  405. if (cond->thread_wait_list)
  406. os_sem_signal(&cond->thread_wait_list->sem);
  407. os_mutex_unlock(&cond->wait_list_lock);
  408. return BHT_OK;
  409. }
  410. static os_thread_local_attribute uint8 *thread_stack_boundary = NULL;
  411. uint8 *
  412. os_thread_get_stack_boundary()
  413. {
  414. ULONG_PTR low_limit = 0, high_limit = 0;
  415. uint32 page_size;
  416. if (thread_stack_boundary)
  417. return thread_stack_boundary;
  418. page_size = os_getpagesize();
  419. GetCurrentThreadStackLimits(&low_limit, &high_limit);
  420. /* 4 pages are set unaccessible by system, we reserved
  421. one more page at least for safety */
  422. thread_stack_boundary = (uint8*)(uintptr_t)low_limit + page_size * 5;
  423. return thread_stack_boundary;
  424. }
  425. static os_thread_local_attribute bool stack_guard_pages_inited = false;
  426. bool
  427. os_thread_init_stack_guard_pages()
  428. {
  429. ULONG StackSizeInBytes = 16 * 1024;
  430. bool ret;
  431. if (stack_guard_pages_inited)
  432. return true;
  433. ret = SetThreadStackGuarantee(&StackSizeInBytes);
  434. if (ret)
  435. stack_guard_pages_inited = true;
  436. return ret;
  437. }
  438. void
  439. os_thread_destroy_stack_guard_pages()
  440. {
  441. }