lib_pthread_wrapper.c 27 KB

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