smq_read_timestamp.c
1 
26 #include <unistd.h> /* for usleep() */
27 #include <signal.h> /* for signal() */
28 #include <shm_arena.h> /* for Shared Memory Arena Library */
29 
30 static int running = 1;
31 static smq_t q;
32 
33 static void catcher(int sig)
34 {
35  printf("catch signal %d\n", sig);
36 
37  /* Unblock the current or next smq_rdlock() call. */
39  running = 0;
40 }
41 
42 int main(void)
43 {
44  long double offset = 0;
45 
46  signal(SIGINT, catcher);
47 
48  /* We include the SHM_TIMESTAMP bit flag in smq_get() to make sure
49  * the Shared Multi-Queue is time stamped. */
50  q = smq_get(NULL, sizeof(int), 24, "q_count", O_CREAT|SHM_TIMESTAMP);
51  if(!q)
52  {
53  printf("smq_get() failed\n");
54  return 1;
55  }
56 
57  while(running)
58  {
59  int num;
60 
61  /* block until there is at least 1 in the queue */
62  num = smq_rdlock(q, 1);
63 
64  while(num > 0)
65  {
66  int *ptr;
67  long double *t;
68  ptr = smq_read(q);
69  t = smq_timestamp(q, ptr);
70  if(offset == 0) offset = *t;
71  printf("(%Lf)%d ", *t - offset, *ptr);
72  fflush(stdout);
73  num--;
74  }
75 
76  smq_unlock(q);
77  }
78 
79  /* Because smq_unblock_rdlock() spawns a thread to release the
80  * blocking smq_rdlock() and that thread must make mutex calls, we
81  * need to call smq_delete(q) to wait for thread to finish, to avoid
82  * deadlocking the multi-queue. smq_delete(q) only cleans up the
83  * local process data and not the shared memory data. */
84  smq_delete(q);
85 
86  return 0;
87 }
long double * smq_timestamp(smq_t q, const void *ptr)
read or write the time stamp
Definition: smq.c:1136
smq_t smq_get(shm_arena_t arena, size_t element_size, int q_length, const char *name, int flags)
get a Shared Multi-Queue object
Definition: smq.c:358
int smq_rdlock(smq_t q, int num)
acquire Shared Multi-Queue read-lock
Definition: smq.c:881
int smq_unblock_rdlock(smq_t q)
unblock a smq_rdlock() call if needed
Definition: smq.c:1027
#define SHM_TIMESTAMP
smq_get() flag for adding a timestamp to entries
Definition: shm_arena.h:158
int smq_delete(smq_t q)
delete the local Shared Multi-Queue object
Definition: smq.c:810
struct smq * smq_t
shared multi-queue object
Definition: shm_arena.h:196
int smq_unlock(smq_t q)
release Shared Multi-Queue read or write lock
Definition: smq.c:1102
void * smq_read(smq_t q)
read the next entry from the Shared Multi-Queue
Definition: smq.c:1262

Shared Memory Arena version RC-0.0.25