Apache2
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 
27 typedef struct mbox {
28  char msg[1024];
30 } mbox;
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 
41 static APR_INLINE
42 int msgwait(const char *msg, int count, int duration, int sleep_ms)
43 {
44  int recvd = 0, i;
45  apr_time_t start = apr_time_now();
46  apr_interval_time_t sleep_duration = apr_time_from_sec(duration);
47  apr_interval_time_t sleep_delay = apr_time_from_msec(sleep_ms);
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  }
60  apr_sleep(sleep_delay);
61  }
62  return recvd;
63 }
64 
65 static APR_INLINE
66 int 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 Platform Definitions.
APR Atomic Operations.
APR Strings library.
APR Time Library.
void apr_atomic_set32(volatile apr_uint32_t *mem, apr_uint32_t val)
apr_uint32_t apr_atomic_cas32(volatile apr_uint32_t *mem, apr_uint32_t with, apr_uint32_t cmp)
unsigned int apr_uint32_t
Definition: apr.h:348
#define APR_INLINE
Definition: apr.h:65
char * apr_cpystrn(char *dst, const char *src, apr_size_t dst_size)
void apr_sleep(apr_interval_time_t t)
#define apr_time_from_msec(msec)
Definition: apr_time.h:75
apr_int64_t apr_interval_time_t
Definition: apr_time.h:55
apr_time_t apr_time_now(void)
apr_int64_t apr_time_t
Definition: apr_time.h:45
#define apr_time_from_sec(sec)
Definition: apr_time.h:78
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
struct mbox mbox
#define N_BOXES
Definition: testshm.h:33