Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * svm_queue.h - shared-memory queues |
| 4 | * |
Florin Coras | c5df8c7 | 2019-04-08 07:42:30 -0700 | [diff] [blame] | 5 | * Copyright (c) 2009-2019 Cisco and/or its affiliates. |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at: |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | *------------------------------------------------------------------ |
| 18 | */ |
| 19 | |
| 20 | #ifndef included_svm_queue_h |
| 21 | #define included_svm_queue_h |
| 22 | |
| 23 | #include <pthread.h> |
| 24 | |
| 25 | typedef struct _svm_queue |
| 26 | { |
| 27 | pthread_mutex_t mutex; /* 8 bytes */ |
| 28 | pthread_cond_t condvar; /* 8 bytes */ |
| 29 | int head; |
| 30 | int tail; |
| 31 | volatile int cursize; |
| 32 | int maxsize; |
| 33 | int elsize; |
| 34 | int consumer_pid; |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 35 | int producer_evtfd; |
| 36 | int consumer_evtfd; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 37 | char data[0]; |
| 38 | } svm_queue_t; |
| 39 | |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 40 | typedef enum |
| 41 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 42 | SVM_Q_WAIT = 0, /**< blocking call - best used in combination with |
| 43 | condvars, for eventfds we don't yield the cpu */ |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 44 | SVM_Q_NOWAIT, /**< non-blocking call - works with both condvar and |
| 45 | eventfd signaling */ |
| 46 | SVM_Q_TIMEDWAIT, /**< blocking call, returns on signal or time-out - |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 47 | best used in combination with condvars, with |
| 48 | eventfds we don't yield the cpu */ |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 49 | } svm_q_conditional_wait_t; |
| 50 | |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 51 | /** |
| 52 | * Allocate and initialize svm queue |
| 53 | * |
| 54 | * @param nels number of elements on the queue |
| 55 | * @param elsize element size, presumably 4 and cacheline-size will |
| 56 | * be popular choices. |
| 57 | * @param pid consumer pid |
| 58 | * @return a newly initialized svm queue |
| 59 | * |
| 60 | * The idea is to call this function in the queue consumer, |
| 61 | * and e-mail the queue pointer to the producer(s). |
| 62 | * |
| 63 | * The vpp process / main thread allocates one of these |
| 64 | * at startup; its main input queue. The vpp main input queue |
| 65 | * has a pointer to it in the shared memory segment header. |
| 66 | * |
| 67 | * You probably want to be on an svm data heap before calling this |
| 68 | * function. |
| 69 | */ |
| 70 | svm_queue_t *svm_queue_alloc_and_init (int nels, int elsize, |
| 71 | int consumer_pid); |
| 72 | svm_queue_t *svm_queue_init (void *base, int nels, int elsize); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 73 | void svm_queue_free (svm_queue_t * q); |
| 74 | int svm_queue_add (svm_queue_t * q, u8 * elem, int nowait); |
| 75 | int svm_queue_add2 (svm_queue_t * q, u8 * elem, u8 * elem2, int nowait); |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 76 | int svm_queue_sub (svm_queue_t * q, u8 * elem, svm_q_conditional_wait_t cond, |
| 77 | u32 time); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 78 | int svm_queue_sub2 (svm_queue_t * q, u8 * elem); |
| 79 | void svm_queue_lock (svm_queue_t * q); |
Florin Coras | fea813a | 2019-12-27 10:26:56 -0800 | [diff] [blame] | 80 | void svm_queue_send_signal (svm_queue_t * q, u8 is_prod); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 81 | void svm_queue_unlock (svm_queue_t * q); |
| 82 | int svm_queue_is_full (svm_queue_t * q); |
| 83 | int svm_queue_add_nolock (svm_queue_t * q, u8 * elem); |
| 84 | int svm_queue_sub_raw (svm_queue_t * q, u8 * elem); |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 85 | |
| 86 | /** |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 87 | * Wait for queue event |
| 88 | * |
| 89 | * Must be called with mutex held. |
| 90 | */ |
| 91 | void svm_queue_wait (svm_queue_t * q); |
| 92 | |
| 93 | /** |
| 94 | * Timed wait for queue event |
| 95 | * |
| 96 | * Must be called with mutex held. |
| 97 | * |
| 98 | * @param q svm queue |
| 99 | * @param timeout time in seconds |
| 100 | * @return 0 on success, ETIMEDOUT on timeout or an error |
| 101 | */ |
| 102 | int svm_queue_timedwait (svm_queue_t * q, double timeout); |
| 103 | |
| 104 | /** |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 105 | * Add element to queue with mutex held |
| 106 | * @param q queue |
| 107 | * @param elem pointer element data to add |
| 108 | */ |
| 109 | void svm_queue_add_raw (svm_queue_t * q, u8 * elem); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 110 | |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 111 | /** |
| 112 | * Set producer's event fd |
| 113 | * |
| 114 | * When the producer must generate an event it writes 1 to the provided fd. |
| 115 | * Once this is set, condvars are not used anymore for signaling. |
| 116 | */ |
| 117 | void svm_queue_set_producer_event_fd (svm_queue_t * q, int fd); |
| 118 | |
| 119 | /** |
| 120 | * Set consumer's event fd |
| 121 | * |
| 122 | * When the consumer must generate an event it writes 1 to the provided fd. |
| 123 | * Although in practice the two fds point to the same underlying file |
| 124 | * description, because the producer and consumer are different processes |
| 125 | * the descriptors will be different. It's the caller's responsibility to |
| 126 | * ensure the file descriptors are properly exchanged between the two peers. |
| 127 | */ |
| 128 | void svm_queue_set_consumer_event_fd (svm_queue_t * q, int fd); |
| 129 | |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 130 | /* |
| 131 | * DEPRECATED please use svm_queue_t instead |
| 132 | */ |
| 133 | typedef svm_queue_t unix_shared_memory_queue_t; |
| 134 | |
| 135 | #endif /* included_svm_queue_h */ |
| 136 | |
| 137 | /* |
| 138 | * fd.io coding-style-patch-verification: ON |
| 139 | * |
| 140 | * Local Variables: |
| 141 | * eval: (c-set-style "gnu") |
| 142 | * End: |
| 143 | */ |