tatomics.nim 21 KB

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