diff options
Diffstat (limited to 'test/testshm.h')
-rw-r--r-- | test/testshm.h | 56 |
1 files changed, 53 insertions, 3 deletions
diff --git a/test/testshm.h b/test/testshm.h index 5b24a9d..4d4f0c5 100644 --- a/test/testshm.h +++ b/test/testshm.h @@ -17,17 +17,67 @@ #ifndef TESTSHM_H #define TESTSHM_H +#include "apr.h" +#include "apr_time.h" +#include "apr_atomic.h" +#include "apr_strings.h" + +#include <assert.h> + typedef struct mbox { char msg[1024]; - int msgavail; + apr_uint32_t msgavail; } mbox; mbox *boxes; #define N_BOXES 10 +#define N_MESSAGES 100 #define SHARED_SIZE (apr_size_t)(N_BOXES * sizeof(mbox)) #define SHARED_FILENAME "data/apr.testshm.shm" -#define N_MESSAGES 100 #define MSG "Sending a message" -#endif +#if APR_HAS_SHARED_MEMORY +static APR_INLINE +int msgwait(const char *msg, int count, int duration, int sleep_ms) +{ + int recvd = 0, i; + apr_time_t start = apr_time_now(); + apr_interval_time_t sleep_duration = apr_time_from_sec(duration); + apr_interval_time_t sleep_delay = apr_time_from_msec(sleep_ms); + while (apr_time_now() - start < sleep_duration) { + for (i = 0; i < N_BOXES; i++) { + if (apr_atomic_cas32(&boxes[i].msgavail, 0, 1) == 1) { + if (msg) { + assert(strcmp(boxes[i].msg, msg) == 0); + } + *boxes[i].msg = '\0'; + if (++recvd == count) { + return recvd; + } + } + } + apr_sleep(sleep_delay); + } + return recvd; +} + +static APR_INLINE +int msgput(const char *msg, int boxnum) +{ + if (apr_atomic_cas32(&boxes[boxnum].msgavail, -1, 0) != 0) { + return 0; + } + if (msg) { + apr_cpystrn(boxes[boxnum].msg, msg, sizeof(boxes[boxnum].msg)); + } + else { + *boxes[boxnum].msg = '\0'; + } + apr_atomic_set32(&boxes[boxnum].msgavail, 1); + return 1; +} + +#endif /* APR_HAS_SHARED_MEMORY */ + +#endif |