atomic_test2.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. *
  3. * simple atomic ops testing program
  4. * (no parallel stuff, just see if the opcodes are "legal")
  5. *
  6. * Defines: TYPE - not defined => use atomic_t and the corresponding
  7. * atomic functions
  8. * - long => use volatile long* and the atomic_*_long functions
  9. * - int => use volatile int* and the atomic_*_int functions
  10. * MEMBAR - if defined use mb_atomic_* instead of atomic_*
  11. * NOSMP - use non smp versions
  12. * NOASM - don't use asm inline version
  13. * __CPU_xxx - use __CPU_xxx code
  14. * SPARC64_MODE - compile for a sparc 64 in 64 bit mode (gcc -m64
  15. * must be used on solaris in this case)
  16. * Example:
  17. * gcc -Wall -O3 -D__CPU_i386 -DNOSMP -DMEMBAR -DTYPE=long atomic_test2.c
  18. *
  19. * Compile with: gcc -Wall -O3 -D__CPU_i386 ... on x86 machines
  20. * gcc -Wall -O3 -D__CPU_x86_64 ... on amd64 machines
  21. * gcc -mips2 -Wall -O2 -D__CPU_mips2 ... on mips machines
  22. * gcc -m64 -Wall -O2 -D__CPU_mips64 ... on mips64 machines
  23. * gcc -O3 -Wall -D__CPU_ppc ... on powerpc machines
  24. * gcc -m64 -O3 -Wall -D__CPU_ppc64 ... on powerpc machines
  25. * gcc -m64 -O3 -Wall -D__CPU_sparc64 -DSPARC64_MODE ... on
  26. * ultrasparc machines
  27. * gcc -mcpu=v9 -O3 -Wall -D__CPU_sparc64 ... for 32 bit code
  28. * (sparc32plus) on
  29. * ultrasparc machines
  30. * gcc -O3 -Wall -D__CPU_sparc ... on sparc v8 machines
  31. * -- andrei
  32. *
  33. *
  34. */
  35. #include <stdio.h>
  36. #ifndef NOASM
  37. #define CC_GCC_LIKE_ASM
  38. #endif
  39. #include "../atomic_ops.h"
  40. #if defined ATOMIC_OPS_USE_LOCK || defined MEMBAR_USES_LOCK || \
  41. defined ATOMIC_OPS_USE_LOCK_SET
  42. /* hack to make lock work */
  43. #include "../lock_ops.h"
  44. #endif
  45. #ifdef MEMBAR_USES_LOCK
  46. gen_lock_t* __membar_lock=0; /* init in atomic_ops.c */
  47. gen_lock_t dummy_membar_lock;
  48. #endif
  49. #ifdef ATOMIC_OPS_USE_LOCK_SET
  50. gen_lock_set_t* _atomic_lock_set=0;
  51. gen_lock_set_t dummy_atomic_lock_set;
  52. gen_lock_t locks_array[_ATOMIC_LS_SIZE];
  53. #elif defined ATOMIC_OPS_USE_LOCK
  54. gen_lock_t* _atomic_lock=0;
  55. gen_lock_t dummy_atomic_lock;
  56. #endif /* ATOMIC_OPS_USE_LOCK / _SET */
  57. #if defined MB || defined MEMBAR
  58. #undef MB
  59. #define MB mb_
  60. #define MEMBAR_STR "membar "
  61. #else
  62. #define MB /* empty */
  63. #define MEMBAR_STR ""
  64. #endif
  65. #ifndef TYPE
  66. #define SUF
  67. #define ATOMIC_TYPE atomic_t
  68. #define VALUE_TYPE volatile int
  69. #define get_val(v) (v->val)
  70. #else
  71. #define _SUF(T) _##T
  72. #define _SUF1(T) _SUF(T)
  73. #define SUF _SUF1(TYPE)
  74. #define ATOMIC_TYPE volatile TYPE
  75. #define VALUE_TYPE ATOMIC_TYPE
  76. #define get_val(v) (*v)
  77. #endif
  78. #define _STR(S) #S
  79. #define STR(S) _STR(S)
  80. static char* flags=
  81. #ifdef NOASM
  82. "no_inline_asm "
  83. #endif
  84. #ifdef NOSMP
  85. "nosmp "
  86. #else
  87. "smp "
  88. #endif
  89. MEMBAR_STR
  90. #ifndef HAVE_ASM_INLINE_MEMBAR
  91. "no_asm_membar(slow) "
  92. #endif
  93. #ifndef HAVE_ASM_INLINE_ATOMIC_OPS
  94. "no_asm_atomic_ops"
  95. #ifdef ATOMIC_OPS_USE_LOCK_SET
  96. ":use_lock_set"
  97. #elif defined ATOMIC_OPS_USE_LOCK
  98. ":use_lock"
  99. #endif
  100. " "
  101. #endif
  102. #ifdef TYPE
  103. STR(TYPE) " "
  104. #else
  105. "atomic_t "
  106. #endif
  107. ;
  108. /* macros for atomic_* functions */
  109. #define _AT_DECL(OP, P, S) \
  110. P##atomic_##OP##S
  111. /* to make sure all the macro passed as params are expanded,
  112. * go through a 2 level deep macro decl. */
  113. #define _AT_DECL1(OP, P, S) _AT_DECL(OP, P, S)
  114. #define AT_DECL(OP) _AT_DECL1(OP, MB, SUF)
  115. #define at_set AT_DECL(set)
  116. #define at_get AT_DECL(get)
  117. #define at_inc AT_DECL(inc)
  118. #define at_dec AT_DECL(dec)
  119. #define at_inc_and_test AT_DECL(inc_and_test)
  120. #define at_dec_and_test AT_DECL(dec_and_test)
  121. #define at_and AT_DECL(and)
  122. #define at_or AT_DECL(or)
  123. #define at_get_and_set AT_DECL(get_and_set)
  124. #define at_cmpxchg AT_DECL(cmpxchg)
  125. #define at_add AT_DECL(add)
  126. #define CHECK_ERR(txt, x, y) \
  127. if (x!=y) { \
  128. fprintf(stderr, "ERROR: line %d: %s failed: expected 0x%02x but got "\
  129. "0x%02x.\n", \
  130. __LINE__, #txt, (unsigned) x, (unsigned) y);\
  131. goto error; \
  132. }
  133. #define VERIFY(ops, y) \
  134. ops ; \
  135. CHECK_ERR( ops, y, get_val(v))
  136. int main(int argc, char** argv)
  137. {
  138. ATOMIC_TYPE var;
  139. VALUE_TYPE r;
  140. ATOMIC_TYPE* v;
  141. v=&var;
  142. #ifdef MEMBAR_USES_LOCK
  143. __membar_lock=&dummy_membar_lock;
  144. if (lock_init(__membar_lock)==0){
  145. fprintf(stderr, "ERROR: failed to initialize membar_lock\n");
  146. __membar_lock=0;
  147. goto error;
  148. }
  149. _membar_lock; /* start with the lock "taken" so that we can safely use
  150. unlock/lock sequences on it later */
  151. #endif
  152. #ifdef ATOMIC_OPS_USE_LOCK_SET
  153. /* init the lock (emulate atomic_ops.c) */
  154. dummy_atomic_lock_set.locks=&locks_array[0];
  155. _atomic_lock_set=&dummy_atomic_lock_set;
  156. if (lock_set_init(_atomic_lock_set)==0){
  157. fprintf(stderr, "ERROR: failed to initialize atomic_lock\n");
  158. _atomic_lock_set=0;
  159. goto error;
  160. }
  161. #elif defined ATOMIC_OPS_USE_LOCK
  162. /* init the lock (emulate atomic_ops.c) */
  163. _atomic_lock=&dummy_atomic_lock;
  164. if (lock_init(_atomic_lock)==0){
  165. fprintf(stderr, "ERROR: failed to initialize atomic_lock\n");
  166. _atomic_lock=0;
  167. goto error;
  168. }
  169. #endif
  170. printf("%s\n", flags);
  171. printf("starting memory barrier opcode tests...\n");
  172. membar();
  173. printf(" membar() .............................. ok\n");
  174. membar_write();
  175. printf(" membar_write() ........................ ok\n");
  176. membar_read();
  177. printf(" membar_read() ......................... ok\n");
  178. membar_depends();
  179. printf(" membar_depends() ...................... ok\n");
  180. membar_enter_lock();
  181. printf(" membar_enter_lock() ................... ok\n");
  182. membar_leave_lock();
  183. printf(" membar_leave_lock() ................... ok\n");
  184. membar_atomic_op();
  185. printf(" membar_atomic_op() .................... ok\n");
  186. membar_atomic_setget();
  187. printf(" membar_atomic_setget() ................ ok\n");
  188. membar_read_atomic_op();
  189. printf(" membar_read_atomic_op() ............... ok\n");
  190. membar_read_atomic_setget();
  191. printf(" membar_read_atomic_setget() ........... ok\n");
  192. membar_write_atomic_op();
  193. printf(" membar_write_atomic_op() .............. ok\n");
  194. membar_write_atomic_setget();
  195. printf(" membar_write_atomic_setget() .......... ok\n");
  196. printf("\nstarting atomic ops basic tests...\n");
  197. VERIFY(at_set(v, 1), 1);
  198. printf(" atomic_set, v should be 1 ............. %2d\n", (int)at_get(v));
  199. VERIFY(at_inc(v), 2);
  200. printf(" atomic_inc, v should be 2 ............. %2d\n", (int)at_get(v));
  201. VERIFY(r=at_inc_and_test(v), 3);
  202. printf(" atomic_inc_and_test, v should be 3 ... %2d\n", (int)at_get(v));
  203. printf(" r should be 0 ... %2d\n", (int)r);
  204. VERIFY(at_dec(v), 2);
  205. printf(" atomic_dec, v should be 2 ............. %2d\n", (int)at_get(v));
  206. VERIFY(r=at_dec_and_test(v), 1);
  207. printf(" atomic_dec_and_test, v should be 1 ... %2d\n", (int)at_get(v));
  208. printf(" r should be 0 ... %2d\n", (int)r);
  209. VERIFY(r=at_dec_and_test(v), 0);
  210. printf(" atomic_dec_and_test, v should be 0 ... %2d\n", (int)at_get(v));
  211. printf(" r should be 1 ... %2d\n", (int)r);
  212. VERIFY(r=at_dec_and_test(v), -1);
  213. printf(" atomic_dec_and_test, v should be -1 ... %2d\n", (int)at_get(v));
  214. printf(" r should be 0 ... %2d\n", (int)r);
  215. VERIFY(at_and(v, 2), 2);
  216. printf(" atomic_and, v should be 2 ............. %2d\n", (int)at_get(v));
  217. VERIFY(at_or(v, 5), 7);
  218. printf(" atomic_or, v should be 7 ............. %2d\n", (int)at_get(v));
  219. VERIFY(r=at_get_and_set(v, 0), 0);
  220. printf(" atomic_get_and_set, v should be 0 ..... %2d\n", (int)at_get(v));
  221. VERIFY(r=at_cmpxchg(v, 0, 7), 7);
  222. CHECK_ERR(cmpxchg, r, 0);
  223. printf(" atomic_cmpxchg, v should be 7 ......... %2d\n", (int)at_get(v));
  224. printf(" r should be 0 ......... %2d\n", (int)r);
  225. VERIFY(r=at_cmpxchg(v, 2, 3), 7);
  226. CHECK_ERR(cmpxchg, r, 7);
  227. printf(" atomic_cmpxchg (fail), v should be 7 .. %2d\n", (int)at_get(v));
  228. printf(" r should be 7 .. %2d\n", (int)r);
  229. VERIFY(r=at_add(v, 2), 9);
  230. CHECK_ERR(atomic_add, r, 9);
  231. printf(" atomic_add, v should be 9 ............. %2d\n", (int)at_get(v));
  232. printf(" r should be 9 ............. %2d\n", (int)r);
  233. VERIFY(r=at_add(v, -10), -1);
  234. CHECK_ERR(atomic_add, r, -1);
  235. printf(" atomic_add, v should be -1 ............ %2d\n", (int)at_get(v));
  236. printf(" r should be -1 ............ %2d\n", (int)r);
  237. printf("\ndone.\n");
  238. #ifdef MEMBAR_USES_LOCK
  239. lock_destroy(__membar_lock);
  240. #endif
  241. #ifdef ATOMIC_OPS_USE_LOCK_SET
  242. lock_set_destroy(_atomic_lock_set);
  243. #elif defined ATOMIC_OPS_USE_LOCK
  244. lock_destroy(_atomic_lock);
  245. #endif
  246. return 0;
  247. error:
  248. #ifdef MEMBAR_USES_LOCK
  249. if (__membar_lock)
  250. lock_destroy(__membar_lock);
  251. #endif
  252. #ifdef ATOMIC_OPS_USE_LOCK_SET
  253. if (_atomic_lock_set)
  254. lock_set_destroy(_atomic_lock_set);
  255. #elif defined ATOMIC_OPS_USE_LOCK
  256. if (_atomic_lock)
  257. lock_destroy(_atomic_lock);
  258. #endif
  259. return -1;
  260. }