2 *------------------------------------------------------------------
3 * svm_queue.c - unidirectional shared-memory queues
5 * Copyright (c) 2009-2019 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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
25 #include <vppinfra/mem.h>
26 #include <vppinfra/format.h>
27 #include <vppinfra/cache.h>
28 #include <svm/queue.h>
29 #include <vppinfra/time.h>
30 #include <vppinfra/lock.h>
33 svm_queue_init (void *base, int nels, int elsize)
36 pthread_mutexattr_t attr;
37 pthread_condattr_t cattr;
39 q = (svm_queue_t *) base;
40 clib_memset (q, 0, sizeof (*q));
44 q->producer_evtfd = -1;
45 q->consumer_evtfd = -1;
47 clib_memset (&attr, 0, sizeof (attr));
48 clib_memset (&cattr, 0, sizeof (cattr));
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");
72 svm_queue_alloc_and_init (int nels, int elsize, int consumer_pid)
76 q = clib_mem_alloc_aligned (sizeof (svm_queue_t)
77 + nels * elsize, CLIB_CACHE_LINE_BYTES);
78 clib_memset (q, 0, sizeof (*q));
79 q = svm_queue_init (q, nels, elsize);
80 q->consumer_pid = consumer_pid;
89 svm_queue_free (svm_queue_t * q)
91 (void) pthread_mutex_destroy (&q->mutex);
92 (void) pthread_cond_destroy (&q->condvar);
97 svm_queue_lock (svm_queue_t * q)
99 pthread_mutex_lock (&q->mutex);
103 svm_queue_unlock (svm_queue_t * q)
105 pthread_mutex_unlock (&q->mutex);
109 svm_queue_is_full (svm_queue_t * q)
111 return q->cursize == q->maxsize;
115 svm_queue_send_signal (svm_queue_t * q, u8 is_prod)
117 if (q->producer_evtfd == -1)
119 (void) pthread_cond_broadcast (&q->condvar);
123 int __clib_unused rv, fd;
125 ASSERT (q->consumer_evtfd > 0 && q->producer_evtfd > 0);
126 fd = is_prod ? q->producer_evtfd : q->consumer_evtfd;
127 rv = write (fd, &data, sizeof (data));
132 svm_queue_wait_inline (svm_queue_t * q)
134 if (q->producer_evtfd == -1)
136 pthread_cond_wait (&q->condvar, &q->mutex);
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 svm_queue_unlock (q);
144 while (q->cursize == cursize)
151 svm_queue_wait (svm_queue_t * q)
153 svm_queue_wait_inline (q);
157 svm_queue_timedwait_inline (svm_queue_t * q, double timeout)
160 ts.tv_sec = unix_time_now () + (u32) timeout;
161 ts.tv_nsec = (timeout - (u32) timeout) * 1e9;
163 if (q->producer_evtfd == -1)
165 return pthread_cond_timedwait (&q->condvar, &q->mutex, &ts);
169 double max_time = unix_time_now () + timeout;
170 u32 cursize = q->cursize;
173 svm_queue_unlock (q);
174 while (q->cursize == cursize && unix_time_now () < max_time)
176 rv = unix_time_now () < max_time ? 0 : ETIMEDOUT;
183 svm_queue_timedwait (svm_queue_t * q, double timeout)
185 return svm_queue_timedwait_inline (q, timeout);
189 * svm_queue_add_nolock
192 svm_queue_add_nolock (svm_queue_t * q, u8 * elem)
195 int need_broadcast = 0;
197 if (PREDICT_FALSE (q->cursize == q->maxsize))
199 while (q->cursize == q->maxsize)
200 svm_queue_wait_inline (q);
203 tailp = (i8 *) (&q->data[0] + q->elsize * q->tail);
204 clib_memcpy_fast (tailp, elem, q->elsize);
209 need_broadcast = (q->cursize == 1);
211 if (q->tail == q->maxsize)
215 svm_queue_send_signal (q, 1);
220 svm_queue_add_raw (svm_queue_t * q, u8 * elem)
224 tailp = (i8 *) (&q->data[0] + q->elsize * q->tail);
225 clib_memcpy_fast (tailp, elem, q->elsize);
227 q->tail = (q->tail + 1) % q->maxsize;
231 svm_queue_send_signal (q, 1);
239 svm_queue_add (svm_queue_t * q, u8 * elem, int nowait)
242 int need_broadcast = 0;
246 /* zero on success */
247 if (pthread_mutex_trylock (&q->mutex))
255 if (PREDICT_FALSE (q->cursize == q->maxsize))
259 svm_queue_unlock (q);
262 while (q->cursize == q->maxsize)
263 svm_queue_wait_inline (q);
266 tailp = (i8 *) (&q->data[0] + q->elsize * q->tail);
267 clib_memcpy_fast (tailp, elem, q->elsize);
272 need_broadcast = (q->cursize == 1);
274 if (q->tail == q->maxsize)
278 svm_queue_send_signal (q, 1);
280 svm_queue_unlock (q);
289 svm_queue_add2 (svm_queue_t * q, u8 * elem, u8 * elem2, int nowait)
292 int need_broadcast = 0;
296 /* zero on success */
297 if (pthread_mutex_trylock (&q->mutex))
305 if (PREDICT_FALSE (q->cursize + 1 == q->maxsize))
309 svm_queue_unlock (q);
312 while (q->cursize + 1 == q->maxsize)
313 svm_queue_wait_inline (q);
316 tailp = (i8 *) (&q->data[0] + q->elsize * q->tail);
317 clib_memcpy_fast (tailp, elem, q->elsize);
322 if (q->tail == q->maxsize)
325 need_broadcast = (q->cursize == 1);
327 tailp = (i8 *) (&q->data[0] + q->elsize * q->tail);
328 clib_memcpy_fast (tailp, elem2, q->elsize);
333 if (q->tail == q->maxsize)
337 svm_queue_send_signal (q, 1);
339 svm_queue_unlock (q);
348 svm_queue_sub (svm_queue_t * q, u8 * elem, svm_q_conditional_wait_t cond,
352 int need_broadcast = 0;
355 if (cond == SVM_Q_NOWAIT)
357 /* zero on success */
358 if (pthread_mutex_trylock (&q->mutex))
366 if (PREDICT_FALSE (q->cursize == 0))
368 if (cond == SVM_Q_NOWAIT)
370 svm_queue_unlock (q);
373 else if (cond == SVM_Q_TIMEDWAIT)
375 while (q->cursize == 0 && rc == 0)
376 rc = svm_queue_timedwait_inline (q, time);
380 svm_queue_unlock (q);
386 while (q->cursize == 0)
387 svm_queue_wait_inline (q);
391 headp = (i8 *) (&q->data[0] + q->elsize * q->head);
392 clib_memcpy_fast (elem, headp, q->elsize);
395 /* $$$$ JFC shouldn't this be == 0? */
396 if (q->cursize == q->maxsize)
401 if (q->head == q->maxsize)
405 svm_queue_send_signal (q, 0);
407 svm_queue_unlock (q);
413 svm_queue_sub2 (svm_queue_t * q, u8 * elem)
421 svm_queue_unlock (q);
425 headp = (i8 *) (&q->data[0] + q->elsize * q->head);
426 clib_memcpy_fast (elem, headp, q->elsize);
429 need_broadcast = (q->cursize == q->maxsize / 2);
432 if (PREDICT_FALSE (q->head == q->maxsize))
434 svm_queue_unlock (q);
437 svm_queue_send_signal (q, 0);
443 svm_queue_sub_raw (svm_queue_t * q, u8 * elem)
447 if (PREDICT_FALSE (q->cursize == 0))
449 while (q->cursize == 0)
453 headp = (i8 *) (&q->data[0] + q->elsize * q->head);
454 clib_memcpy_fast (elem, headp, q->elsize);
456 q->head = (q->head + 1) % q->maxsize;
463 svm_queue_set_producer_event_fd (svm_queue_t * q, int fd)
465 q->producer_evtfd = fd;
469 svm_queue_set_consumer_event_fd (svm_queue_t * q, int fd)
471 q->consumer_evtfd = fd;
475 * fd.io coding-style-patch-verification: ON
478 * eval: (c-set-style "gnu")