lib_pthread_wrapper.c 29 KB

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