lib_pthread_wrapper.c 37 KB

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