lib_pthread_wrapper.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "bh_common.h"
  6. #include "bh_log.h"
  7. #include "wasm_export.h"
  8. #include "../interpreter/wasm.h"
  9. #include "../common/wasm_runtime_common.h"
  10. #include "thread_manager.h"
  11. #if WASM_ENABLE_INTERP != 0
  12. #include "wasm_runtime.h"
  13. #endif
  14. #if WASM_ENABLE_AOT != 0
  15. #include "aot_runtime.h"
  16. #endif
  17. #define WAMR_PTHREAD_KEYS_MAX 32
  18. /* clang-format off */
  19. #define get_module(exec_env) \
  20. wasm_exec_env_get_module(exec_env)
  21. #define get_module_inst(exec_env) \
  22. wasm_runtime_get_module_inst(exec_env)
  23. #define get_thread_arg(exec_env) \
  24. wasm_exec_env_get_thread_arg(exec_env)
  25. #define get_wasi_ctx(module_inst) \
  26. wasm_runtime_get_wasi_ctx(module_inst)
  27. #define validate_app_addr(offset, size) \
  28. wasm_runtime_validate_app_addr(module_inst, offset, size)
  29. #define validate_native_addr(addr, size) \
  30. wasm_runtime_validate_native_addr(module_inst, addr, size)
  31. #define addr_app_to_native(offset) \
  32. wasm_runtime_addr_app_to_native(module_inst, offset)
  33. #define addr_native_to_app(ptr) \
  34. wasm_runtime_addr_native_to_app(module_inst, ptr)
  35. /* clang-format on */
  36. extern bool
  37. wasm_runtime_call_indirect(wasm_exec_env_t exec_env, uint32 element_indices,
  38. uint32 argc, uint32 argv[]);
  39. enum {
  40. T_THREAD,
  41. T_MUTEX,
  42. T_COND,
  43. T_SEM,
  44. };
  45. enum thread_status_t {
  46. THREAD_INIT,
  47. THREAD_RUNNING,
  48. THREAD_CANCELLED,
  49. THREAD_EXIT,
  50. };
  51. enum mutex_status_t {
  52. MUTEX_CREATED,
  53. MUTEX_DESTROYED,
  54. };
  55. enum cond_status_t {
  56. COND_CREATED,
  57. COND_DESTROYED,
  58. };
  59. enum sem_status_t {
  60. SEM_CREATED,
  61. SEM_CLOSED,
  62. SEM_DESTROYED,
  63. };
  64. typedef struct ThreadKeyValueNode {
  65. bh_list_link l;
  66. wasm_exec_env_t exec_env;
  67. int32 thread_key_values[WAMR_PTHREAD_KEYS_MAX];
  68. } ThreadKeyValueNode;
  69. typedef struct KeyData {
  70. int32 destructor_func;
  71. bool is_created;
  72. } KeyData;
  73. typedef struct ClusterInfoNode {
  74. bh_list_link l;
  75. WASMCluster *cluster;
  76. HashMap *thread_info_map;
  77. /* Key data list */
  78. KeyData key_data_list[WAMR_PTHREAD_KEYS_MAX];
  79. korp_mutex key_data_list_lock;
  80. /* Every node contains the key value list for a thread */
  81. bh_list thread_list_head;
  82. bh_list *thread_list;
  83. } ClusterInfoNode;
  84. typedef struct ThreadInfoNode {
  85. wasm_exec_env_t parent_exec_env;
  86. wasm_exec_env_t exec_env;
  87. /* the id returned to app */
  88. uint32 handle;
  89. /* type can be [THREAD | MUTEX | CONDITION] */
  90. uint32 type;
  91. /* Thread status, this variable should be volatile
  92. as its value may be changed in different threads */
  93. volatile uint32 status;
  94. bool joinable;
  95. union {
  96. korp_tid thread;
  97. korp_mutex *mutex;
  98. korp_cond *cond;
  99. #if WASM_ENABLE_LIB_PTHREAD_SEMAPHORE != 0
  100. korp_sem *sem;
  101. #endif
  102. /* A copy of the thread return value */
  103. void *ret;
  104. } u;
  105. } ThreadInfoNode;
  106. typedef struct {
  107. ThreadInfoNode *info_node;
  108. /* table elem index of the app's entry function */
  109. uint32 elem_index;
  110. /* arg of the app's entry function */
  111. uint32 arg;
  112. wasm_module_inst_t module_inst;
  113. } ThreadRoutineArgs;
  114. typedef struct {
  115. uint32 handle;
  116. ThreadInfoNode *node;
  117. } SemCallbackArgs;
  118. static bh_list cluster_info_list;
  119. #if WASM_ENABLE_LIB_PTHREAD_SEMAPHORE != 0
  120. static HashMap *sem_info_map;
  121. #endif
  122. static korp_mutex thread_global_lock;
  123. static uint32 handle_id = 1;
  124. static void
  125. lib_pthread_destroy_callback(WASMCluster *cluster);
  126. static uint32
  127. thread_handle_hash(void *handle)
  128. {
  129. return (uint32)(uintptr_t)handle;
  130. }
  131. static bool
  132. thread_handle_equal(void *h1, void *h2)
  133. {
  134. return (uint32)(uintptr_t)h1 == (uint32)(uintptr_t)h2 ? true : false;
  135. }
  136. static void
  137. thread_info_destroy(void *node)
  138. {
  139. ThreadInfoNode *info_node = (ThreadInfoNode *)node;
  140. os_mutex_lock(&thread_global_lock);
  141. if (info_node->type == T_MUTEX) {
  142. if (info_node->status != MUTEX_DESTROYED)
  143. os_mutex_destroy(info_node->u.mutex);
  144. wasm_runtime_free(info_node->u.mutex);
  145. }
  146. else if (info_node->type == T_COND) {
  147. if (info_node->status != COND_DESTROYED)
  148. os_cond_destroy(info_node->u.cond);
  149. wasm_runtime_free(info_node->u.cond);
  150. }
  151. #if WASM_ENABLE_LIB_PTHREAD_SEMAPHORE != 0
  152. else if (info_node->type == T_SEM) {
  153. if (info_node->status != SEM_DESTROYED)
  154. os_sem_close(info_node->u.sem);
  155. }
  156. #endif
  157. wasm_runtime_free(info_node);
  158. os_mutex_unlock(&thread_global_lock);
  159. }
  160. bool
  161. lib_pthread_init()
  162. {
  163. if (0 != os_mutex_init(&thread_global_lock))
  164. return false;
  165. bh_list_init(&cluster_info_list);
  166. if (!wasm_cluster_register_destroy_callback(lib_pthread_destroy_callback)) {
  167. os_mutex_destroy(&thread_global_lock);
  168. return false;
  169. }
  170. #if WASM_ENABLE_LIB_PTHREAD_SEMAPHORE != 0
  171. if (!(sem_info_map = bh_hash_map_create(
  172. 32, true, (HashFunc)wasm_string_hash,
  173. (KeyEqualFunc)wasm_string_equal, NULL, thread_info_destroy))) {
  174. os_mutex_destroy(&thread_global_lock);
  175. return false;
  176. }
  177. #endif
  178. return true;
  179. }
  180. void
  181. lib_pthread_destroy()
  182. {
  183. #if WASM_ENABLE_LIB_PTHREAD_SEMAPHORE != 0
  184. bh_hash_map_destroy(sem_info_map);
  185. #endif
  186. os_mutex_destroy(&thread_global_lock);
  187. }
  188. static ClusterInfoNode *
  189. get_cluster_info(WASMCluster *cluster)
  190. {
  191. ClusterInfoNode *node;
  192. os_mutex_lock(&thread_global_lock);
  193. node = bh_list_first_elem(&cluster_info_list);
  194. while (node) {
  195. if (cluster == node->cluster) {
  196. os_mutex_unlock(&thread_global_lock);
  197. return node;
  198. }
  199. node = bh_list_elem_next(node);
  200. }
  201. os_mutex_unlock(&thread_global_lock);
  202. return NULL;
  203. }
  204. static KeyData *
  205. key_data_list_lookup(wasm_exec_env_t exec_env, int32 key)
  206. {
  207. ClusterInfoNode *node;
  208. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  209. if ((node = get_cluster_info(cluster))) {
  210. return (key >= 0 && key < WAMR_PTHREAD_KEYS_MAX
  211. && node->key_data_list[key].is_created)
  212. ? &(node->key_data_list[key])
  213. : NULL;
  214. }
  215. return NULL;
  216. }
  217. /**
  218. * Lookup the thread key value node for a thread, create a new one if failed
  219. * This design will reduce the memory usage. If the thread doesn't use the
  220. * local storage, it will not occupy memory space.
  221. */
  222. static int32 *
  223. key_value_list_lookup_or_create(wasm_exec_env_t exec_env, ClusterInfoNode *info,
  224. int32 key)
  225. {
  226. KeyData *key_node;
  227. ThreadKeyValueNode *data;
  228. /* Check if the key is valid */
  229. key_node = key_data_list_lookup(exec_env, key);
  230. if (!key_node) {
  231. return NULL;
  232. }
  233. /* Find key values node */
  234. data = bh_list_first_elem(info->thread_list);
  235. while (data) {
  236. if (data->exec_env == exec_env)
  237. return data->thread_key_values;
  238. data = bh_list_elem_next(data);
  239. }
  240. /* If not found, create a new node for this thread */
  241. if (!(data = wasm_runtime_malloc(sizeof(ThreadKeyValueNode))))
  242. return NULL;
  243. memset(data, 0, sizeof(ThreadKeyValueNode));
  244. data->exec_env = exec_env;
  245. if (bh_list_insert(info->thread_list, data) != 0) {
  246. wasm_runtime_free(data);
  247. return NULL;
  248. }
  249. return data->thread_key_values;
  250. }
  251. static void
  252. call_key_destructor(wasm_exec_env_t exec_env)
  253. {
  254. int32 i;
  255. uint32 destructor_index;
  256. KeyData *key_node;
  257. ThreadKeyValueNode *value_node;
  258. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  259. ClusterInfoNode *info = get_cluster_info(cluster);
  260. if (!info) {
  261. return;
  262. }
  263. value_node = bh_list_first_elem(info->thread_list);
  264. while (value_node) {
  265. if (value_node->exec_env == exec_env)
  266. break;
  267. value_node = bh_list_elem_next(value_node);
  268. }
  269. /* This thread hasn't created key value node */
  270. if (!value_node)
  271. return;
  272. /* Destroy key values */
  273. for (i = 0; i < WAMR_PTHREAD_KEYS_MAX; i++) {
  274. if (value_node->thread_key_values[i] != 0) {
  275. int32 value = value_node->thread_key_values[i];
  276. os_mutex_lock(&info->key_data_list_lock);
  277. if ((key_node = key_data_list_lookup(exec_env, i)))
  278. destructor_index = key_node->destructor_func;
  279. else
  280. destructor_index = 0;
  281. os_mutex_unlock(&info->key_data_list_lock);
  282. /* reset key value */
  283. value_node->thread_key_values[i] = 0;
  284. /* Call the destructor func provided by app */
  285. if (destructor_index) {
  286. uint32 argv[1];
  287. argv[0] = value;
  288. wasm_runtime_call_indirect(exec_env, destructor_index, 1, argv);
  289. }
  290. }
  291. }
  292. bh_list_remove(info->thread_list, value_node);
  293. wasm_runtime_free(value_node);
  294. }
  295. static void
  296. destroy_thread_key_value_list(bh_list *list)
  297. {
  298. ThreadKeyValueNode *node, *next;
  299. /* There should be only one node for main thread */
  300. bh_assert(list->len <= 1);
  301. if (list->len) {
  302. node = bh_list_first_elem(list);
  303. while (node) {
  304. next = bh_list_elem_next(node);
  305. call_key_destructor(node->exec_env);
  306. node = next;
  307. }
  308. }
  309. }
  310. static ClusterInfoNode *
  311. create_cluster_info(WASMCluster *cluster)
  312. {
  313. ClusterInfoNode *node;
  314. bh_list_status ret;
  315. if (!(node = wasm_runtime_malloc(sizeof(ClusterInfoNode)))) {
  316. return NULL;
  317. }
  318. memset(node, 0, sizeof(ClusterInfoNode));
  319. node->thread_list = &node->thread_list_head;
  320. ret = bh_list_init(node->thread_list);
  321. bh_assert(ret == BH_LIST_SUCCESS);
  322. if (os_mutex_init(&node->key_data_list_lock) != 0) {
  323. wasm_runtime_free(node);
  324. return NULL;
  325. }
  326. node->cluster = cluster;
  327. if (!(node->thread_info_map = bh_hash_map_create(
  328. 32, true, (HashFunc)thread_handle_hash,
  329. (KeyEqualFunc)thread_handle_equal, NULL, thread_info_destroy))) {
  330. os_mutex_destroy(&node->key_data_list_lock);
  331. wasm_runtime_free(node);
  332. return NULL;
  333. }
  334. os_mutex_lock(&thread_global_lock);
  335. ret = bh_list_insert(&cluster_info_list, node);
  336. bh_assert(ret == BH_LIST_SUCCESS);
  337. os_mutex_unlock(&thread_global_lock);
  338. (void)ret;
  339. return node;
  340. }
  341. static bool
  342. destroy_cluster_info(WASMCluster *cluster)
  343. {
  344. ClusterInfoNode *node = get_cluster_info(cluster);
  345. if (node) {
  346. bh_hash_map_destroy(node->thread_info_map);
  347. destroy_thread_key_value_list(node->thread_list);
  348. os_mutex_destroy(&node->key_data_list_lock);
  349. /* Remove from the cluster info list */
  350. os_mutex_lock(&thread_global_lock);
  351. bh_list_remove(&cluster_info_list, node);
  352. wasm_runtime_free(node);
  353. os_mutex_unlock(&thread_global_lock);
  354. return true;
  355. }
  356. return false;
  357. }
  358. static void
  359. lib_pthread_destroy_callback(WASMCluster *cluster)
  360. {
  361. destroy_cluster_info(cluster);
  362. }
  363. static void
  364. delete_thread_info_node(ThreadInfoNode *thread_info)
  365. {
  366. ClusterInfoNode *node;
  367. bool ret;
  368. WASMCluster *cluster = wasm_exec_env_get_cluster(thread_info->exec_env);
  369. if ((node = get_cluster_info(cluster))) {
  370. ret = bh_hash_map_remove(node->thread_info_map,
  371. (void *)(uintptr_t)thread_info->handle, NULL,
  372. NULL);
  373. (void)ret;
  374. }
  375. thread_info_destroy(thread_info);
  376. }
  377. static bool
  378. append_thread_info_node(ThreadInfoNode *thread_info)
  379. {
  380. ClusterInfoNode *node;
  381. WASMCluster *cluster = wasm_exec_env_get_cluster(thread_info->exec_env);
  382. if (!(node = get_cluster_info(cluster))) {
  383. if (!(node = create_cluster_info(cluster))) {
  384. return false;
  385. }
  386. }
  387. if (!bh_hash_map_insert(node->thread_info_map,
  388. (void *)(uintptr_t)thread_info->handle,
  389. thread_info)) {
  390. return false;
  391. }
  392. return true;
  393. }
  394. static ThreadInfoNode *
  395. get_thread_info(wasm_exec_env_t exec_env, uint32 handle)
  396. {
  397. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  398. ClusterInfoNode *info = get_cluster_info(cluster);
  399. if (!info) {
  400. return NULL;
  401. }
  402. return bh_hash_map_find(info->thread_info_map, (void *)(uintptr_t)handle);
  403. }
  404. static uint32
  405. allocate_handle()
  406. {
  407. uint32 id;
  408. os_mutex_lock(&thread_global_lock);
  409. id = handle_id++;
  410. os_mutex_unlock(&thread_global_lock);
  411. return id;
  412. }
  413. static void *
  414. pthread_start_routine(void *arg)
  415. {
  416. wasm_exec_env_t exec_env = (wasm_exec_env_t)arg;
  417. wasm_exec_env_t parent_exec_env;
  418. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  419. ThreadRoutineArgs *routine_args = exec_env->thread_arg;
  420. ThreadInfoNode *info_node = routine_args->info_node;
  421. uint32 argv[1];
  422. parent_exec_env = info_node->parent_exec_env;
  423. os_mutex_lock(&parent_exec_env->wait_lock);
  424. info_node->exec_env = exec_env;
  425. info_node->u.thread = exec_env->handle;
  426. if (!append_thread_info_node(info_node)) {
  427. wasm_runtime_deinstantiate_internal(module_inst, true);
  428. delete_thread_info_node(info_node);
  429. os_cond_signal(&parent_exec_env->wait_cond);
  430. os_mutex_unlock(&parent_exec_env->wait_lock);
  431. return NULL;
  432. }
  433. info_node->status = THREAD_RUNNING;
  434. os_cond_signal(&parent_exec_env->wait_cond);
  435. os_mutex_unlock(&parent_exec_env->wait_lock);
  436. wasm_exec_env_set_thread_info(exec_env);
  437. argv[0] = routine_args->arg;
  438. if (!wasm_runtime_call_indirect(exec_env, routine_args->elem_index, 1,
  439. argv)) {
  440. if (wasm_runtime_get_exception(module_inst))
  441. wasm_cluster_spread_exception(exec_env);
  442. }
  443. /* destroy pthread key values */
  444. call_key_destructor(exec_env);
  445. /* routine exit, destroy instance */
  446. wasm_runtime_deinstantiate_internal(module_inst, true);
  447. wasm_runtime_free(routine_args);
  448. /* if the thread is joinable, store the result in its info node,
  449. if the other threads join this thread after exited, then we
  450. can return the stored result */
  451. if (!info_node->joinable) {
  452. delete_thread_info_node(info_node);
  453. }
  454. else {
  455. info_node->u.ret = (void *)(uintptr_t)argv[0];
  456. #ifdef OS_ENABLE_HW_BOUND_CHECK
  457. if (exec_env->suspend_flags.flags & 0x08)
  458. /* argv[0] isn't set after longjmp(1) to
  459. invoke_native_with_hw_bound_check */
  460. info_node->u.ret = exec_env->thread_ret_value;
  461. #endif
  462. /* Update node status after ret value was set */
  463. info_node->status = THREAD_EXIT;
  464. }
  465. return (void *)(uintptr_t)argv[0];
  466. }
  467. static int
  468. pthread_create_wrapper(wasm_exec_env_t exec_env,
  469. uint32 *thread, /* thread_handle */
  470. const void *attr, /* not supported */
  471. uint32 elem_index, /* entry function */
  472. uint32 arg) /* arguments buffer */
  473. {
  474. wasm_module_t module = get_module(exec_env);
  475. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  476. wasm_module_inst_t new_module_inst = NULL;
  477. ThreadInfoNode *info_node = NULL;
  478. ThreadRoutineArgs *routine_args = NULL;
  479. uint32 thread_handle;
  480. uint32 stack_size = 8192;
  481. int32 ret = -1;
  482. #if WASM_ENABLE_LIBC_WASI != 0
  483. WASIContext *wasi_ctx;
  484. #endif
  485. bh_assert(module);
  486. bh_assert(module_inst);
  487. #if WASM_ENABLE_INTERP != 0
  488. if (module_inst->module_type == Wasm_Module_Bytecode) {
  489. stack_size =
  490. ((WASMModuleInstance *)module_inst)->default_wasm_stack_size;
  491. }
  492. #endif
  493. #if WASM_ENABLE_AOT != 0
  494. if (module_inst->module_type == Wasm_Module_AoT) {
  495. stack_size =
  496. ((AOTModuleInstance *)module_inst)->default_wasm_stack_size;
  497. }
  498. #endif
  499. if (!(new_module_inst = wasm_runtime_instantiate_internal(
  500. module, true, stack_size, 0, NULL, 0)))
  501. return -1;
  502. /* Set custom_data to new module instance */
  503. wasm_runtime_set_custom_data_internal(
  504. new_module_inst, wasm_runtime_get_custom_data(module_inst));
  505. #if WASM_ENABLE_LIBC_WASI != 0
  506. wasi_ctx = get_wasi_ctx(module_inst);
  507. if (wasi_ctx)
  508. wasm_runtime_set_wasi_ctx(new_module_inst, wasi_ctx);
  509. #endif
  510. if (!(info_node = wasm_runtime_malloc(sizeof(ThreadInfoNode))))
  511. goto fail;
  512. memset(info_node, 0, sizeof(ThreadInfoNode));
  513. thread_handle = allocate_handle();
  514. info_node->parent_exec_env = exec_env;
  515. info_node->handle = thread_handle;
  516. info_node->type = T_THREAD;
  517. info_node->status = THREAD_INIT;
  518. info_node->joinable = true;
  519. if (!(routine_args = wasm_runtime_malloc(sizeof(ThreadRoutineArgs))))
  520. goto fail;
  521. routine_args->arg = arg;
  522. routine_args->elem_index = elem_index;
  523. routine_args->info_node = info_node;
  524. routine_args->module_inst = new_module_inst;
  525. os_mutex_lock(&exec_env->wait_lock);
  526. ret = wasm_cluster_create_thread(
  527. exec_env, new_module_inst, pthread_start_routine, (void *)routine_args);
  528. if (ret != 0) {
  529. os_mutex_unlock(&exec_env->wait_lock);
  530. goto fail;
  531. }
  532. /* Wait for the thread routine to assign the exec_env to
  533. thread_info_node, otherwise the exec_env in the thread
  534. info node may be NULL in the next pthread API call */
  535. os_cond_wait(&exec_env->wait_cond, &exec_env->wait_lock);
  536. os_mutex_unlock(&exec_env->wait_lock);
  537. if (thread)
  538. *thread = thread_handle;
  539. return 0;
  540. fail:
  541. if (new_module_inst)
  542. wasm_runtime_deinstantiate_internal(new_module_inst, true);
  543. if (info_node)
  544. wasm_runtime_free(info_node);
  545. if (routine_args)
  546. wasm_runtime_free(routine_args);
  547. return ret;
  548. }
  549. static int32
  550. pthread_join_wrapper(wasm_exec_env_t exec_env, uint32 thread,
  551. int32 retval_offset) /* void **retval */
  552. {
  553. uint32 *ret;
  554. int32 join_ret;
  555. void **retval;
  556. ThreadInfoNode *node;
  557. wasm_module_inst_t module_inst;
  558. wasm_exec_env_t target_exec_env;
  559. module_inst = get_module_inst(exec_env);
  560. /* validate addr, we can use current thread's
  561. module instance here as the memory is shared */
  562. if (!validate_app_addr(retval_offset, sizeof(int32))) {
  563. /* Join failed, but we don't want to terminate all threads,
  564. do not spread exception here */
  565. wasm_runtime_set_exception(module_inst, NULL);
  566. return -1;
  567. }
  568. retval = (void **)addr_app_to_native(retval_offset);
  569. node = get_thread_info(exec_env, thread);
  570. if (!node) {
  571. /* The thread has exited and not joinable, return 0 to app */
  572. return 0;
  573. }
  574. target_exec_env = node->exec_env;
  575. bh_assert(target_exec_env);
  576. if (node->status != THREAD_EXIT) {
  577. /* if the thread is still running, call the platforms join API */
  578. join_ret = wasm_cluster_join_thread(target_exec_env, (void **)&ret);
  579. }
  580. else {
  581. /* if the thread has exited, return stored results */
  582. /* this thread must be joinable, otherwise the
  583. info_node should be destroyed once exit */
  584. bh_assert(node->joinable);
  585. join_ret = 0;
  586. ret = node->u.ret;
  587. }
  588. if (retval_offset != 0)
  589. *(uint32 *)retval = (uint32)(uintptr_t)ret;
  590. return join_ret;
  591. }
  592. static int32
  593. pthread_detach_wrapper(wasm_exec_env_t exec_env, uint32 thread)
  594. {
  595. ThreadInfoNode *node;
  596. wasm_exec_env_t target_exec_env;
  597. node = get_thread_info(exec_env, thread);
  598. if (!node)
  599. return 0;
  600. node->joinable = false;
  601. target_exec_env = node->exec_env;
  602. bh_assert(target_exec_env != NULL);
  603. return wasm_cluster_detach_thread(target_exec_env);
  604. }
  605. static int32
  606. pthread_cancel_wrapper(wasm_exec_env_t exec_env, uint32 thread)
  607. {
  608. ThreadInfoNode *node;
  609. wasm_exec_env_t target_exec_env;
  610. node = get_thread_info(exec_env, thread);
  611. if (!node)
  612. return 0;
  613. node->status = THREAD_CANCELLED;
  614. node->joinable = false;
  615. target_exec_env = node->exec_env;
  616. bh_assert(target_exec_env != NULL);
  617. return wasm_cluster_cancel_thread(target_exec_env);
  618. }
  619. static int32
  620. pthread_self_wrapper(wasm_exec_env_t exec_env)
  621. {
  622. ThreadRoutineArgs *args = get_thread_arg(exec_env);
  623. /* If thread_arg is NULL, it's the exec_env of the main thread,
  624. return id 0 to app */
  625. if (!args)
  626. return 0;
  627. return args->info_node->handle;
  628. }
  629. /* emcc use __pthread_self rather than pthread_self */
  630. static int32
  631. __pthread_self_wrapper(wasm_exec_env_t exec_env)
  632. {
  633. return pthread_self_wrapper(exec_env);
  634. }
  635. static void
  636. pthread_exit_wrapper(wasm_exec_env_t exec_env, int32 retval_offset)
  637. {
  638. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  639. ThreadRoutineArgs *args = get_thread_arg(exec_env);
  640. /* Currently exit main thread is not allowed */
  641. if (!args)
  642. return;
  643. #if defined(OS_ENABLE_HW_BOUND_CHECK) && !defined(BH_PLATFORM_WINDOWS)
  644. /* If hardware bound check enabled, don't deinstantiate module inst
  645. and thread info node here for AoT module, as they will be freed
  646. in pthread_start_routine */
  647. if (exec_env->jmpbuf_stack_top) {
  648. wasm_cluster_exit_thread(exec_env, (void *)(uintptr_t)retval_offset);
  649. }
  650. #endif
  651. /* destroy pthread key values */
  652. call_key_destructor(exec_env);
  653. /* routine exit, destroy instance */
  654. wasm_runtime_deinstantiate_internal(module_inst, true);
  655. if (!args->info_node->joinable) {
  656. delete_thread_info_node(args->info_node);
  657. }
  658. else {
  659. args->info_node->u.ret = (void *)(uintptr_t)retval_offset;
  660. /* Update node status after ret value was set */
  661. args->info_node->status = THREAD_EXIT;
  662. }
  663. wasm_runtime_free(args);
  664. wasm_cluster_exit_thread(exec_env, (void *)(uintptr_t)retval_offset);
  665. }
  666. static int32
  667. pthread_mutex_init_wrapper(wasm_exec_env_t exec_env, uint32 *mutex, void *attr)
  668. {
  669. korp_mutex *pmutex;
  670. ThreadInfoNode *info_node;
  671. if (!(pmutex = wasm_runtime_malloc(sizeof(korp_mutex)))) {
  672. return -1;
  673. }
  674. if (os_mutex_init(pmutex) != 0) {
  675. goto fail1;
  676. }
  677. if (!(info_node = wasm_runtime_malloc(sizeof(ThreadInfoNode))))
  678. goto fail2;
  679. memset(info_node, 0, sizeof(ThreadInfoNode));
  680. info_node->exec_env = exec_env;
  681. info_node->handle = allocate_handle();
  682. info_node->type = T_MUTEX;
  683. info_node->u.mutex = pmutex;
  684. info_node->status = MUTEX_CREATED;
  685. if (!append_thread_info_node(info_node))
  686. goto fail3;
  687. /* Return the mutex handle to app */
  688. if (mutex)
  689. *(uint32 *)mutex = info_node->handle;
  690. return 0;
  691. fail3:
  692. delete_thread_info_node(info_node);
  693. fail2:
  694. os_mutex_destroy(pmutex);
  695. fail1:
  696. wasm_runtime_free(pmutex);
  697. return -1;
  698. }
  699. static int32
  700. pthread_mutex_lock_wrapper(wasm_exec_env_t exec_env, uint32 *mutex)
  701. {
  702. ThreadInfoNode *info_node = get_thread_info(exec_env, *mutex);
  703. if (!info_node || info_node->type != T_MUTEX)
  704. return -1;
  705. return os_mutex_lock(info_node->u.mutex);
  706. }
  707. static int32
  708. pthread_mutex_unlock_wrapper(wasm_exec_env_t exec_env, uint32 *mutex)
  709. {
  710. ThreadInfoNode *info_node = get_thread_info(exec_env, *mutex);
  711. if (!info_node || info_node->type != T_MUTEX)
  712. return -1;
  713. return os_mutex_unlock(info_node->u.mutex);
  714. }
  715. static int32
  716. pthread_mutex_destroy_wrapper(wasm_exec_env_t exec_env, uint32 *mutex)
  717. {
  718. int32 ret_val;
  719. ThreadInfoNode *info_node = get_thread_info(exec_env, *mutex);
  720. if (!info_node || info_node->type != T_MUTEX)
  721. return -1;
  722. ret_val = os_mutex_destroy(info_node->u.mutex);
  723. info_node->status = MUTEX_DESTROYED;
  724. delete_thread_info_node(info_node);
  725. return ret_val;
  726. }
  727. static int32
  728. pthread_cond_init_wrapper(wasm_exec_env_t exec_env, uint32 *cond, void *attr)
  729. {
  730. korp_cond *pcond;
  731. ThreadInfoNode *info_node;
  732. if (!(pcond = wasm_runtime_malloc(sizeof(korp_cond)))) {
  733. return -1;
  734. }
  735. if (os_cond_init(pcond) != 0) {
  736. goto fail1;
  737. }
  738. if (!(info_node = wasm_runtime_malloc(sizeof(ThreadInfoNode))))
  739. goto fail2;
  740. memset(info_node, 0, sizeof(ThreadInfoNode));
  741. info_node->exec_env = exec_env;
  742. info_node->handle = allocate_handle();
  743. info_node->type = T_COND;
  744. info_node->u.cond = pcond;
  745. info_node->status = COND_CREATED;
  746. if (!append_thread_info_node(info_node))
  747. goto fail3;
  748. /* Return the cond handle to app */
  749. if (cond)
  750. *(uint32 *)cond = info_node->handle;
  751. return 0;
  752. fail3:
  753. delete_thread_info_node(info_node);
  754. fail2:
  755. os_cond_destroy(pcond);
  756. fail1:
  757. wasm_runtime_free(pcond);
  758. return -1;
  759. }
  760. static int32
  761. pthread_cond_wait_wrapper(wasm_exec_env_t exec_env, uint32 *cond, uint32 *mutex)
  762. {
  763. ThreadInfoNode *cond_info_node, *mutex_info_node;
  764. cond_info_node = get_thread_info(exec_env, *cond);
  765. if (!cond_info_node || cond_info_node->type != T_COND)
  766. return -1;
  767. mutex_info_node = get_thread_info(exec_env, *mutex);
  768. if (!mutex_info_node || mutex_info_node->type != T_MUTEX)
  769. return -1;
  770. return os_cond_wait(cond_info_node->u.cond, mutex_info_node->u.mutex);
  771. }
  772. /**
  773. * Currently we don't support struct timespec in built-in libc,
  774. * so the pthread_cond_timedwait use useconds instead
  775. */
  776. static int32
  777. pthread_cond_timedwait_wrapper(wasm_exec_env_t exec_env, uint32 *cond,
  778. uint32 *mutex, uint64 useconds)
  779. {
  780. ThreadInfoNode *cond_info_node, *mutex_info_node;
  781. cond_info_node = get_thread_info(exec_env, *cond);
  782. if (!cond_info_node || cond_info_node->type != T_COND)
  783. return -1;
  784. mutex_info_node = get_thread_info(exec_env, *mutex);
  785. if (!mutex_info_node || mutex_info_node->type != T_MUTEX)
  786. return -1;
  787. return os_cond_reltimedwait(cond_info_node->u.cond,
  788. mutex_info_node->u.mutex, useconds);
  789. }
  790. static int32
  791. pthread_cond_signal_wrapper(wasm_exec_env_t exec_env, uint32 *cond)
  792. {
  793. ThreadInfoNode *info_node = get_thread_info(exec_env, *cond);
  794. if (!info_node || info_node->type != T_COND)
  795. return -1;
  796. return os_cond_signal(info_node->u.cond);
  797. }
  798. static int32
  799. pthread_cond_broadcast_wrapper(wasm_exec_env_t exec_env, uint32 *cond)
  800. {
  801. ThreadInfoNode *info_node = get_thread_info(exec_env, *cond);
  802. if (!info_node || info_node->type != T_COND)
  803. return -1;
  804. return os_cond_broadcast(info_node->u.cond);
  805. }
  806. static int32
  807. pthread_cond_destroy_wrapper(wasm_exec_env_t exec_env, uint32 *cond)
  808. {
  809. int32 ret_val;
  810. ThreadInfoNode *info_node = get_thread_info(exec_env, *cond);
  811. if (!info_node || info_node->type != T_COND)
  812. return -1;
  813. ret_val = os_cond_destroy(info_node->u.cond);
  814. info_node->status = COND_DESTROYED;
  815. delete_thread_info_node(info_node);
  816. return ret_val;
  817. }
  818. static int32
  819. pthread_key_create_wrapper(wasm_exec_env_t exec_env, int32 *key,
  820. int32 destructor_elem_index)
  821. {
  822. uint32 i;
  823. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  824. ClusterInfoNode *info = get_cluster_info(cluster);
  825. if (!info) {
  826. /* The user may call pthread_key_create in main thread,
  827. in this case the cluster info hasn't been created */
  828. if (!(info = create_cluster_info(cluster))) {
  829. return -1;
  830. }
  831. }
  832. os_mutex_lock(&info->key_data_list_lock);
  833. for (i = 0; i < WAMR_PTHREAD_KEYS_MAX; i++) {
  834. if (!info->key_data_list[i].is_created) {
  835. break;
  836. }
  837. }
  838. if (i == WAMR_PTHREAD_KEYS_MAX) {
  839. os_mutex_unlock(&info->key_data_list_lock);
  840. return -1;
  841. }
  842. info->key_data_list[i].destructor_func = destructor_elem_index;
  843. info->key_data_list[i].is_created = true;
  844. *key = i;
  845. os_mutex_unlock(&info->key_data_list_lock);
  846. return 0;
  847. }
  848. static int32
  849. pthread_setspecific_wrapper(wasm_exec_env_t exec_env, int32 key,
  850. int32 value_offset)
  851. {
  852. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  853. ClusterInfoNode *info = get_cluster_info(cluster);
  854. int32 *key_values;
  855. if (!info)
  856. return -1;
  857. os_mutex_lock(&info->key_data_list_lock);
  858. key_values = key_value_list_lookup_or_create(exec_env, info, key);
  859. if (!key_values) {
  860. os_mutex_unlock(&info->key_data_list_lock);
  861. return -1;
  862. }
  863. key_values[key] = value_offset;
  864. os_mutex_unlock(&info->key_data_list_lock);
  865. return 0;
  866. }
  867. static int32
  868. pthread_getspecific_wrapper(wasm_exec_env_t exec_env, int32 key)
  869. {
  870. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  871. ClusterInfoNode *info = get_cluster_info(cluster);
  872. int32 ret, *key_values;
  873. if (!info)
  874. return 0;
  875. os_mutex_lock(&info->key_data_list_lock);
  876. key_values = key_value_list_lookup_or_create(exec_env, info, key);
  877. if (!key_values) {
  878. os_mutex_unlock(&info->key_data_list_lock);
  879. return 0;
  880. }
  881. ret = key_values[key];
  882. os_mutex_unlock(&info->key_data_list_lock);
  883. return ret;
  884. }
  885. static int32
  886. pthread_key_delete_wrapper(wasm_exec_env_t exec_env, int32 key)
  887. {
  888. KeyData *data;
  889. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  890. ClusterInfoNode *info = get_cluster_info(cluster);
  891. if (!info)
  892. return -1;
  893. os_mutex_lock(&info->key_data_list_lock);
  894. data = key_data_list_lookup(exec_env, key);
  895. if (!data) {
  896. os_mutex_unlock(&info->key_data_list_lock);
  897. return -1;
  898. }
  899. memset(data, 0, sizeof(KeyData));
  900. os_mutex_unlock(&info->key_data_list_lock);
  901. return 0;
  902. }
  903. /**
  904. * Currently the memory allocator doesn't support alloc specific aligned
  905. * space, we wrap posix_memalign to simply malloc memory
  906. */
  907. static int32
  908. posix_memalign_wrapper(wasm_exec_env_t exec_env, void **memptr, int32 align,
  909. int32 size)
  910. {
  911. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  912. void *p = NULL;
  913. *((int32 *)memptr) = module_malloc(size, (void **)&p);
  914. if (!p)
  915. return -1;
  916. return 0;
  917. }
  918. #if WASM_ENABLE_LIB_PTHREAD_SEMAPHORE != 0
  919. static int32
  920. sem_open_wrapper(wasm_exec_env_t exec_env, const char *name, int32 oflags,
  921. int32 mode, int32 val)
  922. {
  923. korp_sem *psem = NULL;
  924. ThreadInfoNode *info_node = NULL;
  925. /**
  926. * For RTOS, global semaphore map is safe for share the same semaphore
  927. * between task/pthread.
  928. * For Unix like system, it's dedicated for multiple processes.
  929. */
  930. if ((info_node = bh_hash_map_find(sem_info_map, (void *)name))) {
  931. return info_node->handle;
  932. }
  933. if (!(psem = os_sem_open(name, oflags, mode, val))) {
  934. goto fail1;
  935. }
  936. if (!(info_node = wasm_runtime_malloc(sizeof(ThreadInfoNode))))
  937. goto fail2;
  938. memset(info_node, 0, sizeof(ThreadInfoNode));
  939. info_node->exec_env = exec_env;
  940. info_node->handle = allocate_handle();
  941. info_node->type = T_SEM;
  942. info_node->u.sem = psem;
  943. info_node->status = SEM_CREATED;
  944. if (!bh_hash_map_insert(sem_info_map, (void *)name, info_node))
  945. goto fail3;
  946. return info_node->handle;
  947. fail3:
  948. wasm_runtime_free(info_node);
  949. fail2:
  950. os_sem_close(psem);
  951. fail1:
  952. return -1;
  953. }
  954. void
  955. sem_fetch_cb(void *key, void *value, void *user_data)
  956. {
  957. (void)key;
  958. SemCallbackArgs *args = user_data;
  959. ThreadInfoNode *info_node = value;
  960. if (args->handle == info_node->handle && info_node->status == SEM_CREATED) {
  961. args->node = info_node;
  962. }
  963. }
  964. static int32
  965. sem_close_wrapper(wasm_exec_env_t exec_env, uint32 sem)
  966. {
  967. (void)exec_env;
  968. int ret = -1;
  969. SemCallbackArgs args = { sem, NULL };
  970. bh_hash_map_traverse(sem_info_map, sem_fetch_cb, &args);
  971. if (args.node) {
  972. ret = os_sem_close(args.node->u.sem);
  973. if (ret == 0) {
  974. args.node->status = SEM_CLOSED;
  975. }
  976. }
  977. return ret;
  978. }
  979. static int32
  980. sem_wait_wrapper(wasm_exec_env_t exec_env, uint32 sem)
  981. {
  982. (void)exec_env;
  983. SemCallbackArgs args = { sem, NULL };
  984. bh_hash_map_traverse(sem_info_map, sem_fetch_cb, &args);
  985. if (args.node) {
  986. return os_sem_wait(args.node->u.sem);
  987. }
  988. return -1;
  989. }
  990. static int32
  991. sem_trywait_wrapper(wasm_exec_env_t exec_env, uint32 sem)
  992. {
  993. (void)exec_env;
  994. SemCallbackArgs args = { sem, NULL };
  995. bh_hash_map_traverse(sem_info_map, sem_fetch_cb, &args);
  996. if (args.node) {
  997. return os_sem_trywait(args.node->u.sem);
  998. }
  999. return -1;
  1000. }
  1001. static int32
  1002. sem_post_wrapper(wasm_exec_env_t exec_env, uint32 sem)
  1003. {
  1004. (void)exec_env;
  1005. SemCallbackArgs args = { sem, NULL };
  1006. bh_hash_map_traverse(sem_info_map, sem_fetch_cb, &args);
  1007. if (args.node) {
  1008. return os_sem_post(args.node->u.sem);
  1009. }
  1010. return -1;
  1011. }
  1012. static int32
  1013. sem_getvalue_wrapper(wasm_exec_env_t exec_env, uint32 sem, int32 *sval)
  1014. {
  1015. int32 ret = -1;
  1016. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1017. (void)exec_env;
  1018. SemCallbackArgs args = { sem, NULL };
  1019. if (validate_native_addr(sval, sizeof(int32))) {
  1020. bh_hash_map_traverse(sem_info_map, sem_fetch_cb, &args);
  1021. if (args.node) {
  1022. ret = os_sem_getvalue(args.node->u.sem, sval);
  1023. }
  1024. }
  1025. return ret;
  1026. }
  1027. static int32
  1028. sem_unlink_wrapper(wasm_exec_env_t exec_env, const char *name)
  1029. {
  1030. (void)exec_env;
  1031. int32 ret_val;
  1032. ThreadInfoNode *info_node = bh_hash_map_find(sem_info_map, (void *)name);
  1033. if (!info_node || info_node->type != T_SEM)
  1034. return -1;
  1035. if (info_node->status != SEM_CLOSED) {
  1036. ret_val = os_sem_close(info_node->u.sem);
  1037. if (ret_val != 0) {
  1038. return ret_val;
  1039. }
  1040. }
  1041. ret_val = os_sem_unlink(name);
  1042. if (ret_val == 0) {
  1043. bh_hash_map_remove(sem_info_map, (void *)name, NULL, NULL);
  1044. info_node->status = SEM_DESTROYED;
  1045. thread_info_destroy(info_node);
  1046. }
  1047. return ret_val;
  1048. }
  1049. #endif
  1050. /* clang-format off */
  1051. #define REG_NATIVE_FUNC(func_name, signature) \
  1052. { #func_name, func_name##_wrapper, signature, NULL }
  1053. /* clang-format on */
  1054. static NativeSymbol native_symbols_lib_pthread[] = {
  1055. REG_NATIVE_FUNC(pthread_create, "(**ii)i"),
  1056. REG_NATIVE_FUNC(pthread_join, "(ii)i"),
  1057. REG_NATIVE_FUNC(pthread_detach, "(i)i"),
  1058. REG_NATIVE_FUNC(pthread_cancel, "(i)i"),
  1059. REG_NATIVE_FUNC(pthread_self, "()i"),
  1060. REG_NATIVE_FUNC(__pthread_self, "()i"),
  1061. REG_NATIVE_FUNC(pthread_exit, "(i)"),
  1062. REG_NATIVE_FUNC(pthread_mutex_init, "(**)i"),
  1063. REG_NATIVE_FUNC(pthread_mutex_lock, "(*)i"),
  1064. REG_NATIVE_FUNC(pthread_mutex_unlock, "(*)i"),
  1065. REG_NATIVE_FUNC(pthread_mutex_destroy, "(*)i"),
  1066. REG_NATIVE_FUNC(pthread_cond_init, "(**)i"),
  1067. REG_NATIVE_FUNC(pthread_cond_wait, "(**)i"),
  1068. REG_NATIVE_FUNC(pthread_cond_timedwait, "(**I)i"),
  1069. REG_NATIVE_FUNC(pthread_cond_signal, "(*)i"),
  1070. REG_NATIVE_FUNC(pthread_cond_broadcast, "(*)i"),
  1071. REG_NATIVE_FUNC(pthread_cond_destroy, "(*)i"),
  1072. REG_NATIVE_FUNC(pthread_key_create, "(*i)i"),
  1073. REG_NATIVE_FUNC(pthread_setspecific, "(ii)i"),
  1074. REG_NATIVE_FUNC(pthread_getspecific, "(i)i"),
  1075. REG_NATIVE_FUNC(pthread_key_delete, "(i)i"),
  1076. REG_NATIVE_FUNC(posix_memalign, "(*ii)i"),
  1077. #if WASM_ENABLE_LIB_PTHREAD_SEMAPHORE != 0
  1078. REG_NATIVE_FUNC(sem_open, "($iii)i"),
  1079. REG_NATIVE_FUNC(sem_close, "(i)i"),
  1080. REG_NATIVE_FUNC(sem_wait, "(i)i"),
  1081. REG_NATIVE_FUNC(sem_trywait, "(i)i"),
  1082. REG_NATIVE_FUNC(sem_post, "(i)i"),
  1083. REG_NATIVE_FUNC(sem_getvalue, "(i*)i"),
  1084. REG_NATIVE_FUNC(sem_unlink, "($)i"),
  1085. #endif
  1086. };
  1087. uint32
  1088. get_lib_pthread_export_apis(NativeSymbol **p_lib_pthread_apis)
  1089. {
  1090. *p_lib_pthread_apis = native_symbols_lib_pthread;
  1091. return sizeof(native_symbols_lib_pthread) / sizeof(NativeSymbol);
  1092. }