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.
23 #include "apr_general.h"
24 #include "apr_proc_mutex.h"
25 #include "apr_global_mutex.h"
26 #include "apr_thread_proc.h"
31 printf("This test requires APR thread support.\n");
35 #else /* APR_HAS_THREADS */
37 static apr_thread_mutex_t *thread_mutex;
38 static apr_proc_mutex_t *proc_mutex;
39 static apr_global_mutex_t *global_mutex;
41 static volatile int counter;
42 typedef enum {TEST_GLOBAL, TEST_PROC} test_mode_e;
44 static void lock_init(apr_lockmech_e mech, test_mode_e test_mode)
46 if (test_mode == TEST_PROC) {
47 assert(apr_proc_mutex_create(&proc_mutex,
53 assert(apr_global_mutex_create(&global_mutex,
60 static void lock_destroy(test_mode_e test_mode)
62 if (test_mode == TEST_PROC) {
63 assert(apr_proc_mutex_destroy(proc_mutex) == APR_SUCCESS);
66 assert(apr_global_mutex_destroy(global_mutex) == APR_SUCCESS);
70 static void lock_grab(test_mode_e test_mode)
72 if (test_mode == TEST_PROC) {
73 assert(apr_proc_mutex_lock(proc_mutex) == APR_SUCCESS);
76 assert(apr_global_mutex_lock(global_mutex) == APR_SUCCESS);
80 static void lock_release(test_mode_e test_mode)
82 if (test_mode == TEST_PROC) {
83 assert(apr_proc_mutex_unlock(proc_mutex) == APR_SUCCESS);
86 assert(apr_global_mutex_unlock(global_mutex) == APR_SUCCESS);
90 static void * APR_THREAD_FUNC eachThread(apr_thread_t *id, void *p)
92 test_mode_e test_mode = (test_mode_e)p;
96 assert(apr_thread_mutex_lock(thread_mutex) == APR_SUCCESS);
97 assert(apr_thread_mutex_unlock(thread_mutex) == APR_SUCCESS);
98 lock_release(test_mode);
102 static void test_mech_mode(apr_lockmech_e mech, const char *mech_name,
103 test_mode_e test_mode)
105 apr_thread_t *threads[20];
110 printf("Trying %s mutexes with mechanism `%s'...\n",
111 test_mode == TEST_GLOBAL ? "global" : "proc", mech_name);
113 assert(numThreads <= sizeof(threads) / sizeof(threads[0]));
115 assert(apr_pool_create(&p, NULL) == APR_SUCCESS);
117 assert(apr_thread_mutex_create(&thread_mutex, 0, p) == APR_SUCCESS);
118 assert(apr_thread_mutex_lock(thread_mutex) == APR_SUCCESS);
120 lock_init(mech, test_mode);
125 while (i < numThreads)
127 rv = apr_thread_create(&threads[i],
132 if (rv != APR_SUCCESS) {
133 fprintf(stderr, "apr_thread_create->%d\n", rv);
139 apr_sleep(apr_time_from_sec(5));
141 if (test_mode == TEST_PROC) {
142 printf(" Mutex mechanism `%s' is %sglobal in scope on this platform.\n",
143 mech_name, counter == 1 ? "" : "not ");
147 fprintf(stderr, "\n!!!apr_global_mutex operations are broken on this "
148 "platform for mutex mechanism `%s'!\n"
149 "They don't block out threads within the same process.\n",
151 fprintf(stderr, "counter value: %d\n", counter);
155 printf(" no problems encountered...\n");
159 assert(apr_thread_mutex_unlock(thread_mutex) == APR_SUCCESS);
162 while (i < numThreads)
164 apr_status_t ignored;
166 rv = apr_thread_join(&ignored,
168 assert(rv == APR_SUCCESS);
172 lock_destroy(test_mode);
173 apr_thread_mutex_destroy(thread_mutex);
177 static void test_mech(apr_lockmech_e mech, const char *mech_name)
179 test_mech_mode(mech, mech_name, TEST_PROC);
180 test_mech_mode(mech, mech_name, TEST_GLOBAL);
187 const char *mech_name;
189 {APR_LOCK_DEFAULT, "default"}
190 #if APR_HAS_FLOCK_SERIALIZE
191 ,{APR_LOCK_FLOCK, "flock"}
193 #if APR_HAS_SYSVSEM_SERIALIZE
194 ,{APR_LOCK_SYSVSEM, "sysvsem"}
196 #if APR_HAS_POSIXSEM_SERIALIZE
197 ,{APR_LOCK_POSIXSEM, "posix"}
199 #if APR_HAS_FCNTL_SERIALIZE
200 ,{APR_LOCK_FCNTL, "fcntl"}
202 #if APR_HAS_PROC_PTHREAD_SERIALIZE
203 ,{APR_LOCK_PROC_PTHREAD, "proc_pthread"}
208 assert(apr_initialize() == APR_SUCCESS);
210 for (i = 0; i < sizeof(lockmechs) / sizeof(lockmechs[0]); i++) {
211 test_mech(lockmechs[i].mech, lockmechs[i].mech_name);
218 #endif /* APR_HAS_THREADS */