lib_pthread_wrapper.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  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_start = 0, aux_stack_size;
  475. int32 ret = -1;
  476. bh_assert(module);
  477. bh_assert(module_inst);
  478. #if WASM_ENABLE_INTERP != 0
  479. if (module_inst->module_type == Wasm_Module_Bytecode) {
  480. stack_size =
  481. ((WASMModuleInstance *)module_inst)->default_wasm_stack_size;
  482. }
  483. #endif
  484. #if WASM_ENABLE_AOT != 0
  485. if (module_inst->module_type == Wasm_Module_AoT) {
  486. stack_size =
  487. ((AOTModuleInstance *)module_inst)->default_wasm_stack_size;
  488. }
  489. #endif
  490. if (!(new_module_inst = wasm_runtime_instantiate_internal(
  491. module, module_inst, exec_env, stack_size, 0, NULL, 0)))
  492. return -1;
  493. /* Set custom_data to new module instance */
  494. wasm_runtime_set_custom_data_internal(
  495. new_module_inst, wasm_runtime_get_custom_data(module_inst));
  496. wasm_native_inherit_contexts(new_module_inst, module_inst);
  497. if (!(wasm_cluster_dup_c_api_imports(new_module_inst, module_inst)))
  498. goto fail;
  499. if (!(info_node = wasm_runtime_malloc(sizeof(ThreadInfoNode))))
  500. goto fail;
  501. memset(info_node, 0, sizeof(ThreadInfoNode));
  502. thread_handle = allocate_handle();
  503. info_node->parent_exec_env = exec_env;
  504. info_node->handle = thread_handle;
  505. info_node->type = T_THREAD;
  506. info_node->status = THREAD_INIT;
  507. info_node->joinable = true;
  508. if (!(routine_args = wasm_runtime_malloc(sizeof(ThreadRoutineArgs))))
  509. goto fail;
  510. routine_args->arg = arg;
  511. routine_args->elem_index = elem_index;
  512. routine_args->info_node = info_node;
  513. routine_args->module_inst = new_module_inst;
  514. /* Allocate aux stack previously since exec_env->wait_lock is acquired
  515. below, and if the stack is allocated in wasm_cluster_create_thread,
  516. runtime may call the exported malloc function to allocate the stack,
  517. which acquires exec_env->wait again in wasm_exec_env_set_thread_info,
  518. and recursive lock (or hang) occurs */
  519. if (!wasm_cluster_allocate_aux_stack(exec_env, &aux_stack_start,
  520. &aux_stack_size)) {
  521. LOG_ERROR("thread manager error: "
  522. "failed to allocate aux stack space for new thread");
  523. goto fail;
  524. }
  525. os_mutex_lock(&exec_env->wait_lock);
  526. ret = wasm_cluster_create_thread(
  527. exec_env, new_module_inst, true, aux_stack_start, aux_stack_size,
  528. pthread_start_routine, (void *)routine_args);
  529. if (ret != 0) {
  530. os_mutex_unlock(&exec_env->wait_lock);
  531. goto fail;
  532. }
  533. /* Wait for the thread routine to assign the exec_env to
  534. thread_info_node, otherwise the exec_env in the thread
  535. info node may be NULL in the next pthread API call */
  536. os_cond_wait(&exec_env->wait_cond, &exec_env->wait_lock);
  537. os_mutex_unlock(&exec_env->wait_lock);
  538. if (thread)
  539. *thread = thread_handle;
  540. return 0;
  541. fail:
  542. if (new_module_inst)
  543. wasm_runtime_deinstantiate_internal(new_module_inst, true);
  544. if (info_node)
  545. wasm_runtime_free(info_node);
  546. if (routine_args)
  547. wasm_runtime_free(routine_args);
  548. if (aux_stack_start)
  549. wasm_cluster_free_aux_stack(exec_env, aux_stack_start);
  550. return ret;
  551. }
  552. static int32
  553. pthread_join_wrapper(wasm_exec_env_t exec_env, uint32 thread,
  554. int32 retval_offset) /* void **retval */
  555. {
  556. uint32 *ret;
  557. int32 join_ret;
  558. void **retval;
  559. ThreadInfoNode *node;
  560. wasm_module_inst_t module_inst;
  561. wasm_exec_env_t target_exec_env;
  562. module_inst = get_module_inst(exec_env);
  563. /* validate addr, we can use current thread's
  564. module instance here as the memory is shared */
  565. if (!validate_app_addr(retval_offset, sizeof(int32))) {
  566. /* Join failed, but we don't want to terminate all threads,
  567. do not spread exception here */
  568. wasm_runtime_set_exception(module_inst, NULL);
  569. return -1;
  570. }
  571. retval = (void **)addr_app_to_native(retval_offset);
  572. node = get_thread_info(exec_env, thread);
  573. if (!node) {
  574. /* The thread has exited and not joinable, return 0 to app */
  575. return 0;
  576. }
  577. target_exec_env = node->exec_env;
  578. bh_assert(target_exec_env);
  579. if (node->status != THREAD_EXIT) {
  580. /* if the thread is still running, call the platforms join API */
  581. join_ret = wasm_cluster_join_thread(target_exec_env, (void **)&ret);
  582. }
  583. else {
  584. /* if the thread has exited, return stored results */
  585. /* this thread must be joinable, otherwise the
  586. info_node should be destroyed once exit */
  587. bh_assert(node->joinable);
  588. join_ret = 0;
  589. ret = node->u.ret;
  590. /* The target thread changes the node's status before calling
  591. wasm_cluster_exit_thread to exit, so here its resources may
  592. haven't been destroyed yet, we wait enough time to ensure that
  593. they are actually destroyed to avoid unexpected behavior. */
  594. os_mutex_lock(&exec_env->wait_lock);
  595. os_cond_reltimedwait(&exec_env->wait_cond, &exec_env->wait_lock, 1000);
  596. os_mutex_unlock(&exec_env->wait_lock);
  597. }
  598. if (retval_offset != 0)
  599. *(uint32 *)retval = (uint32)(uintptr_t)ret;
  600. return join_ret;
  601. }
  602. static int32
  603. pthread_detach_wrapper(wasm_exec_env_t exec_env, uint32 thread)
  604. {
  605. ThreadInfoNode *node;
  606. wasm_exec_env_t target_exec_env;
  607. node = get_thread_info(exec_env, thread);
  608. if (!node)
  609. return 0;
  610. node->joinable = false;
  611. target_exec_env = node->exec_env;
  612. bh_assert(target_exec_env != NULL);
  613. return wasm_cluster_detach_thread(target_exec_env);
  614. }
  615. static int32
  616. pthread_cancel_wrapper(wasm_exec_env_t exec_env, uint32 thread)
  617. {
  618. ThreadInfoNode *node;
  619. wasm_exec_env_t target_exec_env;
  620. node = get_thread_info(exec_env, thread);
  621. if (!node)
  622. return 0;
  623. node->status = THREAD_CANCELLED;
  624. node->joinable = false;
  625. target_exec_env = node->exec_env;
  626. bh_assert(target_exec_env != NULL);
  627. return wasm_cluster_cancel_thread(target_exec_env);
  628. }
  629. static int32
  630. pthread_self_wrapper(wasm_exec_env_t exec_env)
  631. {
  632. ThreadRoutineArgs *args = get_thread_arg(exec_env);
  633. /* If thread_arg is NULL, it's the exec_env of the main thread,
  634. return id 0 to app */
  635. if (!args)
  636. return 0;
  637. return args->info_node->handle;
  638. }
  639. /* emcc use __pthread_self rather than pthread_self */
  640. static int32
  641. __pthread_self_wrapper(wasm_exec_env_t exec_env)
  642. {
  643. return pthread_self_wrapper(exec_env);
  644. }
  645. static void
  646. pthread_exit_wrapper(wasm_exec_env_t exec_env, int32 retval_offset)
  647. {
  648. ThreadRoutineArgs *args = get_thread_arg(exec_env);
  649. /* Currently exit main thread is not allowed */
  650. if (!args)
  651. return;
  652. #if defined(OS_ENABLE_HW_BOUND_CHECK) && !defined(BH_PLATFORM_WINDOWS)
  653. /* If hardware bound check enabled, don't deinstantiate module inst
  654. and thread info node here for AoT module, as they will be freed
  655. in pthread_start_routine */
  656. if (exec_env->jmpbuf_stack_top) {
  657. wasm_cluster_exit_thread(exec_env, (void *)(uintptr_t)retval_offset);
  658. }
  659. #endif
  660. /* destroy pthread key values */
  661. call_key_destructor(exec_env);
  662. if (!args->info_node->joinable) {
  663. delete_thread_info_node(args->info_node);
  664. }
  665. else {
  666. args->info_node->u.ret = (void *)(uintptr_t)retval_offset;
  667. /* Update node status after ret value was set */
  668. args->info_node->status = THREAD_EXIT;
  669. }
  670. wasm_runtime_free(args);
  671. /* Don't destroy exec_env->module_inst in this functuntion since
  672. it will be destroyed in wasm_cluster_exit_thread */
  673. wasm_cluster_exit_thread(exec_env, (void *)(uintptr_t)retval_offset);
  674. }
  675. static int32
  676. pthread_mutex_init_wrapper(wasm_exec_env_t exec_env, uint32 *mutex, void *attr)
  677. {
  678. korp_mutex *pmutex;
  679. ThreadInfoNode *info_node;
  680. if (!(pmutex = wasm_runtime_malloc(sizeof(korp_mutex)))) {
  681. return -1;
  682. }
  683. if (os_mutex_init(pmutex) != 0) {
  684. goto fail1;
  685. }
  686. if (!(info_node = wasm_runtime_malloc(sizeof(ThreadInfoNode))))
  687. goto fail2;
  688. memset(info_node, 0, sizeof(ThreadInfoNode));
  689. info_node->exec_env = exec_env;
  690. info_node->handle = allocate_handle();
  691. info_node->type = T_MUTEX;
  692. info_node->u.mutex = pmutex;
  693. info_node->status = MUTEX_CREATED;
  694. if (!append_thread_info_node(info_node))
  695. goto fail3;
  696. /* Return the mutex handle to app */
  697. if (mutex)
  698. *(uint32 *)mutex = info_node->handle;
  699. return 0;
  700. fail3:
  701. delete_thread_info_node(info_node);
  702. fail2:
  703. os_mutex_destroy(pmutex);
  704. fail1:
  705. wasm_runtime_free(pmutex);
  706. return -1;
  707. }
  708. static int32
  709. pthread_mutex_lock_wrapper(wasm_exec_env_t exec_env, uint32 *mutex)
  710. {
  711. ThreadInfoNode *info_node = get_thread_info(exec_env, *mutex);
  712. if (!info_node || info_node->type != T_MUTEX)
  713. return -1;
  714. return os_mutex_lock(info_node->u.mutex);
  715. }
  716. static int32
  717. pthread_mutex_unlock_wrapper(wasm_exec_env_t exec_env, uint32 *mutex)
  718. {
  719. ThreadInfoNode *info_node = get_thread_info(exec_env, *mutex);
  720. if (!info_node || info_node->type != T_MUTEX)
  721. return -1;
  722. return os_mutex_unlock(info_node->u.mutex);
  723. }
  724. static int32
  725. pthread_mutex_destroy_wrapper(wasm_exec_env_t exec_env, uint32 *mutex)
  726. {
  727. int32 ret_val;
  728. ThreadInfoNode *info_node = get_thread_info(exec_env, *mutex);
  729. if (!info_node || info_node->type != T_MUTEX)
  730. return -1;
  731. ret_val = os_mutex_destroy(info_node->u.mutex);
  732. info_node->status = MUTEX_DESTROYED;
  733. delete_thread_info_node(info_node);
  734. return ret_val;
  735. }
  736. static int32
  737. pthread_cond_init_wrapper(wasm_exec_env_t exec_env, uint32 *cond, void *attr)
  738. {
  739. korp_cond *pcond;
  740. ThreadInfoNode *info_node;
  741. if (!(pcond = wasm_runtime_malloc(sizeof(korp_cond)))) {
  742. return -1;
  743. }
  744. if (os_cond_init(pcond) != 0) {
  745. goto fail1;
  746. }
  747. if (!(info_node = wasm_runtime_malloc(sizeof(ThreadInfoNode))))
  748. goto fail2;
  749. memset(info_node, 0, sizeof(ThreadInfoNode));
  750. info_node->exec_env = exec_env;
  751. info_node->handle = allocate_handle();
  752. info_node->type = T_COND;
  753. info_node->u.cond = pcond;
  754. info_node->status = COND_CREATED;
  755. if (!append_thread_info_node(info_node))
  756. goto fail3;
  757. /* Return the cond handle to app */
  758. if (cond)
  759. *(uint32 *)cond = info_node->handle;
  760. return 0;
  761. fail3:
  762. delete_thread_info_node(info_node);
  763. fail2:
  764. os_cond_destroy(pcond);
  765. fail1:
  766. wasm_runtime_free(pcond);
  767. return -1;
  768. }
  769. static int32
  770. pthread_cond_wait_wrapper(wasm_exec_env_t exec_env, uint32 *cond, uint32 *mutex)
  771. {
  772. ThreadInfoNode *cond_info_node, *mutex_info_node;
  773. cond_info_node = get_thread_info(exec_env, *cond);
  774. if (!cond_info_node || cond_info_node->type != T_COND)
  775. return -1;
  776. mutex_info_node = get_thread_info(exec_env, *mutex);
  777. if (!mutex_info_node || mutex_info_node->type != T_MUTEX)
  778. return -1;
  779. return os_cond_wait(cond_info_node->u.cond, mutex_info_node->u.mutex);
  780. }
  781. /**
  782. * Currently we don't support struct timespec in built-in libc,
  783. * so the pthread_cond_timedwait use useconds instead
  784. */
  785. static int32
  786. pthread_cond_timedwait_wrapper(wasm_exec_env_t exec_env, uint32 *cond,
  787. uint32 *mutex, uint64 useconds)
  788. {
  789. ThreadInfoNode *cond_info_node, *mutex_info_node;
  790. cond_info_node = get_thread_info(exec_env, *cond);
  791. if (!cond_info_node || cond_info_node->type != T_COND)
  792. return -1;
  793. mutex_info_node = get_thread_info(exec_env, *mutex);
  794. if (!mutex_info_node || mutex_info_node->type != T_MUTEX)
  795. return -1;
  796. return os_cond_reltimedwait(cond_info_node->u.cond,
  797. mutex_info_node->u.mutex, useconds);
  798. }
  799. static int32
  800. pthread_cond_signal_wrapper(wasm_exec_env_t exec_env, uint32 *cond)
  801. {
  802. ThreadInfoNode *info_node = get_thread_info(exec_env, *cond);
  803. if (!info_node || info_node->type != T_COND)
  804. return -1;
  805. return os_cond_signal(info_node->u.cond);
  806. }
  807. static int32
  808. pthread_cond_broadcast_wrapper(wasm_exec_env_t exec_env, uint32 *cond)
  809. {
  810. ThreadInfoNode *info_node = get_thread_info(exec_env, *cond);
  811. if (!info_node || info_node->type != T_COND)
  812. return -1;
  813. return os_cond_broadcast(info_node->u.cond);
  814. }
  815. static int32
  816. pthread_cond_destroy_wrapper(wasm_exec_env_t exec_env, uint32 *cond)
  817. {
  818. int32 ret_val;
  819. ThreadInfoNode *info_node = get_thread_info(exec_env, *cond);
  820. if (!info_node || info_node->type != T_COND)
  821. return -1;
  822. ret_val = os_cond_destroy(info_node->u.cond);
  823. info_node->status = COND_DESTROYED;
  824. delete_thread_info_node(info_node);
  825. return ret_val;
  826. }
  827. static int32
  828. pthread_key_create_wrapper(wasm_exec_env_t exec_env, int32 *key,
  829. int32 destructor_elem_index)
  830. {
  831. uint32 i;
  832. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  833. ClusterInfoNode *info = get_cluster_info(cluster);
  834. if (!info) {
  835. /* The user may call pthread_key_create in main thread,
  836. in this case the cluster info hasn't been created */
  837. if (!(info = create_cluster_info(cluster))) {
  838. return -1;
  839. }
  840. }
  841. os_mutex_lock(&info->key_data_list_lock);
  842. for (i = 0; i < WAMR_PTHREAD_KEYS_MAX; i++) {
  843. if (!info->key_data_list[i].is_created) {
  844. break;
  845. }
  846. }
  847. if (i == WAMR_PTHREAD_KEYS_MAX) {
  848. os_mutex_unlock(&info->key_data_list_lock);
  849. return -1;
  850. }
  851. info->key_data_list[i].destructor_func = destructor_elem_index;
  852. info->key_data_list[i].is_created = true;
  853. *key = i;
  854. os_mutex_unlock(&info->key_data_list_lock);
  855. return 0;
  856. }
  857. static int32
  858. pthread_setspecific_wrapper(wasm_exec_env_t exec_env, int32 key,
  859. int32 value_offset)
  860. {
  861. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  862. ClusterInfoNode *info = get_cluster_info(cluster);
  863. int32 *key_values;
  864. if (!info)
  865. return -1;
  866. os_mutex_lock(&info->key_data_list_lock);
  867. key_values = key_value_list_lookup_or_create(exec_env, info, key);
  868. if (!key_values) {
  869. os_mutex_unlock(&info->key_data_list_lock);
  870. return -1;
  871. }
  872. key_values[key] = value_offset;
  873. os_mutex_unlock(&info->key_data_list_lock);
  874. return 0;
  875. }
  876. static int32
  877. pthread_getspecific_wrapper(wasm_exec_env_t exec_env, int32 key)
  878. {
  879. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  880. ClusterInfoNode *info = get_cluster_info(cluster);
  881. int32 ret, *key_values;
  882. if (!info)
  883. return 0;
  884. os_mutex_lock(&info->key_data_list_lock);
  885. key_values = key_value_list_lookup_or_create(exec_env, info, key);
  886. if (!key_values) {
  887. os_mutex_unlock(&info->key_data_list_lock);
  888. return 0;
  889. }
  890. ret = key_values[key];
  891. os_mutex_unlock(&info->key_data_list_lock);
  892. return ret;
  893. }
  894. static int32
  895. pthread_key_delete_wrapper(wasm_exec_env_t exec_env, int32 key)
  896. {
  897. KeyData *data;
  898. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  899. ClusterInfoNode *info = get_cluster_info(cluster);
  900. if (!info)
  901. return -1;
  902. os_mutex_lock(&info->key_data_list_lock);
  903. data = key_data_list_lookup(exec_env, key);
  904. if (!data) {
  905. os_mutex_unlock(&info->key_data_list_lock);
  906. return -1;
  907. }
  908. memset(data, 0, sizeof(KeyData));
  909. os_mutex_unlock(&info->key_data_list_lock);
  910. return 0;
  911. }
  912. /**
  913. * Currently the memory allocator doesn't support alloc specific aligned
  914. * space, we wrap posix_memalign to simply malloc memory
  915. */
  916. static int32
  917. posix_memalign_wrapper(wasm_exec_env_t exec_env, void **memptr, int32 align,
  918. int32 size)
  919. {
  920. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  921. void *p = NULL;
  922. *((int32 *)memptr) = module_malloc(size, (void **)&p);
  923. if (!p)
  924. return -1;
  925. return 0;
  926. }
  927. #if WASM_ENABLE_LIB_PTHREAD_SEMAPHORE != 0
  928. static int32
  929. sem_open_wrapper(wasm_exec_env_t exec_env, const char *name, int32 oflags,
  930. int32 mode, int32 val)
  931. {
  932. korp_sem *psem = NULL;
  933. ThreadInfoNode *info_node = NULL;
  934. /**
  935. * For RTOS, global semaphore map is safe for share the same semaphore
  936. * between task/pthread.
  937. * For Unix like system, it's dedicated for multiple processes.
  938. */
  939. if (!name) { /* avoid passing NULL to bh_hash_map_find and os_sem_open */
  940. return -1;
  941. }
  942. if ((info_node = bh_hash_map_find(sem_info_map, (void *)name))) {
  943. return info_node->handle;
  944. }
  945. if (!(psem = os_sem_open(name, oflags, mode, val))) {
  946. goto fail1;
  947. }
  948. if (!(info_node = wasm_runtime_malloc(sizeof(ThreadInfoNode))))
  949. goto fail2;
  950. memset(info_node, 0, sizeof(ThreadInfoNode));
  951. info_node->exec_env = exec_env;
  952. info_node->handle = allocate_handle();
  953. info_node->type = T_SEM;
  954. info_node->u.sem = psem;
  955. info_node->status = SEM_CREATED;
  956. if (!bh_hash_map_insert(sem_info_map, (void *)name, info_node))
  957. goto fail3;
  958. return info_node->handle;
  959. fail3:
  960. wasm_runtime_free(info_node);
  961. fail2:
  962. os_sem_close(psem);
  963. fail1:
  964. return -1;
  965. }
  966. void
  967. sem_fetch_cb(void *key, void *value, void *user_data)
  968. {
  969. (void)key;
  970. SemCallbackArgs *args = user_data;
  971. ThreadInfoNode *info_node = value;
  972. if (args->handle == info_node->handle && info_node->status == SEM_CREATED) {
  973. args->node = info_node;
  974. }
  975. }
  976. static int32
  977. sem_close_wrapper(wasm_exec_env_t exec_env, uint32 sem)
  978. {
  979. (void)exec_env;
  980. int ret = -1;
  981. SemCallbackArgs args = { sem, NULL };
  982. bh_hash_map_traverse(sem_info_map, sem_fetch_cb, &args);
  983. if (args.node) {
  984. ret = os_sem_close(args.node->u.sem);
  985. if (ret == 0) {
  986. args.node->status = SEM_CLOSED;
  987. }
  988. }
  989. return ret;
  990. }
  991. static int32
  992. sem_wait_wrapper(wasm_exec_env_t exec_env, uint32 sem)
  993. {
  994. (void)exec_env;
  995. SemCallbackArgs args = { sem, NULL };
  996. bh_hash_map_traverse(sem_info_map, sem_fetch_cb, &args);
  997. if (args.node) {
  998. return os_sem_wait(args.node->u.sem);
  999. }
  1000. return -1;
  1001. }
  1002. static int32
  1003. sem_trywait_wrapper(wasm_exec_env_t exec_env, uint32 sem)
  1004. {
  1005. (void)exec_env;
  1006. SemCallbackArgs args = { sem, NULL };
  1007. bh_hash_map_traverse(sem_info_map, sem_fetch_cb, &args);
  1008. if (args.node) {
  1009. return os_sem_trywait(args.node->u.sem);
  1010. }
  1011. return -1;
  1012. }
  1013. static int32
  1014. sem_post_wrapper(wasm_exec_env_t exec_env, uint32 sem)
  1015. {
  1016. (void)exec_env;
  1017. SemCallbackArgs args = { sem, NULL };
  1018. bh_hash_map_traverse(sem_info_map, sem_fetch_cb, &args);
  1019. if (args.node) {
  1020. return os_sem_post(args.node->u.sem);
  1021. }
  1022. return -1;
  1023. }
  1024. static int32
  1025. sem_getvalue_wrapper(wasm_exec_env_t exec_env, uint32 sem, int32 *sval)
  1026. {
  1027. int32 ret = -1;
  1028. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1029. (void)exec_env;
  1030. SemCallbackArgs args = { sem, NULL };
  1031. if (validate_native_addr(sval, sizeof(int32))) {
  1032. bh_hash_map_traverse(sem_info_map, sem_fetch_cb, &args);
  1033. if (args.node) {
  1034. ret = os_sem_getvalue(args.node->u.sem, sval);
  1035. }
  1036. }
  1037. return ret;
  1038. }
  1039. static int32
  1040. sem_unlink_wrapper(wasm_exec_env_t exec_env, const char *name)
  1041. {
  1042. (void)exec_env;
  1043. int32 ret_val;
  1044. ThreadInfoNode *info_node;
  1045. if (!name) { /* avoid passing NULL to bh_hash_map_find */
  1046. return -1;
  1047. }
  1048. info_node = bh_hash_map_find(sem_info_map, (void *)name);
  1049. if (!info_node || info_node->type != T_SEM)
  1050. return -1;
  1051. if (info_node->status != SEM_CLOSED) {
  1052. ret_val = os_sem_close(info_node->u.sem);
  1053. if (ret_val != 0) {
  1054. return ret_val;
  1055. }
  1056. }
  1057. ret_val = os_sem_unlink(name);
  1058. if (ret_val == 0) {
  1059. bh_hash_map_remove(sem_info_map, (void *)name, NULL, NULL);
  1060. info_node->status = SEM_DESTROYED;
  1061. thread_info_destroy(info_node);
  1062. }
  1063. return ret_val;
  1064. }
  1065. #endif
  1066. /* clang-format off */
  1067. #define REG_NATIVE_FUNC(func_name, signature) \
  1068. { #func_name, func_name##_wrapper, signature, NULL }
  1069. /* clang-format on */
  1070. static NativeSymbol native_symbols_lib_pthread[] = {
  1071. REG_NATIVE_FUNC(pthread_create, "(**ii)i"),
  1072. REG_NATIVE_FUNC(pthread_join, "(ii)i"),
  1073. REG_NATIVE_FUNC(pthread_detach, "(i)i"),
  1074. REG_NATIVE_FUNC(pthread_cancel, "(i)i"),
  1075. REG_NATIVE_FUNC(pthread_self, "()i"),
  1076. REG_NATIVE_FUNC(__pthread_self, "()i"),
  1077. REG_NATIVE_FUNC(pthread_exit, "(i)"),
  1078. REG_NATIVE_FUNC(pthread_mutex_init, "(**)i"),
  1079. REG_NATIVE_FUNC(pthread_mutex_lock, "(*)i"),
  1080. REG_NATIVE_FUNC(pthread_mutex_unlock, "(*)i"),
  1081. REG_NATIVE_FUNC(pthread_mutex_destroy, "(*)i"),
  1082. REG_NATIVE_FUNC(pthread_cond_init, "(**)i"),
  1083. REG_NATIVE_FUNC(pthread_cond_wait, "(**)i"),
  1084. REG_NATIVE_FUNC(pthread_cond_timedwait, "(**I)i"),
  1085. REG_NATIVE_FUNC(pthread_cond_signal, "(*)i"),
  1086. REG_NATIVE_FUNC(pthread_cond_broadcast, "(*)i"),
  1087. REG_NATIVE_FUNC(pthread_cond_destroy, "(*)i"),
  1088. REG_NATIVE_FUNC(pthread_key_create, "(*i)i"),
  1089. REG_NATIVE_FUNC(pthread_setspecific, "(ii)i"),
  1090. REG_NATIVE_FUNC(pthread_getspecific, "(i)i"),
  1091. REG_NATIVE_FUNC(pthread_key_delete, "(i)i"),
  1092. REG_NATIVE_FUNC(posix_memalign, "(*ii)i"),
  1093. #if WASM_ENABLE_LIB_PTHREAD_SEMAPHORE != 0
  1094. REG_NATIVE_FUNC(sem_open, "($iii)i"),
  1095. REG_NATIVE_FUNC(sem_close, "(i)i"),
  1096. REG_NATIVE_FUNC(sem_wait, "(i)i"),
  1097. REG_NATIVE_FUNC(sem_trywait, "(i)i"),
  1098. REG_NATIVE_FUNC(sem_post, "(i)i"),
  1099. REG_NATIVE_FUNC(sem_getvalue, "(i*)i"),
  1100. REG_NATIVE_FUNC(sem_unlink, "($)i"),
  1101. #endif
  1102. };
  1103. uint32
  1104. get_lib_pthread_export_apis(NativeSymbol **p_lib_pthread_apis)
  1105. {
  1106. *p_lib_pthread_apis = native_symbols_lib_pthread;
  1107. return sizeof(native_symbols_lib_pthread) / sizeof(NativeSymbol);
  1108. }