57 int signal_when_queue_non_empty)
60 pthread_mutexattr_t attr;
61 pthread_condattr_t cattr;
65 memset (q, 0,
sizeof (*q));
69 q->consumer_pid = consumer_pid;
70 q->signal_when_queue_non_empty = signal_when_queue_non_empty;
72 memset (&attr, 0,
sizeof (attr));
73 memset (&cattr, 0,
sizeof (attr));
75 if (pthread_mutexattr_init (&attr))
77 if (pthread_mutexattr_setpshared (&attr, PTHREAD_PROCESS_SHARED))
79 if (pthread_mutex_init (&q->mutex, &attr))
81 if (pthread_mutexattr_destroy (&attr))
83 if (pthread_condattr_init (&cattr))
86 if (pthread_condattr_setpshared (&cattr, PTHREAD_PROCESS_SHARED))
88 if (pthread_cond_init (&q->condvar, &cattr))
90 if (pthread_condattr_destroy (&cattr))
102 (void) pthread_mutex_destroy (&q->mutex);
103 (void) pthread_cond_destroy (&q->condvar);
110 pthread_mutex_lock (&q->mutex);
116 pthread_mutex_unlock (&q->mutex);
122 return q->cursize == q->maxsize;
133 int need_broadcast = 0;
137 while (q->cursize == q->maxsize)
139 (void) pthread_cond_wait (&q->condvar, &q->mutex);
143 tailp = (
i8 *) (&q->data[0] + q->elsize * q->tail);
149 need_broadcast = (q->cursize == 1);
151 if (q->tail == q->maxsize)
156 (void) pthread_cond_broadcast (&q->condvar);
157 if (q->signal_when_queue_non_empty)
158 kill (q->consumer_pid, q->signal_when_queue_non_empty);
170 while (q->cursize == q->maxsize)
174 tailp = (
i8 *) (&q->data[0] + q->elsize * q->tail);
180 if (q->tail == q->maxsize)
191 u8 * elem,
int nowait)
194 int need_broadcast = 0;
199 if (pthread_mutex_trylock (&q->mutex))
205 pthread_mutex_lock (&q->mutex);
211 pthread_mutex_unlock (&q->mutex);
214 while (q->cursize == q->maxsize)
216 (void) pthread_cond_wait (&q->condvar, &q->mutex);
220 tailp = (
i8 *) (&q->data[0] + q->elsize * q->tail);
226 need_broadcast = (q->cursize == 1);
228 if (q->tail == q->maxsize)
233 (void) pthread_cond_broadcast (&q->condvar);
234 if (q->signal_when_queue_non_empty)
235 kill (q->consumer_pid, q->signal_when_queue_non_empty);
237 pthread_mutex_unlock (&q->mutex);
247 u8 * elem,
int nowait)
250 int need_broadcast = 0;
255 if (pthread_mutex_trylock (&q->mutex))
261 pthread_mutex_lock (&q->mutex);
267 pthread_mutex_unlock (&q->mutex);
270 while (q->cursize == 0)
272 (void) pthread_cond_wait (&q->condvar, &q->mutex);
276 headp = (
i8 *) (&q->data[0] + q->elsize * q->head);
280 if (q->cursize == q->maxsize)
285 if (q->head == q->maxsize)
289 (void) pthread_cond_broadcast (&q->condvar);
291 pthread_mutex_unlock (&q->mutex);
303 while (q->cursize == 0)
307 headp = (
i8 *) (&q->data[0] + q->elsize * q->head);
313 if (q->head == q->maxsize)
int unix_shared_memory_queue_is_full(unix_shared_memory_queue_t *q)
void unix_shared_memory_queue_free(unix_shared_memory_queue_t *q)
int unix_shared_memory_queue_add_nolock(unix_shared_memory_queue_t *q, u8 *elem)
int unix_shared_memory_queue_add(unix_shared_memory_queue_t *q, u8 *elem, int nowait)
int unix_shared_memory_queue_sub_raw(unix_shared_memory_queue_t *q, u8 *elem)
void unix_shared_memory_queue_lock(unix_shared_memory_queue_t *q)
int unix_shared_memory_queue_sub(unix_shared_memory_queue_t *q, u8 *elem, int nowait)
#define clib_memcpy(a, b, c)
#define clib_unix_warning(format, args...)
unix_shared_memory_queue_t * unix_shared_memory_queue_init(int nels, int elsize, int consumer_pid, int signal_when_queue_non_empty)
static void clib_mem_free(void *p)
int unix_shared_memory_queue_add_raw(unix_shared_memory_queue_t *q, u8 *elem)
void unix_shared_memory_queue_unlock(unix_shared_memory_queue_t *q)
static void * clib_mem_alloc_aligned(uword size, uword align)
#define CLIB_CACHE_LINE_BYTES
struct _unix_shared_memory_queue unix_shared_memory_queue_t