lib_pthread_wrapper.c 28 KB

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