lib_pthread_wrapper.c 29 KB

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