tst-atomic.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /* Tests for atomic.h macros.
  2. Copyright (C) 2003-2015 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <stdio.h>
  17. #include <atomic.h>
  18. #ifndef atomic_t
  19. # define atomic_t int
  20. #endif
  21. #define CHK2(f,a1,a2,rv,new_mem) \
  22. retval = f(&mem, a1, a2); \
  23. if (retval != rv) { \
  24. printf("%s(mem, %lu, %lu): retval %lu != expected %lu\n", \
  25. #f, a1, a2, retval, rv); \
  26. ret = 1; \
  27. } \
  28. if (mem != new_mem) { \
  29. printf("%s(mem, %lu, %lu): mem %lu != expected %lu\n", \
  30. #f, a1, a2, mem, new_mem); \
  31. ret = 1; \
  32. }
  33. #define CHK1(f,a1,rv,new_mem) \
  34. retval = f(&mem, a1); \
  35. if (retval != rv) { \
  36. printf("%s(mem, %lu): retval %lu != expected %lu\n", \
  37. #f, a1, retval, rv); \
  38. ret = 1; \
  39. } \
  40. if (mem != new_mem) { \
  41. printf("%s(mem, %lu): mem %lu != expected %lu\n", \
  42. #f, a1, mem, new_mem); \
  43. ret = 1; \
  44. }
  45. #define CHK0(f,rv,new_mem) \
  46. retval = f(&mem); \
  47. if (retval != rv) { \
  48. printf("%s(mem): retval %lu != expected %lu\n", \
  49. #f, retval, rv); \
  50. ret = 1; \
  51. } \
  52. if (mem != new_mem) { \
  53. printf("%s(mem): mem %lu != expected %lu\n", \
  54. #f, mem, new_mem); \
  55. ret = 1; \
  56. }
  57. /* Test various atomic.h macros. */
  58. static int
  59. do_test (void)
  60. {
  61. atomic_t mem, expected, retval;
  62. int ret = 0;
  63. #ifdef atomic_compare_and_exchange_val_acq
  64. mem = 24;
  65. CHK2(atomic_compare_and_exchange_val_acq, 35, 24, 24, 35);
  66. mem = 12;
  67. CHK2(atomic_compare_and_exchange_val_acq, 10, 15, 12, 12);
  68. mem = -15;
  69. CHK2(atomic_compare_and_exchange_val_acq, -56, -15, -15, -56);
  70. mem = -1;
  71. CHK2(atomic_compare_and_exchange_val_acq, 17, 0, -1, -1);
  72. #endif
  73. mem = 24;
  74. CHK2(atomic_compare_and_exchange_bool_acq, 35, 24, 0, 35);
  75. mem = 12;
  76. CHK2(atomic_compare_and_exchange_bool_acq, 10, 15, 1, 12);
  77. mem = -15;
  78. CHK2(atomic_compare_and_exchange_bool_acq, -56, -15, 0, -56);
  79. mem = -1;
  80. CHK2(atomic_compare_and_exchange_bool_acq, 17, 0, 1, -1);
  81. mem = 64;
  82. CHK1(atomic_exchange_acq, 31, 64, 31);
  83. mem = 2;
  84. CHK1(atomic_exchange_and_add, 11, 2, 13);
  85. mem = 2;
  86. CHK1(atomic_exchange_and_add_acq, 11, 2, 13);
  87. mem = 2;
  88. CHK1(atomic_exchange_and_add_rel, 11, 2, 13);
  89. mem = -21;
  90. atomic_add (&mem, 22);
  91. if (mem != 1)
  92. {
  93. printf ("atomic_add(mem, 22): mem %lu != expected 1\n", mem);
  94. ret = 1;
  95. }
  96. mem = -1;
  97. atomic_increment (&mem);
  98. if (mem != 0)
  99. {
  100. printf ("atomic_increment(mem): mem %lu != expected 0\n", mem);
  101. ret = 1;
  102. }
  103. mem = 2;
  104. if ((retval = atomic_increment_val (&mem)) != 3)
  105. {
  106. printf ("atomic_increment_val(mem): retval %lu != expected 3\n", retval);
  107. ret = 1;
  108. }
  109. mem = 0;
  110. CHK0(atomic_increment_and_test, 0, 1);
  111. mem = 35;
  112. CHK0(atomic_increment_and_test, 0, 36);
  113. mem = -1;
  114. CHK0(atomic_increment_and_test, 1, 0);
  115. mem = 17;
  116. atomic_decrement (&mem);
  117. if (mem != 16)
  118. {
  119. printf ("atomic_increment(mem): mem %lu != expected 16\n", mem);
  120. ret = 1;
  121. }
  122. if ((retval = atomic_decrement_val (&mem)) != 15)
  123. {
  124. printf ("atomic_decrement_val(mem): retval %lu != expected 15\n", retval);
  125. ret = 1;
  126. }
  127. mem = 0;
  128. CHK0(atomic_decrement_and_test, 0, -1);
  129. mem = 15;
  130. CHK0(atomic_decrement_and_test, 0, 14);
  131. mem = 1;
  132. CHK0(atomic_decrement_and_test, 1, 0);
  133. mem = 1;
  134. if (atomic_decrement_if_positive (&mem) != 1
  135. || mem != 0)
  136. {
  137. puts ("atomic_decrement_if_positive test 1 failed");
  138. ret = 1;
  139. }
  140. mem = 0;
  141. if (atomic_decrement_if_positive (&mem) != 0
  142. || mem != 0)
  143. {
  144. puts ("atomic_decrement_if_positive test 2 failed");
  145. ret = 1;
  146. }
  147. mem = -1;
  148. if (atomic_decrement_if_positive (&mem) != -1
  149. || mem != -1)
  150. {
  151. puts ("atomic_decrement_if_positive test 3 failed");
  152. ret = 1;
  153. }
  154. mem = -12;
  155. if (! atomic_add_negative (&mem, 10)
  156. || mem != -2)
  157. {
  158. puts ("atomic_add_negative test 1 failed");
  159. ret = 1;
  160. }
  161. mem = 0;
  162. if (atomic_add_negative (&mem, 100)
  163. || mem != 100)
  164. {
  165. puts ("atomic_add_negative test 2 failed");
  166. ret = 1;
  167. }
  168. mem = 15;
  169. if (atomic_add_negative (&mem, -10)
  170. || mem != 5)
  171. {
  172. puts ("atomic_add_negative test 3 failed");
  173. ret = 1;
  174. }
  175. mem = -12;
  176. if (atomic_add_negative (&mem, 14)
  177. || mem != 2)
  178. {
  179. puts ("atomic_add_negative test 4 failed");
  180. ret = 1;
  181. }
  182. mem = 0;
  183. if (! atomic_add_negative (&mem, -1)
  184. || mem != -1)
  185. {
  186. puts ("atomic_add_negative test 5 failed");
  187. ret = 1;
  188. }
  189. mem = -31;
  190. if (atomic_add_negative (&mem, 31)
  191. || mem != 0)
  192. {
  193. puts ("atomic_add_negative test 6 failed");
  194. ret = 1;
  195. }
  196. mem = -34;
  197. if (atomic_add_zero (&mem, 31)
  198. || mem != -3)
  199. {
  200. puts ("atomic_add_zero test 1 failed");
  201. ret = 1;
  202. }
  203. mem = -36;
  204. if (! atomic_add_zero (&mem, 36)
  205. || mem != 0)
  206. {
  207. puts ("atomic_add_zero test 2 failed");
  208. ret = 1;
  209. }
  210. mem = 113;
  211. if (atomic_add_zero (&mem, -13)
  212. || mem != 100)
  213. {
  214. puts ("atomic_add_zero test 3 failed");
  215. ret = 1;
  216. }
  217. mem = -18;
  218. if (atomic_add_zero (&mem, 20)
  219. || mem != 2)
  220. {
  221. puts ("atomic_add_zero test 4 failed");
  222. ret = 1;
  223. }
  224. mem = 10;
  225. if (atomic_add_zero (&mem, -20)
  226. || mem != -10)
  227. {
  228. puts ("atomic_add_zero test 5 failed");
  229. ret = 1;
  230. }
  231. mem = 10;
  232. if (! atomic_add_zero (&mem, -10)
  233. || mem != 0)
  234. {
  235. puts ("atomic_add_zero test 6 failed");
  236. ret = 1;
  237. }
  238. mem = 0;
  239. atomic_bit_set (&mem, 1);
  240. if (mem != 2)
  241. {
  242. puts ("atomic_bit_set test 1 failed");
  243. ret = 1;
  244. }
  245. mem = 8;
  246. atomic_bit_set (&mem, 3);
  247. if (mem != 8)
  248. {
  249. puts ("atomic_bit_set test 2 failed");
  250. ret = 1;
  251. }
  252. #ifdef TEST_ATOMIC64
  253. mem = 16;
  254. atomic_bit_set (&mem, 35);
  255. if (mem != 0x800000010LL)
  256. {
  257. puts ("atomic_bit_set test 3 failed");
  258. ret = 1;
  259. }
  260. #endif
  261. mem = 0;
  262. if (atomic_bit_test_set (&mem, 1)
  263. || mem != 2)
  264. {
  265. puts ("atomic_bit_test_set test 1 failed");
  266. ret = 1;
  267. }
  268. mem = 8;
  269. if (! atomic_bit_test_set (&mem, 3)
  270. || mem != 8)
  271. {
  272. puts ("atomic_bit_test_set test 2 failed");
  273. ret = 1;
  274. }
  275. #ifdef TEST_ATOMIC64
  276. mem = 16;
  277. if (atomic_bit_test_set (&mem, 35)
  278. || mem != 0x800000010LL)
  279. {
  280. puts ("atomic_bit_test_set test 3 failed");
  281. ret = 1;
  282. }
  283. mem = 0x100000000LL;
  284. if (! atomic_bit_test_set (&mem, 32)
  285. || mem != 0x100000000LL)
  286. {
  287. puts ("atomic_bit_test_set test 4 failed");
  288. ret = 1;
  289. }
  290. #endif
  291. #ifdef catomic_compare_and_exchange_val_acq
  292. mem = 24;
  293. if (catomic_compare_and_exchange_val_acq (&mem, 35, 24) != 24
  294. || mem != 35)
  295. {
  296. puts ("catomic_compare_and_exchange_val_acq test 1 failed");
  297. ret = 1;
  298. }
  299. mem = 12;
  300. if (catomic_compare_and_exchange_val_acq (&mem, 10, 15) != 12
  301. || mem != 12)
  302. {
  303. puts ("catomic_compare_and_exchange_val_acq test 2 failed");
  304. ret = 1;
  305. }
  306. mem = -15;
  307. if (catomic_compare_and_exchange_val_acq (&mem, -56, -15) != -15
  308. || mem != -56)
  309. {
  310. puts ("catomic_compare_and_exchange_val_acq test 3 failed");
  311. ret = 1;
  312. }
  313. mem = -1;
  314. if (catomic_compare_and_exchange_val_acq (&mem, 17, 0) != -1
  315. || mem != -1)
  316. {
  317. puts ("catomic_compare_and_exchange_val_acq test 4 failed");
  318. ret = 1;
  319. }
  320. #endif
  321. mem = 24;
  322. if (catomic_compare_and_exchange_bool_acq (&mem, 35, 24)
  323. || mem != 35)
  324. {
  325. puts ("catomic_compare_and_exchange_bool_acq test 1 failed");
  326. ret = 1;
  327. }
  328. mem = 12;
  329. if (! catomic_compare_and_exchange_bool_acq (&mem, 10, 15)
  330. || mem != 12)
  331. {
  332. puts ("catomic_compare_and_exchange_bool_acq test 2 failed");
  333. ret = 1;
  334. }
  335. mem = -15;
  336. if (catomic_compare_and_exchange_bool_acq (&mem, -56, -15)
  337. || mem != -56)
  338. {
  339. puts ("catomic_compare_and_exchange_bool_acq test 3 failed");
  340. ret = 1;
  341. }
  342. mem = -1;
  343. if (! catomic_compare_and_exchange_bool_acq (&mem, 17, 0)
  344. || mem != -1)
  345. {
  346. puts ("catomic_compare_and_exchange_bool_acq test 4 failed");
  347. ret = 1;
  348. }
  349. mem = 2;
  350. if (catomic_exchange_and_add (&mem, 11) != 2
  351. || mem != 13)
  352. {
  353. puts ("catomic_exchange_and_add test failed");
  354. ret = 1;
  355. }
  356. mem = -21;
  357. catomic_add (&mem, 22);
  358. if (mem != 1)
  359. {
  360. puts ("catomic_add test failed");
  361. ret = 1;
  362. }
  363. mem = -1;
  364. catomic_increment (&mem);
  365. if (mem != 0)
  366. {
  367. puts ("catomic_increment test failed");
  368. ret = 1;
  369. }
  370. mem = 2;
  371. if (catomic_increment_val (&mem) != 3)
  372. {
  373. puts ("catomic_increment_val test failed");
  374. ret = 1;
  375. }
  376. mem = 17;
  377. catomic_decrement (&mem);
  378. if (mem != 16)
  379. {
  380. puts ("catomic_decrement test failed");
  381. ret = 1;
  382. }
  383. if (catomic_decrement_val (&mem) != 15)
  384. {
  385. puts ("catomic_decrement_val test failed");
  386. ret = 1;
  387. }
  388. /* Tests for C11-like atomics. */
  389. mem = 11;
  390. if (atomic_load_relaxed (&mem) != 11 || atomic_load_acquire (&mem) != 11)
  391. {
  392. puts ("atomic_load_{relaxed,acquire} test failed");
  393. ret = 1;
  394. }
  395. atomic_store_relaxed (&mem, 12);
  396. if (mem != 12)
  397. {
  398. puts ("atomic_store_relaxed test failed");
  399. ret = 1;
  400. }
  401. atomic_store_release (&mem, 13);
  402. if (mem != 13)
  403. {
  404. puts ("atomic_store_release test failed");
  405. ret = 1;
  406. }
  407. mem = 14;
  408. expected = 14;
  409. if (!atomic_compare_exchange_weak_relaxed (&mem, &expected, 25)
  410. || mem != 25 || expected != 14)
  411. {
  412. puts ("atomic_compare_exchange_weak_relaxed test 1 failed");
  413. ret = 1;
  414. }
  415. if (atomic_compare_exchange_weak_relaxed (&mem, &expected, 14)
  416. || mem != 25 || expected != 25)
  417. {
  418. puts ("atomic_compare_exchange_weak_relaxed test 2 failed");
  419. ret = 1;
  420. }
  421. mem = 14;
  422. expected = 14;
  423. if (!atomic_compare_exchange_weak_acquire (&mem, &expected, 25)
  424. || mem != 25 || expected != 14)
  425. {
  426. puts ("atomic_compare_exchange_weak_acquire test 1 failed");
  427. ret = 1;
  428. }
  429. if (atomic_compare_exchange_weak_acquire (&mem, &expected, 14)
  430. || mem != 25 || expected != 25)
  431. {
  432. puts ("atomic_compare_exchange_weak_acquire test 2 failed");
  433. ret = 1;
  434. }
  435. mem = 14;
  436. expected = 14;
  437. if (!atomic_compare_exchange_weak_release (&mem, &expected, 25)
  438. || mem != 25 || expected != 14)
  439. {
  440. puts ("atomic_compare_exchange_weak_release test 1 failed");
  441. ret = 1;
  442. }
  443. if (atomic_compare_exchange_weak_release (&mem, &expected, 14)
  444. || mem != 25 || expected != 25)
  445. {
  446. puts ("atomic_compare_exchange_weak_release test 2 failed");
  447. ret = 1;
  448. }
  449. mem = 23;
  450. if (atomic_exchange_acquire (&mem, 42) != 23 || mem != 42)
  451. {
  452. puts ("atomic_exchange_acquire test failed");
  453. ret = 1;
  454. }
  455. mem = 23;
  456. if (atomic_exchange_release (&mem, 42) != 23 || mem != 42)
  457. {
  458. puts ("atomic_exchange_release test failed");
  459. ret = 1;
  460. }
  461. mem = 23;
  462. if (atomic_fetch_add_relaxed (&mem, 1) != 23 || mem != 24)
  463. {
  464. puts ("atomic_fetch_add_relaxed test failed");
  465. ret = 1;
  466. }
  467. mem = 23;
  468. if (atomic_fetch_add_acquire (&mem, 1) != 23 || mem != 24)
  469. {
  470. puts ("atomic_fetch_add_acquire test failed");
  471. ret = 1;
  472. }
  473. mem = 23;
  474. if (atomic_fetch_add_release (&mem, 1) != 23 || mem != 24)
  475. {
  476. puts ("atomic_fetch_add_release test failed");
  477. ret = 1;
  478. }
  479. mem = 23;
  480. if (atomic_fetch_add_acq_rel (&mem, 1) != 23 || mem != 24)
  481. {
  482. puts ("atomic_fetch_add_acq_rel test failed");
  483. ret = 1;
  484. }
  485. mem = 3;
  486. if (atomic_fetch_and_acquire (&mem, 2) != 3 || mem != 2)
  487. {
  488. puts ("atomic_fetch_and_acquire test failed");
  489. ret = 1;
  490. }
  491. mem = 4;
  492. if (atomic_fetch_or_relaxed (&mem, 2) != 4 || mem != 6)
  493. {
  494. puts ("atomic_fetch_or_relaxed test failed");
  495. ret = 1;
  496. }
  497. mem = 4;
  498. if (atomic_fetch_or_acquire (&mem, 2) != 4 || mem != 6)
  499. {
  500. puts ("atomic_fetch_or_acquire test failed");
  501. ret = 1;
  502. }
  503. /* This is a single-threaded test, so we can't test the effects of the
  504. fences. */
  505. atomic_thread_fence_acquire ();
  506. atomic_thread_fence_release ();
  507. atomic_thread_fence_seq_cst ();
  508. return ret;
  509. }
  510. #define TEST_FUNCTION do_test ()
  511. #include "../test-skeleton.c"