tst-atomic.c 13 KB

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