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))
101 (
void) pthread_mutex_destroy(&q->mutex);
102 (
void) pthread_cond_destroy(&q->condvar);
108 pthread_mutex_lock(&q->mutex);
113 pthread_mutex_unlock(&q->mutex);
118 return q->cursize == q->maxsize;
128 int need_broadcast=0;
131 while(q->cursize == q->maxsize) {
132 (
void) pthread_cond_wait(&q->condvar, &q->mutex);
136 tailp = (
i8 *)(&q->data[0] + q->elsize*q->tail);
142 need_broadcast = (q->cursize == 1);
144 if (q->tail == q->maxsize)
147 if (need_broadcast) {
148 (
void) pthread_cond_broadcast(&q->condvar);
149 if (q->signal_when_queue_non_empty)
150 kill (q->consumer_pid, q->signal_when_queue_non_empty);
161 while(q->cursize == q->maxsize)
165 tailp = (
i8 *)(&q->data[0] + q->elsize*q->tail);
171 if (q->tail == q->maxsize)
181 u8 *elem,
int nowait)
184 int need_broadcast=0;
188 if (pthread_mutex_trylock (&q->mutex)) {
192 pthread_mutex_lock(&q->mutex);
196 pthread_mutex_unlock(&q->mutex);
199 while(q->cursize == q->maxsize) {
200 (
void) pthread_cond_wait(&q->condvar, &q->mutex);
204 tailp = (
i8 *)(&q->data[0] + q->elsize*q->tail);
210 need_broadcast = (q->cursize == 1);
212 if (q->tail == q->maxsize)
215 if (need_broadcast) {
216 (
void) pthread_cond_broadcast(&q->condvar);
217 if (q->signal_when_queue_non_empty)
218 kill (q->consumer_pid, q->signal_when_queue_non_empty);
220 pthread_mutex_unlock(&q->mutex);
229 u8 *elem,
int nowait)
232 int need_broadcast=0;
236 if (pthread_mutex_trylock (&q->mutex)) {
240 pthread_mutex_lock(&q->mutex);
244 pthread_mutex_unlock(&q->mutex);
247 while (q->cursize == 0) {
248 (
void) pthread_cond_wait(&q->condvar, &q->mutex);
252 headp = (
i8 *)(&q->data[0] + q->elsize*q->head);
256 if (q->cursize == q->maxsize)
261 if(q->head == q->maxsize)
265 (
void) pthread_cond_broadcast(&q->condvar);
267 pthread_mutex_unlock(&q->mutex);
278 while (q->cursize == 0)
282 headp = (
i8 *)(&q->data[0] + q->elsize*q->head);
288 if(q->head == q->maxsize)
int unix_shared_memory_queue_is_full(unix_shared_memory_queue_t *q)
static void(BVT(clib_bihash)*h, BVT(clib_bihash_value)*v)
always_inline void clib_mem_free(void *p)
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)
always_inline void * clib_mem_alloc_aligned(uword size, uword align)
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)
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_CACHE_LINE_BYTES
struct _unix_shared_memory_queue unix_shared_memory_queue_t