2 *------------------------------------------------------------------
3 * svm_queue.c - unidirectional shared-memory queues
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:
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>
35 * nels = number of elements on the queue
36 * elsize = element size, presumably 4 and cacheline-size will
40 * The idea is to call this function in the queue consumer,
41 * and e-mail the queue pointer to the producer(s).
43 * The vpp process / main thread allocates one of these
44 * at startup; its main input queue. The vpp main input queue
45 * has a pointer to it in the shared memory segment header.
47 * You probably want to be on an svm data heap before calling this
51 svm_queue_init (int nels,
52 int elsize, int consumer_pid, int signal_when_queue_non_empty)
55 pthread_mutexattr_t attr;
56 pthread_condattr_t cattr;
58 q = clib_mem_alloc_aligned (sizeof (svm_queue_t)
59 + nels * elsize, CLIB_CACHE_LINE_BYTES);
60 memset (q, 0, sizeof (*q));
64 q->consumer_pid = consumer_pid;
65 q->signal_when_queue_non_empty = signal_when_queue_non_empty;
67 memset (&attr, 0, sizeof (attr));
68 memset (&cattr, 0, sizeof (cattr));
70 if (pthread_mutexattr_init (&attr))
71 clib_unix_warning ("mutexattr_init");
72 if (pthread_mutexattr_setpshared (&attr, PTHREAD_PROCESS_SHARED))
73 clib_unix_warning ("pthread_mutexattr_setpshared");
74 if (pthread_mutex_init (&q->mutex, &attr))
75 clib_unix_warning ("mutex_init");
76 if (pthread_mutexattr_destroy (&attr))
77 clib_unix_warning ("mutexattr_destroy");
78 if (pthread_condattr_init (&cattr))
79 clib_unix_warning ("condattr_init");
80 /* prints funny-looking messages in the Linux target */
81 if (pthread_condattr_setpshared (&cattr, PTHREAD_PROCESS_SHARED))
82 clib_unix_warning ("condattr_setpshared");
83 if (pthread_cond_init (&q->condvar, &cattr))
84 clib_unix_warning ("cond_init1");
85 if (pthread_condattr_destroy (&cattr))
86 clib_unix_warning ("cond_init2");
95 svm_queue_free (svm_queue_t * q)
97 (void) pthread_mutex_destroy (&q->mutex);
98 (void) pthread_cond_destroy (&q->condvar);
103 svm_queue_lock (svm_queue_t * q)
105 pthread_mutex_lock (&q->mutex);
109 svm_queue_unlock (svm_queue_t * q)
111 pthread_mutex_unlock (&q->mutex);
115 svm_queue_is_full (svm_queue_t * q)
117 return q->cursize == q->maxsize;
121 * svm_queue_add_nolock
124 svm_queue_add_nolock (svm_queue_t * q, u8 * elem)
127 int need_broadcast = 0;
129 if (PREDICT_FALSE (q->cursize == q->maxsize))
131 while (q->cursize == q->maxsize)
133 (void) pthread_cond_wait (&q->condvar, &q->mutex);
137 tailp = (i8 *) (&q->data[0] + q->elsize * q->tail);
138 clib_memcpy (tailp, elem, q->elsize);
143 need_broadcast = (q->cursize == 1);
145 if (q->tail == q->maxsize)
150 (void) pthread_cond_broadcast (&q->condvar);
151 if (q->signal_when_queue_non_empty)
152 kill (q->consumer_pid, q->signal_when_queue_non_empty);
158 svm_queue_add_raw (svm_queue_t * q, u8 * elem)
162 tailp = (i8 *) (&q->data[0] + q->elsize * q->tail);
163 clib_memcpy (tailp, elem, q->elsize);
165 q->tail = (q->tail + 1) % q->maxsize;
174 svm_queue_add (svm_queue_t * q, u8 * elem, int nowait)
177 int need_broadcast = 0;
181 /* zero on success */
182 if (pthread_mutex_trylock (&q->mutex))
188 pthread_mutex_lock (&q->mutex);
190 if (PREDICT_FALSE (q->cursize == q->maxsize))
194 pthread_mutex_unlock (&q->mutex);
197 while (q->cursize == q->maxsize)
199 (void) pthread_cond_wait (&q->condvar, &q->mutex);
203 tailp = (i8 *) (&q->data[0] + q->elsize * q->tail);
204 clib_memcpy (tailp, elem, q->elsize);
209 need_broadcast = (q->cursize == 1);
211 if (q->tail == q->maxsize)
216 (void) pthread_cond_broadcast (&q->condvar);
217 if (q->signal_when_queue_non_empty)
218 kill (q->consumer_pid, q->signal_when_queue_non_empty);
220 pthread_mutex_unlock (&q->mutex);
229 svm_queue_add2 (svm_queue_t * q, u8 * elem, u8 * elem2, int nowait)
232 int need_broadcast = 0;
236 /* zero on success */
237 if (pthread_mutex_trylock (&q->mutex))
243 pthread_mutex_lock (&q->mutex);
245 if (PREDICT_FALSE (q->cursize + 1 == q->maxsize))
249 pthread_mutex_unlock (&q->mutex);
252 while (q->cursize + 1 == q->maxsize)
254 (void) pthread_cond_wait (&q->condvar, &q->mutex);
258 tailp = (i8 *) (&q->data[0] + q->elsize * q->tail);
259 clib_memcpy (tailp, elem, q->elsize);
264 if (q->tail == q->maxsize)
267 need_broadcast = (q->cursize == 1);
269 tailp = (i8 *) (&q->data[0] + q->elsize * q->tail);
270 clib_memcpy (tailp, elem2, q->elsize);
275 if (q->tail == q->maxsize)
280 (void) pthread_cond_broadcast (&q->condvar);
281 if (q->signal_when_queue_non_empty)
282 kill (q->consumer_pid, q->signal_when_queue_non_empty);
284 pthread_mutex_unlock (&q->mutex);
293 svm_queue_sub (svm_queue_t * q, u8 * elem, svm_q_conditional_wait_t cond,
297 int need_broadcast = 0;
300 if (cond == SVM_Q_NOWAIT)
302 /* zero on success */
303 if (pthread_mutex_trylock (&q->mutex))
309 pthread_mutex_lock (&q->mutex);
311 if (PREDICT_FALSE (q->cursize == 0))
313 if (cond == SVM_Q_NOWAIT)
315 pthread_mutex_unlock (&q->mutex);
318 else if (cond == SVM_Q_TIMEDWAIT)
321 ts.tv_sec = unix_time_now () + time;
323 while (q->cursize == 0 && rc == 0)
325 rc = pthread_cond_timedwait (&q->condvar, &q->mutex, &ts);
329 pthread_mutex_unlock (&q->mutex);
335 while (q->cursize == 0)
337 (void) pthread_cond_wait (&q->condvar, &q->mutex);
342 headp = (i8 *) (&q->data[0] + q->elsize * q->head);
343 clib_memcpy (elem, headp, q->elsize);
346 /* $$$$ JFC shouldn't this be == 0? */
347 if (q->cursize == q->maxsize)
352 if (q->head == q->maxsize)
356 (void) pthread_cond_broadcast (&q->condvar);
358 pthread_mutex_unlock (&q->mutex);
364 svm_queue_sub2 (svm_queue_t * q, u8 * elem)
369 pthread_mutex_lock (&q->mutex);
372 pthread_mutex_unlock (&q->mutex);
376 headp = (i8 *) (&q->data[0] + q->elsize * q->head);
377 clib_memcpy (elem, headp, q->elsize);
380 need_broadcast = (q->cursize == q->maxsize / 2);
383 if (PREDICT_FALSE (q->head == q->maxsize))
385 pthread_mutex_unlock (&q->mutex);
388 (void) pthread_cond_broadcast (&q->condvar);
394 svm_queue_sub_raw (svm_queue_t * q, u8 * elem)
398 if (PREDICT_FALSE (q->cursize == 0))
400 while (q->cursize == 0)
404 headp = (i8 *) (&q->data[0] + q->elsize * q->head);
405 clib_memcpy (elem, headp, q->elsize);
407 q->head = (q->head + 1) % q->maxsize;
414 * fd.io coding-style-patch-verification: ON
417 * eval: (c-set-style "gnu")