lib_pthread_wrapper.c 36 KB

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