lib_pthread_wrapper.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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. argv[0] = addr_native_to_app(routine_args->arg);
  411. if(!wasm_runtime_call_indirect(exec_env,
  412. routine_args->elem_index,
  413. 1, argv)) {
  414. wasm_cluster_spread_exception(exec_env);
  415. }
  416. /* destroy pthread key values */
  417. call_key_destructor(exec_env);
  418. /* routine exit, destroy instance */
  419. wasm_runtime_deinstantiate_internal(module_inst, true);
  420. info_node->status = THREAD_EXIT;
  421. delete_thread_info_node(info_node);
  422. return (void *)(uintptr_t)argv[0];
  423. }
  424. static int
  425. pthread_create_wrapper(wasm_exec_env_t exec_env,
  426. uint32 *thread, /* thread_handle */
  427. const void *attr, /* not supported */
  428. uint32 elem_index, /* entry function */
  429. void *arg) /* arguments buffer */
  430. {
  431. wasm_module_t module = get_module(exec_env);
  432. wasm_module_inst_t new_module_inst = NULL;
  433. ThreadInfoNode *info_node = NULL;
  434. ThreadRoutineArgs *routine_args = NULL;
  435. uint32 thread_handle;
  436. int32 ret = -1;
  437. #if WASM_ENABLE_LIBC_WASI != 0
  438. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  439. WASIContext *wasi_ctx = get_wasi_ctx(module_inst);
  440. #endif
  441. if (!(new_module_inst =
  442. wasm_runtime_instantiate_internal(module, true, 8192, 0,
  443. NULL, 0)))
  444. return -1;
  445. #if WASM_ENABLE_LIBC_WASI != 0
  446. if (wasi_ctx)
  447. wasm_runtime_set_wasi_ctx(new_module_inst, wasi_ctx);
  448. #endif
  449. if (!(info_node = wasm_runtime_malloc(sizeof(ThreadInfoNode))))
  450. goto fail;
  451. memset(info_node, 0, sizeof(ThreadInfoNode));
  452. thread_handle = allocate_handle();
  453. info_node->parent_exec_env = exec_env;
  454. info_node->handle = thread_handle;
  455. info_node->type = T_THREAD;
  456. info_node->status = THREAD_INIT;
  457. if (!(routine_args = wasm_runtime_malloc(sizeof(ThreadRoutineArgs))))
  458. goto fail;
  459. routine_args->arg = arg;
  460. routine_args->elem_index = elem_index;
  461. routine_args->info_node = info_node;
  462. routine_args->module_inst = new_module_inst;
  463. os_mutex_lock(&exec_env->wait_lock);
  464. ret = wasm_cluster_create_thread(exec_env, new_module_inst,
  465. pthread_start_routine,
  466. (void *)routine_args);
  467. if (ret != 0) {
  468. os_mutex_unlock(&exec_env->wait_lock);
  469. goto fail;
  470. }
  471. /* Wait for the thread routine to assign the exec_env to
  472. thread_info_node, otherwise the exec_env in the thread
  473. info node may be NULL in the next pthread API call */
  474. os_cond_wait(&exec_env->wait_cond, &exec_env->wait_lock);
  475. os_mutex_unlock(&exec_env->wait_lock);
  476. if (thread)
  477. *thread = thread_handle;
  478. return 0;
  479. fail:
  480. if (new_module_inst)
  481. wasm_runtime_deinstantiate_internal(new_module_inst, true);
  482. if (info_node)
  483. wasm_runtime_free(info_node);
  484. if (routine_args)
  485. wasm_runtime_free(routine_args);
  486. return ret;
  487. }
  488. static int32
  489. pthread_join_wrapper(wasm_exec_env_t exec_env, uint32 thread,
  490. int32 retval_offset) /* void **retval */
  491. {
  492. uint32 *ret;
  493. int32 join_ret;
  494. void **retval;
  495. ThreadInfoNode *node;
  496. wasm_module_inst_t module_inst;
  497. wasm_exec_env_t target_exec_env;
  498. node = get_thread_info(exec_env, thread);
  499. if (!node) {
  500. /* The thread has exited, return 0 to app */
  501. return 0;
  502. }
  503. target_exec_env = node->exec_env;
  504. bh_assert(target_exec_env != NULL);
  505. module_inst = get_module_inst(target_exec_env);
  506. /* validate addr before join thread, otherwise
  507. the module_inst may be freed */
  508. if (!validate_app_addr(retval_offset, sizeof(uint32))) {
  509. /* Join failed, but we don't want to terminate all threads,
  510. do not spread exception here */
  511. wasm_runtime_set_exception(module_inst, NULL);
  512. return -1;
  513. }
  514. retval = (void **)addr_app_to_native(retval_offset);
  515. join_ret = wasm_cluster_join_thread(target_exec_env, (void **)&ret);
  516. if (retval_offset != 0)
  517. *retval = (void*)ret;
  518. return join_ret;
  519. }
  520. static int32
  521. pthread_detach_wrapper(wasm_exec_env_t exec_env, uint32 thread)
  522. {
  523. ThreadInfoNode *node;
  524. wasm_exec_env_t target_exec_env;
  525. node = get_thread_info(exec_env, thread);
  526. if (!node)
  527. return 0;
  528. target_exec_env = node->exec_env;
  529. bh_assert(target_exec_env != NULL);
  530. return wasm_cluster_detach_thread(target_exec_env);
  531. }
  532. static int32
  533. pthread_cancel_wrapper(wasm_exec_env_t exec_env, uint32 thread)
  534. {
  535. ThreadInfoNode *node;
  536. wasm_exec_env_t target_exec_env;
  537. node = get_thread_info(exec_env, thread);
  538. if (!node)
  539. return 0;
  540. target_exec_env = node->exec_env;
  541. bh_assert(target_exec_env != NULL);
  542. return wasm_cluster_cancel_thread(target_exec_env);
  543. }
  544. static int32
  545. pthread_self_wrapper(wasm_exec_env_t exec_env)
  546. {
  547. ThreadRoutineArgs *args = get_thread_arg(exec_env);
  548. /* If thread_arg is NULL, it's the exec_env of the main thread,
  549. return id 0 to app */
  550. if (!args)
  551. return 0;
  552. return args->info_node->handle;
  553. }
  554. static void
  555. pthread_exit_wrapper(wasm_exec_env_t exec_env, int32 retval_offset)
  556. {
  557. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  558. ThreadRoutineArgs *args = get_thread_arg(exec_env);
  559. /* Currently exit main thread is not allowed */
  560. if (!args)
  561. return;
  562. /* destroy pthread key values */
  563. call_key_destructor(exec_env);
  564. /* routine exit, destroy instance */
  565. wasm_runtime_deinstantiate_internal(module_inst, true);
  566. delete_thread_info_node(args->info_node);
  567. wasm_cluster_exit_thread(exec_env, (void *)(uintptr_t)retval_offset);
  568. }
  569. static int32
  570. pthread_mutex_init_wrapper(wasm_exec_env_t exec_env, uint32 *mutex, void *attr)
  571. {
  572. korp_mutex *pmutex;
  573. ThreadInfoNode *info_node;
  574. if (!(pmutex = wasm_runtime_malloc(sizeof(korp_mutex)))) {
  575. return -1;
  576. }
  577. if (os_mutex_init(pmutex) != 0) {
  578. goto fail1;
  579. }
  580. if (!(info_node = wasm_runtime_malloc(sizeof(ThreadInfoNode))))
  581. goto fail2;
  582. memset(info_node, 0, sizeof(ThreadInfoNode));
  583. info_node->exec_env = exec_env;
  584. info_node->handle = allocate_handle();
  585. info_node->type = T_MUTEX;
  586. info_node->u.mutex = pmutex;
  587. info_node->status = MUTEX_CREATED;
  588. if (!append_thread_info_node(info_node))
  589. goto fail3;
  590. /* Return the mutex handle to app */
  591. if (mutex)
  592. *(uint32*)mutex = info_node->handle;
  593. return 0;
  594. fail3:
  595. delete_thread_info_node(info_node);
  596. fail2:
  597. os_mutex_destroy(pmutex);
  598. fail1:
  599. wasm_runtime_free(pmutex);
  600. return -1;
  601. }
  602. static int32
  603. pthread_mutex_lock_wrapper(wasm_exec_env_t exec_env, uint32 *mutex)
  604. {
  605. ThreadInfoNode* info_node = get_thread_info(exec_env, *mutex);
  606. if (!info_node || info_node->type != T_MUTEX)
  607. return -1;
  608. return os_mutex_lock(info_node->u.mutex);
  609. }
  610. static int32
  611. pthread_mutex_unlock_wrapper(wasm_exec_env_t exec_env, uint32 *mutex)
  612. {
  613. ThreadInfoNode* info_node = get_thread_info(exec_env, *mutex);
  614. if (!info_node || info_node->type != T_MUTEX)
  615. return -1;
  616. return os_mutex_unlock(info_node->u.mutex);
  617. }
  618. static int32
  619. pthread_mutex_destroy_wrapper(wasm_exec_env_t exec_env, uint32 *mutex)
  620. {
  621. int32 ret_val;
  622. ThreadInfoNode* info_node = get_thread_info(exec_env, *mutex);
  623. if (!info_node || info_node->type != T_MUTEX)
  624. return -1;
  625. ret_val = os_mutex_destroy(info_node->u.mutex);
  626. info_node->status = MUTEX_DESTROYED;
  627. delete_thread_info_node(info_node);
  628. return ret_val;
  629. }
  630. static int32
  631. pthread_cond_init_wrapper(wasm_exec_env_t exec_env, uint32 *cond, void *attr)
  632. {
  633. korp_cond *pcond;
  634. ThreadInfoNode *info_node;
  635. if (!(pcond = wasm_runtime_malloc(sizeof(korp_cond)))) {
  636. return -1;
  637. }
  638. if (os_cond_init(pcond) != 0) {
  639. goto fail1;
  640. }
  641. if (!(info_node = wasm_runtime_malloc(sizeof(ThreadInfoNode))))
  642. goto fail2;
  643. memset(info_node, 0, sizeof(ThreadInfoNode));
  644. info_node->exec_env = exec_env;
  645. info_node->handle = allocate_handle();
  646. info_node->type = T_COND;
  647. info_node->u.cond = pcond;
  648. info_node->status = COND_CREATED;
  649. if (!append_thread_info_node(info_node))
  650. goto fail3;
  651. /* Return the cond handle to app */
  652. if (cond)
  653. *(uint32*)cond = info_node->handle;
  654. return 0;
  655. fail3:
  656. delete_thread_info_node(info_node);
  657. fail2:
  658. os_cond_destroy(pcond);
  659. fail1:
  660. wasm_runtime_free(pcond);
  661. return -1;
  662. }
  663. static int32
  664. pthread_cond_wait_wrapper(wasm_exec_env_t exec_env, uint32 *cond, uint32 *mutex)
  665. {
  666. ThreadInfoNode *cond_info_node, *mutex_info_node;
  667. cond_info_node = get_thread_info(exec_env, *cond);
  668. if (!cond_info_node || cond_info_node->type != T_COND)
  669. return -1;
  670. mutex_info_node = get_thread_info(exec_env, *mutex);
  671. if (!mutex_info_node || mutex_info_node->type != T_MUTEX)
  672. return -1;
  673. return os_cond_wait(cond_info_node->u.cond, mutex_info_node->u.mutex);
  674. }
  675. /* Currently we don't support struct timespec in built-in libc,
  676. so the pthread_cond_timedwait use useconds instead
  677. */
  678. static int32
  679. pthread_cond_timedwait_wrapper(wasm_exec_env_t exec_env, uint32 *cond,
  680. uint32 *mutex, uint32 useconds)
  681. {
  682. ThreadInfoNode *cond_info_node, *mutex_info_node;
  683. cond_info_node = get_thread_info(exec_env, *cond);
  684. if (!cond_info_node || cond_info_node->type != T_COND)
  685. return -1;
  686. mutex_info_node = get_thread_info(exec_env, *mutex);
  687. if (!mutex_info_node || mutex_info_node->type != T_MUTEX)
  688. return -1;
  689. return os_cond_reltimedwait(cond_info_node->u.cond,
  690. mutex_info_node->u.mutex, useconds);
  691. }
  692. static int32
  693. pthread_cond_signal_wrapper(wasm_exec_env_t exec_env, uint32 *cond)
  694. {
  695. ThreadInfoNode* info_node = get_thread_info(exec_env, *cond);
  696. if (!info_node || info_node->type != T_COND)
  697. return -1;
  698. return os_cond_signal(info_node->u.cond);
  699. }
  700. static int32
  701. pthread_cond_destroy_wrapper(wasm_exec_env_t exec_env, uint32 *cond)
  702. {
  703. int32 ret_val;
  704. ThreadInfoNode* info_node = get_thread_info(exec_env, *cond);
  705. if (!info_node || info_node->type != T_COND)
  706. return -1;
  707. ret_val = os_cond_destroy(info_node->u.cond);
  708. info_node->status = COND_DESTROYED;
  709. delete_thread_info_node(info_node);
  710. return ret_val;
  711. }
  712. static int32
  713. pthread_key_create_wrapper(wasm_exec_env_t exec_env, int32 *key,
  714. int32 destructor_elem_index)
  715. {
  716. uint32 i;
  717. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  718. ClusterInfoNode *info = get_cluster_info(cluster);
  719. if (!info) {
  720. /* The user may call pthread_key_create in main thread,
  721. in this case the cluster info hasn't been created */
  722. if (!(info = create_cluster_info(cluster))) {
  723. return -1;
  724. }
  725. }
  726. os_mutex_lock(&info->key_data_list_lock);
  727. for (i = 0; i < WAMR_PTHREAD_KEYS_MAX; i++) {
  728. if (!info->key_data_list[i].is_created) {
  729. break;
  730. }
  731. }
  732. if (i == WAMR_PTHREAD_KEYS_MAX) {
  733. os_mutex_unlock(&info->key_data_list_lock);
  734. return -1;
  735. }
  736. info->key_data_list[i].destructor_func = destructor_elem_index;
  737. info->key_data_list[i].is_created = true;
  738. *key = i;
  739. os_mutex_unlock(&info->key_data_list_lock);
  740. return 0;
  741. }
  742. static int32
  743. pthread_setspecific_wrapper(wasm_exec_env_t exec_env, int32 key,
  744. int32 value_offset)
  745. {
  746. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  747. ClusterInfoNode *info = get_cluster_info(cluster);
  748. int32 *key_values;
  749. if (!info)
  750. return -1;
  751. os_mutex_lock(&info->key_data_list_lock);
  752. key_values = key_value_list_lookup_or_create(exec_env, info, key);
  753. if (!key_values) {
  754. os_mutex_unlock(&info->key_data_list_lock);
  755. return 0;
  756. }
  757. key_values[key] = value_offset;
  758. os_mutex_unlock(&info->key_data_list_lock);
  759. return 0;
  760. }
  761. static int32
  762. pthread_getspecific_wrapper(wasm_exec_env_t exec_env, int32 key)
  763. {
  764. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  765. ClusterInfoNode *info = get_cluster_info(cluster);
  766. int32 ret, *key_values;
  767. if (!info)
  768. return -1;
  769. os_mutex_lock(&info->key_data_list_lock);
  770. key_values = key_value_list_lookup_or_create(exec_env, info, key);
  771. if (!key_values) {
  772. os_mutex_unlock(&info->key_data_list_lock);
  773. return 0;
  774. }
  775. ret = key_values[key];
  776. os_mutex_unlock(&info->key_data_list_lock);
  777. return ret;
  778. }
  779. static int32
  780. pthread_key_delete_wrapper(wasm_exec_env_t exec_env, int32 key)
  781. {
  782. KeyData *data;
  783. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  784. ClusterInfoNode *info = get_cluster_info(cluster);
  785. if (!info)
  786. return -1;
  787. os_mutex_lock(&info->key_data_list_lock);
  788. data = key_data_list_lookup(exec_env, key);
  789. if (!data) {
  790. os_mutex_unlock(&info->key_data_list_lock);
  791. return -1;
  792. }
  793. memset(data, 0, sizeof(KeyData));
  794. os_mutex_unlock(&info->key_data_list_lock);
  795. return 0;
  796. }
  797. #define REG_NATIVE_FUNC(func_name, signature) \
  798. { #func_name, func_name##_wrapper, signature, NULL }
  799. static NativeSymbol native_symbols_lib_pthread[] = {
  800. REG_NATIVE_FUNC(pthread_create, "(**i*)i"),
  801. REG_NATIVE_FUNC(pthread_join, "(ii)i"),
  802. REG_NATIVE_FUNC(pthread_detach, "(i)i"),
  803. REG_NATIVE_FUNC(pthread_cancel, "(i)i"),
  804. REG_NATIVE_FUNC(pthread_self, "()i"),
  805. REG_NATIVE_FUNC(pthread_exit, "(i)"),
  806. REG_NATIVE_FUNC(pthread_mutex_init, "(**)i"),
  807. REG_NATIVE_FUNC(pthread_mutex_lock, "(*)i"),
  808. REG_NATIVE_FUNC(pthread_mutex_unlock, "(*)i"),
  809. REG_NATIVE_FUNC(pthread_mutex_destroy, "(*)i"),
  810. REG_NATIVE_FUNC(pthread_cond_init, "(**)i"),
  811. REG_NATIVE_FUNC(pthread_cond_wait, "(**)i"),
  812. REG_NATIVE_FUNC(pthread_cond_timedwait, "(**i)i"),
  813. REG_NATIVE_FUNC(pthread_cond_signal, "(*)i"),
  814. REG_NATIVE_FUNC(pthread_cond_destroy, "(*)i"),
  815. REG_NATIVE_FUNC(pthread_key_create, "(*i)i"),
  816. REG_NATIVE_FUNC(pthread_setspecific, "(ii)i"),
  817. REG_NATIVE_FUNC(pthread_getspecific, "(i)i"),
  818. REG_NATIVE_FUNC(pthread_key_delete, "(i)i"),
  819. };
  820. uint32
  821. get_lib_pthread_export_apis(NativeSymbol **p_lib_pthread_apis)
  822. {
  823. *p_lib_pthread_apis = native_symbols_lib_pthread;
  824. return sizeof(native_symbols_lib_pthread) / sizeof(NativeSymbol);
  825. }