Apache HTTPD
testmutexscope.c
Go to the documentation of this file.
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/* This program won't run or check correctly if assert() is disabled. */
18#undef NDEBUG
19#include <assert.h>
20#include <errno.h>
21#include <stdio.h>
22#include <stdlib.h>
23
24#include "apr.h"
25#include "apr_general.h"
26#include "apr_proc_mutex.h"
27#include "apr_global_mutex.h"
28#include "apr_thread_proc.h"
29
30#if !APR_HAS_THREADS
31int main(void)
32{
33 printf("This test requires APR thread support.\n");
34 return 0;
35}
36
37#else /* APR_HAS_THREADS */
38
42static apr_pool_t *p;
43static volatile int counter;
44typedef enum {TEST_GLOBAL, TEST_PROC} test_mode_e;
45
47{
48 apr_status_t rv;
49 if (test_mode == TEST_PROC) {
51 NULL,
52 mech,
53 p);
54 }
55 else {
57 NULL,
58 mech,
59 p);
60 }
61 return rv;
62}
63
65{
66 if (test_mode == TEST_PROC) {
68 }
69 else {
71 }
72}
73
75{
76 if (test_mode == TEST_PROC) {
78 }
79 else {
81 }
82}
83
85{
86 if (test_mode == TEST_PROC) {
88 }
89 else {
91 }
92}
93
94static void * APR_THREAD_FUNC eachThread(apr_thread_t *id, void *p)
95{
97
99 ++counter;
103 apr_thread_exit(id, 0);
104 return NULL;
105}
106
107static void test_mech_mode(apr_lockmech_e mech, const char *mech_name,
109{
110 apr_thread_t *threads[20];
111 int numThreads = 5;
112 int i;
113 apr_status_t rv;
114
115 printf("Trying %s mutexes with mechanism `%s'...\n",
116 test_mode == TEST_GLOBAL ? "global" : "proc", mech_name);
117
118 assert(numThreads <= sizeof(threads) / sizeof(threads[0]));
119
121
124
125 rv = lock_init(mech, test_mode);
126 if (rv != APR_SUCCESS) {
127 char errmsg[256];
128 printf("%s mutexes with mechanism `%s': %s\n",
129 test_mode == TEST_GLOBAL ? "Global" : "Proc", mech_name,
130 apr_strerror(rv, errmsg, sizeof errmsg));
131 if (rv != APR_ENOTIMPL || mech == APR_LOCK_DEFAULT) {
132 exit(1);
133 }
134 return;
135 }
136
137 counter = 0;
138
139 i = 0;
140 while (i < numThreads)
141 {
142 rv = apr_thread_create(&threads[i],
143 NULL,
145 (void *)(apr_uintptr_t)test_mode,
146 p);
147 if (rv != APR_SUCCESS) {
148 fprintf(stderr, "apr_thread_create->%d\n", rv);
149 exit(1);
150 }
151 ++i;
152 }
153
155
156 if (test_mode == TEST_PROC) {
157 printf(" mutex mechanism `%s' is %sglobal in scope on this platform.\n",
158 mech_name, counter == 1 ? "" : "*NOT* ");
159 }
160 else {
161 if (counter != 1) {
162 fprintf(stderr, "\n!!!apr_global_mutex operations are broken on this "
163 "platform for mutex mechanism `%s'!\n"
164 "They don't block out threads within the same process.\n",
165 mech_name);
166 fprintf(stderr, "counter value: %d\n", counter);
167 exit(1);
168 }
169 else {
170 printf(" no problem encountered...\n");
171 }
172 }
173
175
176 i = 0;
177 while (i < numThreads)
178 {
180
182 threads[i]);
183 assert(rv == APR_SUCCESS);
184 ++i;
185 }
186
190}
191
192static void test_mech(apr_lockmech_e mech, const char *mech_name)
193{
196}
197
198int main(void)
199{
200 struct {
202 const char *mech_name;
203 } lockmechs[] = {
204 {APR_LOCK_DEFAULT, "default"}
205#if APR_HAS_FLOCK_SERIALIZE
206 ,{APR_LOCK_FLOCK, "flock"}
207#endif
208#if APR_HAS_SYSVSEM_SERIALIZE
209 ,{APR_LOCK_SYSVSEM, "sysvsem"}
210#endif
211#if APR_HAS_POSIXSEM_SERIALIZE
212 ,{APR_LOCK_POSIXSEM, "posix"}
213#endif
214#if APR_HAS_FCNTL_SERIALIZE
215 ,{APR_LOCK_FCNTL, "fcntl"}
216#endif
217#if APR_HAS_PROC_PTHREAD_SERIALIZE
218 ,{APR_LOCK_PROC_PTHREAD, "proc_pthread"}
219#endif
220 ,{APR_LOCK_DEFAULT_TIMED, "default_timed"}
221 };
222 int i;
223
225
226 for (i = 0; i < sizeof(lockmechs) / sizeof(lockmechs[0]); i++) {
228 }
229
231 return 0;
232}
233
234#endif /* APR_HAS_THREADS */
APR Miscellaneous library routines.
APR Global Locking Routines.
APR Process Locking Routines.
APR Thread and Process Library.
#define APR_ENOTIMPL
Definition apr_errno.h:476
const char apr_lockmech_e mech
apr_size_t size
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
#define apr_pool_create(newpool, parent)
Definition apr_pools.h:322
apr_lockmech_e
@ APR_LOCK_FLOCK
@ APR_LOCK_SYSVSEM
@ APR_LOCK_POSIXSEM
@ APR_LOCK_DEFAULT_TIMED
@ APR_LOCK_PROC_PTHREAD
@ APR_LOCK_FCNTL
@ APR_LOCK_DEFAULT
#define apr_time_from_sec(sec)
Definition apr_time.h:78
apr_pool_t * p
Definition md_event.c:32
return NULL
Definition mod_so.c:359
int i
Definition mod_so.c:347
apr_status_t apr_thread_exit(apr_thread_t *thd, apr_status_t retval)
Definition thread.c:157
apr_status_t apr_thread_join(apr_status_t *retval, apr_thread_t *thd)
Definition thread.c:166
apr_status_t apr_thread_create(apr_thread_t **new, apr_threadattr_t *attr, apr_thread_start_t func, void *data, apr_pool_t *pool)
Definition thread.c:73
int main(void)
static void proc_mutex(abts_case *tc, void *data)