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
17 #include "testutil.h"
18 #include "apr_strings.h"
19 #include "apr_thread_proc.h"
20 #include "apr_errno.h"
21 #include "apr_general.h"
22 #include "apr_atomic.h"
23 #include "apr_time.h"
24
25 /* Use pthread_setconcurrency where it is available and not a nullop,
26 * i.e. platforms using M:N or M:1 thread models: */
27 #if APR_HAS_THREADS && \
28 ((defined(SOLARIS2) && SOLARIS2 > 6) || defined(_AIX))
29 /* also HP-UX, IRIX? ... */
30 #define HAVE_PTHREAD_SETCONCURRENCY
31 #endif
32
33 #ifdef HAVE_PTHREAD_SETCONCURRENCY
34 #include <pthread.h>
35 #endif
36
test_init(abts_case * tc,void * data)37 static void test_init(abts_case *tc, void *data)
38 {
39 APR_ASSERT_SUCCESS(tc, "Could not initliaze atomics", apr_atomic_init(p));
40 }
41
test_set32(abts_case * tc,void * data)42 static void test_set32(abts_case *tc, void *data)
43 {
44 apr_uint32_t y32;
45 apr_atomic_set32(&y32, 2);
46 ABTS_INT_EQUAL(tc, 2, y32);
47 }
48
test_read32(abts_case * tc,void * data)49 static void test_read32(abts_case *tc, void *data)
50 {
51 apr_uint32_t y32;
52 apr_atomic_set32(&y32, 2);
53 ABTS_INT_EQUAL(tc, 2, apr_atomic_read32(&y32));
54 }
55
test_dec32(abts_case * tc,void * data)56 static void test_dec32(abts_case *tc, void *data)
57 {
58 apr_uint32_t y32;
59 int rv;
60
61 apr_atomic_set32(&y32, 2);
62
63 rv = apr_atomic_dec32(&y32);
64 ABTS_INT_EQUAL(tc, 1, y32);
65 ABTS_ASSERT(tc, "atomic_dec returned zero when it shouldn't", rv != 0);
66
67 rv = apr_atomic_dec32(&y32);
68 ABTS_INT_EQUAL(tc, 0, y32);
69 ABTS_ASSERT(tc, "atomic_dec didn't returned zero when it should", rv == 0);
70 }
71
test_xchg32(abts_case * tc,void * data)72 static void test_xchg32(abts_case *tc, void *data)
73 {
74 apr_uint32_t oldval;
75 apr_uint32_t y32;
76
77 apr_atomic_set32(&y32, 100);
78 oldval = apr_atomic_xchg32(&y32, 50);
79
80 ABTS_INT_EQUAL(tc, 100, oldval);
81 ABTS_INT_EQUAL(tc, 50, y32);
82 }
83
test_xchgptr(abts_case * tc,void * data)84 static void test_xchgptr(abts_case *tc, void *data)
85 {
86 int a;
87 void *ref = "little piggy";
88 volatile void *target_ptr = ref;
89 void *old_ptr;
90
91 old_ptr = apr_atomic_xchgptr(&target_ptr, &a);
92 ABTS_PTR_EQUAL(tc, ref, old_ptr);
93 ABTS_PTR_EQUAL(tc, &a, (void *) target_ptr);
94 }
95
test_cas_equal(abts_case * tc,void * data)96 static void test_cas_equal(abts_case *tc, void *data)
97 {
98 apr_uint32_t casval = 0;
99 apr_uint32_t oldval;
100
101 oldval = apr_atomic_cas32(&casval, 12, 0);
102 ABTS_INT_EQUAL(tc, 0, oldval);
103 ABTS_INT_EQUAL(tc, 12, casval);
104 }
105
test_cas_equal_nonnull(abts_case * tc,void * data)106 static void test_cas_equal_nonnull(abts_case *tc, void *data)
107 {
108 apr_uint32_t casval = 12;
109 apr_uint32_t oldval;
110
111 oldval = apr_atomic_cas32(&casval, 23, 12);
112 ABTS_INT_EQUAL(tc, 12, oldval);
113 ABTS_INT_EQUAL(tc, 23, casval);
114 }
115
test_cas_notequal(abts_case * tc,void * data)116 static void test_cas_notequal(abts_case *tc, void *data)
117 {
118 apr_uint32_t casval = 12;
119 apr_uint32_t oldval;
120
121 oldval = apr_atomic_cas32(&casval, 23, 2);
122 ABTS_INT_EQUAL(tc, 12, oldval);
123 ABTS_INT_EQUAL(tc, 12, casval);
124 }
125
test_casptr_equal(abts_case * tc,void * data)126 static void test_casptr_equal(abts_case *tc, void *data)
127 {
128 int a;
129 volatile void *target_ptr = NULL;
130 void *old_ptr;
131
132 old_ptr = apr_atomic_casptr(&target_ptr, &a, NULL);
133 ABTS_PTR_EQUAL(tc, NULL, old_ptr);
134 ABTS_PTR_EQUAL(tc, &a, (void *) target_ptr);
135 }
136
test_casptr_equal_nonnull(abts_case * tc,void * data)137 static void test_casptr_equal_nonnull(abts_case *tc, void *data)
138 {
139 int a, b;
140 volatile void *target_ptr = &a;
141 void *old_ptr;
142
143 old_ptr = apr_atomic_casptr(&target_ptr, &b, &a);
144 ABTS_PTR_EQUAL(tc, &a, old_ptr);
145 ABTS_PTR_EQUAL(tc, &b, (void *) target_ptr);
146 }
147
test_casptr_notequal(abts_case * tc,void * data)148 static void test_casptr_notequal(abts_case *tc, void *data)
149 {
150 int a, b;
151 volatile void *target_ptr = &a;
152 void *old_ptr;
153
154 old_ptr = apr_atomic_casptr(&target_ptr, &a, &b);
155 ABTS_PTR_EQUAL(tc, &a, old_ptr);
156 ABTS_PTR_EQUAL(tc, &a, (void *) target_ptr);
157 }
158
test_add32(abts_case * tc,void * data)159 static void test_add32(abts_case *tc, void *data)
160 {
161 apr_uint32_t oldval;
162 apr_uint32_t y32;
163
164 apr_atomic_set32(&y32, 23);
165 oldval = apr_atomic_add32(&y32, 4);
166 ABTS_INT_EQUAL(tc, 23, oldval);
167 ABTS_INT_EQUAL(tc, 27, y32);
168 }
169
test_add32_neg(abts_case * tc,void * data)170 static void test_add32_neg(abts_case *tc, void *data)
171 {
172 apr_uint32_t oldval;
173 apr_uint32_t y32;
174
175 apr_atomic_set32(&y32, 23);
176 oldval = apr_atomic_add32(&y32, -10);
177 ABTS_INT_EQUAL(tc, 23, oldval);
178 ABTS_INT_EQUAL(tc, 13, y32);
179 }
180
test_inc32(abts_case * tc,void * data)181 static void test_inc32(abts_case *tc, void *data)
182 {
183 apr_uint32_t oldval;
184 apr_uint32_t y32;
185
186 apr_atomic_set32(&y32, 23);
187 oldval = apr_atomic_inc32(&y32);
188 ABTS_INT_EQUAL(tc, 23, oldval);
189 ABTS_INT_EQUAL(tc, 24, y32);
190 }
191
test_set_add_inc_sub(abts_case * tc,void * data)192 static void test_set_add_inc_sub(abts_case *tc, void *data)
193 {
194 apr_uint32_t y32;
195
196 apr_atomic_set32(&y32, 0);
197 apr_atomic_add32(&y32, 20);
198 apr_atomic_inc32(&y32);
199 apr_atomic_sub32(&y32, 10);
200
201 ABTS_INT_EQUAL(tc, 11, y32);
202 }
203
test_wrap_zero(abts_case * tc,void * data)204 static void test_wrap_zero(abts_case *tc, void *data)
205 {
206 apr_uint32_t y32;
207 apr_uint32_t rv;
208 apr_uint32_t minus1 = (apr_uint32_t)-1;
209 char *str;
210
211 apr_atomic_set32(&y32, 0);
212 rv = apr_atomic_dec32(&y32);
213
214 ABTS_ASSERT(tc, "apr_atomic_dec32 on zero returned zero.", rv != 0);
215 str = apr_psprintf(p, "zero wrap failed: 0 - 1 = %d", y32);
216 ABTS_ASSERT(tc, str, y32 == minus1);
217 }
218
test_inc_neg1(abts_case * tc,void * data)219 static void test_inc_neg1(abts_case *tc, void *data)
220 {
221 apr_uint32_t y32 = (apr_uint32_t)-1;
222 apr_uint32_t minus1 = (apr_uint32_t)-1;
223 apr_uint32_t rv;
224 char *str;
225
226 rv = apr_atomic_inc32(&y32);
227
228 ABTS_ASSERT(tc, "apr_atomic_inc32 didn't return the old value.", rv == minus1);
229 str = apr_psprintf(p, "zero wrap failed: -1 + 1 = %d", y32);
230 ABTS_ASSERT(tc, str, y32 == 0);
231 }
232
233
234 #if APR_HAS_THREADS
235
236 void *APR_THREAD_FUNC thread_func_mutex(apr_thread_t *thd, void *data);
237 void *APR_THREAD_FUNC thread_func_atomic(apr_thread_t *thd, void *data);
238
239 apr_thread_mutex_t *thread_lock;
240 volatile apr_uint32_t mutex_locks = 0;
241 volatile apr_uint32_t atomic_ops = 0;
242 apr_status_t exit_ret_val = 123; /* just some made up number to check on later */
243
244 #define NUM_THREADS 40
245 #define NUM_ITERATIONS 20000
246
thread_func_mutex(apr_thread_t * thd,void * data)247 void *APR_THREAD_FUNC thread_func_mutex(apr_thread_t *thd, void *data)
248 {
249 int i;
250
251 for (i = 0; i < NUM_ITERATIONS; i++) {
252 apr_thread_mutex_lock(thread_lock);
253 mutex_locks++;
254 apr_thread_mutex_unlock(thread_lock);
255 }
256 apr_thread_exit(thd, exit_ret_val);
257 return NULL;
258 }
259
thread_func_atomic(apr_thread_t * thd,void * data)260 void *APR_THREAD_FUNC thread_func_atomic(apr_thread_t *thd, void *data)
261 {
262 int i;
263
264 for (i = 0; i < NUM_ITERATIONS ; i++) {
265 apr_atomic_inc32(&atomic_ops);
266 apr_atomic_add32(&atomic_ops, 2);
267 apr_atomic_dec32(&atomic_ops);
268 apr_atomic_dec32(&atomic_ops);
269 }
270 apr_thread_exit(thd, exit_ret_val);
271 return NULL;
272 }
273
test_atomics_threaded(abts_case * tc,void * data)274 static void test_atomics_threaded(abts_case *tc, void *data)
275 {
276 apr_thread_t *t1[NUM_THREADS];
277 apr_thread_t *t2[NUM_THREADS];
278 apr_status_t rv;
279 int i;
280
281 #ifdef HAVE_PTHREAD_SETCONCURRENCY
282 pthread_setconcurrency(8);
283 #endif
284
285 rv = apr_thread_mutex_create(&thread_lock, APR_THREAD_MUTEX_DEFAULT, p);
286 APR_ASSERT_SUCCESS(tc, "Could not create lock", rv);
287
288 for (i = 0; i < NUM_THREADS; i++) {
289 apr_status_t r1, r2;
290 r1 = apr_thread_create(&t1[i], NULL, thread_func_mutex, NULL, p);
291 r2 = apr_thread_create(&t2[i], NULL, thread_func_atomic, NULL, p);
292 ABTS_ASSERT(tc, "Failed creating threads", !r1 && !r2);
293 }
294
295 for (i = 0; i < NUM_THREADS; i++) {
296 apr_status_t s1, s2;
297 apr_thread_join(&s1, t1[i]);
298 apr_thread_join(&s2, t2[i]);
299
300 ABTS_ASSERT(tc, "Invalid return value from thread_join",
301 s1 == exit_ret_val && s2 == exit_ret_val);
302 }
303
304 ABTS_INT_EQUAL(tc, NUM_THREADS * NUM_ITERATIONS, mutex_locks);
305 ABTS_INT_EQUAL(tc, NUM_THREADS * NUM_ITERATIONS,
306 apr_atomic_read32(&atomic_ops));
307
308 rv = apr_thread_mutex_destroy(thread_lock);
309 ABTS_ASSERT(tc, "Failed creating threads", rv == APR_SUCCESS);
310 }
311
312 #undef NUM_THREADS
313 #define NUM_THREADS 7
314
315 typedef struct tbox_t tbox_t;
316
317 struct tbox_t {
318 abts_case *tc;
319 apr_uint32_t *mem;
320 apr_uint32_t preval;
321 apr_uint32_t postval;
322 apr_uint32_t loop;
323 void (*func)(tbox_t *box);
324 };
325
busyloop_read32(tbox_t * tbox)326 static APR_INLINE void busyloop_read32(tbox_t *tbox)
327 {
328 apr_uint32_t val;
329
330 do {
331 val = apr_atomic_read32(tbox->mem);
332
333 if (val != tbox->preval)
334 apr_thread_yield();
335 else
336 break;
337 } while (1);
338 }
339
busyloop_set32(tbox_t * tbox)340 static void busyloop_set32(tbox_t *tbox)
341 {
342 do {
343 busyloop_read32(tbox);
344 apr_atomic_set32(tbox->mem, tbox->postval);
345 } while (--tbox->loop);
346 }
347
busyloop_add32(tbox_t * tbox)348 static void busyloop_add32(tbox_t *tbox)
349 {
350 apr_uint32_t val;
351
352 do {
353 busyloop_read32(tbox);
354 val = apr_atomic_add32(tbox->mem, tbox->postval);
355 apr_thread_mutex_lock(thread_lock);
356 ABTS_INT_EQUAL(tbox->tc, val, tbox->preval);
357 apr_thread_mutex_unlock(thread_lock);
358 } while (--tbox->loop);
359 }
360
busyloop_sub32(tbox_t * tbox)361 static void busyloop_sub32(tbox_t *tbox)
362 {
363 do {
364 busyloop_read32(tbox);
365 apr_atomic_sub32(tbox->mem, tbox->postval);
366 } while (--tbox->loop);
367 }
368
busyloop_inc32(tbox_t * tbox)369 static void busyloop_inc32(tbox_t *tbox)
370 {
371 apr_uint32_t val;
372
373 do {
374 busyloop_read32(tbox);
375 val = apr_atomic_inc32(tbox->mem);
376 apr_thread_mutex_lock(thread_lock);
377 ABTS_INT_EQUAL(tbox->tc, val, tbox->preval);
378 apr_thread_mutex_unlock(thread_lock);
379 } while (--tbox->loop);
380 }
381
busyloop_dec32(tbox_t * tbox)382 static void busyloop_dec32(tbox_t *tbox)
383 {
384 apr_uint32_t val;
385
386 do {
387 busyloop_read32(tbox);
388 val = apr_atomic_dec32(tbox->mem);
389 apr_thread_mutex_lock(thread_lock);
390 ABTS_INT_NEQUAL(tbox->tc, 0, val);
391 apr_thread_mutex_unlock(thread_lock);
392 } while (--tbox->loop);
393 }
394
busyloop_cas32(tbox_t * tbox)395 static void busyloop_cas32(tbox_t *tbox)
396 {
397 apr_uint32_t val;
398
399 do {
400 do {
401 val = apr_atomic_cas32(tbox->mem, tbox->postval, tbox->preval);
402
403 if (val != tbox->preval)
404 apr_thread_yield();
405 else
406 break;
407 } while (1);
408 } while (--tbox->loop);
409 }
410
busyloop_xchg32(tbox_t * tbox)411 static void busyloop_xchg32(tbox_t *tbox)
412 {
413 apr_uint32_t val;
414
415 do {
416 busyloop_read32(tbox);
417 val = apr_atomic_xchg32(tbox->mem, tbox->postval);
418 apr_thread_mutex_lock(thread_lock);
419 ABTS_INT_EQUAL(tbox->tc, val, tbox->preval);
420 apr_thread_mutex_unlock(thread_lock);
421 } while (--tbox->loop);
422 }
423
thread_func_busyloop(apr_thread_t * thd,void * data)424 static void *APR_THREAD_FUNC thread_func_busyloop(apr_thread_t *thd, void *data)
425 {
426 tbox_t *tbox = data;
427
428 tbox->func(tbox);
429
430 apr_thread_exit(thd, 0);
431
432 return NULL;
433 }
434
test_atomics_busyloop_threaded(abts_case * tc,void * data)435 static void test_atomics_busyloop_threaded(abts_case *tc, void *data)
436 {
437 unsigned int i;
438 apr_status_t rv;
439 apr_uint32_t count = 0;
440 tbox_t tbox[NUM_THREADS];
441 apr_thread_t *thread[NUM_THREADS];
442
443 rv = apr_thread_mutex_create(&thread_lock, APR_THREAD_MUTEX_DEFAULT, p);
444 APR_ASSERT_SUCCESS(tc, "Could not create lock", rv);
445
446 /* get ready */
447 for (i = 0; i < NUM_THREADS; i++) {
448 tbox[i].tc = tc;
449 tbox[i].mem = &count;
450 tbox[i].loop = 50;
451 }
452
453 tbox[0].preval = 98;
454 tbox[0].postval = 3891;
455 tbox[0].func = busyloop_add32;
456
457 tbox[1].preval = 3989;
458 tbox[1].postval = 1010;
459 tbox[1].func = busyloop_sub32;
460
461 tbox[2].preval = 2979;
462 tbox[2].postval = 0; /* not used */
463 tbox[2].func = busyloop_inc32;
464
465 tbox[3].preval = 2980;
466 tbox[3].postval = 16384;
467 tbox[3].func = busyloop_set32;
468
469 tbox[4].preval = 16384;
470 tbox[4].postval = 0; /* not used */
471 tbox[4].func = busyloop_dec32;
472
473 tbox[5].preval = 16383;
474 tbox[5].postval = 1048576;
475 tbox[5].func = busyloop_cas32;
476
477 tbox[6].preval = 1048576;
478 tbox[6].postval = 98; /* goto tbox[0] */
479 tbox[6].func = busyloop_xchg32;
480
481 /* get set */
482 for (i = 0; i < NUM_THREADS; i++) {
483 rv = apr_thread_create(&thread[i], NULL, thread_func_busyloop,
484 &tbox[i], p);
485 ABTS_ASSERT(tc, "Failed creating thread", rv == APR_SUCCESS);
486 }
487
488 /* go! */
489 apr_atomic_set32(tbox->mem, 98);
490
491 for (i = 0; i < NUM_THREADS; i++) {
492 apr_status_t retval;
493 rv = apr_thread_join(&retval, thread[i]);
494 ABTS_ASSERT(tc, "Thread join failed", rv == APR_SUCCESS);
495 ABTS_ASSERT(tc, "Invalid return value from thread_join", retval == 0);
496 }
497
498 ABTS_INT_EQUAL(tbox->tc, 98, count);
499
500 rv = apr_thread_mutex_destroy(thread_lock);
501 ABTS_ASSERT(tc, "Failed creating threads", rv == APR_SUCCESS);
502 }
503
504 #endif /* !APR_HAS_THREADS */
505
testatomic(abts_suite * suite)506 abts_suite *testatomic(abts_suite *suite)
507 {
508 suite = ADD_SUITE(suite)
509
510 abts_run_test(suite, test_init, NULL);
511 abts_run_test(suite, test_set32, NULL);
512 abts_run_test(suite, test_read32, NULL);
513 abts_run_test(suite, test_dec32, NULL);
514 abts_run_test(suite, test_xchg32, NULL);
515 abts_run_test(suite, test_xchgptr, NULL);
516 abts_run_test(suite, test_cas_equal, NULL);
517 abts_run_test(suite, test_cas_equal_nonnull, NULL);
518 abts_run_test(suite, test_cas_notequal, NULL);
519 abts_run_test(suite, test_casptr_equal, NULL);
520 abts_run_test(suite, test_casptr_equal_nonnull, NULL);
521 abts_run_test(suite, test_casptr_notequal, NULL);
522 abts_run_test(suite, test_add32, NULL);
523 abts_run_test(suite, test_add32_neg, NULL);
524 abts_run_test(suite, test_inc32, NULL);
525 abts_run_test(suite, test_set_add_inc_sub, NULL);
526 abts_run_test(suite, test_wrap_zero, NULL);
527 abts_run_test(suite, test_inc_neg1, NULL);
528
529 #if APR_HAS_THREADS
530 abts_run_test(suite, test_atomics_threaded, NULL);
531 abts_run_test(suite, test_atomics_busyloop_threaded, NULL);
532 #endif
533
534 return suite;
535 }
536
537