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> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 30 | |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 31 | svm_queue_t * |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 32 | svm_queue_init (void *base, int nels, int elsize) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 33 | { |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 34 | svm_queue_t *q; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 35 | pthread_mutexattr_t attr; |
| 36 | pthread_condattr_t cattr; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 37 | |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 38 | q = (svm_queue_t *) base; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 39 | memset (q, 0, sizeof (*q)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 40 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 41 | q->elsize = elsize; |
| 42 | q->maxsize = nels; |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 43 | q->producer_evtfd = -1; |
| 44 | q->consumer_evtfd = -1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 45 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 46 | memset (&attr, 0, sizeof (attr)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 47 | memset (&cattr, 0, sizeof (cattr)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 48 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 49 | if (pthread_mutexattr_init (&attr)) |
| 50 | clib_unix_warning ("mutexattr_init"); |
| 51 | if (pthread_mutexattr_setpshared (&attr, PTHREAD_PROCESS_SHARED)) |
| 52 | clib_unix_warning ("pthread_mutexattr_setpshared"); |
| 53 | if (pthread_mutex_init (&q->mutex, &attr)) |
| 54 | clib_unix_warning ("mutex_init"); |
| 55 | if (pthread_mutexattr_destroy (&attr)) |
| 56 | clib_unix_warning ("mutexattr_destroy"); |
| 57 | if (pthread_condattr_init (&cattr)) |
| 58 | clib_unix_warning ("condattr_init"); |
| 59 | /* prints funny-looking messages in the Linux target */ |
| 60 | if (pthread_condattr_setpshared (&cattr, PTHREAD_PROCESS_SHARED)) |
| 61 | clib_unix_warning ("condattr_setpshared"); |
| 62 | if (pthread_cond_init (&q->condvar, &cattr)) |
| 63 | clib_unix_warning ("cond_init1"); |
| 64 | if (pthread_condattr_destroy (&cattr)) |
| 65 | clib_unix_warning ("cond_init2"); |
| 66 | |
| 67 | return (q); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 70 | svm_queue_t * |
| 71 | svm_queue_alloc_and_init (int nels, int elsize, int consumer_pid) |
| 72 | { |
| 73 | svm_queue_t *q; |
| 74 | |
| 75 | q = clib_mem_alloc_aligned (sizeof (svm_queue_t) |
| 76 | + nels * elsize, CLIB_CACHE_LINE_BYTES); |
| 77 | memset (q, 0, sizeof (*q)); |
| 78 | q = svm_queue_init (q, nels, elsize); |
| 79 | q->consumer_pid = consumer_pid; |
| 80 | |
| 81 | return q; |
| 82 | } |
| 83 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 84 | /* |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 85 | * svm_queue_free |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 86 | */ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 87 | void |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 88 | svm_queue_free (svm_queue_t * q) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 89 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 90 | (void) pthread_mutex_destroy (&q->mutex); |
| 91 | (void) pthread_cond_destroy (&q->condvar); |
| 92 | clib_mem_free (q); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 95 | void |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 96 | svm_queue_lock (svm_queue_t * q) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 97 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 98 | pthread_mutex_lock (&q->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 101 | void |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 102 | svm_queue_unlock (svm_queue_t * q) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 103 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 104 | pthread_mutex_unlock (&q->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 107 | int |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 108 | svm_queue_is_full (svm_queue_t * q) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 109 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 110 | return q->cursize == q->maxsize; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 113 | static inline void |
| 114 | svm_queue_send_signal (svm_queue_t * q, u8 is_prod) |
| 115 | { |
| 116 | if (q->producer_evtfd == -1) |
| 117 | { |
| 118 | (void) pthread_cond_broadcast (&q->condvar); |
| 119 | } |
| 120 | else |
| 121 | { |
| 122 | int __clib_unused rv, fd; |
| 123 | u64 data = 1; |
| 124 | ASSERT (q->consumer_evtfd != -1); |
| 125 | fd = is_prod ? q->producer_evtfd : q->consumer_evtfd; |
| 126 | rv = write (fd, &data, sizeof (data)); |
| 127 | } |
| 128 | } |
| 129 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 130 | /* |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 131 | * svm_queue_add_nolock |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 132 | */ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 133 | int |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 134 | svm_queue_add_nolock (svm_queue_t * q, u8 * elem) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 135 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 136 | i8 *tailp; |
| 137 | int need_broadcast = 0; |
| 138 | |
| 139 | if (PREDICT_FALSE (q->cursize == q->maxsize)) |
| 140 | { |
| 141 | while (q->cursize == q->maxsize) |
| 142 | { |
| 143 | (void) pthread_cond_wait (&q->condvar, &q->mutex); |
| 144 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 145 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 146 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 147 | tailp = (i8 *) (&q->data[0] + q->elsize * q->tail); |
| 148 | clib_memcpy (tailp, elem, q->elsize); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 149 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 150 | q->tail++; |
| 151 | q->cursize++; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 152 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 153 | need_broadcast = (q->cursize == 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 154 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 155 | if (q->tail == q->maxsize) |
| 156 | q->tail = 0; |
| 157 | |
| 158 | if (need_broadcast) |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 159 | svm_queue_send_signal (q, 1); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 160 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 163 | void |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 164 | svm_queue_add_raw (svm_queue_t * q, u8 * elem) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 165 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 166 | i8 *tailp; |
| 167 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 168 | tailp = (i8 *) (&q->data[0] + q->elsize * q->tail); |
| 169 | clib_memcpy (tailp, elem, q->elsize); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 170 | |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 171 | q->tail = (q->tail + 1) % q->maxsize; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 172 | q->cursize++; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | |
| 176 | /* |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 177 | * svm_queue_add |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 178 | */ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 179 | int |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 180 | svm_queue_add (svm_queue_t * q, u8 * elem, int nowait) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 181 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 182 | i8 *tailp; |
| 183 | int need_broadcast = 0; |
| 184 | |
| 185 | if (nowait) |
| 186 | { |
| 187 | /* zero on success */ |
| 188 | if (pthread_mutex_trylock (&q->mutex)) |
| 189 | { |
| 190 | return (-1); |
| 191 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 192 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 193 | else |
| 194 | pthread_mutex_lock (&q->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 195 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 196 | if (PREDICT_FALSE (q->cursize == q->maxsize)) |
| 197 | { |
| 198 | if (nowait) |
| 199 | { |
| 200 | pthread_mutex_unlock (&q->mutex); |
| 201 | return (-2); |
| 202 | } |
| 203 | while (q->cursize == q->maxsize) |
| 204 | { |
| 205 | (void) pthread_cond_wait (&q->condvar, &q->mutex); |
| 206 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 207 | } |
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 | tailp = (i8 *) (&q->data[0] + q->elsize * q->tail); |
| 210 | clib_memcpy (tailp, elem, q->elsize); |
| 211 | |
| 212 | q->tail++; |
| 213 | q->cursize++; |
| 214 | |
| 215 | need_broadcast = (q->cursize == 1); |
| 216 | |
| 217 | if (q->tail == q->maxsize) |
| 218 | q->tail = 0; |
| 219 | |
| 220 | if (need_broadcast) |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 221 | svm_queue_send_signal (q, 1); |
| 222 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 223 | pthread_mutex_unlock (&q->mutex); |
| 224 | |
| 225 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | /* |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 229 | * svm_queue_add2 |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 230 | */ |
| 231 | int |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 232 | 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] | 233 | { |
| 234 | i8 *tailp; |
| 235 | int need_broadcast = 0; |
| 236 | |
| 237 | if (nowait) |
| 238 | { |
| 239 | /* zero on success */ |
| 240 | if (pthread_mutex_trylock (&q->mutex)) |
| 241 | { |
| 242 | return (-1); |
| 243 | } |
| 244 | } |
| 245 | else |
| 246 | pthread_mutex_lock (&q->mutex); |
| 247 | |
| 248 | if (PREDICT_FALSE (q->cursize + 1 == q->maxsize)) |
| 249 | { |
| 250 | if (nowait) |
| 251 | { |
| 252 | pthread_mutex_unlock (&q->mutex); |
| 253 | return (-2); |
| 254 | } |
| 255 | while (q->cursize + 1 == q->maxsize) |
| 256 | { |
| 257 | (void) pthread_cond_wait (&q->condvar, &q->mutex); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | tailp = (i8 *) (&q->data[0] + q->elsize * q->tail); |
| 262 | clib_memcpy (tailp, elem, q->elsize); |
| 263 | |
| 264 | q->tail++; |
| 265 | q->cursize++; |
| 266 | |
| 267 | if (q->tail == q->maxsize) |
| 268 | q->tail = 0; |
| 269 | |
| 270 | need_broadcast = (q->cursize == 1); |
| 271 | |
| 272 | tailp = (i8 *) (&q->data[0] + q->elsize * q->tail); |
| 273 | clib_memcpy (tailp, elem2, q->elsize); |
| 274 | |
| 275 | q->tail++; |
| 276 | q->cursize++; |
| 277 | |
| 278 | if (q->tail == q->maxsize) |
| 279 | q->tail = 0; |
| 280 | |
| 281 | if (need_broadcast) |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 282 | svm_queue_send_signal (q, 1); |
| 283 | |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 284 | pthread_mutex_unlock (&q->mutex); |
| 285 | |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | /* |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 290 | * svm_queue_sub |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 291 | */ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 292 | int |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 293 | svm_queue_sub (svm_queue_t * q, u8 * elem, svm_q_conditional_wait_t cond, |
| 294 | u32 time) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 295 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 296 | i8 *headp; |
| 297 | int need_broadcast = 0; |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 298 | int rc = 0; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 299 | |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 300 | if (cond == SVM_Q_NOWAIT) |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 301 | { |
| 302 | /* zero on success */ |
| 303 | if (pthread_mutex_trylock (&q->mutex)) |
| 304 | { |
| 305 | return (-1); |
| 306 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 307 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 308 | else |
| 309 | pthread_mutex_lock (&q->mutex); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 310 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 311 | if (PREDICT_FALSE (q->cursize == 0)) |
| 312 | { |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 313 | if (cond == SVM_Q_NOWAIT) |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 314 | { |
| 315 | pthread_mutex_unlock (&q->mutex); |
| 316 | return (-2); |
| 317 | } |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 318 | else if (cond == SVM_Q_TIMEDWAIT) |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 319 | { |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 320 | struct timespec ts; |
| 321 | ts.tv_sec = unix_time_now () + time; |
| 322 | ts.tv_nsec = 0; |
| 323 | while (q->cursize == 0 && rc == 0) |
| 324 | { |
| 325 | rc = pthread_cond_timedwait (&q->condvar, &q->mutex, &ts); |
| 326 | } |
| 327 | if (rc == ETIMEDOUT) |
| 328 | { |
| 329 | pthread_mutex_unlock (&q->mutex); |
| 330 | return ETIMEDOUT; |
| 331 | } |
| 332 | } |
| 333 | else |
| 334 | { |
| 335 | while (q->cursize == 0) |
| 336 | { |
| 337 | (void) pthread_cond_wait (&q->condvar, &q->mutex); |
| 338 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 339 | } |
| 340 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 341 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 342 | headp = (i8 *) (&q->data[0] + q->elsize * q->head); |
| 343 | clib_memcpy (elem, headp, q->elsize); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 344 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 345 | q->head++; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 346 | /* $$$$ JFC shouldn't this be == 0? */ |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 347 | if (q->cursize == q->maxsize) |
| 348 | need_broadcast = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 349 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 350 | q->cursize--; |
| 351 | |
| 352 | if (q->head == q->maxsize) |
| 353 | q->head = 0; |
| 354 | |
| 355 | if (need_broadcast) |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 356 | svm_queue_send_signal (q, 0); |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 357 | |
| 358 | pthread_mutex_unlock (&q->mutex); |
| 359 | |
| 360 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 363 | int |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 364 | svm_queue_sub2 (svm_queue_t * q, u8 * elem) |
| 365 | { |
| 366 | int need_broadcast; |
| 367 | i8 *headp; |
| 368 | |
| 369 | pthread_mutex_lock (&q->mutex); |
| 370 | if (q->cursize == 0) |
| 371 | { |
| 372 | pthread_mutex_unlock (&q->mutex); |
| 373 | return -1; |
| 374 | } |
| 375 | |
| 376 | headp = (i8 *) (&q->data[0] + q->elsize * q->head); |
| 377 | clib_memcpy (elem, headp, q->elsize); |
| 378 | |
| 379 | q->head++; |
| 380 | need_broadcast = (q->cursize == q->maxsize / 2); |
| 381 | q->cursize--; |
| 382 | |
| 383 | if (PREDICT_FALSE (q->head == q->maxsize)) |
| 384 | q->head = 0; |
| 385 | pthread_mutex_unlock (&q->mutex); |
| 386 | |
| 387 | if (need_broadcast) |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 388 | svm_queue_send_signal (q, 0); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 389 | |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | int |
| 394 | svm_queue_sub_raw (svm_queue_t * q, u8 * elem) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 395 | { |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 396 | i8 *headp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 397 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 398 | if (PREDICT_FALSE (q->cursize == 0)) |
| 399 | { |
| 400 | while (q->cursize == 0) |
| 401 | ; |
| 402 | } |
| 403 | |
| 404 | headp = (i8 *) (&q->data[0] + q->elsize * q->head); |
| 405 | clib_memcpy (elem, headp, q->elsize); |
| 406 | |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 407 | q->head = (q->head + 1) % q->maxsize; |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 408 | q->cursize--; |
| 409 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 410 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 411 | } |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 412 | |
Florin Coras | c470e22 | 2018-08-01 07:53:18 -0700 | [diff] [blame] | 413 | void |
| 414 | svm_queue_set_producer_event_fd (svm_queue_t * q, int fd) |
| 415 | { |
| 416 | q->producer_evtfd = fd; |
| 417 | } |
| 418 | |
| 419 | void |
| 420 | svm_queue_set_consumer_event_fd (svm_queue_t * q, int fd) |
| 421 | { |
| 422 | q->consumer_evtfd = fd; |
| 423 | } |
| 424 | |
Dave Barach | 371e4e1 | 2016-07-08 09:38:52 -0400 | [diff] [blame] | 425 | /* |
| 426 | * fd.io coding-style-patch-verification: ON |
| 427 | * |
| 428 | * Local Variables: |
| 429 | * eval: (c-set-style "gnu") |
| 430 | * End: |
| 431 | */ |