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