tatomics.nim 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. # test atomic operations
  2. import atomics, bitops
  3. type
  4. Object = object
  5. val: int
  6. # Atomic operations for trivial objects
  7. block trivialLoad:
  8. var location: Atomic[int]
  9. location.store(1)
  10. doAssert location.load == 1
  11. location.store(2)
  12. doAssert location.load(moRelaxed) == 2
  13. location.store(3)
  14. doAssert location.load(moAcquire) == 3
  15. block trivialStore:
  16. var location: Atomic[int]
  17. location.store(1)
  18. doAssert location.load == 1
  19. location.store(2, moRelaxed)
  20. doAssert location.load == 2
  21. location.store(3, moRelease)
  22. doAssert location.load == 3
  23. block trivialExchange:
  24. var location: Atomic[int]
  25. location.store(1)
  26. doAssert location.exchange(2) == 1
  27. doAssert location.exchange(3, moRelaxed) == 2
  28. doAssert location.exchange(4, moAcquire) == 3
  29. doAssert location.exchange(5, moRelease) == 4
  30. doAssert location.exchange(6, moAcquireRelease) == 5
  31. doAssert location.load == 6
  32. block trivialCompareExchangeDoesExchange:
  33. var location: Atomic[int]
  34. var expected = 1
  35. location.store(1)
  36. doAssert location.compareExchange(expected, 2)
  37. doAssert expected == 1
  38. doAssert location.load == 2
  39. expected = 2
  40. doAssert location.compareExchange(expected, 3, moRelaxed)
  41. doAssert expected == 2
  42. doAssert location.load == 3
  43. expected = 3
  44. doAssert location.compareExchange(expected, 4, moAcquire)
  45. doAssert expected == 3
  46. doAssert location.load == 4
  47. expected = 4
  48. doAssert location.compareExchange(expected, 5, moRelease)
  49. doAssert expected == 4
  50. doAssert location.load == 5
  51. expected = 5
  52. doAssert location.compareExchange(expected, 6, moAcquireRelease)
  53. doAssert expected == 5
  54. doAssert location.load == 6
  55. block trivialCompareExchangeDoesNotExchange:
  56. var location: Atomic[int]
  57. var expected = 10
  58. location.store(1)
  59. doAssert not location.compareExchange(expected, 2)
  60. doAssert expected == 1
  61. doAssert location.load == 1
  62. expected = 10
  63. doAssert not location.compareExchange(expected, 3, moRelaxed)
  64. doAssert expected == 1
  65. doAssert location.load == 1
  66. expected = 10
  67. doAssert not location.compareExchange(expected, 4, moAcquire)
  68. doAssert expected == 1
  69. doAssert location.load == 1
  70. expected = 10
  71. doAssert not location.compareExchange(expected, 5, moRelease)
  72. doAssert expected == 1
  73. doAssert location.load == 1
  74. expected = 10
  75. doAssert not location.compareExchange(expected, 6, moAcquireRelease)
  76. doAssert expected == 1
  77. doAssert location.load == 1
  78. block trivialCompareExchangeSuccessFailureDoesExchange:
  79. var location: Atomic[int]
  80. var expected = 1
  81. location.store(1)
  82. doAssert location.compareExchange(expected, 2, moSequentiallyConsistent, moSequentiallyConsistent)
  83. doAssert expected == 1
  84. doAssert location.load == 2
  85. expected = 2
  86. doAssert location.compareExchange(expected, 3, moRelaxed, moRelaxed)
  87. doAssert expected == 2
  88. doAssert location.load == 3
  89. expected = 3
  90. doAssert location.compareExchange(expected, 4, moAcquire, moAcquire)
  91. doAssert expected == 3
  92. doAssert location.load == 4
  93. expected = 4
  94. doAssert location.compareExchange(expected, 5, moRelease, moRelease)
  95. doAssert expected == 4
  96. doAssert location.load == 5
  97. expected = 5
  98. doAssert location.compareExchange(expected, 6, moAcquireRelease, moAcquireRelease)
  99. doAssert expected == 5
  100. doAssert location.load == 6
  101. block trivialCompareExchangeSuccessFailureDoesNotExchange:
  102. var location: Atomic[int]
  103. var expected = 10
  104. location.store(1)
  105. doAssert not location.compareExchange(expected, 2, moSequentiallyConsistent, moSequentiallyConsistent)
  106. doAssert expected == 1
  107. doAssert location.load == 1
  108. expected = 10
  109. doAssert not location.compareExchange(expected, 3, moRelaxed, moRelaxed)
  110. doAssert expected == 1
  111. doAssert location.load == 1
  112. expected = 10
  113. doAssert not location.compareExchange(expected, 4, moAcquire, moAcquire)
  114. doAssert expected == 1
  115. doAssert location.load == 1
  116. expected = 10
  117. doAssert not location.compareExchange(expected, 5, moRelease, moRelease)
  118. doAssert expected == 1
  119. doAssert location.load == 1
  120. expected = 10
  121. doAssert not location.compareExchange(expected, 6, moAcquireRelease, moAcquireRelease)
  122. doAssert expected == 1
  123. doAssert location.load == 1
  124. block trivialCompareExchangeWeakDoesExchange:
  125. var location: Atomic[int]
  126. var expected = 1
  127. location.store(1)
  128. doAssert location.compareExchangeWeak(expected, 2)
  129. doAssert expected == 1
  130. doAssert location.load == 2
  131. expected = 2
  132. doAssert location.compareExchangeWeak(expected, 3, moRelaxed)
  133. doAssert expected == 2
  134. doAssert location.load == 3
  135. expected = 3
  136. doAssert location.compareExchangeWeak(expected, 4, moAcquire)
  137. doAssert expected == 3
  138. doAssert location.load == 4
  139. expected = 4
  140. doAssert location.compareExchangeWeak(expected, 5, moRelease)
  141. doAssert expected == 4
  142. doAssert location.load == 5
  143. expected = 5
  144. doAssert location.compareExchangeWeak(expected, 6, moAcquireRelease)
  145. doAssert expected == 5
  146. doAssert location.load == 6
  147. block trivialCompareExchangeWeakDoesNotExchange:
  148. var location: Atomic[int]
  149. var expected = 10
  150. location.store(1)
  151. doAssert not location.compareExchangeWeak(expected, 2)
  152. doAssert expected == 1
  153. doAssert location.load == 1
  154. expected = 10
  155. doAssert not location.compareExchangeWeak(expected, 3, moRelaxed)
  156. doAssert expected == 1
  157. doAssert location.load == 1
  158. expected = 10
  159. doAssert not location.compareExchangeWeak(expected, 4, moAcquire)
  160. doAssert expected == 1
  161. doAssert location.load == 1
  162. expected = 10
  163. doAssert not location.compareExchangeWeak(expected, 5, moRelease)
  164. doAssert expected == 1
  165. doAssert location.load == 1
  166. expected = 10
  167. doAssert not location.compareExchangeWeak(expected, 6, moAcquireRelease)
  168. doAssert expected == 1
  169. doAssert location.load == 1
  170. block trivialCompareExchangeWeakSuccessFailureDoesExchange:
  171. var location: Atomic[int]
  172. var expected = 1
  173. location.store(1)
  174. doAssert location.compareExchangeWeak(expected, 2, moSequentiallyConsistent, moSequentiallyConsistent)
  175. doAssert expected == 1
  176. doAssert location.load == 2
  177. expected = 2
  178. doAssert location.compareExchangeWeak(expected, 3, moRelaxed, moRelaxed)
  179. doAssert expected == 2
  180. doAssert location.load == 3
  181. expected = 3
  182. doAssert location.compareExchangeWeak(expected, 4, moAcquire, moAcquire)
  183. doAssert expected == 3
  184. doAssert location.load == 4
  185. expected = 4
  186. doAssert location.compareExchangeWeak(expected, 5, moRelease, moRelease)
  187. doAssert expected == 4
  188. doAssert location.load == 5
  189. expected = 5
  190. doAssert location.compareExchangeWeak(expected, 6, moAcquireRelease, moAcquireRelease)
  191. doAssert expected == 5
  192. doAssert location.load == 6
  193. block trivialCompareExchangeWeakSuccessFailureDoesNotExchange:
  194. var location: Atomic[int]
  195. var expected = 10
  196. location.store(1)
  197. doAssert not location.compareExchangeWeak(expected, 2, moSequentiallyConsistent, moSequentiallyConsistent)
  198. doAssert expected == 1
  199. doAssert location.load == 1
  200. expected = 10
  201. doAssert not location.compareExchangeWeak(expected, 3, moRelaxed, moRelaxed)
  202. doAssert expected == 1
  203. doAssert location.load == 1
  204. expected = 10
  205. doAssert not location.compareExchangeWeak(expected, 4, moAcquire, moAcquire)
  206. doAssert expected == 1
  207. doAssert location.load == 1
  208. expected = 10
  209. doAssert not location.compareExchangeWeak(expected, 5, moRelease, moRelease)
  210. doAssert expected == 1
  211. doAssert location.load == 1
  212. expected = 10
  213. doAssert not location.compareExchangeWeak(expected, 6, moAcquireRelease, moAcquireRelease)
  214. doAssert expected == 1
  215. doAssert location.load == 1
  216. # Atomic operations for non-trivial objects
  217. block objectLoad:
  218. var location: Atomic[Object]
  219. location.store(Object(val: 1))
  220. doAssert location.load == Object(val: 1)
  221. location.store(Object(val: 2))
  222. doAssert location.load(moRelaxed) == Object(val: 2)
  223. location.store(Object(val: 3))
  224. doAssert location.load(moAcquire) == Object(val: 3)
  225. block objectStore:
  226. var location: Atomic[Object]
  227. location.store(Object(val: 1))
  228. doAssert location.load == Object(val: 1)
  229. location.store(Object(val: 2), moRelaxed)
  230. doAssert location.load == Object(val: 2)
  231. location.store(Object(val: 3), moRelease)
  232. doAssert location.load == Object(val: 3)
  233. block objectExchange:
  234. var location: Atomic[Object]
  235. location.store(Object(val: 1))
  236. doAssert location.exchange(Object(val: 2)) == Object(val: 1)
  237. doAssert location.exchange(Object(val: 3), moRelaxed) == Object(val: 2)
  238. doAssert location.exchange(Object(val: 4), moAcquire) == Object(val: 3)
  239. doAssert location.exchange(Object(val: 5), moRelease) == Object(val: 4)
  240. doAssert location.exchange(Object(val: 6), moAcquireRelease) == Object(val: 5)
  241. doAssert location.load == Object(val: 6)
  242. block objectCompareExchangeDoesExchange:
  243. var location: Atomic[Object]
  244. var expected = Object(val: 1)
  245. location.store(Object(val: 1))
  246. doAssert location.compareExchange(expected, Object(val: 2))
  247. doAssert expected == Object(val: 1)
  248. doAssert location.load == Object(val: 2)
  249. expected = Object(val: 2)
  250. doAssert location.compareExchange(expected, Object(val: 3), moRelaxed)
  251. doAssert expected == Object(val: 2)
  252. doAssert location.load == Object(val: 3)
  253. expected = Object(val: 3)
  254. doAssert location.compareExchange(expected, Object(val: 4), moAcquire)
  255. doAssert expected == Object(val: 3)
  256. doAssert location.load == Object(val: 4)
  257. expected = Object(val: 4)
  258. doAssert location.compareExchange(expected, Object(val: 5), moRelease)
  259. doAssert expected == Object(val: 4)
  260. doAssert location.load == Object(val: 5)
  261. expected = Object(val: 5)
  262. doAssert location.compareExchange(expected, Object(val: 6), moAcquireRelease)
  263. doAssert expected == Object(val: 5)
  264. doAssert location.load == Object(val: 6)
  265. block objectCompareExchangeDoesNotExchange:
  266. var location: Atomic[Object]
  267. var expected = Object(val: 10)
  268. location.store(Object(val: 1))
  269. doAssert not location.compareExchange(expected, Object(val: 2))
  270. doAssert expected == Object(val: 1)
  271. doAssert location.load == Object(val: 1)
  272. expected = Object(val: 10)
  273. doAssert not location.compareExchange(expected, Object(val: 3), moRelaxed)
  274. doAssert expected == Object(val: 1)
  275. doAssert location.load == Object(val: 1)
  276. expected = Object(val: 10)
  277. doAssert not location.compareExchange(expected, Object(val: 4), moAcquire)
  278. doAssert expected == Object(val: 1)
  279. doAssert location.load == Object(val: 1)
  280. expected = Object(val: 10)
  281. doAssert not location.compareExchange(expected, Object(val: 5), moRelease)
  282. doAssert expected == Object(val: 1)
  283. doAssert location.load == Object(val: 1)
  284. expected = Object(val: 10)
  285. doAssert not location.compareExchange(expected, Object(val: 6), moAcquireRelease)
  286. doAssert expected == Object(val: 1)
  287. doAssert location.load == Object(val: 1)
  288. block objectCompareExchangeSuccessFailureDoesExchange:
  289. var location: Atomic[Object]
  290. var expected = Object(val: 1)
  291. location.store(Object(val: 1))
  292. doAssert location.compareExchange(expected, Object(val: 2), moSequentiallyConsistent, moSequentiallyConsistent)
  293. doAssert expected == Object(val: 1)
  294. doAssert location.load == Object(val: 2)
  295. expected = Object(val: 2)
  296. doAssert location.compareExchange(expected, Object(val: 3), moRelaxed, moRelaxed)
  297. doAssert expected == Object(val: 2)
  298. doAssert location.load == Object(val: 3)
  299. expected = Object(val: 3)
  300. doAssert location.compareExchange(expected, Object(val: 4), moAcquire, moAcquire)
  301. doAssert expected == Object(val: 3)
  302. doAssert location.load == Object(val: 4)
  303. expected = Object(val: 4)
  304. doAssert location.compareExchange(expected, Object(val: 5), moRelease, moRelease)
  305. doAssert expected == Object(val: 4)
  306. doAssert location.load == Object(val: 5)
  307. expected = Object(val: 5)
  308. doAssert location.compareExchange(expected, Object(val: 6), moAcquireRelease, moAcquireRelease)
  309. doAssert expected == Object(val: 5)
  310. doAssert location.load == Object(val: 6)
  311. block objectCompareExchangeSuccessFailureDoesNotExchange:
  312. var location: Atomic[Object]
  313. var expected = Object(val: 10)
  314. location.store(Object(val: 1))
  315. doAssert not location.compareExchange(expected, Object(val: 2), moSequentiallyConsistent, moSequentiallyConsistent)
  316. doAssert expected == Object(val: 1)
  317. doAssert location.load == Object(val: 1)
  318. expected = Object(val: 10)
  319. doAssert not location.compareExchange(expected, Object(val: 3), moRelaxed, moRelaxed)
  320. doAssert expected == Object(val: 1)
  321. doAssert location.load == Object(val: 1)
  322. expected = Object(val: 10)
  323. doAssert not location.compareExchange(expected, Object(val: 4), moAcquire, moAcquire)
  324. doAssert expected == Object(val: 1)
  325. doAssert location.load == Object(val: 1)
  326. expected = Object(val: 10)
  327. doAssert not location.compareExchange(expected, Object(val: 5), moRelease, moRelease)
  328. doAssert expected == Object(val: 1)
  329. doAssert location.load == Object(val: 1)
  330. expected = Object(val: 10)
  331. doAssert not location.compareExchange(expected, Object(val: 6), moAcquireRelease, moAcquireRelease)
  332. doAssert expected == Object(val: 1)
  333. doAssert location.load == Object(val: 1)
  334. block objectCompareExchangeWeakDoesExchange:
  335. var location: Atomic[Object]
  336. var expected = Object(val: 1)
  337. location.store(Object(val: 1))
  338. doAssert location.compareExchangeWeak(expected, Object(val: 2))
  339. doAssert expected == Object(val: 1)
  340. doAssert location.load == Object(val: 2)
  341. expected = Object(val: 2)
  342. doAssert location.compareExchangeWeak(expected, Object(val: 3), moRelaxed)
  343. doAssert expected == Object(val: 2)
  344. doAssert location.load == Object(val: 3)
  345. expected = Object(val: 3)
  346. doAssert location.compareExchangeWeak(expected, Object(val: 4), moAcquire)
  347. doAssert expected == Object(val: 3)
  348. doAssert location.load == Object(val: 4)
  349. expected = Object(val: 4)
  350. doAssert location.compareExchangeWeak(expected, Object(val: 5), moRelease)
  351. doAssert expected == Object(val: 4)
  352. doAssert location.load == Object(val: 5)
  353. expected = Object(val: 5)
  354. doAssert location.compareExchangeWeak(expected, Object(val: 6), moAcquireRelease)
  355. doAssert expected == Object(val: 5)
  356. doAssert location.load == Object(val: 6)
  357. block objectCompareExchangeWeakDoesNotExchange:
  358. var location: Atomic[Object]
  359. var expected = Object(val: 10)
  360. location.store(Object(val: 1))
  361. doAssert not location.compareExchangeWeak(expected, Object(val: 2))
  362. doAssert expected == Object(val: 1)
  363. doAssert location.load == Object(val: 1)
  364. expected = Object(val: 10)
  365. doAssert not location.compareExchangeWeak(expected, Object(val: 3), moRelaxed)
  366. doAssert expected == Object(val: 1)
  367. doAssert location.load == Object(val: 1)
  368. expected = Object(val: 10)
  369. doAssert not location.compareExchangeWeak(expected, Object(val: 4), moAcquire)
  370. doAssert expected == Object(val: 1)
  371. doAssert location.load == Object(val: 1)
  372. expected = Object(val: 10)
  373. doAssert not location.compareExchangeWeak(expected, Object(val: 5), moRelease)
  374. doAssert expected == Object(val: 1)
  375. doAssert location.load == Object(val: 1)
  376. expected = Object(val: 10)
  377. doAssert not location.compareExchangeWeak(expected, Object(val: 6), moAcquireRelease)
  378. doAssert expected == Object(val: 1)
  379. doAssert location.load == Object(val: 1)
  380. block objectCompareExchangeWeakSuccessFailureDoesExchange:
  381. var location: Atomic[Object]
  382. var expected = Object(val: 1)
  383. location.store(Object(val: 1))
  384. doAssert location.compareExchangeWeak(expected, Object(val: 2), moSequentiallyConsistent, moSequentiallyConsistent)
  385. doAssert expected == Object(val: 1)
  386. doAssert location.load == Object(val: 2)
  387. expected = Object(val: 2)
  388. doAssert location.compareExchangeWeak(expected, Object(val: 3), moRelaxed, moRelaxed)
  389. doAssert expected == Object(val: 2)
  390. doAssert location.load == Object(val: 3)
  391. expected = Object(val: 3)
  392. doAssert location.compareExchangeWeak(expected, Object(val: 4), moAcquire, moAcquire)
  393. doAssert expected == Object(val: 3)
  394. doAssert location.load == Object(val: 4)
  395. expected = Object(val: 4)
  396. doAssert location.compareExchangeWeak(expected, Object(val: 5), moRelease, moRelease)
  397. doAssert expected == Object(val: 4)
  398. doAssert location.load == Object(val: 5)
  399. expected = Object(val: 5)
  400. doAssert location.compareExchangeWeak(expected, Object(val: 6), moAcquireRelease, moAcquireRelease)
  401. doAssert expected == Object(val: 5)
  402. doAssert location.load == Object(val: 6)
  403. block objectCompareExchangeWeakSuccessFailureDoesNotExchange:
  404. var location: Atomic[Object]
  405. var expected = Object(val: 10)
  406. location.store(Object(val: 1))
  407. doAssert not location.compareExchangeWeak(expected, Object(val: 2), moSequentiallyConsistent, moSequentiallyConsistent)
  408. doAssert expected == Object(val: 1)
  409. doAssert location.load == Object(val: 1)
  410. expected = Object(val: 10)
  411. doAssert not location.compareExchangeWeak(expected, Object(val: 3), moRelaxed, moRelaxed)
  412. doAssert expected == Object(val: 1)
  413. doAssert location.load == Object(val: 1)
  414. expected = Object(val: 10)
  415. doAssert not location.compareExchangeWeak(expected, Object(val: 4), moAcquire, moAcquire)
  416. doAssert expected == Object(val: 1)
  417. doAssert location.load == Object(val: 1)
  418. expected = Object(val: 10)
  419. doAssert not location.compareExchangeWeak(expected, Object(val: 5), moRelease, moRelease)
  420. doAssert expected == Object(val: 1)
  421. doAssert location.load == Object(val: 1)
  422. expected = Object(val: 10)
  423. doAssert not location.compareExchangeWeak(expected, Object(val: 6), moAcquireRelease, moAcquireRelease)
  424. doAssert expected == Object(val: 1)
  425. doAssert location.load == Object(val: 1)
  426. # Numerical operations
  427. block fetchAdd:
  428. var location: Atomic[int]
  429. doAssert location.fetchAdd(1) == 0
  430. doAssert location.fetchAdd(1, moRelaxed) == 1
  431. doAssert location.fetchAdd(1, moRelease) == 2
  432. doAssert location.load == 3
  433. block fetchSub:
  434. var location: Atomic[int]
  435. doAssert location.fetchSub(1) == 0
  436. doAssert location.fetchSub(1, moRelaxed) == -1
  437. doAssert location.fetchSub(1, moRelease) == -2
  438. doAssert location.load == -3
  439. block fetchAnd:
  440. var location: Atomic[int]
  441. for i in 0..16:
  442. for j in 0..16:
  443. location.store(i)
  444. doAssert(location.fetchAnd(j) == i)
  445. doAssert(location.load == i.bitand(j))
  446. for i in 0..16:
  447. for j in 0..16:
  448. location.store(i)
  449. doAssert(location.fetchAnd(j, moRelaxed) == i)
  450. doAssert(location.load == i.bitand(j))
  451. for i in 0..16:
  452. for j in 0..16:
  453. location.store(i)
  454. doAssert(location.fetchAnd(j, moRelease) == i)
  455. doAssert(location.load == i.bitand(j))
  456. block fetchOr:
  457. var location: Atomic[int]
  458. for i in 0..16:
  459. for j in 0..16:
  460. location.store(i)
  461. doAssert(location.fetchOr(j) == i)
  462. doAssert(location.load == i.bitor(j))
  463. for i in 0..16:
  464. for j in 0..16:
  465. location.store(i)
  466. doAssert(location.fetchOr(j, moRelaxed) == i)
  467. doAssert(location.load == i.bitor(j))
  468. for i in 0..16:
  469. for j in 0..16:
  470. location.store(i)
  471. doAssert(location.fetchOr(j, moRelease) == i)
  472. doAssert(location.load == i.bitor(j))
  473. block fetchXor:
  474. var location: Atomic[int]
  475. for i in 0..16:
  476. for j in 0..16:
  477. location.store(i)
  478. doAssert(location.fetchXor(j) == i)
  479. doAssert(location.load == i.bitxor(j))
  480. for i in 0..16:
  481. for j in 0..16:
  482. location.store(i)
  483. doAssert(location.fetchXor(j, moRelaxed) == i)
  484. doAssert(location.load == i.bitxor(j))
  485. for i in 0..16:
  486. for j in 0..16:
  487. location.store(i)
  488. doAssert(location.fetchXor(j, moRelease) == i)
  489. doAssert(location.load == i.bitxor(j))
  490. block atomicInc:
  491. var location: Atomic[int]
  492. location.atomicInc
  493. doAssert location.load == 1
  494. location.atomicInc(1)
  495. doAssert location.load == 2
  496. location += 1
  497. doAssert location.load == 3
  498. block atomicDec:
  499. var location: Atomic[int]
  500. location.atomicDec
  501. doAssert location.load == -1
  502. location.atomicDec(1)
  503. doAssert location.load == -2
  504. location -= 1
  505. doAssert location.load == -3
  506. # Flag operations
  507. block testAndSet:
  508. var location: AtomicFlag
  509. doAssert not location.testAndSet
  510. doAssert location.testAndSet
  511. doAssert location.testAndSet
  512. location.clear()
  513. doAssert not location.testAndSet(moRelaxed)
  514. doAssert location.testAndSet(moRelaxed)
  515. doAssert location.testAndSet(moRelaxed)
  516. location.clear()
  517. doAssert not location.testAndSet(moRelease)
  518. doAssert location.testAndSet(moRelease)
  519. doAssert location.testAndSet(moRelease)
  520. block clear:
  521. var location: AtomicFlag
  522. discard location.testAndSet
  523. location.clear
  524. doAssert not location.testAndSet
  525. location.clear(moRelaxed)
  526. doAssert not location.testAndSet
  527. location.clear(moRelease)
  528. doAssert not location.testAndSet