atomic-compare-exchange-1.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* Test __atomic routines for existence and proper execution on 1 byte
  2. values with each valid memory model. */
  3. /* { dg-do run } */
  4. /* Test the execution of the __atomic_compare_exchange_n builtin for a char. */
  5. extern void abort(void);
  6. char v = 0;
  7. char expected = 0;
  8. char max = ~0;
  9. char desired = ~0;
  10. char zero = 0;
  11. #define STRONG 0
  12. #define WEAK 1
  13. int
  14. main ()
  15. {
  16. if (!__atomic_compare_exchange_n (&v, &expected, max, STRONG , __ATOMIC_RELAXED, __ATOMIC_RELAXED))
  17. abort ();
  18. if (expected != 0)
  19. abort ();
  20. if (__atomic_compare_exchange_n (&v, &expected, 0, STRONG , __ATOMIC_ACQUIRE, __ATOMIC_RELAXED))
  21. abort ();
  22. if (expected != max)
  23. abort ();
  24. if (!__atomic_compare_exchange_n (&v, &expected, 0, STRONG , __ATOMIC_RELEASE, __ATOMIC_ACQUIRE))
  25. abort ();
  26. if (expected != max)
  27. abort ();
  28. if (v != 0)
  29. abort ();
  30. if (__atomic_compare_exchange_n (&v, &expected, desired, WEAK, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE))
  31. abort ();
  32. if (expected != 0)
  33. abort ();
  34. if (!__atomic_compare_exchange_n (&v, &expected, desired, STRONG , __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST))
  35. abort ();
  36. if (expected != 0)
  37. abort ();
  38. if (v != max)
  39. abort ();
  40. /* Now test the generic version. */
  41. v = 0;
  42. if (!__atomic_compare_exchange (&v, &expected, &max, STRONG, __ATOMIC_RELAXED, __ATOMIC_RELAXED))
  43. abort ();
  44. if (expected != 0)
  45. abort ();
  46. if (__atomic_compare_exchange (&v, &expected, &zero, STRONG , __ATOMIC_ACQUIRE, __ATOMIC_RELAXED))
  47. abort ();
  48. if (expected != max)
  49. abort ();
  50. if (!__atomic_compare_exchange (&v, &expected, &zero, STRONG , __ATOMIC_RELEASE, __ATOMIC_ACQUIRE))
  51. abort ();
  52. if (expected != max)
  53. abort ();
  54. if (v != 0)
  55. abort ();
  56. if (__atomic_compare_exchange (&v, &expected, &desired, WEAK, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE))
  57. abort ();
  58. if (expected != 0)
  59. abort ();
  60. if (!__atomic_compare_exchange (&v, &expected, &desired, STRONG , __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST))
  61. abort ();
  62. if (expected != 0)
  63. abort ();
  64. if (v != max)
  65. abort ();
  66. return 0;
  67. }