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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "apr_thread_proc.h"
20 #include "apr_errno.h"
21 #include "apr_general.h"
22 #include "apr_atomic.h"
29 #if !(defined WIN32) && !(defined NETWARE)
34 apr_atomic_t y; /* atomic locks */
36 static apr_status_t check_basic_atomics(volatile apr_atomic_t*p)
39 apr_uint32_t casval = 0;
40 float object1, object2;
41 #if !(defined NETWARE)
42 volatile void *casptr;
48 apr_atomic_set(&y, 2);
49 printf("%-60s", "testing apr_atomic_dec");
50 if (apr_atomic_dec(&y) == 0) {
51 fprintf(stderr, "Failed\noldval =%d should not be zero\n",
55 if (apr_atomic_dec(&y) != 0) {
56 fprintf(stderr, "Failed\noldval =%d should be zero\n",
62 printf("%-60s", "testing CAS");
63 oldval = apr_atomic_cas(&casval, 12, 0);
65 fprintf(stderr, "Failed\noldval =%d should be zero\n", oldval);
69 printf("%-60s", "testing CAS - match non-null");
70 oldval = apr_atomic_cas(&casval, 23, 12);
72 fprintf(stderr, "Failed\noldval =%d should be 12 y=%d\n",
77 printf("%-60s", "testing CAS - no match");
78 oldval = apr_atomic_cas(&casval, 23, 12);
80 fprintf(stderr, "Failed\noldval =%d should be 23 y=%d\n",
86 printf("%-60s", "testing CAS for pointers");
88 oldptr = apr_atomic_casptr(&casptr, &object1, 0);
90 fprintf(stderr, "Failed\noldval =%p should be zero\n", oldptr);
94 printf("%-60s", "testing CAS for pointers - match non-null");
95 oldptr = apr_atomic_casptr(&casptr, &object2, &object1);
96 if (oldptr != &object1) {
97 fprintf(stderr, "Failed\noldval =%p should be %p\n", oldptr, &object1);
101 printf("%-60s", "testing CAS for pointers - no match");
102 oldptr = apr_atomic_casptr(&casptr, &object2, &object1);
103 if (oldptr != &object2) {
104 fprintf(stderr, "Failed\noldval =%p should be %p\n", oldptr, &object2);
109 printf("%-60s", "testing add");
110 apr_atomic_set(&y, 23);
111 apr_atomic_add(&y, 4);
112 if (apr_atomic_read(&y) != 27) {
114 "Failed\nAtomic Add doesn't add up ;( expected 27 got %d\n",
120 printf("%-60s", "testing add/inc");
121 apr_atomic_set(&y, 0);
122 apr_atomic_add(&y, 20);
124 if (apr_atomic_read(&y) != 21) {
125 fprintf(stderr, "Failed.\natomics do not add up\n");
128 fprintf(stdout, "OK\n");
141 "This program won't work fully on this platform because there is no "
142 "support for threads.\n");
143 if (apr_pool_create(&context, NULL) != APR_SUCCESS) {
145 fprintf(stderr, "Failed.\nCould not initialize\n");
148 rv = apr_atomic_init(context);
149 if (rv != APR_SUCCESS) {
150 fprintf(stderr, "Failed.\nCould not initialize atomics\n");
153 rv = check_basic_atomics(&y);
154 if (rv != APR_SUCCESS) {
155 fprintf(stderr, "Failed.\n");
160 #else /* !APR_HAS_THREADS */
162 void * APR_THREAD_FUNC thread_func_mutex(apr_thread_t *thd, void *data);
163 void * APR_THREAD_FUNC thread_func_atomic(apr_thread_t *thd, void *data);
164 void * APR_THREAD_FUNC thread_func_none(apr_thread_t *thd, void *data);
166 apr_thread_mutex_t *thread_lock;
167 volatile long x = 0; /* mutex locks */
168 volatile long z = 0; /* no locks */
170 apr_status_t exit_ret_val = 123; /* just some made up number to check on later */
172 #define NUM_THREADS 50
173 #define NUM_ITERATIONS 20000
174 void * APR_THREAD_FUNC thread_func_mutex(apr_thread_t *thd, void *data)
178 for (i = 0; i < NUM_ITERATIONS; i++) {
179 apr_thread_mutex_lock(thread_lock);
181 apr_thread_mutex_unlock(thread_lock);
183 apr_thread_exit(thd, exit_ret_val);
187 void * APR_THREAD_FUNC thread_func_atomic(apr_thread_t *thd, void *data)
191 for (i = 0; i < NUM_ITERATIONS ; i++) {
193 apr_atomic_add(&y, 2);
197 apr_thread_exit(thd, exit_ret_val);
201 void * APR_THREAD_FUNC thread_func_none(apr_thread_t *thd, void *data)
205 for (i = 0; i < NUM_ITERATIONS ; i++) {
208 apr_thread_exit(thd, exit_ret_val);
212 int main(int argc, char**argv)
214 apr_thread_t *t1[NUM_THREADS];
215 apr_thread_t *t2[NUM_THREADS];
216 apr_status_t r1[NUM_THREADS];
217 apr_status_t r2[NUM_THREADS];
218 apr_status_t s1[NUM_THREADS];
219 apr_status_t s2[NUM_THREADS];
226 if (argc == 2 && argv[1][0] == 'm') {
233 printf("APR Atomic Test\n===============\n\n");
234 #if !(defined WIN32) && !(defined NETWARE) && !(defined __MVS__) && !(defined DARWIN)
235 pthread_setconcurrency(8);
237 printf("%-60s", "Initializing the context");
238 if (apr_pool_create(&context, NULL) != APR_SUCCESS) {
240 fprintf(stderr, "Failed.\nCould not initialize\n");
246 printf("%-60s", "Initializing the lock");
247 rv = apr_thread_mutex_create(&thread_lock, APR_THREAD_MUTEX_DEFAULT,
249 if (rv != APR_SUCCESS) {
251 fprintf(stderr, "Failed\nCould not create lock\n");
256 printf("%-60s", "Initializing the atomics");
257 rv = apr_atomic_init(context);
258 if (rv != APR_SUCCESS) {
259 fprintf(stderr, "Failed.\n");
264 rv = check_basic_atomics(&y);
265 if (rv != APR_SUCCESS) {
266 fprintf(stderr, "Failed.\n");
269 apr_atomic_set(&y, 0);
271 printf("%-60s", "Starting all the threads");
272 for (i = 0; i < NUM_THREADS; i++) {
273 r1[i] = apr_thread_create(&t1[i], NULL,
274 (mutex == 1 ? thread_func_mutex : thread_func_atomic),
276 r2[i] = apr_thread_create(&t2[i], NULL, thread_func_none, NULL,
278 if (r1[i] != APR_SUCCESS || r2[i] != APR_SUCCESS ) {
280 fprintf(stderr, "Failed\nError starting thread in group %d\n",i);
286 printf("%-60s\n", "Waiting for threads to exit");
287 printf("%-60s", "(Note that this may take a while to complete.)");
290 for (i = 0; i < NUM_THREADS; i++) {
291 apr_thread_join(&s1[i], t1[i]);
292 apr_thread_join(&s2[i], t2[i]);
293 if (s1[i] != exit_ret_val || s2[i] != exit_ret_val) {
295 "Invalid return value\n"
296 "Got %d/%d, but expected %d for all \n",
297 s1[i], s2[i], exit_ret_val);
303 printf("%-60s", "Checking if mutex locks worked");
304 if (x != NUM_THREADS * NUM_ITERATIONS) {
307 "No!\nThe locks didn't work?? x = %ld instead of %ld\n",
309 (long)NUM_THREADS * NUM_ITERATIONS);
316 printf("%-60s", "Checking if atomic worked");
317 if (apr_atomic_read(&y) != NUM_THREADS * NUM_ITERATIONS) {
320 "No!\nThe atomics didn't work?? y = %ld instead of %ld\n",
321 (long)apr_atomic_read(&y),
322 (long)NUM_THREADS * NUM_ITERATIONS);
328 printf("%-60s", "Checking if nolock worked");
329 if (z != NUM_THREADS * NUM_ITERATIONS) {
333 "The no-locks didn't work. z = %ld instead of %ld\n",
335 (long)NUM_THREADS * NUM_ITERATIONS);
344 #endif /* !APR_HAS_THREADS */