test_atomic_ops.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (c) 2017 Ivan Maidanski
  3. *
  4. * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  5. * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
  6. *
  7. * Permission is hereby granted to use or copy this program
  8. * for any purpose, provided the above notices are retained on all copies.
  9. * Permission to modify the code and to distribute modified code is granted,
  10. * provided the above notices are retained, and a notice that the code was
  11. * modified is included with the above copyright notice.
  12. */
  13. /* Minimal testing of atomic operations used by the BDWGC. Primary use */
  14. /* is to determine whether compiler atomic intrinsics can be relied on. */
  15. #ifdef HAVE_CONFIG_H
  16. # include "config.h"
  17. #endif
  18. #include <stdio.h>
  19. #if defined(GC_BUILTIN_ATOMIC) || defined(GC_THREADS)
  20. # include <stdlib.h>
  21. # ifdef PARALLEL_MARK
  22. # define AO_REQUIRE_CAS
  23. # endif
  24. # include "private/gc_atomic_ops.h"
  25. # define TA_assert(e) \
  26. if (!(e)) { \
  27. fprintf(stderr, "Assertion failure, line %d: " #e "\n", __LINE__); \
  28. exit(-1); \
  29. }
  30. int main(void) {
  31. AO_t x = 13;
  32. # if defined(AO_HAVE_char_load) || defined(AO_HAVE_char_store)
  33. unsigned char c = 117;
  34. # endif
  35. # ifdef AO_HAVE_test_and_set_acquire
  36. AO_TS_t z = AO_TS_INITIALIZER;
  37. TA_assert(AO_test_and_set_acquire(&z) == AO_TS_CLEAR);
  38. TA_assert(AO_test_and_set_acquire(&z) == AO_TS_SET);
  39. AO_CLEAR(&z);
  40. # endif
  41. AO_compiler_barrier();
  42. # ifdef AO_HAVE_nop_full
  43. AO_nop_full();
  44. # endif
  45. # ifdef AO_HAVE_char_load
  46. TA_assert(AO_char_load(&c) == 117);
  47. # endif
  48. # ifdef AO_HAVE_char_store
  49. AO_char_store(&c, 119);
  50. TA_assert(c == 119);
  51. # endif
  52. # ifdef AO_HAVE_load_acquire
  53. TA_assert(AO_load_acquire(&x) == 13);
  54. # endif
  55. # if defined(AO_HAVE_fetch_and_add) && defined(AO_HAVE_fetch_and_add1)
  56. TA_assert(AO_fetch_and_add(&x, 42) == 13);
  57. TA_assert(AO_fetch_and_add(&x, (AO_t)(-43)) == 55);
  58. TA_assert(AO_fetch_and_add1(&x) == 12);
  59. # endif
  60. # ifdef AO_HAVE_compare_and_swap_release
  61. TA_assert(!AO_compare_and_swap(&x, 14, 42));
  62. TA_assert(x == 13);
  63. TA_assert(AO_compare_and_swap_release(&x, 13, 42));
  64. TA_assert(x == 42);
  65. # else
  66. if (*(volatile AO_t *)&x == 13)
  67. *(volatile AO_t *)&x = 42;
  68. # endif
  69. # ifdef AO_HAVE_or
  70. AO_or(&x, 66);
  71. TA_assert(x == 106);
  72. # endif
  73. # ifdef AO_HAVE_store_release
  74. AO_store_release(&x, 113);
  75. TA_assert(x == 113);
  76. # endif
  77. return 0;
  78. }
  79. #else
  80. int main(void)
  81. {
  82. printf("test_atomic_ops skipped\n");
  83. return 0;
  84. }
  85. #endif