wasm_shared_memory.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "bh_log.h"
  6. #include "wasm_shared_memory.h"
  7. static bh_list shared_memory_list_head;
  8. static bh_list *const shared_memory_list = &shared_memory_list_head;
  9. static korp_mutex shared_memory_list_lock;
  10. enum {
  11. S_WAITING, S_NOTIFIED
  12. };
  13. typedef struct AtomicWaitInfo {
  14. korp_mutex wait_list_lock;
  15. bh_list wait_list_head;
  16. bh_list *wait_list;
  17. } AtomicWaitInfo;
  18. typedef struct AtomicWaitNode {
  19. bh_list_link l;
  20. uint8 status;
  21. korp_mutex wait_lock;
  22. korp_cond wait_cond;
  23. } AtomicWaitNode;
  24. /* Atomic wait map */
  25. static HashMap *wait_map;
  26. static uint32
  27. wait_address_hash(void *address);
  28. static bool
  29. wait_address_equal(void *h1, void *h2);
  30. static void
  31. destroy_wait_info(void *wait_info);
  32. bool
  33. wasm_shared_memory_init()
  34. {
  35. if (os_mutex_init(&shared_memory_list_lock) != 0)
  36. return false;
  37. /* wait map not exists, create new map */
  38. if (!(wait_map =
  39. bh_hash_map_create(32, true,
  40. (HashFunc)wait_address_hash,
  41. (KeyEqualFunc)wait_address_equal,
  42. NULL, destroy_wait_info))) {
  43. os_mutex_destroy(&shared_memory_list_lock);
  44. return false;
  45. }
  46. return true;
  47. }
  48. void
  49. wasm_shared_memory_destroy()
  50. {
  51. os_mutex_destroy(&shared_memory_list_lock);
  52. if (wait_map) {
  53. bh_hash_map_destroy(wait_map);
  54. }
  55. }
  56. static WASMSharedMemNode*
  57. search_module(WASMModuleCommon *module)
  58. {
  59. WASMSharedMemNode *node;
  60. os_mutex_lock(&shared_memory_list_lock);
  61. node = bh_list_first_elem(shared_memory_list);
  62. while (node) {
  63. if (module == node->module) {
  64. os_mutex_unlock(&shared_memory_list_lock);
  65. return node;
  66. }
  67. node = bh_list_elem_next(node);
  68. }
  69. os_mutex_unlock(&shared_memory_list_lock);
  70. return NULL;
  71. }
  72. WASMSharedMemNode*
  73. wasm_module_get_shared_memory(WASMModuleCommon *module)
  74. {
  75. return search_module(module);
  76. }
  77. int32
  78. shared_memory_inc_reference(WASMModuleCommon *module)
  79. {
  80. WASMSharedMemNode *node = search_module(module);
  81. if (node) {
  82. os_mutex_lock(&node->lock);
  83. node->ref_count++;
  84. os_mutex_unlock(&node->lock);
  85. return node->ref_count;
  86. }
  87. return -1;
  88. }
  89. int32
  90. shared_memory_dec_reference(WASMModuleCommon *module)
  91. {
  92. WASMSharedMemNode *node = search_module(module);
  93. uint32 ref_count = 0;
  94. if (node) {
  95. os_mutex_lock(&node->lock);
  96. ref_count = --node->ref_count;
  97. os_mutex_unlock(&node->lock);
  98. if (ref_count == 0) {
  99. os_mutex_lock(&shared_memory_list_lock);
  100. bh_list_remove(shared_memory_list, node);
  101. os_mutex_unlock(&shared_memory_list_lock);
  102. os_mutex_destroy(&node->lock);
  103. wasm_runtime_free(node);
  104. }
  105. return ref_count;
  106. }
  107. return -1;
  108. }
  109. WASMMemoryInstanceCommon*
  110. shared_memory_get_memory_inst(WASMSharedMemNode *node)
  111. {
  112. return node->memory_inst;
  113. }
  114. WASMSharedMemNode*
  115. shared_memory_set_memory_inst(WASMModuleCommon *module,
  116. WASMMemoryInstanceCommon *memory)
  117. {
  118. WASMSharedMemNode *node;
  119. bh_list_status ret;
  120. if (!(node = wasm_runtime_malloc(sizeof(WASMSharedMemNode))))
  121. return NULL;
  122. node->module = module;
  123. node->memory_inst = memory;
  124. node->ref_count = 1;
  125. if (os_mutex_init(&node->lock) != 0) {
  126. wasm_runtime_free(node);
  127. return NULL;
  128. }
  129. os_mutex_lock(&shared_memory_list_lock);
  130. ret = bh_list_insert(shared_memory_list, node);
  131. bh_assert(ret == BH_LIST_SUCCESS);
  132. os_mutex_unlock(&shared_memory_list_lock);
  133. (void)ret;
  134. return node;
  135. }
  136. /* Atomics wait && notify APIs */
  137. static uint32
  138. wait_address_hash(void *address)
  139. {
  140. return (uint32)(uintptr_t)address;
  141. }
  142. static bool
  143. wait_address_equal(void *h1, void *h2)
  144. {
  145. return h1 == h2 ? true : false;
  146. }
  147. static bool
  148. is_wait_node_exists(bh_list *wait_list, AtomicWaitNode *node)
  149. {
  150. AtomicWaitNode *curr;
  151. curr = bh_list_first_elem(wait_list);
  152. while (curr) {
  153. if (curr == node) {
  154. return true;
  155. }
  156. curr = bh_list_elem_next(curr);
  157. }
  158. return false;
  159. }
  160. static uint32
  161. notify_wait_list(bh_list *wait_list, uint32 count)
  162. {
  163. AtomicWaitNode *node, *next;
  164. uint32 i, notify_count = count;
  165. if ((count == UINT32_MAX) || (count > wait_list->len))
  166. notify_count = wait_list->len;
  167. node = bh_list_first_elem(wait_list);
  168. if (!node)
  169. return 0;
  170. for (i = 0; i < notify_count; i++) {
  171. bh_assert(node);
  172. next = bh_list_elem_next(node);
  173. node->status = S_NOTIFIED;
  174. /* wakeup */
  175. os_cond_signal(&node->wait_cond);
  176. node = next;
  177. }
  178. return notify_count;
  179. }
  180. static AtomicWaitInfo *
  181. acquire_wait_info(void *address, bool create)
  182. {
  183. AtomicWaitInfo *wait_info = NULL;
  184. bh_list_status ret;
  185. wait_info = (AtomicWaitInfo *)
  186. bh_hash_map_find(wait_map, address);
  187. if (!create)
  188. return wait_info;
  189. /* No wait info on this address, create new info */
  190. if (!wait_info) {
  191. if (!(wait_info =
  192. (AtomicWaitInfo *)wasm_runtime_malloc(sizeof(AtomicWaitInfo))))
  193. return NULL;
  194. memset(wait_info, 0, sizeof(AtomicWaitInfo));
  195. /* init wait list */
  196. wait_info->wait_list = &wait_info->wait_list_head;
  197. ret = bh_list_init(wait_info->wait_list);
  198. bh_assert(ret == BH_LIST_SUCCESS);
  199. /* init wait list lock */
  200. if (0 != os_mutex_init(&wait_info->wait_list_lock)) {
  201. wasm_runtime_free(wait_info);
  202. return NULL;
  203. }
  204. if (!bh_hash_map_insert(wait_map, address,
  205. (void *)wait_info)) {
  206. os_mutex_destroy(&wait_info->wait_list_lock);
  207. wasm_runtime_free(wait_info);
  208. return NULL;
  209. }
  210. }
  211. bh_assert(wait_info);
  212. (void)ret;
  213. return wait_info;
  214. }
  215. static void
  216. destroy_wait_info(void *wait_info)
  217. {
  218. AtomicWaitNode *node, *next;
  219. if (wait_info) {
  220. node = bh_list_first_elem(((AtomicWaitInfo *)wait_info)->wait_list);
  221. while (node) {
  222. next = bh_list_elem_next(node);
  223. os_mutex_destroy(&node->wait_lock);
  224. os_cond_destroy(&node->wait_cond);
  225. wasm_runtime_free(node);
  226. node = next;
  227. }
  228. os_mutex_destroy(&((AtomicWaitInfo *)wait_info)->wait_list_lock);
  229. wasm_runtime_free(wait_info);
  230. }
  231. }
  232. static void
  233. release_wait_info(HashMap *wait_map,
  234. AtomicWaitInfo *wait_info, void *address)
  235. {
  236. if (wait_info->wait_list->len == 0) {
  237. bh_hash_map_remove(wait_map, address, NULL, NULL);
  238. destroy_wait_info(wait_info);
  239. }
  240. }
  241. uint32
  242. wasm_runtime_atomic_wait(WASMModuleInstanceCommon *module, void *address,
  243. uint64 expect, int64 timeout, bool wait64)
  244. {
  245. AtomicWaitInfo *wait_info;
  246. AtomicWaitNode *wait_node;
  247. bool check_ret, is_timeout;
  248. #if WASM_ENABLE_INTERP != 0
  249. if (module->module_type == Wasm_Module_Bytecode) {
  250. WASMModuleInstance *module_inst = (WASMModuleInstance *)module;
  251. /* Currently we have only one memory instance */
  252. if (!module_inst->memories[0]->is_shared) {
  253. wasm_runtime_set_exception(module, "wait on unshared memory");
  254. return -1;
  255. }
  256. }
  257. #endif
  258. #if WASM_ENABLE_AOT != 0
  259. if (module->module_type == Wasm_Module_AoT) {
  260. AOTModuleInstance *aot_inst = (AOTModuleInstance *)module;
  261. AOTMemoryInstance *aot_memory =
  262. ((AOTMemoryInstance **)aot_inst->memories.ptr)[0];
  263. /* Currently we have only one memory instance */
  264. if (!aot_memory->is_shared) {
  265. wasm_runtime_set_exception(module, "wait on unshared memory");
  266. return -1;
  267. }
  268. }
  269. #endif
  270. /* acquire the wait info, create new one if not exists */
  271. wait_info = acquire_wait_info(address, true);
  272. if (!wait_info) {
  273. wasm_runtime_set_exception(module, "failed to acquire wait_info");
  274. return -1;
  275. }
  276. os_mutex_lock(&wait_info->wait_list_lock);
  277. if ((!wait64 && *(uint32*)address != (uint32)expect)
  278. || (wait64 && *(uint64*)address != expect)) {
  279. os_mutex_unlock(&wait_info->wait_list_lock);
  280. return 1;
  281. }
  282. else {
  283. bh_list_status ret;
  284. if (!(wait_node = wasm_runtime_malloc(sizeof(AtomicWaitNode)))) {
  285. wasm_runtime_set_exception(module, "failed to create wait node");
  286. os_mutex_unlock(&wait_info->wait_list_lock);
  287. return -1;
  288. }
  289. memset(wait_node, 0, sizeof(AtomicWaitNode));
  290. if (0 != os_mutex_init(&wait_node->wait_lock)) {
  291. wasm_runtime_free(wait_node);
  292. os_mutex_unlock(&wait_info->wait_list_lock);
  293. return -1;
  294. }
  295. if (0 != os_cond_init(&wait_node->wait_cond)) {
  296. os_mutex_destroy(&wait_node->wait_lock);
  297. wasm_runtime_free(wait_node);
  298. os_mutex_unlock(&wait_info->wait_list_lock);
  299. return -1;
  300. }
  301. wait_node->status = S_WAITING;
  302. ret = bh_list_insert(wait_info->wait_list, wait_node);
  303. bh_assert(ret == BH_LIST_SUCCESS);
  304. (void)ret;
  305. }
  306. os_mutex_unlock(&wait_info->wait_list_lock);
  307. /* condition wait start */
  308. os_mutex_lock(&wait_node->wait_lock);
  309. if (timeout < 0)
  310. timeout = BHT_WAIT_FOREVER;
  311. os_cond_reltimedwait(&wait_node->wait_cond,
  312. &wait_node->wait_lock, timeout);
  313. os_mutex_unlock(&wait_node->wait_lock);
  314. /* Check the wait node status */
  315. os_mutex_lock(&wait_info->wait_list_lock);
  316. check_ret = is_wait_node_exists(wait_info->wait_list, wait_node);
  317. bh_assert(check_ret);
  318. is_timeout = wait_node->status == S_WAITING ? true : false;
  319. bh_list_remove(wait_info->wait_list, wait_node);
  320. os_mutex_destroy(&wait_node->wait_lock);
  321. os_cond_destroy(&wait_node->wait_cond);
  322. wasm_runtime_free(wait_node);
  323. os_mutex_unlock(&wait_info->wait_list_lock);
  324. release_wait_info(wait_map, wait_info, address);
  325. (void)check_ret;
  326. return is_timeout ? 2 : 0;
  327. }
  328. uint32
  329. wasm_runtime_atomic_notify(WASMModuleInstanceCommon *module,
  330. void *address, uint32 count)
  331. {
  332. uint32 notify_result;
  333. AtomicWaitInfo *wait_info;
  334. /* Nobody wait on this address */
  335. wait_info = acquire_wait_info(address, false);
  336. if (!wait_info)
  337. return 0;
  338. os_mutex_lock(&wait_info->wait_list_lock);
  339. notify_result = notify_wait_list(wait_info->wait_list, count);
  340. os_mutex_unlock(&wait_info->wait_list_lock);
  341. release_wait_info(wait_map, wait_info, address);
  342. return notify_result;
  343. }