lib_pthread_wrapper.c 30 KB

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