lib_pthread_wrapper.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  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. #if WASM_ENABLE_INTERP != 0
  12. #include "wasm_runtime.h"
  13. #endif
  14. #if WASM_ENABLE_AOT != 0
  15. #include "aot_runtime.h"
  16. #endif
  17. #define WAMR_PTHREAD_KEYS_MAX 32
  18. /* clang-format off */
  19. #define get_module(exec_env) \
  20. wasm_exec_env_get_module(exec_env)
  21. #define get_module_inst(exec_env) \
  22. wasm_runtime_get_module_inst(exec_env)
  23. #define get_thread_arg(exec_env) \
  24. wasm_exec_env_get_thread_arg(exec_env)
  25. #define get_wasi_ctx(module_inst) \
  26. wasm_runtime_get_wasi_ctx(module_inst)
  27. #define validate_app_addr(offset, size) \
  28. wasm_runtime_validate_app_addr(module_inst, offset, size)
  29. #define validate_native_addr(addr, size) \
  30. wasm_runtime_validate_native_addr(module_inst, addr, size)
  31. #define addr_app_to_native(offset) \
  32. wasm_runtime_addr_app_to_native(module_inst, offset)
  33. #define addr_native_to_app(ptr) \
  34. wasm_runtime_addr_native_to_app(module_inst, ptr)
  35. /* clang-format on */
  36. extern bool
  37. wasm_runtime_call_indirect(wasm_exec_env_t exec_env, uint32 element_indices,
  38. uint32 argc, uint32 argv[]);
  39. enum {
  40. T_THREAD,
  41. T_MUTEX,
  42. T_COND,
  43. T_SEM,
  44. };
  45. enum thread_status_t {
  46. THREAD_INIT,
  47. THREAD_RUNNING,
  48. THREAD_CANCELLED,
  49. THREAD_EXIT,
  50. };
  51. enum mutex_status_t {
  52. MUTEX_CREATED,
  53. MUTEX_DESTROYED,
  54. };
  55. enum cond_status_t {
  56. COND_CREATED,
  57. COND_DESTROYED,
  58. };
  59. enum sem_status_t {
  60. SEM_CREATED,
  61. SEM_CLOSED,
  62. SEM_DESTROYED,
  63. };
  64. typedef struct ThreadKeyValueNode {
  65. bh_list_link l;
  66. wasm_exec_env_t exec_env;
  67. int32 thread_key_values[WAMR_PTHREAD_KEYS_MAX];
  68. } ThreadKeyValueNode;
  69. typedef struct KeyData {
  70. int32 destructor_func;
  71. bool is_created;
  72. } KeyData;
  73. typedef struct ClusterInfoNode {
  74. bh_list_link l;
  75. WASMCluster *cluster;
  76. HashMap *thread_info_map;
  77. /* Key data list */
  78. KeyData key_data_list[WAMR_PTHREAD_KEYS_MAX];
  79. korp_mutex key_data_list_lock;
  80. /* Every node contains the key value list for a thread */
  81. bh_list thread_list_head;
  82. bh_list *thread_list;
  83. } ClusterInfoNode;
  84. typedef struct ThreadInfoNode {
  85. wasm_exec_env_t parent_exec_env;
  86. wasm_exec_env_t exec_env;
  87. /* the id returned to app */
  88. uint32 handle;
  89. /* type can be [THREAD | MUTEX | CONDITION] */
  90. uint32 type;
  91. /* Thread status, this variable should be volatile
  92. as its value may be changed in different threads */
  93. volatile uint32 status;
  94. bool joinable;
  95. union {
  96. korp_tid thread;
  97. korp_mutex *mutex;
  98. korp_cond *cond;
  99. #if WASM_ENABLE_LIB_PTHREAD_SEMAPHORE != 0
  100. korp_sem *sem;
  101. #endif
  102. /* A copy of the thread return value */
  103. void *ret;
  104. } u;
  105. } ThreadInfoNode;
  106. typedef struct {
  107. ThreadInfoNode *info_node;
  108. /* table elem index of the app's entry function */
  109. uint32 elem_index;
  110. /* arg of the app's entry function */
  111. uint32 arg;
  112. wasm_module_inst_t module_inst;
  113. } ThreadRoutineArgs;
  114. typedef struct {
  115. uint32 handle;
  116. ThreadInfoNode *node;
  117. } SemCallbackArgs;
  118. static bh_list cluster_info_list;
  119. #if WASM_ENABLE_LIB_PTHREAD_SEMAPHORE != 0
  120. static HashMap *sem_info_map;
  121. #endif
  122. static korp_mutex thread_global_lock;
  123. static uint32 handle_id = 1;
  124. static void
  125. lib_pthread_destroy_callback(WASMCluster *cluster);
  126. static uint32
  127. thread_handle_hash(void *handle)
  128. {
  129. return (uint32)(uintptr_t)handle;
  130. }
  131. static bool
  132. thread_handle_equal(void *h1, void *h2)
  133. {
  134. return (uint32)(uintptr_t)h1 == (uint32)(uintptr_t)h2 ? true : false;
  135. }
  136. static void
  137. thread_info_destroy(void *node)
  138. {
  139. ThreadInfoNode *info_node = (ThreadInfoNode *)node;
  140. os_mutex_lock(&thread_global_lock);
  141. if (info_node->type == T_MUTEX) {
  142. if (info_node->status != MUTEX_DESTROYED)
  143. os_mutex_destroy(info_node->u.mutex);
  144. wasm_runtime_free(info_node->u.mutex);
  145. }
  146. else if (info_node->type == T_COND) {
  147. if (info_node->status != COND_DESTROYED)
  148. os_cond_destroy(info_node->u.cond);
  149. wasm_runtime_free(info_node->u.cond);
  150. }
  151. #if WASM_ENABLE_LIB_PTHREAD_SEMAPHORE != 0
  152. else if (info_node->type == T_SEM) {
  153. if (info_node->status != SEM_DESTROYED)
  154. os_sem_close(info_node->u.sem);
  155. }
  156. #endif
  157. wasm_runtime_free(info_node);
  158. os_mutex_unlock(&thread_global_lock);
  159. }
  160. bool
  161. lib_pthread_init()
  162. {
  163. if (0 != os_mutex_init(&thread_global_lock))
  164. return false;
  165. bh_list_init(&cluster_info_list);
  166. if (!wasm_cluster_register_destroy_callback(lib_pthread_destroy_callback)) {
  167. os_mutex_destroy(&thread_global_lock);
  168. return false;
  169. }
  170. #if WASM_ENABLE_LIB_PTHREAD_SEMAPHORE != 0
  171. if (!(sem_info_map = bh_hash_map_create(
  172. 32, true, (HashFunc)wasm_string_hash,
  173. (KeyEqualFunc)wasm_string_equal, NULL, thread_info_destroy))) {
  174. os_mutex_destroy(&thread_global_lock);
  175. return false;
  176. }
  177. #endif
  178. return true;
  179. }
  180. void
  181. lib_pthread_destroy()
  182. {
  183. #if WASM_ENABLE_LIB_PTHREAD_SEMAPHORE != 0
  184. bh_hash_map_destroy(sem_info_map);
  185. #endif
  186. os_mutex_destroy(&thread_global_lock);
  187. }
  188. static ClusterInfoNode *
  189. get_cluster_info(WASMCluster *cluster)
  190. {
  191. ClusterInfoNode *node;
  192. os_mutex_lock(&thread_global_lock);
  193. node = bh_list_first_elem(&cluster_info_list);
  194. while (node) {
  195. if (cluster == node->cluster) {
  196. os_mutex_unlock(&thread_global_lock);
  197. return node;
  198. }
  199. node = bh_list_elem_next(node);
  200. }
  201. os_mutex_unlock(&thread_global_lock);
  202. return NULL;
  203. }
  204. static KeyData *
  205. key_data_list_lookup(wasm_exec_env_t exec_env, int32 key)
  206. {
  207. ClusterInfoNode *node;
  208. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  209. if ((node = get_cluster_info(cluster))) {
  210. return (key >= 0 && key < WAMR_PTHREAD_KEYS_MAX
  211. && node->key_data_list[key].is_created)
  212. ? &(node->key_data_list[key])
  213. : NULL;
  214. }
  215. return NULL;
  216. }
  217. /**
  218. * Lookup the thread key value node for a thread, create a new one if failed
  219. * This design will reduce the memory usage. If the thread doesn't use the
  220. * local storage, it will not occupy memory space.
  221. */
  222. static int32 *
  223. key_value_list_lookup_or_create(wasm_exec_env_t exec_env, ClusterInfoNode *info,
  224. int32 key)
  225. {
  226. KeyData *key_node;
  227. ThreadKeyValueNode *data;
  228. /* Check if the key is valid */
  229. key_node = key_data_list_lookup(exec_env, key);
  230. if (!key_node) {
  231. return NULL;
  232. }
  233. /* Find key values node */
  234. data = bh_list_first_elem(info->thread_list);
  235. while (data) {
  236. if (data->exec_env == exec_env)
  237. return data->thread_key_values;
  238. data = bh_list_elem_next(data);
  239. }
  240. /* If not found, create a new node for this thread */
  241. if (!(data = wasm_runtime_malloc(sizeof(ThreadKeyValueNode))))
  242. return NULL;
  243. memset(data, 0, sizeof(ThreadKeyValueNode));
  244. data->exec_env = exec_env;
  245. if (bh_list_insert(info->thread_list, data) != 0) {
  246. wasm_runtime_free(data);
  247. return NULL;
  248. }
  249. return data->thread_key_values;
  250. }
  251. static void
  252. call_key_destructor(wasm_exec_env_t exec_env)
  253. {
  254. int32 i;
  255. uint32 destructor_index;
  256. KeyData *key_node;
  257. ThreadKeyValueNode *value_node;
  258. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  259. ClusterInfoNode *info = get_cluster_info(cluster);
  260. if (!info) {
  261. return;
  262. }
  263. value_node = bh_list_first_elem(info->thread_list);
  264. while (value_node) {
  265. if (value_node->exec_env == exec_env)
  266. break;
  267. value_node = bh_list_elem_next(value_node);
  268. }
  269. /* This thread hasn't created key value node */
  270. if (!value_node)
  271. return;
  272. /* Destroy key values */
  273. for (i = 0; i < WAMR_PTHREAD_KEYS_MAX; i++) {
  274. if (value_node->thread_key_values[i] != 0) {
  275. int32 value = value_node->thread_key_values[i];
  276. os_mutex_lock(&info->key_data_list_lock);
  277. if ((key_node = key_data_list_lookup(exec_env, i)))
  278. destructor_index = key_node->destructor_func;
  279. else
  280. destructor_index = 0;
  281. os_mutex_unlock(&info->key_data_list_lock);
  282. /* reset key value */
  283. value_node->thread_key_values[i] = 0;
  284. /* Call the destructor func provided by app */
  285. if (destructor_index) {
  286. uint32 argv[1];
  287. argv[0] = value;
  288. wasm_runtime_call_indirect(exec_env, destructor_index, 1, argv);
  289. }
  290. }
  291. }
  292. bh_list_remove(info->thread_list, value_node);
  293. wasm_runtime_free(value_node);
  294. }
  295. static void
  296. destroy_thread_key_value_list(bh_list *list)
  297. {
  298. ThreadKeyValueNode *node, *next;
  299. /* There should be only one node for main thread */
  300. bh_assert(list->len <= 1);
  301. if (list->len) {
  302. node = bh_list_first_elem(list);
  303. while (node) {
  304. next = bh_list_elem_next(node);
  305. call_key_destructor(node->exec_env);
  306. node = next;
  307. }
  308. }
  309. }
  310. static ClusterInfoNode *
  311. create_cluster_info(WASMCluster *cluster)
  312. {
  313. ClusterInfoNode *node;
  314. bh_list_status ret;
  315. if (!(node = wasm_runtime_malloc(sizeof(ClusterInfoNode)))) {
  316. return NULL;
  317. }
  318. memset(node, 0, sizeof(ClusterInfoNode));
  319. node->thread_list = &node->thread_list_head;
  320. ret = bh_list_init(node->thread_list);
  321. bh_assert(ret == BH_LIST_SUCCESS);
  322. if (os_mutex_init(&node->key_data_list_lock) != 0) {
  323. wasm_runtime_free(node);
  324. return NULL;
  325. }
  326. node->cluster = cluster;
  327. if (!(node->thread_info_map = bh_hash_map_create(
  328. 32, true, (HashFunc)thread_handle_hash,
  329. (KeyEqualFunc)thread_handle_equal, NULL, thread_info_destroy))) {
  330. os_mutex_destroy(&node->key_data_list_lock);
  331. wasm_runtime_free(node);
  332. return NULL;
  333. }
  334. os_mutex_lock(&thread_global_lock);
  335. ret = bh_list_insert(&cluster_info_list, node);
  336. bh_assert(ret == BH_LIST_SUCCESS);
  337. os_mutex_unlock(&thread_global_lock);
  338. (void)ret;
  339. return node;
  340. }
  341. static bool
  342. destroy_cluster_info(WASMCluster *cluster)
  343. {
  344. ClusterInfoNode *node = get_cluster_info(cluster);
  345. if (node) {
  346. bh_hash_map_destroy(node->thread_info_map);
  347. destroy_thread_key_value_list(node->thread_list);
  348. os_mutex_destroy(&node->key_data_list_lock);
  349. /* Remove from the cluster info list */
  350. os_mutex_lock(&thread_global_lock);
  351. bh_list_remove(&cluster_info_list, node);
  352. wasm_runtime_free(node);
  353. os_mutex_unlock(&thread_global_lock);
  354. return true;
  355. }
  356. return false;
  357. }
  358. static void
  359. lib_pthread_destroy_callback(WASMCluster *cluster)
  360. {
  361. destroy_cluster_info(cluster);
  362. }
  363. static void
  364. delete_thread_info_node(ThreadInfoNode *thread_info)
  365. {
  366. ClusterInfoNode *node;
  367. bool ret;
  368. WASMCluster *cluster = wasm_exec_env_get_cluster(thread_info->exec_env);
  369. if ((node = get_cluster_info(cluster))) {
  370. ret = bh_hash_map_remove(node->thread_info_map,
  371. (void *)(uintptr_t)thread_info->handle, NULL,
  372. NULL);
  373. (void)ret;
  374. }
  375. thread_info_destroy(thread_info);
  376. }
  377. static bool
  378. append_thread_info_node(ThreadInfoNode *thread_info)
  379. {
  380. ClusterInfoNode *node;
  381. WASMCluster *cluster = wasm_exec_env_get_cluster(thread_info->exec_env);
  382. if (!(node = get_cluster_info(cluster))) {
  383. if (!(node = create_cluster_info(cluster))) {
  384. return false;
  385. }
  386. }
  387. if (!bh_hash_map_insert(node->thread_info_map,
  388. (void *)(uintptr_t)thread_info->handle,
  389. thread_info)) {
  390. return false;
  391. }
  392. return true;
  393. }
  394. static ThreadInfoNode *
  395. get_thread_info(wasm_exec_env_t exec_env, uint32 handle)
  396. {
  397. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  398. ClusterInfoNode *info = get_cluster_info(cluster);
  399. if (!info) {
  400. return NULL;
  401. }
  402. return bh_hash_map_find(info->thread_info_map, (void *)(uintptr_t)handle);
  403. }
  404. static uint32
  405. allocate_handle()
  406. {
  407. uint32 id;
  408. os_mutex_lock(&thread_global_lock);
  409. id = handle_id++;
  410. os_mutex_unlock(&thread_global_lock);
  411. return id;
  412. }
  413. static void *
  414. pthread_start_routine(void *arg)
  415. {
  416. wasm_exec_env_t exec_env = (wasm_exec_env_t)arg;
  417. wasm_exec_env_t parent_exec_env;
  418. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  419. ThreadRoutineArgs *routine_args = exec_env->thread_arg;
  420. ThreadInfoNode *info_node = routine_args->info_node;
  421. uint32 argv[1];
  422. parent_exec_env = info_node->parent_exec_env;
  423. os_mutex_lock(&parent_exec_env->wait_lock);
  424. info_node->exec_env = exec_env;
  425. info_node->u.thread = exec_env->handle;
  426. if (!append_thread_info_node(info_node)) {
  427. delete_thread_info_node(info_node);
  428. os_cond_signal(&parent_exec_env->wait_cond);
  429. os_mutex_unlock(&parent_exec_env->wait_lock);
  430. return NULL;
  431. }
  432. info_node->status = THREAD_RUNNING;
  433. os_cond_signal(&parent_exec_env->wait_cond);
  434. os_mutex_unlock(&parent_exec_env->wait_lock);
  435. wasm_exec_env_set_thread_info(exec_env);
  436. argv[0] = routine_args->arg;
  437. if (!wasm_runtime_call_indirect(exec_env, routine_args->elem_index, 1,
  438. argv)) {
  439. if (wasm_runtime_get_exception(module_inst))
  440. wasm_cluster_spread_exception(exec_env);
  441. }
  442. /* destroy pthread key values */
  443. call_key_destructor(exec_env);
  444. wasm_runtime_free(routine_args);
  445. /* if the thread is joinable, store the result in its info node,
  446. if the other threads join this thread after exited, then we
  447. can return the stored result */
  448. if (!info_node->joinable) {
  449. delete_thread_info_node(info_node);
  450. }
  451. else {
  452. info_node->u.ret = (void *)(uintptr_t)argv[0];
  453. #ifdef OS_ENABLE_HW_BOUND_CHECK
  454. if (exec_env->suspend_flags.flags & 0x08)
  455. /* argv[0] isn't set after longjmp(1) to
  456. invoke_native_with_hw_bound_check */
  457. info_node->u.ret = exec_env->thread_ret_value;
  458. #endif
  459. /* Update node status after ret value was set */
  460. info_node->status = THREAD_EXIT;
  461. }
  462. return (void *)(uintptr_t)argv[0];
  463. }
  464. static int
  465. pthread_create_wrapper(wasm_exec_env_t exec_env,
  466. uint32 *thread, /* thread_handle */
  467. const void *attr, /* not supported */
  468. uint32 elem_index, /* entry function */
  469. uint32 arg) /* arguments buffer */
  470. {
  471. wasm_module_t module = get_module(exec_env);
  472. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  473. wasm_module_inst_t new_module_inst = NULL;
  474. ThreadInfoNode *info_node = NULL;
  475. ThreadRoutineArgs *routine_args = NULL;
  476. uint32 thread_handle;
  477. uint32 stack_size = 8192;
  478. int32 ret = -1;
  479. #if WASM_ENABLE_LIBC_WASI != 0
  480. WASIContext *wasi_ctx;
  481. #endif
  482. bh_assert(module);
  483. bh_assert(module_inst);
  484. #if WASM_ENABLE_INTERP != 0
  485. if (module_inst->module_type == Wasm_Module_Bytecode) {
  486. stack_size =
  487. ((WASMModuleInstance *)module_inst)->default_wasm_stack_size;
  488. }
  489. #endif
  490. #if WASM_ENABLE_AOT != 0
  491. if (module_inst->module_type == Wasm_Module_AoT) {
  492. stack_size =
  493. ((AOTModuleInstance *)module_inst)->default_wasm_stack_size;
  494. }
  495. #endif
  496. if (!(new_module_inst = wasm_runtime_instantiate_internal(
  497. module, true, stack_size, 0, NULL, 0)))
  498. return -1;
  499. /* Set custom_data to new module instance */
  500. wasm_runtime_set_custom_data_internal(
  501. new_module_inst, wasm_runtime_get_custom_data(module_inst));
  502. #if WASM_ENABLE_LIBC_WASI != 0
  503. wasi_ctx = get_wasi_ctx(module_inst);
  504. if (wasi_ctx)
  505. wasm_runtime_set_wasi_ctx(new_module_inst, wasi_ctx);
  506. #endif
  507. if (!(info_node = wasm_runtime_malloc(sizeof(ThreadInfoNode))))
  508. goto fail;
  509. memset(info_node, 0, sizeof(ThreadInfoNode));
  510. thread_handle = allocate_handle();
  511. info_node->parent_exec_env = exec_env;
  512. info_node->handle = thread_handle;
  513. info_node->type = T_THREAD;
  514. info_node->status = THREAD_INIT;
  515. info_node->joinable = true;
  516. if (!(routine_args = wasm_runtime_malloc(sizeof(ThreadRoutineArgs))))
  517. goto fail;
  518. routine_args->arg = arg;
  519. routine_args->elem_index = elem_index;
  520. routine_args->info_node = info_node;
  521. routine_args->module_inst = new_module_inst;
  522. os_mutex_lock(&exec_env->wait_lock);
  523. ret =
  524. wasm_cluster_create_thread(exec_env, new_module_inst, true,
  525. pthread_start_routine, (void *)routine_args);
  526. if (ret != 0) {
  527. os_mutex_unlock(&exec_env->wait_lock);
  528. goto fail;
  529. }
  530. /* Wait for the thread routine to assign the exec_env to
  531. thread_info_node, otherwise the exec_env in the thread
  532. info node may be NULL in the next pthread API call */
  533. os_cond_wait(&exec_env->wait_cond, &exec_env->wait_lock);
  534. os_mutex_unlock(&exec_env->wait_lock);
  535. if (thread)
  536. *thread = thread_handle;
  537. return 0;
  538. fail:
  539. if (new_module_inst)
  540. wasm_runtime_deinstantiate_internal(new_module_inst, true);
  541. if (info_node)
  542. wasm_runtime_free(info_node);
  543. if (routine_args)
  544. wasm_runtime_free(routine_args);
  545. return ret;
  546. }
  547. static int32
  548. pthread_join_wrapper(wasm_exec_env_t exec_env, uint32 thread,
  549. int32 retval_offset) /* void **retval */
  550. {
  551. uint32 *ret;
  552. int32 join_ret;
  553. void **retval;
  554. ThreadInfoNode *node;
  555. wasm_module_inst_t module_inst;
  556. wasm_exec_env_t target_exec_env;
  557. module_inst = get_module_inst(exec_env);
  558. /* validate addr, we can use current thread's
  559. module instance here as the memory is shared */
  560. if (!validate_app_addr(retval_offset, sizeof(int32))) {
  561. /* Join failed, but we don't want to terminate all threads,
  562. do not spread exception here */
  563. wasm_runtime_set_exception(module_inst, NULL);
  564. return -1;
  565. }
  566. retval = (void **)addr_app_to_native(retval_offset);
  567. node = get_thread_info(exec_env, thread);
  568. if (!node) {
  569. /* The thread has exited and not joinable, return 0 to app */
  570. return 0;
  571. }
  572. target_exec_env = node->exec_env;
  573. bh_assert(target_exec_env);
  574. if (node->status != THREAD_EXIT) {
  575. /* if the thread is still running, call the platforms join API */
  576. join_ret = wasm_cluster_join_thread(target_exec_env, (void **)&ret);
  577. }
  578. else {
  579. /* if the thread has exited, return stored results */
  580. /* this thread must be joinable, otherwise the
  581. info_node should be destroyed once exit */
  582. bh_assert(node->joinable);
  583. join_ret = 0;
  584. ret = node->u.ret;
  585. }
  586. if (retval_offset != 0)
  587. *(uint32 *)retval = (uint32)(uintptr_t)ret;
  588. return join_ret;
  589. }
  590. static int32
  591. pthread_detach_wrapper(wasm_exec_env_t exec_env, uint32 thread)
  592. {
  593. ThreadInfoNode *node;
  594. wasm_exec_env_t target_exec_env;
  595. node = get_thread_info(exec_env, thread);
  596. if (!node)
  597. return 0;
  598. node->joinable = false;
  599. target_exec_env = node->exec_env;
  600. bh_assert(target_exec_env != NULL);
  601. return wasm_cluster_detach_thread(target_exec_env);
  602. }
  603. static int32
  604. pthread_cancel_wrapper(wasm_exec_env_t exec_env, uint32 thread)
  605. {
  606. ThreadInfoNode *node;
  607. wasm_exec_env_t target_exec_env;
  608. node = get_thread_info(exec_env, thread);
  609. if (!node)
  610. return 0;
  611. node->status = THREAD_CANCELLED;
  612. node->joinable = false;
  613. target_exec_env = node->exec_env;
  614. bh_assert(target_exec_env != NULL);
  615. return wasm_cluster_cancel_thread(target_exec_env);
  616. }
  617. static int32
  618. pthread_self_wrapper(wasm_exec_env_t exec_env)
  619. {
  620. ThreadRoutineArgs *args = get_thread_arg(exec_env);
  621. /* If thread_arg is NULL, it's the exec_env of the main thread,
  622. return id 0 to app */
  623. if (!args)
  624. return 0;
  625. return args->info_node->handle;
  626. }
  627. /* emcc use __pthread_self rather than pthread_self */
  628. static int32
  629. __pthread_self_wrapper(wasm_exec_env_t exec_env)
  630. {
  631. return pthread_self_wrapper(exec_env);
  632. }
  633. static void
  634. pthread_exit_wrapper(wasm_exec_env_t exec_env, int32 retval_offset)
  635. {
  636. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  637. ThreadRoutineArgs *args = get_thread_arg(exec_env);
  638. /* Currently exit main thread is not allowed */
  639. if (!args)
  640. return;
  641. #if defined(OS_ENABLE_HW_BOUND_CHECK) && !defined(BH_PLATFORM_WINDOWS)
  642. /* If hardware bound check enabled, don't deinstantiate module inst
  643. and thread info node here for AoT module, as they will be freed
  644. in pthread_start_routine */
  645. if (exec_env->jmpbuf_stack_top) {
  646. wasm_cluster_exit_thread(exec_env, (void *)(uintptr_t)retval_offset);
  647. }
  648. #endif
  649. /* destroy pthread key values */
  650. call_key_destructor(exec_env);
  651. /* routine exit, destroy instance */
  652. wasm_runtime_deinstantiate_internal(module_inst, true);
  653. if (!args->info_node->joinable) {
  654. delete_thread_info_node(args->info_node);
  655. }
  656. else {
  657. args->info_node->u.ret = (void *)(uintptr_t)retval_offset;
  658. /* Update node status after ret value was set */
  659. args->info_node->status = THREAD_EXIT;
  660. }
  661. wasm_runtime_free(args);
  662. wasm_cluster_exit_thread(exec_env, (void *)(uintptr_t)retval_offset);
  663. }
  664. static int32
  665. pthread_mutex_init_wrapper(wasm_exec_env_t exec_env, uint32 *mutex, void *attr)
  666. {
  667. korp_mutex *pmutex;
  668. ThreadInfoNode *info_node;
  669. if (!(pmutex = wasm_runtime_malloc(sizeof(korp_mutex)))) {
  670. return -1;
  671. }
  672. if (os_mutex_init(pmutex) != 0) {
  673. goto fail1;
  674. }
  675. if (!(info_node = wasm_runtime_malloc(sizeof(ThreadInfoNode))))
  676. goto fail2;
  677. memset(info_node, 0, sizeof(ThreadInfoNode));
  678. info_node->exec_env = exec_env;
  679. info_node->handle = allocate_handle();
  680. info_node->type = T_MUTEX;
  681. info_node->u.mutex = pmutex;
  682. info_node->status = MUTEX_CREATED;
  683. if (!append_thread_info_node(info_node))
  684. goto fail3;
  685. /* Return the mutex handle to app */
  686. if (mutex)
  687. *(uint32 *)mutex = info_node->handle;
  688. return 0;
  689. fail3:
  690. delete_thread_info_node(info_node);
  691. fail2:
  692. os_mutex_destroy(pmutex);
  693. fail1:
  694. wasm_runtime_free(pmutex);
  695. return -1;
  696. }
  697. static int32
  698. pthread_mutex_lock_wrapper(wasm_exec_env_t exec_env, uint32 *mutex)
  699. {
  700. ThreadInfoNode *info_node = get_thread_info(exec_env, *mutex);
  701. if (!info_node || info_node->type != T_MUTEX)
  702. return -1;
  703. return os_mutex_lock(info_node->u.mutex);
  704. }
  705. static int32
  706. pthread_mutex_unlock_wrapper(wasm_exec_env_t exec_env, uint32 *mutex)
  707. {
  708. ThreadInfoNode *info_node = get_thread_info(exec_env, *mutex);
  709. if (!info_node || info_node->type != T_MUTEX)
  710. return -1;
  711. return os_mutex_unlock(info_node->u.mutex);
  712. }
  713. static int32
  714. pthread_mutex_destroy_wrapper(wasm_exec_env_t exec_env, uint32 *mutex)
  715. {
  716. int32 ret_val;
  717. ThreadInfoNode *info_node = get_thread_info(exec_env, *mutex);
  718. if (!info_node || info_node->type != T_MUTEX)
  719. return -1;
  720. ret_val = os_mutex_destroy(info_node->u.mutex);
  721. info_node->status = MUTEX_DESTROYED;
  722. delete_thread_info_node(info_node);
  723. return ret_val;
  724. }
  725. static int32
  726. pthread_cond_init_wrapper(wasm_exec_env_t exec_env, uint32 *cond, void *attr)
  727. {
  728. korp_cond *pcond;
  729. ThreadInfoNode *info_node;
  730. if (!(pcond = wasm_runtime_malloc(sizeof(korp_cond)))) {
  731. return -1;
  732. }
  733. if (os_cond_init(pcond) != 0) {
  734. goto fail1;
  735. }
  736. if (!(info_node = wasm_runtime_malloc(sizeof(ThreadInfoNode))))
  737. goto fail2;
  738. memset(info_node, 0, sizeof(ThreadInfoNode));
  739. info_node->exec_env = exec_env;
  740. info_node->handle = allocate_handle();
  741. info_node->type = T_COND;
  742. info_node->u.cond = pcond;
  743. info_node->status = COND_CREATED;
  744. if (!append_thread_info_node(info_node))
  745. goto fail3;
  746. /* Return the cond handle to app */
  747. if (cond)
  748. *(uint32 *)cond = info_node->handle;
  749. return 0;
  750. fail3:
  751. delete_thread_info_node(info_node);
  752. fail2:
  753. os_cond_destroy(pcond);
  754. fail1:
  755. wasm_runtime_free(pcond);
  756. return -1;
  757. }
  758. static int32
  759. pthread_cond_wait_wrapper(wasm_exec_env_t exec_env, uint32 *cond, uint32 *mutex)
  760. {
  761. ThreadInfoNode *cond_info_node, *mutex_info_node;
  762. cond_info_node = get_thread_info(exec_env, *cond);
  763. if (!cond_info_node || cond_info_node->type != T_COND)
  764. return -1;
  765. mutex_info_node = get_thread_info(exec_env, *mutex);
  766. if (!mutex_info_node || mutex_info_node->type != T_MUTEX)
  767. return -1;
  768. return os_cond_wait(cond_info_node->u.cond, mutex_info_node->u.mutex);
  769. }
  770. /**
  771. * Currently we don't support struct timespec in built-in libc,
  772. * so the pthread_cond_timedwait use useconds instead
  773. */
  774. static int32
  775. pthread_cond_timedwait_wrapper(wasm_exec_env_t exec_env, uint32 *cond,
  776. uint32 *mutex, uint64 useconds)
  777. {
  778. ThreadInfoNode *cond_info_node, *mutex_info_node;
  779. cond_info_node = get_thread_info(exec_env, *cond);
  780. if (!cond_info_node || cond_info_node->type != T_COND)
  781. return -1;
  782. mutex_info_node = get_thread_info(exec_env, *mutex);
  783. if (!mutex_info_node || mutex_info_node->type != T_MUTEX)
  784. return -1;
  785. return os_cond_reltimedwait(cond_info_node->u.cond,
  786. mutex_info_node->u.mutex, useconds);
  787. }
  788. static int32
  789. pthread_cond_signal_wrapper(wasm_exec_env_t exec_env, uint32 *cond)
  790. {
  791. ThreadInfoNode *info_node = get_thread_info(exec_env, *cond);
  792. if (!info_node || info_node->type != T_COND)
  793. return -1;
  794. return os_cond_signal(info_node->u.cond);
  795. }
  796. static int32
  797. pthread_cond_broadcast_wrapper(wasm_exec_env_t exec_env, uint32 *cond)
  798. {
  799. ThreadInfoNode *info_node = get_thread_info(exec_env, *cond);
  800. if (!info_node || info_node->type != T_COND)
  801. return -1;
  802. return os_cond_broadcast(info_node->u.cond);
  803. }
  804. static int32
  805. pthread_cond_destroy_wrapper(wasm_exec_env_t exec_env, uint32 *cond)
  806. {
  807. int32 ret_val;
  808. ThreadInfoNode *info_node = get_thread_info(exec_env, *cond);
  809. if (!info_node || info_node->type != T_COND)
  810. return -1;
  811. ret_val = os_cond_destroy(info_node->u.cond);
  812. info_node->status = COND_DESTROYED;
  813. delete_thread_info_node(info_node);
  814. return ret_val;
  815. }
  816. static int32
  817. pthread_key_create_wrapper(wasm_exec_env_t exec_env, int32 *key,
  818. int32 destructor_elem_index)
  819. {
  820. uint32 i;
  821. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  822. ClusterInfoNode *info = get_cluster_info(cluster);
  823. if (!info) {
  824. /* The user may call pthread_key_create in main thread,
  825. in this case the cluster info hasn't been created */
  826. if (!(info = create_cluster_info(cluster))) {
  827. return -1;
  828. }
  829. }
  830. os_mutex_lock(&info->key_data_list_lock);
  831. for (i = 0; i < WAMR_PTHREAD_KEYS_MAX; i++) {
  832. if (!info->key_data_list[i].is_created) {
  833. break;
  834. }
  835. }
  836. if (i == WAMR_PTHREAD_KEYS_MAX) {
  837. os_mutex_unlock(&info->key_data_list_lock);
  838. return -1;
  839. }
  840. info->key_data_list[i].destructor_func = destructor_elem_index;
  841. info->key_data_list[i].is_created = true;
  842. *key = i;
  843. os_mutex_unlock(&info->key_data_list_lock);
  844. return 0;
  845. }
  846. static int32
  847. pthread_setspecific_wrapper(wasm_exec_env_t exec_env, int32 key,
  848. int32 value_offset)
  849. {
  850. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  851. ClusterInfoNode *info = get_cluster_info(cluster);
  852. int32 *key_values;
  853. if (!info)
  854. return -1;
  855. os_mutex_lock(&info->key_data_list_lock);
  856. key_values = key_value_list_lookup_or_create(exec_env, info, key);
  857. if (!key_values) {
  858. os_mutex_unlock(&info->key_data_list_lock);
  859. return -1;
  860. }
  861. key_values[key] = value_offset;
  862. os_mutex_unlock(&info->key_data_list_lock);
  863. return 0;
  864. }
  865. static int32
  866. pthread_getspecific_wrapper(wasm_exec_env_t exec_env, int32 key)
  867. {
  868. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  869. ClusterInfoNode *info = get_cluster_info(cluster);
  870. int32 ret, *key_values;
  871. if (!info)
  872. return 0;
  873. os_mutex_lock(&info->key_data_list_lock);
  874. key_values = key_value_list_lookup_or_create(exec_env, info, key);
  875. if (!key_values) {
  876. os_mutex_unlock(&info->key_data_list_lock);
  877. return 0;
  878. }
  879. ret = key_values[key];
  880. os_mutex_unlock(&info->key_data_list_lock);
  881. return ret;
  882. }
  883. static int32
  884. pthread_key_delete_wrapper(wasm_exec_env_t exec_env, int32 key)
  885. {
  886. KeyData *data;
  887. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  888. ClusterInfoNode *info = get_cluster_info(cluster);
  889. if (!info)
  890. return -1;
  891. os_mutex_lock(&info->key_data_list_lock);
  892. data = key_data_list_lookup(exec_env, key);
  893. if (!data) {
  894. os_mutex_unlock(&info->key_data_list_lock);
  895. return -1;
  896. }
  897. memset(data, 0, sizeof(KeyData));
  898. os_mutex_unlock(&info->key_data_list_lock);
  899. return 0;
  900. }
  901. /**
  902. * Currently the memory allocator doesn't support alloc specific aligned
  903. * space, we wrap posix_memalign to simply malloc memory
  904. */
  905. static int32
  906. posix_memalign_wrapper(wasm_exec_env_t exec_env, void **memptr, int32 align,
  907. int32 size)
  908. {
  909. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  910. void *p = NULL;
  911. *((int32 *)memptr) = module_malloc(size, (void **)&p);
  912. if (!p)
  913. return -1;
  914. return 0;
  915. }
  916. #if WASM_ENABLE_LIB_PTHREAD_SEMAPHORE != 0
  917. static int32
  918. sem_open_wrapper(wasm_exec_env_t exec_env, const char *name, int32 oflags,
  919. int32 mode, int32 val)
  920. {
  921. korp_sem *psem = NULL;
  922. ThreadInfoNode *info_node = NULL;
  923. /**
  924. * For RTOS, global semaphore map is safe for share the same semaphore
  925. * between task/pthread.
  926. * For Unix like system, it's dedicated for multiple processes.
  927. */
  928. if ((info_node = bh_hash_map_find(sem_info_map, (void *)name))) {
  929. return info_node->handle;
  930. }
  931. if (!(psem = os_sem_open(name, oflags, mode, val))) {
  932. goto fail1;
  933. }
  934. if (!(info_node = wasm_runtime_malloc(sizeof(ThreadInfoNode))))
  935. goto fail2;
  936. memset(info_node, 0, sizeof(ThreadInfoNode));
  937. info_node->exec_env = exec_env;
  938. info_node->handle = allocate_handle();
  939. info_node->type = T_SEM;
  940. info_node->u.sem = psem;
  941. info_node->status = SEM_CREATED;
  942. if (!bh_hash_map_insert(sem_info_map, (void *)name, info_node))
  943. goto fail3;
  944. return info_node->handle;
  945. fail3:
  946. wasm_runtime_free(info_node);
  947. fail2:
  948. os_sem_close(psem);
  949. fail1:
  950. return -1;
  951. }
  952. void
  953. sem_fetch_cb(void *key, void *value, void *user_data)
  954. {
  955. (void)key;
  956. SemCallbackArgs *args = user_data;
  957. ThreadInfoNode *info_node = value;
  958. if (args->handle == info_node->handle && info_node->status == SEM_CREATED) {
  959. args->node = info_node;
  960. }
  961. }
  962. static int32
  963. sem_close_wrapper(wasm_exec_env_t exec_env, uint32 sem)
  964. {
  965. (void)exec_env;
  966. int ret = -1;
  967. SemCallbackArgs args = { sem, NULL };
  968. bh_hash_map_traverse(sem_info_map, sem_fetch_cb, &args);
  969. if (args.node) {
  970. ret = os_sem_close(args.node->u.sem);
  971. if (ret == 0) {
  972. args.node->status = SEM_CLOSED;
  973. }
  974. }
  975. return ret;
  976. }
  977. static int32
  978. sem_wait_wrapper(wasm_exec_env_t exec_env, uint32 sem)
  979. {
  980. (void)exec_env;
  981. SemCallbackArgs args = { sem, NULL };
  982. bh_hash_map_traverse(sem_info_map, sem_fetch_cb, &args);
  983. if (args.node) {
  984. return os_sem_wait(args.node->u.sem);
  985. }
  986. return -1;
  987. }
  988. static int32
  989. sem_trywait_wrapper(wasm_exec_env_t exec_env, uint32 sem)
  990. {
  991. (void)exec_env;
  992. SemCallbackArgs args = { sem, NULL };
  993. bh_hash_map_traverse(sem_info_map, sem_fetch_cb, &args);
  994. if (args.node) {
  995. return os_sem_trywait(args.node->u.sem);
  996. }
  997. return -1;
  998. }
  999. static int32
  1000. sem_post_wrapper(wasm_exec_env_t exec_env, uint32 sem)
  1001. {
  1002. (void)exec_env;
  1003. SemCallbackArgs args = { sem, NULL };
  1004. bh_hash_map_traverse(sem_info_map, sem_fetch_cb, &args);
  1005. if (args.node) {
  1006. return os_sem_post(args.node->u.sem);
  1007. }
  1008. return -1;
  1009. }
  1010. static int32
  1011. sem_getvalue_wrapper(wasm_exec_env_t exec_env, uint32 sem, int32 *sval)
  1012. {
  1013. int32 ret = -1;
  1014. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  1015. (void)exec_env;
  1016. SemCallbackArgs args = { sem, NULL };
  1017. if (validate_native_addr(sval, sizeof(int32))) {
  1018. bh_hash_map_traverse(sem_info_map, sem_fetch_cb, &args);
  1019. if (args.node) {
  1020. ret = os_sem_getvalue(args.node->u.sem, sval);
  1021. }
  1022. }
  1023. return ret;
  1024. }
  1025. static int32
  1026. sem_unlink_wrapper(wasm_exec_env_t exec_env, const char *name)
  1027. {
  1028. (void)exec_env;
  1029. int32 ret_val;
  1030. ThreadInfoNode *info_node = bh_hash_map_find(sem_info_map, (void *)name);
  1031. if (!info_node || info_node->type != T_SEM)
  1032. return -1;
  1033. if (info_node->status != SEM_CLOSED) {
  1034. ret_val = os_sem_close(info_node->u.sem);
  1035. if (ret_val != 0) {
  1036. return ret_val;
  1037. }
  1038. }
  1039. ret_val = os_sem_unlink(name);
  1040. if (ret_val == 0) {
  1041. bh_hash_map_remove(sem_info_map, (void *)name, NULL, NULL);
  1042. info_node->status = SEM_DESTROYED;
  1043. thread_info_destroy(info_node);
  1044. }
  1045. return ret_val;
  1046. }
  1047. #endif
  1048. /* clang-format off */
  1049. #define REG_NATIVE_FUNC(func_name, signature) \
  1050. { #func_name, func_name##_wrapper, signature, NULL }
  1051. /* clang-format on */
  1052. static NativeSymbol native_symbols_lib_pthread[] = {
  1053. REG_NATIVE_FUNC(pthread_create, "(**ii)i"),
  1054. REG_NATIVE_FUNC(pthread_join, "(ii)i"),
  1055. REG_NATIVE_FUNC(pthread_detach, "(i)i"),
  1056. REG_NATIVE_FUNC(pthread_cancel, "(i)i"),
  1057. REG_NATIVE_FUNC(pthread_self, "()i"),
  1058. REG_NATIVE_FUNC(__pthread_self, "()i"),
  1059. REG_NATIVE_FUNC(pthread_exit, "(i)"),
  1060. REG_NATIVE_FUNC(pthread_mutex_init, "(**)i"),
  1061. REG_NATIVE_FUNC(pthread_mutex_lock, "(*)i"),
  1062. REG_NATIVE_FUNC(pthread_mutex_unlock, "(*)i"),
  1063. REG_NATIVE_FUNC(pthread_mutex_destroy, "(*)i"),
  1064. REG_NATIVE_FUNC(pthread_cond_init, "(**)i"),
  1065. REG_NATIVE_FUNC(pthread_cond_wait, "(**)i"),
  1066. REG_NATIVE_FUNC(pthread_cond_timedwait, "(**I)i"),
  1067. REG_NATIVE_FUNC(pthread_cond_signal, "(*)i"),
  1068. REG_NATIVE_FUNC(pthread_cond_broadcast, "(*)i"),
  1069. REG_NATIVE_FUNC(pthread_cond_destroy, "(*)i"),
  1070. REG_NATIVE_FUNC(pthread_key_create, "(*i)i"),
  1071. REG_NATIVE_FUNC(pthread_setspecific, "(ii)i"),
  1072. REG_NATIVE_FUNC(pthread_getspecific, "(i)i"),
  1073. REG_NATIVE_FUNC(pthread_key_delete, "(i)i"),
  1074. REG_NATIVE_FUNC(posix_memalign, "(*ii)i"),
  1075. #if WASM_ENABLE_LIB_PTHREAD_SEMAPHORE != 0
  1076. REG_NATIVE_FUNC(sem_open, "($iii)i"),
  1077. REG_NATIVE_FUNC(sem_close, "(i)i"),
  1078. REG_NATIVE_FUNC(sem_wait, "(i)i"),
  1079. REG_NATIVE_FUNC(sem_trywait, "(i)i"),
  1080. REG_NATIVE_FUNC(sem_post, "(i)i"),
  1081. REG_NATIVE_FUNC(sem_getvalue, "(i*)i"),
  1082. REG_NATIVE_FUNC(sem_unlink, "($)i"),
  1083. #endif
  1084. };
  1085. uint32
  1086. get_lib_pthread_export_apis(NativeSymbol **p_lib_pthread_apis)
  1087. {
  1088. *p_lib_pthread_apis = native_symbols_lib_pthread;
  1089. return sizeof(native_symbols_lib_pthread) / sizeof(NativeSymbol);
  1090. }