lib_pthread_wrapper.c 35 KB

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