lib_pthread_wrapper.c 29 KB

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