#include <svm/message_queue.h>
#include <vppinfra/mem.h>
+#include <vppinfra/format.h>
+#include <sys/eventfd.h>
static inline svm_msg_q_ring_t *
svm_msg_q_ring_inline (svm_msg_q_t * mq, u32 ring_index)
svm_msg_q_alloc (svm_msg_q_cfg_t * cfg)
{
svm_msg_q_ring_cfg_t *ring_cfg;
+ uword rings_sz = 0, mq_sz;
svm_msg_q_ring_t *ring;
u8 *base, *rings_ptr;
- uword rings_sz = 0;
vec_header_t *vh;
+ u32 vec_sz, q_sz;
svm_msg_q_t *mq;
- u32 vec_sz;
int i;
ASSERT (cfg);
rings_sz += (uword) ring_cfg->nitems * ring_cfg->elsize;
}
- base = clib_mem_alloc_aligned (sizeof (svm_msg_q_t) + vec_sz + rings_sz,
- CLIB_CACHE_LINE_BYTES);
+ q_sz = sizeof (svm_queue_t) + cfg->q_nitems * sizeof (svm_msg_q_msg_t);
+ mq_sz = sizeof (svm_msg_q_t) + vec_sz + rings_sz + q_sz;
+ base = clib_mem_alloc_aligned (mq_sz, CLIB_CACHE_LINE_BYTES);
if (!base)
return 0;
mq = (svm_msg_q_t *) base;
- vh = (vec_header_t *) (base + sizeof (svm_msg_q_t));
+ mq->q = svm_queue_init (base + sizeof (svm_msg_q_t), cfg->q_nitems,
+ sizeof (svm_msg_q_msg_t));
+ mq->q->consumer_pid = cfg->consumer_pid;
+ vh = (vec_header_t *) ((u8 *) mq->q + q_sz);
vh->len = cfg->n_rings;
mq->rings = (svm_msg_q_ring_t *) (vh + 1);
- rings_ptr = (u8 *) mq->rings + vec_sz;
+ rings_ptr = (u8 *) mq->rings + sizeof (svm_msg_q_ring_t) * cfg->n_rings;
for (i = 0; i < cfg->n_rings; i++)
{
ring = &mq->rings[i];
rings_ptr += (uword) ring->nitems * ring->elsize;
}
}
- mq->q = svm_queue_init (cfg->q_nitems, sizeof (svm_msg_q_msg_t),
- cfg->consumer_pid, 0);
return mq;
}
msg.ring_index = ring - mq->rings;
msg.elt_index = ring->tail;
ring->tail = (ring->tail + 1) % ring->nitems;
- __sync_fetch_and_add (&ring->cursize, 1);
+ clib_atomic_fetch_add (&ring->cursize, 1);
return msg;
}
{
if (svm_msg_q_try_lock (mq))
return -1;
- if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, ring_index)))
+ if (PREDICT_FALSE (svm_msg_q_is_full (mq)
+ || svm_msg_q_ring_is_full (mq, ring_index)))
{
svm_msg_q_unlock (mq);
return -2;
}
*msg = svm_msg_q_alloc_msg_w_ring (mq, ring_index);
- if (PREDICT_FALSE (svm_msg_q_msg_is_invalid (msg)))
- {
- svm_msg_q_unlock (mq);
- return -2;
- }
}
else
{
svm_msg_q_lock (mq);
- while (svm_msg_q_ring_is_full (mq, ring_index))
+ while (svm_msg_q_is_full (mq)
+ || svm_msg_q_ring_is_full (mq, ring_index))
svm_msg_q_wait (mq);
*msg = svm_msg_q_alloc_msg_w_ring (mq, ring_index);
}
msg.ring_index = ring - mq->rings;
msg.elt_index = ring->tail;
ring->tail = (ring->tail + 1) % ring->nitems;
- __sync_fetch_and_add (&ring->cursize, 1);
+ clib_atomic_fetch_add (&ring->cursize, 1);
break;
}
return msg;
svm_msg_q_free_msg (svm_msg_q_t * mq, svm_msg_q_msg_t * msg)
{
svm_msg_q_ring_t *ring;
+ int need_signal;
- if (vec_len (mq->rings) <= msg->ring_index)
- return;
+ ASSERT (vec_len (mq->rings) > msg->ring_index);
ring = &mq->rings[msg->ring_index];
if (msg->elt_index == ring->head)
{
}
else
{
+ clib_warning ("message out of order");
/* for now, expect messages to be processed in order */
ASSERT (0);
}
- __sync_fetch_and_sub (&ring->cursize, 1);
+
+ need_signal = ring->cursize == ring->nitems;
+ clib_atomic_fetch_sub (&ring->cursize, 1);
+
+ if (PREDICT_FALSE (need_signal))
+ svm_queue_send_signal (mq->q, 0);
}
static int
svm_queue_sub_raw (mq->q, (u8 *) msg);
}
+void
+svm_msg_q_set_consumer_eventfd (svm_msg_q_t * mq, int fd)
+{
+ mq->q->consumer_evtfd = fd;
+}
+
+void
+svm_msg_q_set_producer_eventfd (svm_msg_q_t * mq, int fd)
+{
+ mq->q->producer_evtfd = fd;
+}
+
+int
+svm_msg_q_alloc_consumer_eventfd (svm_msg_q_t * mq)
+{
+ int fd;
+ if ((fd = eventfd (0, EFD_NONBLOCK)) < 0)
+ return -1;
+ svm_msg_q_set_consumer_eventfd (mq, fd);
+ return 0;
+}
+
+int
+svm_msg_q_alloc_producer_eventfd (svm_msg_q_t * mq)
+{
+ int fd;
+ if ((fd = eventfd (0, EFD_NONBLOCK)) < 0)
+ return -1;
+ svm_msg_q_set_producer_eventfd (mq, fd);
+ return 0;
+}
+
+u8 *
+format_svm_msg_q (u8 * s, va_list * args)
+{
+ svm_msg_q_t *mq = va_arg (*args, svm_msg_q_t *);
+ s = format (s, " [Q:%d/%d]", mq->q->cursize, mq->q->maxsize);
+ for (u32 i = 0; i < vec_len (mq->rings); i++)
+ {
+ s = format (s, " [R%d:%d/%d]", i, mq->rings[i].cursize,
+ mq->rings[i].nitems);
+ }
+ return s;
+}
+
/*
* fd.io coding-style-patch-verification: ON
*