X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fsvm%2Fmessage_queue.h;h=7226560124fbac14829eb97da06e1946faaa6f23;hb=c5df8c71c;hp=5ff0c4be10382f9e9a87fc429246a5743af1fb9c;hpb=52207f1b7b60cb0784d5241f0a4d40eef531c67e;p=vpp.git diff --git a/src/svm/message_queue.h b/src/svm/message_queue.h index 5ff0c4be103..7226560124f 100644 --- a/src/svm/message_queue.h +++ b/src/svm/message_queue.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018 Cisco and/or its affiliates. + * Copyright (c) 2018-2019 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: @@ -32,13 +32,13 @@ typedef struct svm_msg_q_ring_ volatile u32 tail; /**< current tail (for enqueue) */ u32 elsize; /**< size of an element */ u8 *data; /**< chunk of memory for msg data */ -} svm_msg_q_ring_t; +} __clib_packed svm_msg_q_ring_t; typedef struct svm_msg_q_ { svm_queue_t *q; /**< queue for exchanging messages */ svm_msg_q_ring_t *rings; /**< rings with message data*/ -} svm_msg_q_t; +} __clib_packed svm_msg_q_t; typedef struct svm_msg_q_ring_cfg_ { @@ -213,6 +213,40 @@ void *svm_msg_q_msg_data (svm_msg_q_t * mq, svm_msg_q_msg_t * msg); */ svm_msg_q_ring_t *svm_msg_q_ring (svm_msg_q_t * mq, u32 ring_index); +/** + * Set event fd for queue consumer + * + * If set, queue will exclusively use eventfds for signaling. Moreover, + * afterwards, the queue should only be used in non-blocking mode. Waiting + * for events should be done externally using something like epoll. + * + * @param mq message queue + * @param fd consumer eventfd + */ +void svm_msg_q_set_consumer_eventfd (svm_msg_q_t * mq, int fd); + +/** + * Set event fd for queue producer + * + * If set, queue will exclusively use eventfds for signaling. Moreover, + * afterwards, the queue should only be used in non-blocking mode. Waiting + * for events should be done externally using something like epoll. + * + * @param mq message queue + * @param fd producer eventfd + */ +void svm_msg_q_set_producer_eventfd (svm_msg_q_t * mq, int fd); + +/** + * Allocate event fd for queue consumer + */ +int svm_msg_q_alloc_consumer_eventfd (svm_msg_q_t * mq); + +/** + * Allocate event fd for queue consumer + */ +int svm_msg_q_alloc_producer_eventfd (svm_msg_q_t * mq); + /** * Check if message queue is full */ @@ -274,22 +308,51 @@ svm_msg_q_lock (svm_msg_q_t * mq) return pthread_mutex_lock (&mq->q->mutex); } +/** + * Unlock message queue + */ static inline void -svm_msg_q_wait (svm_msg_q_t * mq) +svm_msg_q_unlock (svm_msg_q_t * mq) { - pthread_cond_wait (&mq->q->condvar, &mq->q->mutex); + pthread_mutex_unlock (&mq->q->mutex); } /** - * Unlock message queue + * Wait for message queue event + * + * Must be called with mutex held. The queue only works non-blocking + * with eventfds, so handle blocking calls as an exception here. */ static inline void -svm_msg_q_unlock (svm_msg_q_t * mq) +svm_msg_q_wait (svm_msg_q_t * mq) { - /* The other side of the connection is not polling */ - if (mq->q->cursize < (mq->q->maxsize / 8)) - (void) pthread_cond_broadcast (&mq->q->condvar); - pthread_mutex_unlock (&mq->q->mutex); + svm_queue_wait (mq->q); +} + +/** + * Timed wait for message queue event + * + * Must be called with mutex held. + * + * @param mq message queue + * @param timeout time in seconds + */ +static inline int +svm_msg_q_timedwait (svm_msg_q_t * mq, double timeout) +{ + return svm_queue_timedwait (mq->q, timeout); +} + +static inline int +svm_msg_q_get_consumer_eventfd (svm_msg_q_t * mq) +{ + return mq->q->consumer_evtfd; +} + +static inline int +svm_msg_q_get_producer_eventfd (svm_msg_q_t * mq) +{ + return mq->q->producer_evtfd; } #endif /* SRC_SVM_MESSAGE_QUEUE_H_ */