summaryrefslogtreecommitdiffstats
path: root/src/vlibmemory
diff options
context:
space:
mode:
authorKlement Sekera <ksekera@cisco.com>2017-05-04 06:15:18 +0200
committerNeale Ranns <nranns@cisco.com>2017-09-19 12:23:44 +0000
commit8f2a4eafeaa439432107563033728e09665c16d9 (patch)
treea8db2c9f35e169a2eb48a7f5aebb88f146029f5f /src/vlibmemory
parentf4215a65cb5858b276b0b096273270e7c017209d (diff)
Add new C API
Change-Id: I717ce3cd7c867c155de149ec56623269d26d0ff7 Signed-off-by: Klement Sekera <ksekera@cisco.com>
Diffstat (limited to 'src/vlibmemory')
-rw-r--r--src/vlibmemory/unix_shared_memory_queue.c65
-rw-r--r--src/vlibmemory/unix_shared_memory_queue.h11
2 files changed, 71 insertions, 5 deletions
diff --git a/src/vlibmemory/unix_shared_memory_queue.c b/src/vlibmemory/unix_shared_memory_queue.c
index e86edec3c65..4db4851c111 100644
--- a/src/vlibmemory/unix_shared_memory_queue.c
+++ b/src/vlibmemory/unix_shared_memory_queue.c
@@ -235,6 +235,71 @@ unix_shared_memory_queue_add (unix_shared_memory_queue_t * q,
}
/*
+ * unix_shared_memory_queue_add2
+ */
+int
+unix_shared_memory_queue_add2 (unix_shared_memory_queue_t * q, u8 * elem,
+ u8 * elem2, int nowait)
+{
+ i8 *tailp;
+ int need_broadcast = 0;
+
+ if (nowait)
+ {
+ /* zero on success */
+ if (pthread_mutex_trylock (&q->mutex))
+ {
+ return (-1);
+ }
+ }
+ else
+ pthread_mutex_lock (&q->mutex);
+
+ if (PREDICT_FALSE (q->cursize + 1 == q->maxsize))
+ {
+ if (nowait)
+ {
+ pthread_mutex_unlock (&q->mutex);
+ return (-2);
+ }
+ while (q->cursize + 1 == q->maxsize)
+ {
+ (void) pthread_cond_wait (&q->condvar, &q->mutex);
+ }
+ }
+
+ tailp = (i8 *) (&q->data[0] + q->elsize * q->tail);
+ clib_memcpy (tailp, elem, q->elsize);
+
+ q->tail++;
+ q->cursize++;
+
+ if (q->tail == q->maxsize)
+ q->tail = 0;
+
+ need_broadcast = (q->cursize == 1);
+
+ tailp = (i8 *) (&q->data[0] + q->elsize * q->tail);
+ clib_memcpy (tailp, elem2, q->elsize);
+
+ q->tail++;
+ q->cursize++;
+
+ if (q->tail == q->maxsize)
+ q->tail = 0;
+
+ if (need_broadcast)
+ {
+ (void) pthread_cond_broadcast (&q->condvar);
+ if (q->signal_when_queue_non_empty)
+ kill (q->consumer_pid, q->signal_when_queue_non_empty);
+ }
+ pthread_mutex_unlock (&q->mutex);
+
+ return 0;
+}
+
+/*
* unix_shared_memory_queue_sub
*/
int
diff --git a/src/vlibmemory/unix_shared_memory_queue.h b/src/vlibmemory/unix_shared_memory_queue.h
index 13800065beb..27de3218223 100644
--- a/src/vlibmemory/unix_shared_memory_queue.h
+++ b/src/vlibmemory/unix_shared_memory_queue.h
@@ -21,7 +21,6 @@
#define included_unix_shared_memory_queue_h
#include <pthread.h>
-#include <vppinfra/mem.h>
typedef struct _unix_shared_memory_queue
{
@@ -43,10 +42,12 @@ unix_shared_memory_queue_t *unix_shared_memory_queue_init (int nels,
int
signal_when_queue_non_empty);
void unix_shared_memory_queue_free (unix_shared_memory_queue_t * q);
-int unix_shared_memory_queue_add (unix_shared_memory_queue_t * q,
- u8 * elem, int nowait);
-int unix_shared_memory_queue_sub (unix_shared_memory_queue_t * q,
- u8 * elem, int nowait);
+int unix_shared_memory_queue_add (unix_shared_memory_queue_t * q, u8 * elem,
+ int nowait);
+int unix_shared_memory_queue_add2 (unix_shared_memory_queue_t * q, u8 * elem,
+ u8 * elem2, int nowait);
+int unix_shared_memory_queue_sub (unix_shared_memory_queue_t * q, u8 * elem,
+ int nowait);
void unix_shared_memory_queue_lock (unix_shared_memory_queue_t * q);
void unix_shared_memory_queue_unlock (unix_shared_memory_queue_t * q);
int unix_shared_memory_queue_is_full (unix_shared_memory_queue_t * q);