ExtendedAtomicOps-arm64-windows.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. #include <intrin.h>
  2. static inline void atomic_thread_fence(memory_order_relaxed_t)
  3. {
  4. }
  5. static inline void atomic_thread_fence(memory_order_acquire_t)
  6. {
  7. __dmb(_ARM64_BARRIER_ISH);
  8. }
  9. static inline void atomic_thread_fence(memory_order_release_t)
  10. {
  11. __dmb(_ARM64_BARRIER_ISH);
  12. }
  13. static inline void atomic_thread_fence(memory_order_acq_rel_t)
  14. {
  15. __dmb(_ARM64_BARRIER_ISH);
  16. }
  17. static inline void atomic_thread_fence(int /* memory_order_seq_cst_t */)
  18. {
  19. __dmb(_ARM64_BARRIER_ISH);
  20. }
  21. static inline int atomic_load_explicit(const volatile int* p, memory_order_relaxed_t)
  22. {
  23. return __iso_volatile_load32(p);
  24. }
  25. static inline int atomic_load_explicit(const volatile int* p, memory_order_acquire_t)
  26. {
  27. int res = __iso_volatile_load32(p);
  28. __dmb(_ARM64_BARRIER_ISH);
  29. return res;
  30. }
  31. static inline int atomic_load_explicit(const volatile int* p, int /* memory_order_seq_cst_t */)
  32. {
  33. int res = __iso_volatile_load32(p);
  34. __dmb(_ARM64_BARRIER_ISH);
  35. return res;
  36. }
  37. static inline atomic_word atomic_load_explicit(const volatile atomic_word* p, memory_order_relaxed_t)
  38. {
  39. return __iso_volatile_load64(p);
  40. }
  41. static inline atomic_word atomic_load_explicit(const volatile atomic_word* p, memory_order_acquire_t)
  42. {
  43. atomic_word res = __iso_volatile_load64(p);
  44. __dmb(_ARM64_BARRIER_ISH);
  45. return res;
  46. }
  47. static inline atomic_word atomic_load_explicit(const volatile atomic_word* p, int /* memory_order_seq_cst_t */)
  48. {
  49. atomic_word res = __iso_volatile_load64(p);
  50. __dmb(_ARM64_BARRIER_ISH);
  51. return res;
  52. }
  53. static inline void atomic_store_explicit(volatile int* p, int v, memory_order_relaxed_t)
  54. {
  55. __iso_volatile_store32(p, v);
  56. }
  57. static inline void atomic_store_explicit(volatile int* p, int v, memory_order_release_t)
  58. {
  59. __dmb(_ARM64_BARRIER_ISH);
  60. __iso_volatile_store32(p, v);
  61. }
  62. static inline void atomic_store_explicit(volatile int* p, int v, int /* memory_order_seq_cst_t */)
  63. {
  64. __dmb(_ARM64_BARRIER_ISH);
  65. __iso_volatile_store32(p, v);
  66. __dmb(_ARM64_BARRIER_ISH);
  67. }
  68. static inline void atomic_store_explicit(volatile atomic_word* p, atomic_word v, memory_order_relaxed_t)
  69. {
  70. __iso_volatile_store64(p, v);
  71. }
  72. static inline void atomic_store_explicit(volatile atomic_word* p, atomic_word v, memory_order_release_t)
  73. {
  74. __dmb(_ARM64_BARRIER_ISH);
  75. __iso_volatile_store64(p, v);
  76. }
  77. static inline void atomic_store_explicit(volatile atomic_word* p, atomic_word v, int /* memory_order_seq_cst_t */)
  78. {
  79. __dmb(_ARM64_BARRIER_ISH);
  80. __iso_volatile_store64(p, v);
  81. __dmb(_ARM64_BARRIER_ISH);
  82. }
  83. static inline int atomic_exchange_explicit(volatile int* p, int v, memory_order_relaxed_t)
  84. {
  85. return _InterlockedExchange_nf(reinterpret_cast<volatile long*>(p), v);
  86. }
  87. static inline int atomic_exchange_explicit(volatile int* p, int v, memory_order_acquire_t)
  88. {
  89. return _InterlockedExchange_acq(reinterpret_cast<volatile long*>(p), v);
  90. }
  91. static inline int atomic_exchange_explicit(volatile int* p, int v, memory_order_release_t)
  92. {
  93. return _InterlockedExchange_rel(reinterpret_cast<volatile long*>(p), v);
  94. }
  95. static inline int atomic_exchange_explicit(volatile int* p, int v, memory_order_acq_rel_t)
  96. {
  97. return _InterlockedExchange(reinterpret_cast<volatile long*>(p), v);
  98. }
  99. static inline int atomic_exchange_explicit(volatile int* p, int v, int /* memory_order_seq_cst_t */)
  100. {
  101. return _InterlockedExchange(reinterpret_cast<volatile long*>(p), v);
  102. }
  103. static inline atomic_word atomic_exchange_explicit(volatile atomic_word* p, atomic_word v, memory_order_relaxed_t)
  104. {
  105. return _InterlockedExchange64_nf(p, v);
  106. }
  107. static inline atomic_word atomic_exchange_explicit(volatile atomic_word* p, atomic_word v, memory_order_acquire_t)
  108. {
  109. return _InterlockedExchange64_acq(p, v);
  110. }
  111. static inline atomic_word atomic_exchange_explicit(volatile atomic_word* p, atomic_word v, memory_order_release_t)
  112. {
  113. return _InterlockedExchange64_rel(p, v);
  114. }
  115. static inline atomic_word atomic_exchange_explicit(volatile atomic_word* p, atomic_word v, memory_order_acq_rel_t)
  116. {
  117. return _InterlockedExchange64(p, v);
  118. }
  119. static inline atomic_word atomic_exchange_explicit(volatile atomic_word* p, atomic_word v, int /* memory_order_seq_cst_t */)
  120. {
  121. return _InterlockedExchange64(p, v);
  122. }
  123. // atomic_compare_exchange_weak_explicit: can fail spuriously even if *p == *oldval
  124. #undef ATOMIC_CMP_XCHG
  125. #define ATOMIC_CMP_XCHG(valueType, p, oldval, newval, exchangeFunction) \
  126. do \
  127. { \
  128. valueType oldValue = *oldval; \
  129. valueType previousValue = exchangeFunction(p, newval, oldValue); \
  130. if (previousValue == oldValue) \
  131. return true; \
  132. \
  133. *oldval = previousValue; \
  134. return false; \
  135. } \
  136. while (false)
  137. static inline bool atomic_compare_exchange_weak_explicit(volatile int* p, int *oldval, int newval, memory_order_relaxed_t, memory_order_relaxed_t)
  138. {
  139. ATOMIC_CMP_XCHG(long, reinterpret_cast<volatile long*>(p), oldval, newval, _InterlockedCompareExchange_nf);
  140. }
  141. static inline bool atomic_compare_exchange_weak_explicit(volatile int* p, int *oldval, int newval, memory_order_acquire_t, memory_order_relaxed_t)
  142. {
  143. ATOMIC_CMP_XCHG(long, reinterpret_cast<volatile long*>(p), oldval, newval, _InterlockedCompareExchange_acq);
  144. }
  145. static inline bool atomic_compare_exchange_weak_explicit(volatile int* p, int *oldval, int newval, memory_order_release_t, memory_order_relaxed_t)
  146. {
  147. ATOMIC_CMP_XCHG(long, reinterpret_cast<volatile long*>(p), oldval, newval, _InterlockedCompareExchange_rel);
  148. }
  149. static inline bool atomic_compare_exchange_weak_explicit(volatile int* p, int *oldval, int newval, memory_order_acq_rel_t, memory_order_relaxed_t)
  150. {
  151. ATOMIC_CMP_XCHG(long, reinterpret_cast<volatile long*>(p), oldval, newval, _InterlockedCompareExchange);
  152. }
  153. static inline bool atomic_compare_exchange_weak_explicit(volatile int* p, int *oldval, int newval, int /* memory_order_seq_cst_t */, memory_order_relaxed_t)
  154. {
  155. ATOMIC_CMP_XCHG(long, reinterpret_cast<volatile long*>(p), oldval, newval, _InterlockedCompareExchange);
  156. }
  157. static inline bool atomic_compare_exchange_weak_explicit(volatile int* p, int *oldval, int newval, memory_order_acquire_t, memory_order_acquire_t)
  158. {
  159. ATOMIC_CMP_XCHG(long, reinterpret_cast<volatile long*>(p), oldval, newval, _InterlockedCompareExchange_acq);
  160. }
  161. static inline bool atomic_compare_exchange_weak_explicit(volatile int* p, int *oldval, int newval, memory_order_release_t, memory_order_release_t)
  162. {
  163. ATOMIC_CMP_XCHG(long, reinterpret_cast<volatile long*>(p), oldval, newval, _InterlockedCompareExchange_rel);
  164. }
  165. static inline bool atomic_compare_exchange_weak_explicit(volatile int* p, int *oldval, int newval, memory_order_acq_rel_t, memory_order_acq_rel_t)
  166. {
  167. ATOMIC_CMP_XCHG(long, reinterpret_cast<volatile long*>(p), oldval, newval, _InterlockedCompareExchange);
  168. }
  169. static inline bool atomic_compare_exchange_weak_explicit(volatile int* p, int *oldval, int newval, int /* memory_order_seq_cst_t */, int /* memory_order_seq_cst_t */)
  170. {
  171. ATOMIC_CMP_XCHG(long, reinterpret_cast<volatile long*>(p), oldval, newval, _InterlockedCompareExchange);
  172. }
  173. static inline bool atomic_compare_exchange_weak_explicit(volatile atomic_word* p, atomic_word *oldval, atomic_word newval, memory_order_relaxed_t, memory_order_relaxed_t)
  174. {
  175. ATOMIC_CMP_XCHG(atomic_word, p, oldval, newval, _InterlockedCompareExchange64_nf);
  176. }
  177. static inline bool atomic_compare_exchange_weak_explicit(volatile atomic_word* p, atomic_word *oldval, atomic_word newval, memory_order_acquire_t, memory_order_relaxed_t)
  178. {
  179. ATOMIC_CMP_XCHG(atomic_word, p, oldval, newval, _InterlockedCompareExchange64_acq);
  180. }
  181. static inline bool atomic_compare_exchange_weak_explicit(volatile atomic_word* p, atomic_word *oldval, atomic_word newval, memory_order_release_t, memory_order_relaxed_t)
  182. {
  183. ATOMIC_CMP_XCHG(atomic_word, p, oldval, newval, _InterlockedCompareExchange64_rel);
  184. }
  185. static inline bool atomic_compare_exchange_weak_explicit(volatile atomic_word* p, atomic_word *oldval, atomic_word newval, memory_order_acq_rel_t, memory_order_relaxed_t)
  186. {
  187. ATOMIC_CMP_XCHG(atomic_word, p, oldval, newval, _InterlockedCompareExchange64);
  188. }
  189. static inline bool atomic_compare_exchange_weak_explicit(volatile atomic_word* p, atomic_word *oldval, atomic_word newval, int /* memory_order_seq_cst_t */, memory_order_relaxed_t)
  190. {
  191. ATOMIC_CMP_XCHG(atomic_word, p, oldval, newval, _InterlockedCompareExchange64);
  192. }
  193. static inline bool atomic_compare_exchange_weak_explicit(volatile atomic_word* p, atomic_word *oldval, atomic_word newval, memory_order_acquire_t, memory_order_acquire_t)
  194. {
  195. ATOMIC_CMP_XCHG(atomic_word, p, oldval, newval, _InterlockedCompareExchange64_acq);
  196. }
  197. static inline bool atomic_compare_exchange_weak_explicit(volatile atomic_word* p, atomic_word *oldval, atomic_word newval, memory_order_release_t, memory_order_release_t)
  198. {
  199. ATOMIC_CMP_XCHG(atomic_word, p, oldval, newval, _InterlockedCompareExchange64_rel);
  200. }
  201. static inline bool atomic_compare_exchange_weak_explicit(volatile atomic_word* p, atomic_word *oldval, atomic_word newval, memory_order_acq_rel_t, memory_order_acq_rel_t)
  202. {
  203. ATOMIC_CMP_XCHG(atomic_word, p, oldval, newval, _InterlockedCompareExchange64);
  204. }
  205. static inline bool atomic_compare_exchange_weak_explicit(volatile atomic_word* p, atomic_word *oldval, atomic_word newval, int /* memory_order_seq_cst_t */, int /* memory_order_seq_cst_t */)
  206. {
  207. ATOMIC_CMP_XCHG(atomic_word, p, oldval, newval, _InterlockedCompareExchange64);
  208. }
  209. // atomic_compare_exchange_strong_explicit: does loop and only returns false if *p != *oldval
  210. static inline bool atomic_compare_exchange_strong_explicit(volatile int* p, int *oldval, int newval, memory_order_relaxed_t, memory_order_relaxed_t)
  211. {
  212. ATOMIC_CMP_XCHG(long, reinterpret_cast<volatile long*>(p), oldval, newval, _InterlockedCompareExchange_nf);
  213. }
  214. static inline bool atomic_compare_exchange_strong_explicit(volatile int* p, int *oldval, int newval, memory_order_acquire_t, memory_order_relaxed_t)
  215. {
  216. ATOMIC_CMP_XCHG(long, reinterpret_cast<volatile long*>(p), oldval, newval, _InterlockedCompareExchange_acq);
  217. }
  218. static inline bool atomic_compare_exchange_strong_explicit(volatile int* p, int *oldval, int newval, memory_order_release_t, memory_order_relaxed_t)
  219. {
  220. ATOMIC_CMP_XCHG(long, reinterpret_cast<volatile long*>(p), oldval, newval, _InterlockedCompareExchange_rel);
  221. }
  222. static inline bool atomic_compare_exchange_strong_explicit(volatile int* p, int *oldval, int newval, memory_order_acq_rel_t, memory_order_relaxed_t)
  223. {
  224. ATOMIC_CMP_XCHG(long, reinterpret_cast<volatile long*>(p), oldval, newval, _InterlockedCompareExchange);
  225. }
  226. static inline bool atomic_compare_exchange_strong_explicit(volatile int* p, int *oldval, int newval, int /* memory_order_seq_cst_t */, memory_order_relaxed_t)
  227. {
  228. ATOMIC_CMP_XCHG(long, reinterpret_cast<volatile long*>(p), oldval, newval, _InterlockedCompareExchange);
  229. }
  230. static inline bool atomic_compare_exchange_strong_explicit(volatile int* p, int *oldval, int newval, memory_order_acquire_t, memory_order_acquire_t)
  231. {
  232. ATOMIC_CMP_XCHG(long, reinterpret_cast<volatile long*>(p), oldval, newval, _InterlockedCompareExchange_acq);
  233. }
  234. static inline bool atomic_compare_exchange_strong_explicit(volatile int* p, int *oldval, int newval, memory_order_release_t, memory_order_release_t)
  235. {
  236. ATOMIC_CMP_XCHG(long, reinterpret_cast<volatile long*>(p), oldval, newval, _InterlockedCompareExchange_rel);
  237. }
  238. static inline bool atomic_compare_exchange_strong_explicit(volatile int* p, int *oldval, int newval, memory_order_acq_rel_t, memory_order_acq_rel_t)
  239. {
  240. ATOMIC_CMP_XCHG(long, reinterpret_cast<volatile long*>(p), oldval, newval, _InterlockedCompareExchange);
  241. }
  242. static inline bool atomic_compare_exchange_strong_explicit(volatile int* p, int *oldval, int newval, int /* memory_order_seq_cst_t */, int /* memory_order_seq_cst_t */)
  243. {
  244. ATOMIC_CMP_XCHG(long, reinterpret_cast<volatile long*>(p), oldval, newval, _InterlockedCompareExchange);
  245. }
  246. static inline bool atomic_compare_exchange_strong_explicit(volatile atomic_word* p, atomic_word *oldval, atomic_word newval, memory_order_relaxed_t, memory_order_relaxed_t)
  247. {
  248. ATOMIC_CMP_XCHG(atomic_word, p, oldval, newval, _InterlockedCompareExchange64_nf);
  249. }
  250. static inline bool atomic_compare_exchange_strong_explicit(volatile atomic_word* p, atomic_word *oldval, atomic_word newval, memory_order_acquire_t, memory_order_relaxed_t)
  251. {
  252. ATOMIC_CMP_XCHG(atomic_word, p, oldval, newval, _InterlockedCompareExchange64_acq);
  253. }
  254. static inline bool atomic_compare_exchange_strong_explicit(volatile atomic_word* p, atomic_word *oldval, atomic_word newval, memory_order_release_t, memory_order_relaxed_t)
  255. {
  256. ATOMIC_CMP_XCHG(atomic_word, p, oldval, newval, _InterlockedCompareExchange64_rel);
  257. }
  258. static inline bool atomic_compare_exchange_strong_explicit(volatile atomic_word* p, atomic_word *oldval, atomic_word newval, memory_order_acq_rel_t, memory_order_relaxed_t)
  259. {
  260. ATOMIC_CMP_XCHG(atomic_word, p, oldval, newval, _InterlockedCompareExchange64);
  261. }
  262. static inline bool atomic_compare_exchange_strong_explicit(volatile atomic_word* p, atomic_word *oldval, atomic_word newval, int /* memory_order_seq_cst_t */, memory_order_relaxed_t)
  263. {
  264. ATOMIC_CMP_XCHG(atomic_word, p, oldval, newval, _InterlockedCompareExchange64);
  265. }
  266. static inline bool atomic_compare_exchange_strong_explicit(volatile atomic_word* p, atomic_word *oldval, atomic_word newval, memory_order_acquire_t, memory_order_acquire_t)
  267. {
  268. ATOMIC_CMP_XCHG(atomic_word, p, oldval, newval, _InterlockedCompareExchange64_acq);
  269. }
  270. static inline bool atomic_compare_exchange_strong_explicit(volatile atomic_word* p, atomic_word *oldval, atomic_word newval, memory_order_release_t, memory_order_release_t)
  271. {
  272. ATOMIC_CMP_XCHG(atomic_word, p, oldval, newval, _InterlockedCompareExchange64_rel);
  273. }
  274. static inline bool atomic_compare_exchange_strong_explicit(volatile atomic_word* p, atomic_word *oldval, atomic_word newval, memory_order_acq_rel_t, memory_order_acq_rel_t)
  275. {
  276. ATOMIC_CMP_XCHG(atomic_word, p, oldval, newval, _InterlockedCompareExchange64);
  277. }
  278. static inline bool atomic_compare_exchange_strong_explicit(volatile atomic_word* p, atomic_word *oldval, atomic_word newval, int /* memory_order_seq_cst_t */, int /* memory_order_seq_cst_t */)
  279. {
  280. ATOMIC_CMP_XCHG(atomic_word, p, oldval, newval, _InterlockedCompareExchange64);
  281. }
  282. static inline int atomic_fetch_add_explicit(volatile int* p, int v, memory_order_relaxed_t)
  283. {
  284. return _InterlockedExchangeAdd_nf(reinterpret_cast<volatile long*>(p), v);
  285. }
  286. static inline int atomic_fetch_add_explicit(volatile int* p, int v, memory_order_acquire_t)
  287. {
  288. return _InterlockedExchangeAdd_acq(reinterpret_cast<volatile long*>(p), v);
  289. }
  290. static inline int atomic_fetch_add_explicit(volatile int* p, int v, memory_order_release_t)
  291. {
  292. return _InterlockedExchangeAdd_rel(reinterpret_cast<volatile long*>(p), v);
  293. }
  294. static inline int atomic_fetch_add_explicit(volatile int* p, int v, memory_order_acq_rel_t)
  295. {
  296. return _InterlockedExchangeAdd(reinterpret_cast<volatile long*>(p), v);
  297. }
  298. static inline int atomic_fetch_add_explicit(volatile int* p, int v, int /* memory_order_seq_cst_t */)
  299. {
  300. return _InterlockedExchangeAdd(reinterpret_cast<volatile long*>(p), v);
  301. }
  302. static inline atomic_word atomic_fetch_add_explicit(volatile atomic_word* p, atomic_word v, memory_order_relaxed_t)
  303. {
  304. return _InterlockedExchangeAdd64_nf(p, v);
  305. }
  306. static inline atomic_word atomic_fetch_add_explicit(volatile atomic_word* p, atomic_word v, memory_order_acquire_t)
  307. {
  308. return _InterlockedExchangeAdd64_acq(p, v);
  309. }
  310. static inline atomic_word atomic_fetch_add_explicit(volatile atomic_word* p, atomic_word v, memory_order_release_t)
  311. {
  312. return _InterlockedExchangeAdd64_rel(p, v);
  313. }
  314. static inline atomic_word atomic_fetch_add_explicit(volatile atomic_word* p, atomic_word v, memory_order_acq_rel_t)
  315. {
  316. return _InterlockedExchangeAdd64(p, v);
  317. }
  318. static inline atomic_word atomic_fetch_add_explicit(volatile atomic_word* p, atomic_word v, int /* memory_order_seq_cst_t */)
  319. {
  320. return _InterlockedExchangeAdd64(p, v);
  321. }
  322. static inline int atomic_fetch_sub_explicit(volatile int* p, int v, memory_order_relaxed_t)
  323. {
  324. return _InterlockedExchangeAdd_nf(reinterpret_cast<volatile long*>(p), -v);
  325. }
  326. static inline int atomic_fetch_sub_explicit(volatile int* p, int v, memory_order_acquire_t)
  327. {
  328. return _InterlockedExchangeAdd_acq(reinterpret_cast<volatile long*>(p), -v);
  329. }
  330. static inline int atomic_fetch_sub_explicit(volatile int* p, int v, memory_order_release_t)
  331. {
  332. return _InterlockedExchangeAdd_rel(reinterpret_cast<volatile long*>(p), -v);
  333. }
  334. static inline int atomic_fetch_sub_explicit(volatile int* p, int v, memory_order_acq_rel_t)
  335. {
  336. return _InterlockedExchangeAdd(reinterpret_cast<volatile long*>(p), -v);
  337. }
  338. static inline int atomic_fetch_sub_explicit(volatile int* p, int v, int /* memory_order_seq_cst_t */)
  339. {
  340. return _InterlockedExchangeAdd(reinterpret_cast<volatile long*>(p), -v);
  341. }
  342. static inline atomic_word atomic_fetch_sub_explicit(volatile atomic_word* p, atomic_word v, memory_order_relaxed_t)
  343. {
  344. return _InterlockedExchangeAdd64_nf(p, -v);
  345. }
  346. static inline atomic_word atomic_fetch_sub_explicit(volatile atomic_word* p, atomic_word v, memory_order_acquire_t)
  347. {
  348. return _InterlockedExchangeAdd64_acq(p, -v);
  349. }
  350. static inline atomic_word atomic_fetch_sub_explicit(volatile atomic_word* p, atomic_word v, memory_order_release_t)
  351. {
  352. return _InterlockedExchangeAdd64_rel(p, -v);
  353. }
  354. static inline atomic_word atomic_fetch_sub_explicit(volatile atomic_word* p, atomic_word v, memory_order_acq_rel_t)
  355. {
  356. return _InterlockedExchangeAdd64(p, -v);
  357. }
  358. static inline atomic_word atomic_fetch_sub_explicit(volatile atomic_word* p, atomic_word v, int /* memory_order_seq_cst_t */)
  359. {
  360. return _InterlockedExchangeAdd64(p, -v);
  361. }
  362. /*
  363. * extensions
  364. */
  365. static inline void atomic_retain(volatile int* p)
  366. {
  367. atomic_fetch_add_explicit(p, 1, memory_order_relaxed);
  368. }
  369. static inline bool atomic_release(volatile int* p)
  370. {
  371. bool res = atomic_fetch_sub_explicit(p, 1, memory_order_release) == 1;
  372. if (res)
  373. {
  374. atomic_thread_fence(memory_order_acquire);
  375. }
  376. return res;
  377. }
  378. /*
  379. * double word
  380. */
  381. static inline atomic_word2 atomic_load_explicit(const volatile atomic_word2* p, memory_order_relaxed_t)
  382. {
  383. atomic_word2 result = { 0, 0 };
  384. _InterlockedCompareExchange128_nf(/* _Destination */ const_cast<volatile atomic_word*>(&p->lo), /* _ExchangeHigh */ 0, /* _ExchangeLow */ 0, /* _ComparandResult */ &result.lo);
  385. return result;
  386. }
  387. static inline atomic_word2 atomic_load_explicit(const volatile atomic_word2* p, memory_order_acquire_t)
  388. {
  389. atomic_word2 result = { 0, 0 };
  390. _InterlockedCompareExchange128_acq(/* _Destination */ const_cast<volatile atomic_word*>(&p->lo), /* _ExchangeHigh */ 0, /* _ExchangeLow */ 0, /* _ComparandResult */ &result.lo);
  391. return result;
  392. }
  393. static inline void atomic_store_explicit(volatile atomic_word2* p, atomic_word2 v, memory_order_relaxed_t)
  394. {
  395. atomic_word2 comparand = v;
  396. while (!_InterlockedCompareExchange128_nf(/* _Destination */ &p->lo, /* _ExchangeHigh */ v.hi, /* _ExchangeLow */ v.lo, /* _ComparandResult */ &comparand.lo))
  397. {
  398. }
  399. }
  400. static inline void atomic_store_explicit(volatile atomic_word2* p, atomic_word2 v, memory_order_release_t)
  401. {
  402. atomic_word2 comparand = v;
  403. while (!_InterlockedCompareExchange128_rel(/* _Destination */ &p->lo, /* _ExchangeHigh */ v.hi, /* _ExchangeLow */ v.lo, /* _ComparandResult */ &comparand.lo))
  404. {
  405. }
  406. }
  407. static inline atomic_word2 atomic_exchange_explicit(volatile atomic_word2* p, atomic_word2 val, memory_order_acq_rel_t)
  408. {
  409. atomic_word2 comparand = val;
  410. while (!_InterlockedCompareExchange128(/* _Destination */ &p->lo, /* _ExchangeHigh */ val.hi, /* _ExchangeLow */ val.lo, /* _ComparandResult */ &comparand.lo))
  411. {
  412. }
  413. return comparand;
  414. }
  415. static inline bool atomic_compare_exchange_strong_explicit(volatile atomic_word2* p, atomic_word2* oldval, atomic_word2 newval, memory_order_acquire_t, memory_order_relaxed_t)
  416. {
  417. return _InterlockedCompareExchange128_acq(/* _Destination */ &p->lo, /* _ExchangeHigh */ newval.hi, /* _ExchangeLow */ newval.lo, /* _ComparandResult */ &oldval->lo);
  418. }
  419. static inline bool atomic_compare_exchange_strong_explicit(volatile atomic_word2* p, atomic_word2* oldval, atomic_word2 newval, memory_order_release_t, memory_order_relaxed_t)
  420. {
  421. return _InterlockedCompareExchange128_rel(/* _Destination */ &p->lo, /* _ExchangeHigh */ newval.hi, /* _ExchangeLow */ newval.lo, /* _ComparandResult */ &oldval->lo);
  422. }
  423. static inline bool atomic_compare_exchange_strong_explicit(volatile atomic_word2* p, atomic_word2* oldval, atomic_word2 newval, int /*memory_order_acq_rel_t*/, memory_order_relaxed_t)
  424. {
  425. return _InterlockedCompareExchange128(/* _Destination */ &p->lo, /* _ExchangeHigh */ newval.hi, /* _ExchangeLow */ newval.lo, /* _ComparandResult */ &oldval->lo);
  426. }
  427. template<class SuccOrder, class FailOrder>
  428. static inline bool atomic_compare_exchange_weak_explicit(volatile atomic_word2* p, atomic_word2* oldval, atomic_word2 newval, SuccOrder o1, FailOrder o2)
  429. {
  430. // TODO: implement proper weak compare exchange
  431. return atomic_compare_exchange_strong_explicit(p, oldval, newval, o1, o2);
  432. }