52 int signal_when_queue_non_empty)
55 pthread_mutexattr_t attr;
56 pthread_condattr_t cattr;
60 memset (q, 0,
sizeof (*q));
64 q->consumer_pid = consumer_pid;
65 q->signal_when_queue_non_empty = signal_when_queue_non_empty;
67 memset (&attr, 0,
sizeof (attr));
68 memset (&cattr, 0,
sizeof (cattr));
70 if (pthread_mutexattr_init (&attr))
72 if (pthread_mutexattr_setpshared (&attr, PTHREAD_PROCESS_SHARED))
74 if (pthread_mutex_init (&q->mutex, &attr))
76 if (pthread_mutexattr_destroy (&attr))
78 if (pthread_condattr_init (&cattr))
81 if (pthread_condattr_setpshared (&cattr, PTHREAD_PROCESS_SHARED))
83 if (pthread_cond_init (&q->condvar, &cattr))
85 if (pthread_condattr_destroy (&cattr))
97 (void) pthread_mutex_destroy (&q->mutex);
98 (void) pthread_cond_destroy (&q->condvar);
105 pthread_mutex_lock (&q->mutex);
111 pthread_mutex_unlock (&q->mutex);
117 return q->cursize == q->maxsize;
128 int need_broadcast = 0;
132 while (q->cursize == q->maxsize)
134 (void) pthread_cond_wait (&q->condvar, &q->mutex);
138 tailp = (
i8 *) (&q->data[0] + q->elsize * q->tail);
144 need_broadcast = (q->cursize == 1);
146 if (q->tail == q->maxsize)
151 (void) pthread_cond_broadcast (&q->condvar);
152 if (q->signal_when_queue_non_empty)
153 kill (q->consumer_pid, q->signal_when_queue_non_empty);
165 while (q->cursize == q->maxsize)
169 tailp = (
i8 *) (&q->data[0] + q->elsize * q->tail);
175 if (q->tail == q->maxsize)
186 u8 * elem,
int nowait)
189 int need_broadcast = 0;
194 if (pthread_mutex_trylock (&q->mutex))
200 pthread_mutex_lock (&q->mutex);
206 pthread_mutex_unlock (&q->mutex);
209 while (q->cursize == q->maxsize)
211 (void) pthread_cond_wait (&q->condvar, &q->mutex);
215 tailp = (
i8 *) (&q->data[0] + q->elsize * q->tail);
221 need_broadcast = (q->cursize == 1);
223 if (q->tail == q->maxsize)
228 (void) pthread_cond_broadcast (&q->condvar);
229 if (q->signal_when_queue_non_empty)
230 kill (q->consumer_pid, q->signal_when_queue_non_empty);
232 pthread_mutex_unlock (&q->mutex);
242 u8 * elem2,
int nowait)
245 int need_broadcast = 0;
250 if (pthread_mutex_trylock (&q->mutex))
256 pthread_mutex_lock (&q->mutex);
262 pthread_mutex_unlock (&q->mutex);
265 while (q->cursize + 1 == q->maxsize)
267 (void) pthread_cond_wait (&q->condvar, &q->mutex);
271 tailp = (
i8 *) (&q->data[0] + q->elsize * q->tail);
277 if (q->tail == q->maxsize)
280 need_broadcast = (q->cursize == 1);
282 tailp = (
i8 *) (&q->data[0] + q->elsize * q->tail);
288 if (q->tail == q->maxsize)
293 (void) pthread_cond_broadcast (&q->condvar);
294 if (q->signal_when_queue_non_empty)
295 kill (q->consumer_pid, q->signal_when_queue_non_empty);
297 pthread_mutex_unlock (&q->mutex);
307 u8 * elem,
int nowait)
310 int need_broadcast = 0;
315 if (pthread_mutex_trylock (&q->mutex))
321 pthread_mutex_lock (&q->mutex);
327 pthread_mutex_unlock (&q->mutex);
330 while (q->cursize == 0)
332 (void) pthread_cond_wait (&q->condvar, &q->mutex);
336 headp = (
i8 *) (&q->data[0] + q->elsize * q->head);
341 if (q->cursize == q->maxsize)
346 if (q->head == q->maxsize)
350 (void) pthread_cond_broadcast (&q->condvar);
352 pthread_mutex_unlock (&q->mutex);
364 while (q->cursize == 0)
368 headp = (
i8 *) (&q->data[0] + q->elsize * q->head);
374 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_add2(unix_shared_memory_queue_t *q, u8 *elem, u8 *elem2, int nowait)
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)
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)
#define clib_unix_warning(format, args...)
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