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 | * |
| 5 | * Copyright (c) 2009 Cisco and/or its affiliates. |
| 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 | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 40 | 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 | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 47 | memset (&attr, 0, sizeof (attr)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 48 | 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); |
| 78 | memset (q, 0, sizeof (*q)); |
| 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 |
| 115 | svm_queue_send_signal (svm_queue_t * q, u8 is_prod) |
| 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; |
| 125 | ASSERT (q->consumer_evtfd != -1); |
| 126 | fd = is_prod ? q->producer_evtfd : q->consumer_evtfd; |
| 127 | rv = write (fd, &data, sizeof (data)); |
| 128 | } |
| 129 | } |
| 130 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 131 | static inline void |
| 132 | svm_queue_wait_inline (svm_queue_t * q) |
| 133 | { |
| 134 | if (q->producer_evtfd == -1) |
| 135 | { |
| 136 | pthread_cond_wait (&q->condvar, &q->mutex); |
| 137 | } |
| 138 | else |
| 139 | { |
| 140 | /* Fake a wait for event. We could use epoll but that would mean |
| 141 | * using yet another fd. Should do for now */ |
| 142 | u32 cursize = q->cursize; |
| 143 | pthread_mutex_unlock (&q->mutex); |
| 144 | while (q->cursize == cursize) |
| 145 | CLIB_PAUSE (); |
| 146 | pthread_mutex_lock (&q->mutex); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | void |
| 151 | svm_queue_wait (svm_queue_t * q) |
| 152 | { |
| 153 | svm_queue_wait_inline (q); |
| 154 | } |
| 155 | |
| 156 | static inline int |
| 157 | svm_queue_timedwait_inline (svm_queue_t * q, double timeout) |
| 158 | { |
| 159 | struct timespec ts; |
| 160 | ts.tv_sec = unix_time_now () + (u32) timeout; |
| 161 | ts.tv_nsec = (timeout - (u32) timeout) * 1e9; |
| 162 | |
| 163 | if (q->producer_evtfd == -1) |
| 164 | { |
| 165 | return pthread_cond_timedwait (&q->condvar, &q->mutex, &ts); |
| 166 | } |
| 167 | else |
| 168 | { |
| 169 | double max_time = unix_time_now () + timeout; |
| 170 | u32 cursize = q->cursize; |
| 171 | int rv; |
| 172 | |
| 173 | pthread_mutex_unlock (&q->mutex); |
| 174 | while (q->cursize == cursize && unix_time_now () < max_time) |
| 175 | CLIB_PAUSE (); |
| 176 | rv = unix_time_now () < max_time ? 0 : ETIMEDOUT; |
| 177 | pthread_mutex_lock (&q->mutex); |
| 178 | return rv; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | int |
| 183 | svm_queue_timedwait (svm_queue_t * q, double timeout) |
| 184 | { |
| 185 | return svm_queue_timedwait_inline (q, timeout); |
| 186 | } |
| 187 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 188 | /* |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 189 | * svm_queue_add_nolock |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 190 | */ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 191 | int |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 192 | svm_queue_add_nolock (svm_queue_t * q, u8 * elem) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 193 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 194 | i8 *tailp; |
| 195 | int need_broadcast = 0; |
| 196 | |
| 197 | if (PREDICT_FALSE (q->cursize == q->maxsize)) |
| 198 | { |
| 199 | while (q->cursize == q->maxsize) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 200 | svm_queue_wait_inline (q); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 201 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 202 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 203 | tailp = (i8 *) (&q->data[0] + q->elsize * q->tail); |
| 204 | clib_memcpy (tailp, elem, q->elsize); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 205 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 206 | q->tail++; |
| 207 | q->cursize++; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 208 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 209 | need_broadcast = (q->cursize == 1); |
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 | if (q->tail == q->maxsize) |
| 212 | q->tail = 0; |
| 213 | |
| 214 | if (need_broadcast) |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 215 | svm_queue_send_signal (q, 1); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 216 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 219 | void |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 220 | svm_queue_add_raw (svm_queue_t * q, u8 * elem) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 221 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 222 | i8 *tailp; |
| 223 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 224 | tailp = (i8 *) (&q->data[0] + q->elsize * q->tail); |
| 225 | clib_memcpy (tailp, elem, q->elsize); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 226 | |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 227 | q->tail = (q->tail + 1) % q->maxsize; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 228 | q->cursize++; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 229 | |
| 230 | if (q->cursize == 1) |
| 231 | svm_queue_send_signal (q, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | |
| 235 | /* |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 236 | * svm_queue_add |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 237 | */ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 238 | int |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 239 | svm_queue_add (svm_queue_t * q, u8 * elem, int nowait) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 240 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 241 | i8 *tailp; |
| 242 | int need_broadcast = 0; |
| 243 | |
| 244 | if (nowait) |
| 245 | { |
| 246 | /* zero on success */ |
| 247 | if (pthread_mutex_trylock (&q->mutex)) |
| 248 | { |
| 249 | return (-1); |
| 250 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 251 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 252 | else |
| 253 | pthread_mutex_lock (&q->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 254 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 255 | if (PREDICT_FALSE (q->cursize == q->maxsize)) |
| 256 | { |
| 257 | if (nowait) |
| 258 | { |
| 259 | pthread_mutex_unlock (&q->mutex); |
| 260 | return (-2); |
| 261 | } |
| 262 | while (q->cursize == q->maxsize) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 263 | svm_queue_wait_inline (q); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 264 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 265 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 266 | tailp = (i8 *) (&q->data[0] + q->elsize * q->tail); |
| 267 | clib_memcpy (tailp, elem, q->elsize); |
| 268 | |
| 269 | q->tail++; |
| 270 | q->cursize++; |
| 271 | |
| 272 | need_broadcast = (q->cursize == 1); |
| 273 | |
| 274 | if (q->tail == q->maxsize) |
| 275 | q->tail = 0; |
| 276 | |
| 277 | if (need_broadcast) |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 278 | svm_queue_send_signal (q, 1); |
| 279 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 280 | pthread_mutex_unlock (&q->mutex); |
| 281 | |
| 282 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /* |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 286 | * svm_queue_add2 |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 287 | */ |
| 288 | int |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 289 | 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] | 290 | { |
| 291 | i8 *tailp; |
| 292 | int need_broadcast = 0; |
| 293 | |
| 294 | if (nowait) |
| 295 | { |
| 296 | /* zero on success */ |
| 297 | if (pthread_mutex_trylock (&q->mutex)) |
| 298 | { |
| 299 | return (-1); |
| 300 | } |
| 301 | } |
| 302 | else |
| 303 | pthread_mutex_lock (&q->mutex); |
| 304 | |
| 305 | if (PREDICT_FALSE (q->cursize + 1 == q->maxsize)) |
| 306 | { |
| 307 | if (nowait) |
| 308 | { |
| 309 | pthread_mutex_unlock (&q->mutex); |
| 310 | return (-2); |
| 311 | } |
| 312 | while (q->cursize + 1 == q->maxsize) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 313 | svm_queue_wait_inline (q); |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | tailp = (i8 *) (&q->data[0] + q->elsize * q->tail); |
| 317 | clib_memcpy (tailp, elem, q->elsize); |
| 318 | |
| 319 | q->tail++; |
| 320 | q->cursize++; |
| 321 | |
| 322 | if (q->tail == q->maxsize) |
| 323 | q->tail = 0; |
| 324 | |
| 325 | need_broadcast = (q->cursize == 1); |
| 326 | |
| 327 | tailp = (i8 *) (&q->data[0] + q->elsize * q->tail); |
| 328 | clib_memcpy (tailp, elem2, q->elsize); |
| 329 | |
| 330 | q->tail++; |
| 331 | q->cursize++; |
| 332 | |
| 333 | if (q->tail == q->maxsize) |
| 334 | q->tail = 0; |
| 335 | |
| 336 | if (need_broadcast) |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 337 | svm_queue_send_signal (q, 1); |
| 338 | |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 339 | pthread_mutex_unlock (&q->mutex); |
| 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | /* |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 345 | * svm_queue_sub |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 346 | */ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 347 | int |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 348 | svm_queue_sub (svm_queue_t * q, u8 * elem, svm_q_conditional_wait_t cond, |
| 349 | u32 time) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 350 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 351 | i8 *headp; |
| 352 | int need_broadcast = 0; |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 353 | int rc = 0; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 354 | |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 355 | if (cond == SVM_Q_NOWAIT) |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 356 | { |
| 357 | /* zero on success */ |
| 358 | if (pthread_mutex_trylock (&q->mutex)) |
| 359 | { |
| 360 | return (-1); |
| 361 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 362 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 363 | else |
| 364 | pthread_mutex_lock (&q->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 365 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 366 | if (PREDICT_FALSE (q->cursize == 0)) |
| 367 | { |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 368 | if (cond == SVM_Q_NOWAIT) |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 369 | { |
| 370 | pthread_mutex_unlock (&q->mutex); |
| 371 | return (-2); |
| 372 | } |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 373 | else if (cond == SVM_Q_TIMEDWAIT) |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 374 | { |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 375 | while (q->cursize == 0 && rc == 0) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 376 | rc = svm_queue_timedwait_inline (q, time); |
| 377 | |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 378 | if (rc == ETIMEDOUT) |
| 379 | { |
| 380 | pthread_mutex_unlock (&q->mutex); |
| 381 | return ETIMEDOUT; |
| 382 | } |
| 383 | } |
| 384 | else |
| 385 | { |
| 386 | while (q->cursize == 0) |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 387 | svm_queue_wait_inline (q); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 388 | } |
| 389 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 390 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 391 | headp = (i8 *) (&q->data[0] + q->elsize * q->head); |
| 392 | clib_memcpy (elem, headp, q->elsize); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 393 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 394 | q->head++; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 395 | /* $$$$ JFC shouldn't this be == 0? */ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 396 | if (q->cursize == q->maxsize) |
| 397 | need_broadcast = 1; |
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 | q->cursize--; |
| 400 | |
| 401 | if (q->head == q->maxsize) |
| 402 | q->head = 0; |
| 403 | |
| 404 | if (need_broadcast) |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 405 | svm_queue_send_signal (q, 0); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 406 | |
| 407 | pthread_mutex_unlock (&q->mutex); |
| 408 | |
| 409 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 412 | int |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 413 | svm_queue_sub2 (svm_queue_t * q, u8 * elem) |
| 414 | { |
| 415 | int need_broadcast; |
| 416 | i8 *headp; |
| 417 | |
| 418 | pthread_mutex_lock (&q->mutex); |
| 419 | if (q->cursize == 0) |
| 420 | { |
| 421 | pthread_mutex_unlock (&q->mutex); |
| 422 | return -1; |
| 423 | } |
| 424 | |
| 425 | headp = (i8 *) (&q->data[0] + q->elsize * q->head); |
| 426 | clib_memcpy (elem, headp, q->elsize); |
| 427 | |
| 428 | q->head++; |
| 429 | need_broadcast = (q->cursize == q->maxsize / 2); |
| 430 | q->cursize--; |
| 431 | |
| 432 | if (PREDICT_FALSE (q->head == q->maxsize)) |
| 433 | q->head = 0; |
| 434 | pthread_mutex_unlock (&q->mutex); |
| 435 | |
| 436 | if (need_broadcast) |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 437 | svm_queue_send_signal (q, 0); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 438 | |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | int |
| 443 | svm_queue_sub_raw (svm_queue_t * q, u8 * elem) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 444 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 445 | i8 *headp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 446 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 447 | if (PREDICT_FALSE (q->cursize == 0)) |
| 448 | { |
| 449 | while (q->cursize == 0) |
| 450 | ; |
| 451 | } |
| 452 | |
| 453 | headp = (i8 *) (&q->data[0] + q->elsize * q->head); |
| 454 | clib_memcpy (elem, headp, q->elsize); |
| 455 | |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 456 | q->head = (q->head + 1) % q->maxsize; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 457 | q->cursize--; |
| 458 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 459 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 460 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 461 | |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 462 | void |
| 463 | svm_queue_set_producer_event_fd (svm_queue_t * q, int fd) |
| 464 | { |
| 465 | q->producer_evtfd = fd; |
| 466 | } |
| 467 | |
| 468 | void |
| 469 | svm_queue_set_consumer_event_fd (svm_queue_t * q, int fd) |
| 470 | { |
| 471 | q->consumer_evtfd = fd; |
| 472 | } |
| 473 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 474 | /* |
| 475 | * fd.io coding-style-patch-verification: ON |
| 476 | * |
| 477 | * Local Variables: |
| 478 | * eval: (c-set-style "gnu") |
| 479 | * End: |
| 480 | */ |