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.
17 #include "apr_thread_proc.h"
18 #include "apr_thread_mutex.h"
19 #include "apr_thread_rwlock.h"
20 #include "apr_file_io.h"
21 #include "apr_errno.h"
22 #include "apr_general.h"
23 #include "apr_getopt.h"
32 printf("This program won't work on this platform because there is no "
33 "support for threads.\n");
36 #else /* !APR_HAS_THREADS */
38 #define MAX_COUNTER 1000000
41 static long mutex_counter;
43 static apr_thread_mutex_t *thread_lock;
44 void * APR_THREAD_FUNC thread_mutex_func(apr_thread_t *thd, void *data);
45 apr_status_t test_thread_mutex(int num_threads); /* apr_thread_mutex_t */
47 static apr_thread_rwlock_t *thread_rwlock;
48 void * APR_THREAD_FUNC thread_rwlock_func(apr_thread_t *thd, void *data);
49 apr_status_t test_thread_rwlock(int num_threads); /* apr_thread_rwlock_t */
51 int test_thread_mutex_nested(int num_threads);
56 void * APR_THREAD_FUNC thread_mutex_func(apr_thread_t *thd, void *data)
60 for (i = 0; i < MAX_COUNTER; i++) {
61 apr_thread_mutex_lock(thread_lock);
63 apr_thread_mutex_unlock(thread_lock);
68 void * APR_THREAD_FUNC thread_rwlock_func(apr_thread_t *thd, void *data)
72 for (i = 0; i < MAX_COUNTER; i++) {
73 apr_thread_rwlock_wrlock(thread_rwlock);
75 apr_thread_rwlock_unlock(thread_rwlock);
80 int test_thread_mutex(int num_threads)
82 apr_thread_t *t[MAX_THREADS];
83 apr_status_t s[MAX_THREADS];
84 apr_time_t time_start, time_stop;
89 printf("apr_thread_mutex_t Tests\n");
90 printf("%-60s", " Initializing the apr_thread_mutex_t (UNNESTED)");
91 s[0] = apr_thread_mutex_create(&thread_lock, APR_THREAD_MUTEX_UNNESTED, pool);
92 if (s[0] != APR_SUCCESS) {
98 apr_thread_mutex_lock(thread_lock);
99 /* set_concurrency(4)? -aaron */
100 printf(" Starting %d threads ", num_threads);
101 for (i = 0; i < num_threads; ++i) {
102 s[i] = apr_thread_create(&t[i], NULL, thread_mutex_func, NULL, pool);
103 if (s[i] != APR_SUCCESS) {
110 time_start = apr_time_now();
111 apr_thread_mutex_unlock(thread_lock);
113 /* printf("%-60s", " Waiting for threads to exit"); */
114 for (i = 0; i < num_threads; ++i) {
115 apr_thread_join(&s[i], t[i]);
117 /* printf("OK\n"); */
119 time_stop = apr_time_now();
120 printf("microseconds: %" APR_INT64_T_FMT " usec\n",
121 (time_stop - time_start));
122 if (mutex_counter != MAX_COUNTER * num_threads)
123 printf("error: counter = %ld\n", mutex_counter);
128 int test_thread_mutex_nested(int num_threads)
130 apr_thread_t *t[MAX_THREADS];
131 apr_status_t s[MAX_THREADS];
132 apr_time_t time_start, time_stop;
137 printf("apr_thread_mutex_t Tests\n");
138 printf("%-60s", " Initializing the apr_thread_mutex_t (NESTED)");
139 s[0] = apr_thread_mutex_create(&thread_lock, APR_THREAD_MUTEX_NESTED, pool);
140 if (s[0] != APR_SUCCESS) {
146 apr_thread_mutex_lock(thread_lock);
147 /* set_concurrency(4)? -aaron */
148 printf(" Starting %d threads ", num_threads);
149 for (i = 0; i < num_threads; ++i) {
150 s[i] = apr_thread_create(&t[i], NULL, thread_mutex_func, NULL, pool);
151 if (s[i] != APR_SUCCESS) {
158 time_start = apr_time_now();
159 apr_thread_mutex_unlock(thread_lock);
161 /* printf("%-60s", " Waiting for threads to exit"); */
162 for (i = 0; i < num_threads; ++i) {
163 apr_thread_join(&s[i], t[i]);
165 /* printf("OK\n"); */
167 time_stop = apr_time_now();
168 printf("microseconds: %" APR_INT64_T_FMT " usec\n",
169 (time_stop - time_start));
170 if (mutex_counter != MAX_COUNTER * num_threads)
171 printf("error: counter = %ld\n", mutex_counter);
176 int test_thread_rwlock(int num_threads)
178 apr_thread_t *t[MAX_THREADS];
179 apr_status_t s[MAX_THREADS];
180 apr_time_t time_start, time_stop;
185 printf("apr_thread_rwlock_t Tests\n");
186 printf("%-60s", " Initializing the apr_thread_rwlock_t");
187 s[0] = apr_thread_rwlock_create(&thread_rwlock, pool);
188 if (s[0] != APR_SUCCESS) {
194 apr_thread_rwlock_wrlock(thread_rwlock);
195 /* set_concurrency(4)? -aaron */
196 printf(" Starting %d threads ", num_threads);
197 for (i = 0; i < num_threads; ++i) {
198 s[i] = apr_thread_create(&t[i], NULL, thread_rwlock_func, NULL, pool);
199 if (s[i] != APR_SUCCESS) {
206 time_start = apr_time_now();
207 apr_thread_rwlock_unlock(thread_rwlock);
209 /* printf("%-60s", " Waiting for threads to exit"); */
210 for (i = 0; i < num_threads; ++i) {
211 apr_thread_join(&s[i], t[i]);
213 /* printf("OK\n"); */
215 time_stop = apr_time_now();
216 printf("microseconds: %" APR_INT64_T_FMT " usec\n",
217 (time_stop - time_start));
218 if (mutex_counter != MAX_COUNTER * num_threads)
219 printf("error: counter = %ld\n", mutex_counter);
224 int main(int argc, const char * const *argv)
228 const char *lockname = "multi.lock";
233 printf("APR Lock Performance Test\n==============\n\n");
236 atexit(apr_terminate);
238 if (apr_pool_create(&pool, NULL) != APR_SUCCESS)
241 if ((rv = apr_getopt_init(&opt, pool, argc, argv)) != APR_SUCCESS) {
242 fprintf(stderr, "Could not set up to parse options: [%d] %s\n",
243 rv, apr_strerror(rv, errmsg, sizeof errmsg));
247 while ((rv = apr_getopt(opt, "f:", &optchar, &optarg)) == APR_SUCCESS) {
248 if (optchar == 'f') {
253 if (rv != APR_SUCCESS && rv != APR_EOF) {
254 fprintf(stderr, "Could not parse options: [%d] %s\n",
255 rv, apr_strerror(rv, errmsg, sizeof errmsg));
259 for (i = 1; i <= MAX_THREADS; ++i) {
260 if ((rv = test_thread_mutex(i)) != APR_SUCCESS) {
261 fprintf(stderr,"thread_mutex test failed : [%d] %s\n",
262 rv, apr_strerror(rv, (char*)errmsg, 200));
266 if ((rv = test_thread_mutex_nested(i)) != APR_SUCCESS) {
267 fprintf(stderr,"thread_mutex (NESTED) test failed : [%d] %s\n",
268 rv, apr_strerror(rv, (char*)errmsg, 200));
272 if ((rv = test_thread_rwlock(i)) != APR_SUCCESS) {
273 fprintf(stderr,"thread_rwlock test failed : [%d] %s\n",
274 rv, apr_strerror(rv, (char*)errmsg, 200));
282 #endif /* !APR_HAS_THREADS */