lib_pthread_wrapper.c 27 KB

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