xref: /aosp_15_r20/external/cronet/third_party/apache-portable-runtime/src/test/testshmconsumer.c (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 "testshm.h"
24 #include "apr.h"
25 
26 #if APR_HAVE_STDLIB_H
27 #include <stdlib.h>
28 #endif
29 
30 
31 #if APR_HAS_SHARED_MEMORY
32 
msgwait(int sleep_sec,int first_box,int last_box)33 static int msgwait(int sleep_sec, int first_box, int last_box)
34 {
35     int i;
36     int recvd = 0;
37     apr_time_t start = apr_time_now();
38     apr_interval_time_t sleep_duration = apr_time_from_sec(sleep_sec);
39     while (apr_time_now() - start < sleep_duration) {
40         for (i = first_box; i < last_box; i++) {
41             if (boxes[i].msgavail && !strcmp(boxes[i].msg, MSG)) {
42                 recvd++;
43                 boxes[i].msgavail = 0; /* reset back to 0 */
44                 memset(boxes[i].msg, 0, 1024);
45             }
46         }
47         apr_sleep(apr_time_from_sec(1));
48     }
49     return recvd;
50 }
51 
main(void)52 int main(void)
53 {
54     apr_status_t rv;
55     apr_pool_t *pool;
56     apr_shm_t *shm;
57     int recvd;
58 
59     apr_initialize();
60 
61     if (apr_pool_create(&pool, NULL) != APR_SUCCESS) {
62         exit(-1);
63     }
64 
65     rv = apr_shm_attach(&shm, SHARED_FILENAME, pool);
66     if (rv != APR_SUCCESS) {
67         exit(-2);
68     }
69 
70     boxes = apr_shm_baseaddr_get(shm);
71 
72     /* consume messages on all of the boxes */
73     recvd = msgwait(30, 0, N_BOXES); /* wait for 30 seconds for messages */
74 
75     rv = apr_shm_detach(shm);
76     if (rv != APR_SUCCESS) {
77         exit(-3);
78     }
79 
80     return recvd;
81 }
82 
83 #else /* APR_HAS_SHARED_MEMORY */
84 
main(void)85 int main(void)
86 {
87     /* Just return, this program will never be called, so we don't need
88      * to print a message
89      */
90     return 0;
91 }
92 
93 #endif /* APR_HAS_SHARED_MEMORY */
94 
95