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_global_mutex.h"
21 #include "apr_errno.h"
22 #include "apr_general.h"
23 #include "apr_getopt.h"
31 #define MAX_COUNTER (MAX_ITER * 4)
33 apr_global_mutex_t *global_lock;
37 static int make_child(apr_proc_t **proc, apr_pool_t *p)
40 *proc = apr_pcalloc(p, sizeof(**proc));
42 /* slight delay to allow things to settle */
45 if (apr_proc_fork(*proc, p) == APR_INCHILD) {
48 apr_global_mutex_child_init(&global_lock, NULL, p);
51 apr_global_mutex_lock(global_lock);
53 apr_global_mutex_unlock(global_lock);
58 apr_global_mutex_unlock(global_lock);
65 static apr_status_t test_exclusive(const char *lockname)
67 apr_proc_t *p1, *p2, *p3, *p4;
68 apr_status_t s1, s2, s3, s4;
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);
74 if (s1 != APR_SUCCESS) {
80 printf("%-60s", " Starting all of the processes");
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) {
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);
100 if ((*x) != MAX_COUNTER) {
101 fprintf(stderr, "Locks don't appear to work! x = %d instead of %d\n",
105 printf("Test passed\n");
110 int main(int argc, const char * const *argv)
114 const char *lockname = NULL;
115 const char *shmname = "shm.file";
121 printf("APR Proc Mutex Test\n==============\n\n");
124 atexit(apr_terminate);
126 if (apr_pool_create(&pool, NULL) != APR_SUCCESS)
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));
135 while ((rv = apr_getopt(opt, "f:", &optchar, &optarg)) == APR_SUCCESS) {
136 if (optchar == 'f') {
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));
147 apr_shm_create(&shm, sizeof(int), shmname, pool);
148 x = apr_shm_baseaddr_get(shm);
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));