upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / test / testshmconsumer.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_errno.h"
19 #include "apr_general.h"
20 #include "apr_lib.h"
21 #include "apr_strings.h"
22 #include "apr_time.h"
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #if APR_HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29
30 #if APR_HAS_SHARED_MEMORY
31
32 typedef struct mbox {
33     char msg[1024]; 
34     int msgavail; 
35 } mbox;
36 mbox *boxes;
37
38 #define N_BOXES 10
39 #define SHARED_SIZE (apr_size_t)(N_BOXES * sizeof(mbox))
40 #define SHARED_FILENAME "/tmp/apr.testshm.shm"
41
42 static void msgwait(int sleep_sec, int first_box, int last_box)
43 {
44     int i;
45     apr_time_t start = apr_time_now();
46     apr_interval_time_t sleep_duration = apr_time_from_sec(sleep_sec);
47     while (apr_time_now() - start < sleep_duration) {
48         for (i = first_box; i < last_box; i++) {
49             if (boxes[i].msgavail) {
50                 fprintf(stdout, "Consumer: received a message in box %d, message was: %s\n", 
51                         i, boxes[i].msg); 
52                 boxes[i].msgavail = 0; /* reset back to 0 */
53             }
54         }
55         apr_sleep(apr_time_from_sec(1));
56     }
57     fprintf(stdout, "Consumer: done waiting on mailboxes...\n");
58 }
59
60 int main(void)
61 {
62     apr_status_t rv;
63     apr_pool_t *pool;
64     apr_shm_t *shm;
65     char errmsg[200];
66
67     apr_initialize();
68     
69     printf("APR Shared Memory Test: CONSUMER\n");
70
71     printf("Initializing the pool............................"); 
72     if (apr_pool_create(&pool, NULL) != APR_SUCCESS) {
73         printf("could not initialize pool\n");
74         exit(-1);
75     }
76     printf("OK\n");
77
78     printf("Consumer attaching to name-based shared memory....");
79     rv = apr_shm_attach(&shm, SHARED_FILENAME, pool);
80     if (rv != APR_SUCCESS) {
81         printf("Consumer unable to attach to name-based shared memory "
82                "segment: [%d] %s \n", rv,
83                apr_strerror(rv, errmsg, sizeof(errmsg)));
84         exit(-2);
85     }
86     printf("OK\n");
87
88     boxes = apr_shm_baseaddr_get(shm);
89
90     /* consume messages on all of the boxes */
91     msgwait(30, 0, N_BOXES); /* wait for 30 seconds for messages */
92
93     printf("Consumer detaching from name-based shared memory....");
94     rv = apr_shm_detach(shm);
95     if (rv != APR_SUCCESS) {
96         printf("Consumer unable to detach from name-based shared memory "
97                "segment: [%d] %s \n", rv,
98                apr_strerror(rv, errmsg, sizeof(errmsg)));
99         exit(-3);
100     }
101     printf("OK\n");
102
103     return 0;
104 }
105
106 #else /* APR_HAS_SHARED_MEMORY */
107
108 int main(void)
109 {
110     printf("APR SHMEM test not run!\n");
111     printf("shmem is not supported on this platform\n"); 
112     return -1;
113 }
114
115 #endif /* APR_HAS_SHARED_MEMORY */
116