lib_pthread_wrapper.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  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. static void
  597. pthread_exit_wrapper(wasm_exec_env_t exec_env, int32 retval_offset)
  598. {
  599. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  600. ThreadRoutineArgs *args = get_thread_arg(exec_env);
  601. /* Currently exit main thread is not allowed */
  602. if (!args)
  603. return;
  604. #if defined(OS_ENABLE_HW_BOUND_CHECK) && !defined(BH_PLATFORM_WINDOWS)
  605. /* If hardware bound check enabled, don't deinstantiate module inst
  606. and thread info node here for AoT module, as they will be freed
  607. in pthread_start_routine */
  608. if (exec_env->jmpbuf_stack_top) {
  609. wasm_cluster_exit_thread(exec_env, (void *)(uintptr_t)retval_offset);
  610. }
  611. #endif
  612. /* destroy pthread key values */
  613. call_key_destructor(exec_env);
  614. /* routine exit, destroy instance */
  615. wasm_runtime_deinstantiate_internal(module_inst, true);
  616. if (!args->info_node->joinable) {
  617. delete_thread_info_node(args->info_node);
  618. }
  619. else {
  620. args->info_node->u.ret = (void *)(uintptr_t)retval_offset;
  621. /* Update node status after ret value was set */
  622. args->info_node->status = THREAD_EXIT;
  623. }
  624. wasm_runtime_free(args);
  625. wasm_cluster_exit_thread(exec_env, (void *)(uintptr_t)retval_offset);
  626. }
  627. static int32
  628. pthread_mutex_init_wrapper(wasm_exec_env_t exec_env, uint32 *mutex, void *attr)
  629. {
  630. korp_mutex *pmutex;
  631. ThreadInfoNode *info_node;
  632. if (!(pmutex = wasm_runtime_malloc(sizeof(korp_mutex)))) {
  633. return -1;
  634. }
  635. if (os_mutex_init(pmutex) != 0) {
  636. goto fail1;
  637. }
  638. if (!(info_node = wasm_runtime_malloc(sizeof(ThreadInfoNode))))
  639. goto fail2;
  640. memset(info_node, 0, sizeof(ThreadInfoNode));
  641. info_node->exec_env = exec_env;
  642. info_node->handle = allocate_handle();
  643. info_node->type = T_MUTEX;
  644. info_node->u.mutex = pmutex;
  645. info_node->status = MUTEX_CREATED;
  646. if (!append_thread_info_node(info_node))
  647. goto fail3;
  648. /* Return the mutex handle to app */
  649. if (mutex)
  650. *(uint32 *)mutex = info_node->handle;
  651. return 0;
  652. fail3:
  653. delete_thread_info_node(info_node);
  654. fail2:
  655. os_mutex_destroy(pmutex);
  656. fail1:
  657. wasm_runtime_free(pmutex);
  658. return -1;
  659. }
  660. static int32
  661. pthread_mutex_lock_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_lock(info_node->u.mutex);
  667. }
  668. static int32
  669. pthread_mutex_unlock_wrapper(wasm_exec_env_t exec_env, uint32 *mutex)
  670. {
  671. ThreadInfoNode *info_node = get_thread_info(exec_env, *mutex);
  672. if (!info_node || info_node->type != T_MUTEX)
  673. return -1;
  674. return os_mutex_unlock(info_node->u.mutex);
  675. }
  676. static int32
  677. pthread_mutex_destroy_wrapper(wasm_exec_env_t exec_env, uint32 *mutex)
  678. {
  679. int32 ret_val;
  680. ThreadInfoNode *info_node = get_thread_info(exec_env, *mutex);
  681. if (!info_node || info_node->type != T_MUTEX)
  682. return -1;
  683. ret_val = os_mutex_destroy(info_node->u.mutex);
  684. info_node->status = MUTEX_DESTROYED;
  685. delete_thread_info_node(info_node);
  686. return ret_val;
  687. }
  688. static int32
  689. pthread_cond_init_wrapper(wasm_exec_env_t exec_env, uint32 *cond, void *attr)
  690. {
  691. korp_cond *pcond;
  692. ThreadInfoNode *info_node;
  693. if (!(pcond = wasm_runtime_malloc(sizeof(korp_cond)))) {
  694. return -1;
  695. }
  696. if (os_cond_init(pcond) != 0) {
  697. goto fail1;
  698. }
  699. if (!(info_node = wasm_runtime_malloc(sizeof(ThreadInfoNode))))
  700. goto fail2;
  701. memset(info_node, 0, sizeof(ThreadInfoNode));
  702. info_node->exec_env = exec_env;
  703. info_node->handle = allocate_handle();
  704. info_node->type = T_COND;
  705. info_node->u.cond = pcond;
  706. info_node->status = COND_CREATED;
  707. if (!append_thread_info_node(info_node))
  708. goto fail3;
  709. /* Return the cond handle to app */
  710. if (cond)
  711. *(uint32 *)cond = info_node->handle;
  712. return 0;
  713. fail3:
  714. delete_thread_info_node(info_node);
  715. fail2:
  716. os_cond_destroy(pcond);
  717. fail1:
  718. wasm_runtime_free(pcond);
  719. return -1;
  720. }
  721. static int32
  722. pthread_cond_wait_wrapper(wasm_exec_env_t exec_env, uint32 *cond, uint32 *mutex)
  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_wait(cond_info_node->u.cond, mutex_info_node->u.mutex);
  732. }
  733. /**
  734. * Currently we don't support struct timespec in built-in libc,
  735. * so the pthread_cond_timedwait use useconds instead
  736. */
  737. static int32
  738. pthread_cond_timedwait_wrapper(wasm_exec_env_t exec_env, uint32 *cond,
  739. uint32 *mutex, uint64 useconds)
  740. {
  741. ThreadInfoNode *cond_info_node, *mutex_info_node;
  742. cond_info_node = get_thread_info(exec_env, *cond);
  743. if (!cond_info_node || cond_info_node->type != T_COND)
  744. return -1;
  745. mutex_info_node = get_thread_info(exec_env, *mutex);
  746. if (!mutex_info_node || mutex_info_node->type != T_MUTEX)
  747. return -1;
  748. return os_cond_reltimedwait(cond_info_node->u.cond,
  749. mutex_info_node->u.mutex, useconds);
  750. }
  751. static int32
  752. pthread_cond_signal_wrapper(wasm_exec_env_t exec_env, uint32 *cond)
  753. {
  754. ThreadInfoNode *info_node = get_thread_info(exec_env, *cond);
  755. if (!info_node || info_node->type != T_COND)
  756. return -1;
  757. return os_cond_signal(info_node->u.cond);
  758. }
  759. static int32
  760. pthread_cond_destroy_wrapper(wasm_exec_env_t exec_env, uint32 *cond)
  761. {
  762. int32 ret_val;
  763. ThreadInfoNode *info_node = get_thread_info(exec_env, *cond);
  764. if (!info_node || info_node->type != T_COND)
  765. return -1;
  766. ret_val = os_cond_destroy(info_node->u.cond);
  767. info_node->status = COND_DESTROYED;
  768. delete_thread_info_node(info_node);
  769. return ret_val;
  770. }
  771. static int32
  772. pthread_key_create_wrapper(wasm_exec_env_t exec_env, int32 *key,
  773. int32 destructor_elem_index)
  774. {
  775. uint32 i;
  776. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  777. ClusterInfoNode *info = get_cluster_info(cluster);
  778. if (!info) {
  779. /* The user may call pthread_key_create in main thread,
  780. in this case the cluster info hasn't been created */
  781. if (!(info = create_cluster_info(cluster))) {
  782. return -1;
  783. }
  784. }
  785. os_mutex_lock(&info->key_data_list_lock);
  786. for (i = 0; i < WAMR_PTHREAD_KEYS_MAX; i++) {
  787. if (!info->key_data_list[i].is_created) {
  788. break;
  789. }
  790. }
  791. if (i == WAMR_PTHREAD_KEYS_MAX) {
  792. os_mutex_unlock(&info->key_data_list_lock);
  793. return -1;
  794. }
  795. info->key_data_list[i].destructor_func = destructor_elem_index;
  796. info->key_data_list[i].is_created = true;
  797. *key = i;
  798. os_mutex_unlock(&info->key_data_list_lock);
  799. return 0;
  800. }
  801. static int32
  802. pthread_setspecific_wrapper(wasm_exec_env_t exec_env, int32 key,
  803. int32 value_offset)
  804. {
  805. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  806. ClusterInfoNode *info = get_cluster_info(cluster);
  807. int32 *key_values;
  808. if (!info)
  809. return -1;
  810. os_mutex_lock(&info->key_data_list_lock);
  811. key_values = key_value_list_lookup_or_create(exec_env, info, key);
  812. if (!key_values) {
  813. os_mutex_unlock(&info->key_data_list_lock);
  814. return 0;
  815. }
  816. key_values[key] = value_offset;
  817. os_mutex_unlock(&info->key_data_list_lock);
  818. return 0;
  819. }
  820. static int32
  821. pthread_getspecific_wrapper(wasm_exec_env_t exec_env, int32 key)
  822. {
  823. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  824. ClusterInfoNode *info = get_cluster_info(cluster);
  825. int32 ret, *key_values;
  826. if (!info)
  827. return -1;
  828. os_mutex_lock(&info->key_data_list_lock);
  829. key_values = key_value_list_lookup_or_create(exec_env, info, key);
  830. if (!key_values) {
  831. os_mutex_unlock(&info->key_data_list_lock);
  832. return 0;
  833. }
  834. ret = key_values[key];
  835. os_mutex_unlock(&info->key_data_list_lock);
  836. return ret;
  837. }
  838. static int32
  839. pthread_key_delete_wrapper(wasm_exec_env_t exec_env, int32 key)
  840. {
  841. KeyData *data;
  842. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  843. ClusterInfoNode *info = get_cluster_info(cluster);
  844. if (!info)
  845. return -1;
  846. os_mutex_lock(&info->key_data_list_lock);
  847. data = key_data_list_lookup(exec_env, key);
  848. if (!data) {
  849. os_mutex_unlock(&info->key_data_list_lock);
  850. return -1;
  851. }
  852. memset(data, 0, sizeof(KeyData));
  853. os_mutex_unlock(&info->key_data_list_lock);
  854. return 0;
  855. }
  856. /**
  857. * Currently the memory allocator doesn't support alloc specific aligned
  858. * space, we wrap posix_memalign to simply malloc memory
  859. */
  860. static int32
  861. posix_memalign_wrapper(wasm_exec_env_t exec_env, void **memptr, int32 align,
  862. int32 size)
  863. {
  864. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  865. void *p = NULL;
  866. *((int32 *)memptr) = module_malloc(size, (void **)&p);
  867. if (!p)
  868. return -1;
  869. return 0;
  870. }
  871. /* clang-format off */
  872. #define REG_NATIVE_FUNC(func_name, signature) \
  873. { #func_name, func_name##_wrapper, signature, NULL }
  874. /* clang-format on */
  875. static NativeSymbol native_symbols_lib_pthread[] = {
  876. REG_NATIVE_FUNC(pthread_create, "(**ii)i"),
  877. REG_NATIVE_FUNC(pthread_join, "(ii)i"),
  878. REG_NATIVE_FUNC(pthread_detach, "(i)i"),
  879. REG_NATIVE_FUNC(pthread_cancel, "(i)i"),
  880. REG_NATIVE_FUNC(pthread_self, "()i"),
  881. REG_NATIVE_FUNC(pthread_exit, "(i)"),
  882. REG_NATIVE_FUNC(pthread_mutex_init, "(**)i"),
  883. REG_NATIVE_FUNC(pthread_mutex_lock, "(*)i"),
  884. REG_NATIVE_FUNC(pthread_mutex_unlock, "(*)i"),
  885. REG_NATIVE_FUNC(pthread_mutex_destroy, "(*)i"),
  886. REG_NATIVE_FUNC(pthread_cond_init, "(**)i"),
  887. REG_NATIVE_FUNC(pthread_cond_wait, "(**)i"),
  888. REG_NATIVE_FUNC(pthread_cond_timedwait, "(**I)i"),
  889. REG_NATIVE_FUNC(pthread_cond_signal, "(*)i"),
  890. REG_NATIVE_FUNC(pthread_cond_destroy, "(*)i"),
  891. REG_NATIVE_FUNC(pthread_key_create, "(*i)i"),
  892. REG_NATIVE_FUNC(pthread_setspecific, "(ii)i"),
  893. REG_NATIVE_FUNC(pthread_getspecific, "(i)i"),
  894. REG_NATIVE_FUNC(pthread_key_delete, "(i)i"),
  895. REG_NATIVE_FUNC(posix_memalign, "(*ii)i"),
  896. };
  897. uint32
  898. get_lib_pthread_export_apis(NativeSymbol **p_lib_pthread_apis)
  899. {
  900. *p_lib_pthread_apis = native_symbols_lib_pthread;
  901. return sizeof(native_symbols_lib_pthread) / sizeof(NativeSymbol);
  902. }