testatomic.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "testutil.h"
  17. #include "fspr_strings.h"
  18. #include "fspr_thread_proc.h"
  19. #include "fspr_errno.h"
  20. #include "fspr_general.h"
  21. #include "fspr_atomic.h"
  22. #include "fspr_time.h"
  23. /* Use pthread_setconcurrency where it is available and not a nullop,
  24. * i.e. platforms using M:N or M:1 thread models: */
  25. #if APR_HAS_THREADS && \
  26. ((defined(SOLARIS2) && SOLARIS2 > 26) || defined(_AIX))
  27. /* also HP-UX, IRIX? ... */
  28. #define HAVE_PTHREAD_SETCONCURRENCY
  29. #endif
  30. #ifdef HAVE_PTHREAD_SETCONCURRENCY
  31. #include <pthread.h>
  32. #endif
  33. static void test_init(abts_case *tc, void *data)
  34. {
  35. APR_ASSERT_SUCCESS(tc, "Could not initliaze atomics", fspr_atomic_init(p));
  36. }
  37. static void test_set32(abts_case *tc, void *data)
  38. {
  39. fspr_uint32_t y32;
  40. fspr_atomic_set32(&y32, 2);
  41. ABTS_INT_EQUAL(tc, 2, y32);
  42. }
  43. static void test_read32(abts_case *tc, void *data)
  44. {
  45. fspr_uint32_t y32;
  46. fspr_atomic_set32(&y32, 2);
  47. ABTS_INT_EQUAL(tc, 2, fspr_atomic_read32(&y32));
  48. }
  49. static void test_dec32(abts_case *tc, void *data)
  50. {
  51. fspr_uint32_t y32;
  52. int rv;
  53. fspr_atomic_set32(&y32, 2);
  54. rv = fspr_atomic_dec32(&y32);
  55. ABTS_INT_EQUAL(tc, 1, y32);
  56. ABTS_ASSERT(tc, "atomic_dec returned zero when it shouldn't", rv != 0);
  57. rv = fspr_atomic_dec32(&y32);
  58. ABTS_INT_EQUAL(tc, 0, y32);
  59. ABTS_ASSERT(tc, "atomic_dec didn't returned zero when it should", rv == 0);
  60. }
  61. static void test_xchg32(abts_case *tc, void *data)
  62. {
  63. fspr_uint32_t oldval;
  64. fspr_uint32_t y32;
  65. fspr_atomic_set32(&y32, 100);
  66. oldval = fspr_atomic_xchg32(&y32, 50);
  67. ABTS_INT_EQUAL(tc, 100, oldval);
  68. ABTS_INT_EQUAL(tc, 50, y32);
  69. }
  70. static void test_cas_equal(abts_case *tc, void *data)
  71. {
  72. fspr_uint32_t casval = 0;
  73. fspr_uint32_t oldval;
  74. oldval = fspr_atomic_cas32(&casval, 12, 0);
  75. ABTS_INT_EQUAL(tc, 0, oldval);
  76. ABTS_INT_EQUAL(tc, 12, casval);
  77. }
  78. static void test_cas_equal_nonnull(abts_case *tc, void *data)
  79. {
  80. fspr_uint32_t casval = 12;
  81. fspr_uint32_t oldval;
  82. oldval = fspr_atomic_cas32(&casval, 23, 12);
  83. ABTS_INT_EQUAL(tc, 12, oldval);
  84. ABTS_INT_EQUAL(tc, 23, casval);
  85. }
  86. static void test_cas_notequal(abts_case *tc, void *data)
  87. {
  88. fspr_uint32_t casval = 12;
  89. fspr_uint32_t oldval;
  90. oldval = fspr_atomic_cas32(&casval, 23, 2);
  91. ABTS_INT_EQUAL(tc, 12, oldval);
  92. ABTS_INT_EQUAL(tc, 12, casval);
  93. }
  94. static void test_add32(abts_case *tc, void *data)
  95. {
  96. fspr_uint32_t oldval;
  97. fspr_uint32_t y32;
  98. fspr_atomic_set32(&y32, 23);
  99. oldval = fspr_atomic_add32(&y32, 4);
  100. ABTS_INT_EQUAL(tc, 23, oldval);
  101. ABTS_INT_EQUAL(tc, 27, y32);
  102. }
  103. static void test_inc32(abts_case *tc, void *data)
  104. {
  105. fspr_uint32_t oldval;
  106. fspr_uint32_t y32;
  107. fspr_atomic_set32(&y32, 23);
  108. oldval = fspr_atomic_inc32(&y32);
  109. ABTS_INT_EQUAL(tc, 23, oldval);
  110. ABTS_INT_EQUAL(tc, 24, y32);
  111. }
  112. static void test_set_add_inc_sub(abts_case *tc, void *data)
  113. {
  114. fspr_uint32_t y32;
  115. fspr_atomic_set32(&y32, 0);
  116. fspr_atomic_add32(&y32, 20);
  117. fspr_atomic_inc32(&y32);
  118. fspr_atomic_sub32(&y32, 10);
  119. ABTS_INT_EQUAL(tc, 11, y32);
  120. }
  121. static void test_wrap_zero(abts_case *tc, void *data)
  122. {
  123. fspr_uint32_t y32;
  124. fspr_uint32_t rv;
  125. fspr_uint32_t minus1 = -1;
  126. char *str;
  127. fspr_atomic_set32(&y32, 0);
  128. rv = fspr_atomic_dec32(&y32);
  129. ABTS_ASSERT(tc, "fspr_atomic_dec32 on zero returned zero.", rv != 0);
  130. str = fspr_psprintf(p, "zero wrap failed: 0 - 1 = %d", y32);
  131. ABTS_ASSERT(tc, str, y32 == minus1);
  132. }
  133. static void test_inc_neg1(abts_case *tc, void *data)
  134. {
  135. fspr_uint32_t y32 = -1;
  136. fspr_uint32_t minus1 = -1;
  137. fspr_uint32_t rv;
  138. char *str;
  139. rv = fspr_atomic_inc32(&y32);
  140. ABTS_ASSERT(tc, "fspr_atomic_dec32 on zero returned zero.", rv == minus1);
  141. str = fspr_psprintf(p, "zero wrap failed: -1 + 1 = %d", y32);
  142. ABTS_ASSERT(tc, str, y32 == 0);
  143. }
  144. #if APR_HAS_THREADS
  145. void * APR_THREAD_FUNC thread_func_mutex(fspr_thread_t *thd, void *data);
  146. void * APR_THREAD_FUNC thread_func_atomic(fspr_thread_t *thd, void *data);
  147. void * APR_THREAD_FUNC thread_func_none(fspr_thread_t *thd, void *data);
  148. fspr_thread_mutex_t *thread_lock;
  149. volatile fspr_uint32_t x = 0; /* mutex locks */
  150. volatile fspr_uint32_t y = 0; /* atomic operations */
  151. volatile fspr_uint32_t z = 0; /* no locks */
  152. fspr_status_t exit_ret_val = 123; /* just some made up number to check on later */
  153. #define NUM_THREADS 40
  154. #define NUM_ITERATIONS 20000
  155. void * APR_THREAD_FUNC thread_func_mutex(fspr_thread_t *thd, void *data)
  156. {
  157. int i;
  158. for (i = 0; i < NUM_ITERATIONS; i++) {
  159. fspr_thread_mutex_lock(thread_lock);
  160. x++;
  161. fspr_thread_mutex_unlock(thread_lock);
  162. }
  163. fspr_thread_exit(thd, exit_ret_val);
  164. return NULL;
  165. }
  166. void * APR_THREAD_FUNC thread_func_atomic(fspr_thread_t *thd, void *data)
  167. {
  168. int i;
  169. for (i = 0; i < NUM_ITERATIONS ; i++) {
  170. fspr_atomic_inc32(&y);
  171. fspr_atomic_add32(&y, 2);
  172. fspr_atomic_dec32(&y);
  173. fspr_atomic_dec32(&y);
  174. }
  175. fspr_thread_exit(thd, exit_ret_val);
  176. return NULL;
  177. }
  178. void * APR_THREAD_FUNC thread_func_none(fspr_thread_t *thd, void *data)
  179. {
  180. int i;
  181. for (i = 0; i < NUM_ITERATIONS ; i++) {
  182. z++;
  183. }
  184. fspr_thread_exit(thd, exit_ret_val);
  185. return NULL;
  186. }
  187. static void test_atomics_threaded(abts_case *tc, void *data)
  188. {
  189. fspr_thread_t *t1[NUM_THREADS];
  190. fspr_thread_t *t2[NUM_THREADS];
  191. fspr_thread_t *t3[NUM_THREADS];
  192. fspr_status_t s1[NUM_THREADS];
  193. fspr_status_t s2[NUM_THREADS];
  194. fspr_status_t s3[NUM_THREADS];
  195. fspr_status_t rv;
  196. int i;
  197. #ifdef HAVE_PTHREAD_SETCONCURRENCY
  198. pthread_setconcurrency(8);
  199. #endif
  200. rv = fspr_thread_mutex_create(&thread_lock, APR_THREAD_MUTEX_DEFAULT, p);
  201. APR_ASSERT_SUCCESS(tc, "Could not create lock", rv);
  202. for (i = 0; i < NUM_THREADS; i++) {
  203. fspr_status_t r1, r2, r3;
  204. r1 = fspr_thread_create(&t1[i], NULL, thread_func_mutex, NULL, p);
  205. r2 = fspr_thread_create(&t2[i], NULL, thread_func_atomic, NULL, p);
  206. r3 = fspr_thread_create(&t3[i], NULL, thread_func_none, NULL, p);
  207. ABTS_ASSERT(tc, "Failed creating threads",
  208. r1 == APR_SUCCESS && r2 == APR_SUCCESS &&
  209. r3 == APR_SUCCESS);
  210. }
  211. for (i = 0; i < NUM_THREADS; i++) {
  212. fspr_thread_join(&s1[i], t1[i]);
  213. fspr_thread_join(&s2[i], t2[i]);
  214. fspr_thread_join(&s3[i], t3[i]);
  215. ABTS_ASSERT(tc, "Invalid return value from thread_join",
  216. s1[i] == exit_ret_val && s2[i] == exit_ret_val &&
  217. s3[i] == exit_ret_val);
  218. }
  219. ABTS_INT_EQUAL(tc, x, NUM_THREADS * NUM_ITERATIONS);
  220. ABTS_INT_EQUAL(tc, fspr_atomic_read32(&y), NUM_THREADS * NUM_ITERATIONS);
  221. /* Comment out this test, because I have no clue what this test is
  222. * actually telling us. We are checking something that may or may not
  223. * be true, and it isn't really testing APR at all.
  224. ABTS_ASSERT(tc, "We expect this to fail, because we tried to update "
  225. "an integer in a non-thread-safe manner.",
  226. z != NUM_THREADS * NUM_ITERATIONS);
  227. */
  228. }
  229. #endif /* !APR_HAS_THREADS */
  230. abts_suite *testatomic(abts_suite *suite)
  231. {
  232. suite = ADD_SUITE(suite)
  233. abts_run_test(suite, test_init, NULL);
  234. abts_run_test(suite, test_set32, NULL);
  235. abts_run_test(suite, test_read32, NULL);
  236. abts_run_test(suite, test_dec32, NULL);
  237. abts_run_test(suite, test_xchg32, NULL);
  238. abts_run_test(suite, test_cas_equal, NULL);
  239. abts_run_test(suite, test_cas_equal_nonnull, NULL);
  240. abts_run_test(suite, test_cas_notequal, NULL);
  241. abts_run_test(suite, test_add32, NULL);
  242. abts_run_test(suite, test_inc32, NULL);
  243. abts_run_test(suite, test_set_add_inc_sub, NULL);
  244. abts_run_test(suite, test_wrap_zero, NULL);
  245. abts_run_test(suite, test_inc_neg1, NULL);
  246. #if APR_HAS_THREADS
  247. abts_run_test(suite, test_atomics_threaded, NULL);
  248. #endif
  249. return suite;
  250. }