bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / test / testprocmutex.c
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 #include "apr_shm.h"
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"
24 #include "errno.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include "test_apr.h"
28
29 #if APR_HAS_FORK
30
31 #define MAX_ITER 200
32 #define CHILDREN 6
33 #define MAX_COUNTER (MAX_ITER * CHILDREN)
34
35 static apr_proc_mutex_t *proc_lock;
36 static volatile int *x;
37
38 /* a slower more racy way to implement (*x)++ */
39 static int increment(int n)
40 {
41     apr_sleep(1);
42     return n+1;
43 }
44
45 static void make_child(CuTest *tc, apr_proc_t **proc, apr_pool_t *p)
46 {
47     apr_status_t rv;
48
49     *proc = apr_pcalloc(p, sizeof(**proc));
50
51     /* slight delay to allow things to settle */
52     apr_sleep (1);
53
54     rv = apr_proc_fork(*proc, p);
55     if (rv == APR_INCHILD) {
56         int i = 0;
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.
64          */
65         apr_initialize();
66
67         if (apr_proc_mutex_child_init(&proc_lock, NULL, p))
68             exit(1);
69
70         do {
71             if (apr_proc_mutex_lock(proc_lock))
72                 exit(1);
73             i++;
74             *x = increment(*x);
75             if (apr_proc_mutex_unlock(proc_lock))
76                 exit(1);
77         } while (i < MAX_ITER);
78         exit(0);
79     } 
80
81     CuAssert(tc, "fork failed", rv == APR_INPARENT);
82 }
83
84 /* Wait for a child process and check it terminated with success. */
85 static void await_child(CuTest *tc, apr_proc_t *proc)
86 {
87     int code;
88     apr_exit_why_e why;
89     apr_status_t rv;
90
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);
94 }
95
96 static void test_exclusive(CuTest *tc, const char *lockname)
97 {
98     apr_proc_t *child[CHILDREN];
99     apr_status_t rv;
100     int n;
101  
102     rv = apr_proc_mutex_create(&proc_lock, lockname, APR_LOCK_DEFAULT, p);
103     apr_assert_success(tc, "create the mutex", rv);
104  
105     for (n = 0; n < CHILDREN; n++)
106         make_child(tc, &child[n], p);
107
108     for (n = 0; n < CHILDREN; n++)
109         await_child(tc, child[n]);
110     
111     CuAssert(tc, "Locks don't appear to work", *x == MAX_COUNTER);
112 }
113 #endif
114
115 static void proc_mutex(CuTest *tc)
116 {
117 #if APR_HAS_FORK
118     apr_status_t rv;
119     const char *shmname = "tpm.shm";
120     apr_shm_t *shm;
121
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);
127     }
128
129     apr_assert_success(tc, "create shm segment", rv);
130
131     x = apr_shm_baseaddr_get(shm);
132     test_exclusive(tc, NULL);
133 #else
134     CuNotImpl(tc, "APR lacks fork() support");
135 #endif
136 }
137
138
139 CuSuite *testprocmutex(void)
140 {
141     CuSuite *suite = CuSuiteNew("Cross-Process Mutexes");
142
143     SUITE_ADD_TEST(suite, proc_mutex);
144
145     return suite;
146 }
147