Apache HTTPD
testshm.h
Go to the documentation of this file.
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#ifndef TESTSHM_H
18#define TESTSHM_H
19
20#include "apr.h"
21#include "apr_time.h"
22#include "apr_atomic.h"
23#include "apr_strings.h"
24
25#include <assert.h>
26
27typedef struct mbox {
28 char msg[1024];
32
33#define N_BOXES 10
34#define N_MESSAGES 100
35#define SHARED_SIZE (apr_size_t)(N_BOXES * sizeof(mbox))
36#define SHARED_FILENAME "data/apr.testshm.shm"
37#define MSG "Sending a message"
38
39#if APR_HAS_SHARED_MEMORY
40
41static APR_INLINE
42int msgwait(const char *msg, int count, int duration, int sleep_ms)
43{
44 int recvd = 0, i;
48 while (apr_time_now() - start < sleep_duration) {
49 for (i = 0; i < N_BOXES; i++) {
50 if (apr_atomic_cas32(&boxes[i].msgavail, 0, 1) == 1) {
51 if (msg) {
52 assert(strcmp(boxes[i].msg, msg) == 0);
53 }
54 *boxes[i].msg = '\0';
55 if (++recvd == count) {
56 return recvd;
57 }
58 }
59 }
61 }
62 return recvd;
63}
64
65static APR_INLINE
66int msgput(const char *msg, int boxnum)
67{
68 if (apr_atomic_cas32(&boxes[boxnum].msgavail, -1, 0) != 0) {
69 return 0;
70 }
71 if (msg) {
72 apr_cpystrn(boxes[boxnum].msg, msg, sizeof(boxes[boxnum].msg));
73 }
74 else {
75 *boxes[boxnum].msg = '\0';
76 }
77 apr_atomic_set32(&boxes[boxnum].msgavail, 1);
78 return 1;
79}
80
81#endif /* APR_HAS_SHARED_MEMORY */
82
83#endif
APR Atomic Operations.
APR Strings library.
APR Time Library.
void apr_atomic_set32(volatile apr_uint32_t *mem, apr_uint32_t val)
Definition atomic.c:73
apr_uint32_t apr_atomic_cas32(volatile apr_uint32_t *mem, apr_uint32_t swap, apr_uint32_t cmp)
Definition atomic.c:78
unsigned int count
Definition apr_md5.h:152
apr_file_t apr_off_t start
apr_size_t size
#define apr_time_from_msec(msec)
Definition apr_time.h:75
apr_int64_t apr_interval_time_t
Definition apr_time.h:55
apr_int64_t apr_time_t
Definition apr_time.h:45
#define apr_time_from_sec(sec)
Definition apr_time.h:78
int i
Definition mod_so.c:347
Definition testshm.h:27
apr_uint32_t msgavail
Definition testshm.h:29
char msg[1024]
Definition testshm.h:28
mbox * boxes
Definition testshm.h:31
#define N_BOXES
Definition testshm.h:33