bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / test / testglobalmutex.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_global_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
30 #define MAX_ITER 4000
31 #define MAX_COUNTER (MAX_ITER * 4)
32
33 apr_global_mutex_t *global_lock;
34 apr_pool_t *pool;
35 volatile int *x;
36
37 static int make_child(apr_proc_t **proc, apr_pool_t *p)
38 {
39     int i = 0;
40     *proc = apr_pcalloc(p, sizeof(**proc));
41
42     /* slight delay to allow things to settle */
43     apr_sleep (1);
44
45     if (apr_proc_fork(*proc, p) == APR_INCHILD) {
46         apr_initialize();
47         
48         apr_global_mutex_child_init(&global_lock, NULL, p);
49
50         while (1) {
51             apr_global_mutex_lock(global_lock); 
52             if (i == MAX_ITER) {
53                 apr_global_mutex_unlock(global_lock); 
54                 exit(1);
55             }
56             i++;
57             (*x)++;
58             apr_global_mutex_unlock(global_lock); 
59         }
60         exit(1);
61     }
62     return APR_SUCCESS;
63 }
64
65 static apr_status_t test_exclusive(const char *lockname)
66 {
67     apr_proc_t *p1, *p2, *p3, *p4;
68     apr_status_t s1, s2, s3, s4;
69  
70     printf("Exclusive lock test\n");
71     printf("%-60s", "    Initializing the lock");
72     s1 = apr_global_mutex_create(&global_lock, lockname, APR_LOCK_DEFAULT, pool);
73  
74     if (s1 != APR_SUCCESS) {
75         printf("Failed!\n");
76         return s1;
77     }
78     printf("OK\n");
79  
80     printf("%-60s", "    Starting all of the processes");
81     fflush(stdout);
82     s1 = make_child(&p1, pool);
83     s2 = make_child(&p2, pool);
84     s3 = make_child(&p3, pool);
85     s4 = make_child(&p4, pool);
86     if (s1 != APR_SUCCESS || s2 != APR_SUCCESS ||
87         s3 != APR_SUCCESS || s4 != APR_SUCCESS) {
88         printf("Failed!\n");
89         return s1;
90     }
91     printf("OK\n");
92  
93     printf("%-60s", "    Waiting for processes to exit");
94     s1 = apr_proc_wait(p1, NULL, NULL, APR_WAIT);
95     s2 = apr_proc_wait(p2, NULL, NULL, APR_WAIT);
96     s3 = apr_proc_wait(p3, NULL, NULL, APR_WAIT);
97     s4 = apr_proc_wait(p4, NULL, NULL, APR_WAIT);
98     printf("OK\n");
99  
100     if ((*x) != MAX_COUNTER) {
101         fprintf(stderr, "Locks don't appear to work!  x = %d instead of %d\n",
102                 (*x), MAX_COUNTER);
103     }
104     else {
105         printf("Test passed\n");
106     }
107     return APR_SUCCESS;
108 }
109
110 int main(int argc, const char * const *argv)
111 {
112     apr_status_t rv;
113     char errmsg[200];
114     const char *lockname = NULL;
115     const char *shmname = "shm.file";
116     apr_getopt_t *opt;
117     char optchar;
118     const char *optarg;
119     apr_shm_t *shm;
120
121     printf("APR Proc Mutex Test\n==============\n\n");
122         
123     apr_initialize();
124     atexit(apr_terminate);
125
126     if (apr_pool_create(&pool, NULL) != APR_SUCCESS)
127         exit(-1);
128
129     if ((rv = apr_getopt_init(&opt, pool, argc, argv)) != APR_SUCCESS) {
130         fprintf(stderr, "Could not set up to parse options: [%d] %s\n",
131                 rv, apr_strerror(rv, errmsg, sizeof errmsg));
132         exit(-1);
133     }
134         
135     while ((rv = apr_getopt(opt, "f:", &optchar, &optarg)) == APR_SUCCESS) {
136         if (optchar == 'f') {
137             lockname = optarg;
138         }
139     }
140
141     if (rv != APR_SUCCESS && rv != APR_EOF) {
142         fprintf(stderr, "Could not parse options: [%d] %s\n",
143                 rv, apr_strerror(rv, errmsg, sizeof errmsg));
144         exit(-1);
145     }
146
147     apr_shm_create(&shm, sizeof(int), shmname, pool);
148     x = apr_shm_baseaddr_get(shm);
149
150     if ((rv = test_exclusive(lockname)) != APR_SUCCESS) {
151         fprintf(stderr,"Exclusive Lock test failed : [%d] %s\n",
152                 rv, apr_strerror(rv, (char*)errmsg, 200));
153         exit(-2);
154     }
155     
156     return 0;
157 }
158