Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 1 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2 | *------------------------------------------------------------------ |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 3 | * svm_queue.c - unidirectional shared-memory queues |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 4 | * |
Florin Coras | c5df8c7 | 2019-04-08 07:42:30 -0700 | [diff] [blame] | 5 | * Copyright (c) 2009-2019 Cisco and/or its affiliates. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [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 | |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 20 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <pthread.h> |
| 25 | #include <vppinfra/mem.h> |
| 26 | #include <vppinfra/format.h> |
| 27 | #include <vppinfra/cache.h> |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 28 | #include <svm/queue.h> |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 29 | #include <vppinfra/time.h> |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 30 | #include <vppinfra/lock.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 31 | |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 32 | svm_queue_t * |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 33 | svm_queue_init (void *base, int nels, int elsize) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 34 | { |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 35 | svm_queue_t *q; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 36 | pthread_mutexattr_t attr; |
| 37 | pthread_condattr_t cattr; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 38 | |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 39 | q = (svm_queue_t *) base; |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 40 | clib_memset (q, 0, sizeof (*q)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 41 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 42 | q->elsize = elsize; |
| 43 | q->maxsize = nels; |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 44 | q->producer_evtfd = -1; |
| 45 | q->consumer_evtfd = -1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 46 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 47 | clib_memset (&attr, 0, sizeof (attr)); |
| 48 | clib_memset (&cattr, 0, sizeof (cattr)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 49 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 50 | if (pthread_mutexattr_init (&attr)) |
| 51 | clib_unix_warning ("mutexattr_init"); |
| 52 | if (pthread_mutexattr_setpshared (&attr, PTHREAD_PROCESS_SHARED)) |
| 53 | clib_unix_warning ("pthread_mutexattr_setpshared"); |
| 54 | if (pthread_mutex_init (&q->mutex, &attr)) |
| 55 | clib_unix_warning ("mutex_init"); |
| 56 | if (pthread_mutexattr_destroy (&attr)) |
| 57 | clib_unix_warning ("mutexattr_destroy"); |
| 58 | if (pthread_condattr_init (&cattr)) |
| 59 | clib_unix_warning ("condattr_init"); |
| 60 | /* prints funny-looking messages in the Linux target */ |
| 61 | if (pthread_condattr_setpshared (&cattr, PTHREAD_PROCESS_SHARED)) |
| 62 | clib_unix_warning ("condattr_setpshared"); |
| 63 | if (pthread_cond_init (&q->condvar, &cattr)) |
| 64 | clib_unix_warning ("cond_init1"); |
| 65 | if (pthread_condattr_destroy (&cattr)) |
| 66 | clib_unix_warning ("cond_init2"); |
| 67 | |
| 68 | return (q); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 71 | svm_queue_t * |
| 72 | svm_queue_alloc_and_init (int nels, int elsize, int consumer_pid) |
| 73 | { |
| 74 | svm_queue_t *q; |
| 75 | |
| 76 | q = clib_mem_alloc_aligned (sizeof (svm_queue_t) |
| 77 | + nels * elsize, CLIB_CACHE_LINE_BYTES); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 78 | clib_memset (q, 0, sizeof (*q)); |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 79 | q = svm_queue_init (q, nels, elsize); |
| 80 | q->consumer_pid = consumer_pid; |
| 81 | |
| 82 | return q; |
| 83 | } |
| 84 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 85 | /* |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 86 | * svm_queue_free |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 87 | */ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 88 | void |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 89 | svm_queue_free (svm_queue_t * q) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 90 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 91 | (void) pthread_mutex_destroy (&q->mutex); |
| 92 | (void) pthread_cond_destroy (&q->condvar); |
| 93 | clib_mem_free (q); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 96 | void |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 97 | svm_queue_lock (svm_queue_t * q) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 98 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 99 | pthread_mutex_lock (&q->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 102 | void |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 103 | svm_queue_unlock (svm_queue_t * q) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 104 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 105 | pthread_mutex_unlock (&q->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 108 | int |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 109 | svm_queue_is_full (svm_queue_t * q) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 110 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 111 | return q->cursize == q->maxsize; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 114 | static inline void |
Florin Coras | fea813a | 2019-12-27 10:26:56 -0800 | [diff] [blame] | 115 | svm_queue_send_signal_inline (svm_queue_t * q, u8 is_prod) |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 116 | { |
| 117 | if (q->producer_evtfd == -1) |
| 118 | { |
| 119 | (void) pthread_cond_broadcast (&q->condvar); |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | int __clib_unused rv, fd; |
| 124 | u64 data = 1; |
Florin Coras | 1459877 | 2018-09-04 19:47:52 -0700 | [diff] [blame] | 125 | ASSERT (q->consumer_evtfd > 0 && q->producer_evtfd > 0); |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 126 | fd = is_prod ? q->producer_evtfd : q->consumer_evtfd; |
| 127 | rv = write (fd, &data, sizeof (data)); |
Florin Coras | fea813a | 2019-12-27 10:26:56 -0800 | [diff] [blame] | 128 | if (PREDICT_FALSE (rv)) |
| 129 | clib_unix_warning ("signal write returned %d", rv); |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Florin Coras | fea813a | 2019-12-27 10:26:56 -0800 | [diff] [blame] | 133 | void |
| 134 | svm_queue_send_signal (svm_queue_t * q, u8 is_prod) |
| 135 | { |
| 136 | svm_queue_send_signal_inline (q, is_prod); |
| 137 | } |
| 138 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 139 | static inline void |
| 140 | svm_queue_wait_inline (svm_queue_t * q) |
| 141 | { |
| 142 | if (q->producer_evtfd == -1) |
| 143 | { |
| 144 | pthread_cond_wait (&q->condvar, &q->mutex); |
| 145 | } |
| 146 | else |
| 147 | { |
| 148 | /* Fake a wait for event. We could use epoll but that would mean |
| 149 | * using yet another fd. Should do for now */ |
| 150 | u32 cursize = q->cursize; |
Ping Yu | a474894 | 2019-03-12 05:28:16 -0400 | [diff] [blame] | 151 | svm_queue_unlock (q); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 152 | while (q->cursize == cursize) |
| 153 | CLIB_PAUSE (); |
Ping Yu | a474894 | 2019-03-12 05:28:16 -0400 | [diff] [blame] | 154 | svm_queue_lock (q); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
| 158 | void |
| 159 | svm_queue_wait (svm_queue_t * q) |
| 160 | { |
| 161 | svm_queue_wait_inline (q); |
| 162 | } |
| 163 | |
| 164 | static inline int |
| 165 | svm_queue_timedwait_inline (svm_queue_t * q, double timeout) |
| 166 | { |
| 167 | struct timespec ts; |
| 168 | ts.tv_sec = unix_time_now () + (u32) timeout; |
| 169 | ts.tv_nsec = (timeout - (u32) timeout) * 1e9; |
| 170 | |
| 171 | if (q->producer_evtfd == -1) |
| 172 | { |
| 173 | return pthread_cond_timedwait (&q->condvar, &q->mutex, &ts); |
| 174 | } |
| 175 | else |
| 176 | { |
| 177 | double max_time = unix_time_now () + timeout; |
| 178 | u32 cursize = q->cursize; |
| 179 | int rv; |
| 180 | |
Ping Yu | a474894 | 2019-03-12 05:28:16 -0400 | [diff] [blame] | 181 | svm_queue_unlock (q); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 182 | while (q->cursize == cursize && unix_time_now () < max_time) |
| 183 | CLIB_PAUSE (); |
| 184 | rv = unix_time_now () < max_time ? 0 : ETIMEDOUT; |
Vratko Polak | a29d18a | 2019-05-21 13:47:29 +0200 | [diff] [blame] | 185 | svm_queue_lock (q); |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 186 | return rv; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | int |
| 191 | svm_queue_timedwait (svm_queue_t * q, double timeout) |
| 192 | { |
| 193 | return svm_queue_timedwait_inline (q, timeout); |
| 194 | } |
| 195 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 196 | /* |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 197 | * svm_queue_add_nolock |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 198 | */ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 199 | int |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 200 | svm_queue_add_nolock (svm_queue_t * q, u8 * elem) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 201 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 202 | i8 *tailp; |
| 203 | int need_broadcast = 0; |
| 204 | |
| 205 | if (PREDICT_FALSE (q->cursize == q->maxsize)) |
| 206 | { |
| 207 | while (q->cursize == q->maxsize) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 208 | svm_queue_wait_inline (q); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 209 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 210 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 211 | tailp = (i8 *) (&q->data[0] + q->elsize * q->tail); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 212 | clib_memcpy_fast (tailp, elem, q->elsize); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 213 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 214 | q->tail++; |
| 215 | q->cursize++; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 216 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 217 | need_broadcast = (q->cursize == 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 218 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 219 | if (q->tail == q->maxsize) |
| 220 | q->tail = 0; |
| 221 | |
| 222 | if (need_broadcast) |
Florin Coras | fea813a | 2019-12-27 10:26:56 -0800 | [diff] [blame] | 223 | svm_queue_send_signal_inline (q, 1); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 224 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 227 | void |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 228 | svm_queue_add_raw (svm_queue_t * q, u8 * elem) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 229 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 230 | i8 *tailp; |
| 231 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 232 | tailp = (i8 *) (&q->data[0] + q->elsize * q->tail); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 233 | clib_memcpy_fast (tailp, elem, q->elsize); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 234 | |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 235 | q->tail = (q->tail + 1) % q->maxsize; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 236 | q->cursize++; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 237 | |
| 238 | if (q->cursize == 1) |
Florin Coras | fea813a | 2019-12-27 10:26:56 -0800 | [diff] [blame] | 239 | svm_queue_send_signal_inline (q, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | |
| 243 | /* |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 244 | * svm_queue_add |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 245 | */ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 246 | int |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 247 | svm_queue_add (svm_queue_t * q, u8 * elem, int nowait) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 248 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 249 | i8 *tailp; |
| 250 | int need_broadcast = 0; |
| 251 | |
| 252 | if (nowait) |
| 253 | { |
| 254 | /* zero on success */ |
| 255 | if (pthread_mutex_trylock (&q->mutex)) |
| 256 | { |
| 257 | return (-1); |
| 258 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 259 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 260 | else |
Ping Yu | a474894 | 2019-03-12 05:28:16 -0400 | [diff] [blame] | 261 | svm_queue_lock (q); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 262 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 263 | if (PREDICT_FALSE (q->cursize == q->maxsize)) |
| 264 | { |
| 265 | if (nowait) |
| 266 | { |
Ping Yu | a474894 | 2019-03-12 05:28:16 -0400 | [diff] [blame] | 267 | svm_queue_unlock (q); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 268 | return (-2); |
| 269 | } |
| 270 | while (q->cursize == q->maxsize) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 271 | svm_queue_wait_inline (q); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 272 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 273 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 274 | tailp = (i8 *) (&q->data[0] + q->elsize * q->tail); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 275 | clib_memcpy_fast (tailp, elem, q->elsize); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 276 | |
| 277 | q->tail++; |
| 278 | q->cursize++; |
| 279 | |
| 280 | need_broadcast = (q->cursize == 1); |
| 281 | |
| 282 | if (q->tail == q->maxsize) |
| 283 | q->tail = 0; |
| 284 | |
| 285 | if (need_broadcast) |
Florin Coras | fea813a | 2019-12-27 10:26:56 -0800 | [diff] [blame] | 286 | svm_queue_send_signal_inline (q, 1); |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 287 | |
Ping Yu | a474894 | 2019-03-12 05:28:16 -0400 | [diff] [blame] | 288 | svm_queue_unlock (q); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 289 | |
| 290 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /* |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 294 | * svm_queue_add2 |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 295 | */ |
| 296 | int |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 297 | svm_queue_add2 (svm_queue_t * q, u8 * elem, u8 * elem2, int nowait) |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 298 | { |
| 299 | i8 *tailp; |
| 300 | int need_broadcast = 0; |
| 301 | |
| 302 | if (nowait) |
| 303 | { |
| 304 | /* zero on success */ |
| 305 | if (pthread_mutex_trylock (&q->mutex)) |
| 306 | { |
| 307 | return (-1); |
| 308 | } |
| 309 | } |
| 310 | else |
Ping Yu | a474894 | 2019-03-12 05:28:16 -0400 | [diff] [blame] | 311 | svm_queue_lock (q); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 312 | |
| 313 | if (PREDICT_FALSE (q->cursize + 1 == q->maxsize)) |
| 314 | { |
| 315 | if (nowait) |
| 316 | { |
Ping Yu | a474894 | 2019-03-12 05:28:16 -0400 | [diff] [blame] | 317 | svm_queue_unlock (q); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 318 | return (-2); |
| 319 | } |
| 320 | while (q->cursize + 1 == q->maxsize) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 321 | svm_queue_wait_inline (q); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | tailp = (i8 *) (&q->data[0] + q->elsize * q->tail); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 325 | clib_memcpy_fast (tailp, elem, q->elsize); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 326 | |
| 327 | q->tail++; |
| 328 | q->cursize++; |
| 329 | |
| 330 | if (q->tail == q->maxsize) |
| 331 | q->tail = 0; |
| 332 | |
| 333 | need_broadcast = (q->cursize == 1); |
| 334 | |
| 335 | tailp = (i8 *) (&q->data[0] + q->elsize * q->tail); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 336 | clib_memcpy_fast (tailp, elem2, q->elsize); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 337 | |
| 338 | q->tail++; |
| 339 | q->cursize++; |
| 340 | |
| 341 | if (q->tail == q->maxsize) |
| 342 | q->tail = 0; |
| 343 | |
| 344 | if (need_broadcast) |
Florin Coras | fea813a | 2019-12-27 10:26:56 -0800 | [diff] [blame] | 345 | svm_queue_send_signal_inline (q, 1); |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 346 | |
Ping Yu | a474894 | 2019-03-12 05:28:16 -0400 | [diff] [blame] | 347 | svm_queue_unlock (q); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 348 | |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | /* |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 353 | * svm_queue_sub |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 354 | */ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 355 | int |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 356 | svm_queue_sub (svm_queue_t * q, u8 * elem, svm_q_conditional_wait_t cond, |
| 357 | u32 time) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 358 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 359 | i8 *headp; |
| 360 | int need_broadcast = 0; |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 361 | int rc = 0; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 362 | |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 363 | if (cond == SVM_Q_NOWAIT) |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 364 | { |
| 365 | /* zero on success */ |
| 366 | if (pthread_mutex_trylock (&q->mutex)) |
| 367 | { |
| 368 | return (-1); |
| 369 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 370 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 371 | else |
Ping Yu | a474894 | 2019-03-12 05:28:16 -0400 | [diff] [blame] | 372 | svm_queue_lock (q); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 373 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 374 | if (PREDICT_FALSE (q->cursize == 0)) |
| 375 | { |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 376 | if (cond == SVM_Q_NOWAIT) |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 377 | { |
Ping Yu | a474894 | 2019-03-12 05:28:16 -0400 | [diff] [blame] | 378 | svm_queue_unlock (q); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 379 | return (-2); |
| 380 | } |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 381 | else if (cond == SVM_Q_TIMEDWAIT) |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 382 | { |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 383 | while (q->cursize == 0 && rc == 0) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 384 | rc = svm_queue_timedwait_inline (q, time); |
| 385 | |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 386 | if (rc == ETIMEDOUT) |
| 387 | { |
Ping Yu | a474894 | 2019-03-12 05:28:16 -0400 | [diff] [blame] | 388 | svm_queue_unlock (q); |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 389 | return ETIMEDOUT; |
| 390 | } |
| 391 | } |
| 392 | else |
| 393 | { |
| 394 | while (q->cursize == 0) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 395 | svm_queue_wait_inline (q); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 396 | } |
| 397 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 398 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 399 | headp = (i8 *) (&q->data[0] + q->elsize * q->head); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 400 | clib_memcpy_fast (elem, headp, q->elsize); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 401 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 402 | q->head++; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 403 | /* $$$$ JFC shouldn't this be == 0? */ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 404 | if (q->cursize == q->maxsize) |
| 405 | need_broadcast = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 406 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 407 | q->cursize--; |
| 408 | |
| 409 | if (q->head == q->maxsize) |
| 410 | q->head = 0; |
| 411 | |
| 412 | if (need_broadcast) |
Florin Coras | fea813a | 2019-12-27 10:26:56 -0800 | [diff] [blame] | 413 | svm_queue_send_signal_inline (q, 0); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 414 | |
Ping Yu | a474894 | 2019-03-12 05:28:16 -0400 | [diff] [blame] | 415 | svm_queue_unlock (q); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 416 | |
| 417 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 420 | int |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 421 | svm_queue_sub2 (svm_queue_t * q, u8 * elem) |
| 422 | { |
| 423 | int need_broadcast; |
| 424 | i8 *headp; |
| 425 | |
Ping Yu | a474894 | 2019-03-12 05:28:16 -0400 | [diff] [blame] | 426 | svm_queue_lock (q); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 427 | if (q->cursize == 0) |
| 428 | { |
Ping Yu | a474894 | 2019-03-12 05:28:16 -0400 | [diff] [blame] | 429 | svm_queue_unlock (q); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 430 | return -1; |
| 431 | } |
| 432 | |
| 433 | headp = (i8 *) (&q->data[0] + q->elsize * q->head); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 434 | clib_memcpy_fast (elem, headp, q->elsize); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 435 | |
| 436 | q->head++; |
| 437 | need_broadcast = (q->cursize == q->maxsize / 2); |
| 438 | q->cursize--; |
| 439 | |
| 440 | if (PREDICT_FALSE (q->head == q->maxsize)) |
| 441 | q->head = 0; |
Ping Yu | a474894 | 2019-03-12 05:28:16 -0400 | [diff] [blame] | 442 | svm_queue_unlock (q); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 443 | |
| 444 | if (need_broadcast) |
Florin Coras | fea813a | 2019-12-27 10:26:56 -0800 | [diff] [blame] | 445 | svm_queue_send_signal_inline (q, 0); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 446 | |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | int |
| 451 | svm_queue_sub_raw (svm_queue_t * q, u8 * elem) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 452 | { |
Florin Coras | fea813a | 2019-12-27 10:26:56 -0800 | [diff] [blame] | 453 | int need_broadcast; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 454 | i8 *headp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 455 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 456 | if (PREDICT_FALSE (q->cursize == 0)) |
| 457 | { |
| 458 | while (q->cursize == 0) |
| 459 | ; |
| 460 | } |
| 461 | |
| 462 | headp = (i8 *) (&q->data[0] + q->elsize * q->head); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 463 | clib_memcpy_fast (elem, headp, q->elsize); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 464 | |
Florin Coras | fea813a | 2019-12-27 10:26:56 -0800 | [diff] [blame] | 465 | need_broadcast = q->cursize == q->maxsize; |
| 466 | |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 467 | q->head = (q->head + 1) % q->maxsize; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 468 | q->cursize--; |
| 469 | |
Florin Coras | fea813a | 2019-12-27 10:26:56 -0800 | [diff] [blame] | 470 | if (PREDICT_FALSE (need_broadcast)) |
| 471 | svm_queue_send_signal_inline (q, 0); |
| 472 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 473 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 474 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 475 | |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 476 | void |
| 477 | svm_queue_set_producer_event_fd (svm_queue_t * q, int fd) |
| 478 | { |
| 479 | q->producer_evtfd = fd; |
| 480 | } |
| 481 | |
| 482 | void |
| 483 | svm_queue_set_consumer_event_fd (svm_queue_t * q, int fd) |
| 484 | { |
| 485 | q->consumer_evtfd = fd; |
| 486 | } |
| 487 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 488 | /* |
| 489 | * fd.io coding-style-patch-verification: ON |
| 490 | * |
| 491 | * Local Variables: |
| 492 | * eval: (c-set-style "gnu") |
| 493 | * End: |
| 494 | */ |