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.
18 #include "apr_thread_proc.h"
19 #include "apr_file_io.h"
20 #include "apr_proc_mutex.h"
21 #include "apr_errno.h"
22 #include "apr_general.h"
23 #include "apr_getopt.h"
33 #define MAX_COUNTER (MAX_ITER * CHILDREN)
35 static apr_proc_mutex_t *proc_lock;
36 static volatile int *x;
38 /* a slower more racy way to implement (*x)++ */
39 static int increment(int n)
45 static void make_child(CuTest *tc, apr_proc_t **proc, apr_pool_t *p)
49 *proc = apr_pcalloc(p, sizeof(**proc));
51 /* slight delay to allow things to settle */
54 rv = apr_proc_fork(*proc, p);
55 if (rv == APR_INCHILD) {
57 /* The parent process has setup all processes to call apr_terminate
58 * at exit. But, that means that all processes must also call
59 * apr_initialize at startup. You cannot have an unequal number
60 * of apr_terminate and apr_initialize calls. If you do, bad things
61 * will happen. In this case, the bad thing is that if the mutex
62 * is a semaphore, it will be destroyed before all of the processes
63 * die. That means that the test will most likely fail.
67 if (apr_proc_mutex_child_init(&proc_lock, NULL, p))
71 if (apr_proc_mutex_lock(proc_lock))
75 if (apr_proc_mutex_unlock(proc_lock))
77 } while (i < MAX_ITER);
81 CuAssert(tc, "fork failed", rv == APR_INPARENT);
84 /* Wait for a child process and check it terminated with success. */
85 static void await_child(CuTest *tc, apr_proc_t *proc)
91 rv = apr_proc_wait(proc, &code, &why, APR_WAIT);
92 CuAssert(tc, "child did not terminate with success",
93 rv == APR_CHILD_DONE && why == APR_PROC_EXIT && code == 0);
96 static void test_exclusive(CuTest *tc, const char *lockname)
98 apr_proc_t *child[CHILDREN];
102 rv = apr_proc_mutex_create(&proc_lock, lockname, APR_LOCK_DEFAULT, p);
103 apr_assert_success(tc, "create the mutex", rv);
105 for (n = 0; n < CHILDREN; n++)
106 make_child(tc, &child[n], p);
108 for (n = 0; n < CHILDREN; n++)
109 await_child(tc, child[n]);
111 CuAssert(tc, "Locks don't appear to work", *x == MAX_COUNTER);
115 static void proc_mutex(CuTest *tc)
119 const char *shmname = "tpm.shm";
122 /* Use anonymous shm if available. */
123 rv = apr_shm_create(&shm, sizeof(int), NULL, p);
124 if (rv == APR_ENOTIMPL) {
125 apr_file_remove(shmname, p);
126 rv = apr_shm_create(&shm, sizeof(int), shmname, p);
129 apr_assert_success(tc, "create shm segment", rv);
131 x = apr_shm_baseaddr_get(shm);
132 test_exclusive(tc, NULL);
134 CuNotImpl(tc, "APR lacks fork() support");
139 CuSuite *testprocmutex(void)
141 CuSuite *suite = CuSuiteNew("Cross-Process Mutexes");
143 SUITE_ADD_TEST(suite, proc_mutex);