2 *------------------------------------------------------------------
3 * unix_shared_memory_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 *------------------------------------------------------------------
24 #include <vppinfra/mem.h>
25 #include <vppinfra/format.h>
26 #include <vppinfra/cache.h>
27 #include <vlibmemory/unix_shared_memory_queue.h>
31 * unix_shared_memory_queue_init
33 * nels = number of elements on the queue
34 * elsize = element size, presumably 4 and cacheline-size will
38 * The idea is to call this function in the queue consumer,
39 * and e-mail the queue pointer to the producer(s).
41 * The vpp process / main thread allocates one of these
42 * at startup; its main input queue. The vpp main input queue
43 * has a pointer to it in the shared memory segment header.
45 * You probably want to be on an svm data heap before calling this
48 unix_shared_memory_queue_t *
49 unix_shared_memory_queue_init (int nels,
52 int signal_when_queue_non_empty)
54 unix_shared_memory_queue_t *q;
55 pthread_mutexattr_t attr;
56 pthread_condattr_t cattr;
58 q = clib_mem_alloc_aligned (sizeof (unix_shared_memory_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");
92 * unix_shared_memory_queue_free
95 unix_shared_memory_queue_free (unix_shared_memory_queue_t * q)
97 (void) pthread_mutex_destroy (&q->mutex);
98 (void) pthread_cond_destroy (&q->condvar);
103 unix_shared_memory_queue_lock (unix_shared_memory_queue_t * q)
105 pthread_mutex_lock (&q->mutex);
109 unix_shared_memory_queue_unlock (unix_shared_memory_queue_t * q)
111 pthread_mutex_unlock (&q->mutex);
115 unix_shared_memory_queue_is_full (unix_shared_memory_queue_t * q)
117 return q->cursize == q->maxsize;
121 * unix_shared_memory_queue_add_nolock
124 unix_shared_memory_queue_add_nolock (unix_shared_memory_queue_t * q,
128 int need_broadcast = 0;
130 if (PREDICT_FALSE (q->cursize == q->maxsize))
132 while (q->cursize == q->maxsize)
134 (void) pthread_cond_wait (&q->condvar, &q->mutex);
138 tailp = (i8 *) (&q->data[0] + q->elsize * q->tail);
139 clib_memcpy (tailp, elem, q->elsize);
144 need_broadcast = (q->cursize == 1);
146 if (q->tail == q->maxsize)
151 (void) pthread_cond_broadcast (&q->condvar);
152 if (q->signal_when_queue_non_empty)
153 kill (q->consumer_pid, q->signal_when_queue_non_empty);
159 unix_shared_memory_queue_add_raw (unix_shared_memory_queue_t * q, u8 * elem)
163 if (PREDICT_FALSE (q->cursize == q->maxsize))
165 while (q->cursize == q->maxsize)
169 tailp = (i8 *) (&q->data[0] + q->elsize * q->tail);
170 clib_memcpy (tailp, elem, q->elsize);
175 if (q->tail == q->maxsize)
182 * unix_shared_memory_queue_add
185 unix_shared_memory_queue_add (unix_shared_memory_queue_t * q,
186 u8 * elem, int nowait)
189 int need_broadcast = 0;
193 /* zero on success */
194 if (pthread_mutex_trylock (&q->mutex))
200 pthread_mutex_lock (&q->mutex);
202 if (PREDICT_FALSE (q->cursize == q->maxsize))
206 pthread_mutex_unlock (&q->mutex);
209 while (q->cursize == q->maxsize)
211 (void) pthread_cond_wait (&q->condvar, &q->mutex);
215 tailp = (i8 *) (&q->data[0] + q->elsize * q->tail);
216 clib_memcpy (tailp, elem, q->elsize);
221 need_broadcast = (q->cursize == 1);
223 if (q->tail == q->maxsize)
228 (void) pthread_cond_broadcast (&q->condvar);
229 if (q->signal_when_queue_non_empty)
230 kill (q->consumer_pid, q->signal_when_queue_non_empty);
232 pthread_mutex_unlock (&q->mutex);
238 * unix_shared_memory_queue_add2
241 unix_shared_memory_queue_add2 (unix_shared_memory_queue_t * q, u8 * elem,
242 u8 * elem2, int nowait)
245 int need_broadcast = 0;
249 /* zero on success */
250 if (pthread_mutex_trylock (&q->mutex))
256 pthread_mutex_lock (&q->mutex);
258 if (PREDICT_FALSE (q->cursize + 1 == q->maxsize))
262 pthread_mutex_unlock (&q->mutex);
265 while (q->cursize + 1 == q->maxsize)
267 (void) pthread_cond_wait (&q->condvar, &q->mutex);
271 tailp = (i8 *) (&q->data[0] + q->elsize * q->tail);
272 clib_memcpy (tailp, elem, q->elsize);
277 if (q->tail == q->maxsize)
280 need_broadcast = (q->cursize == 1);
282 tailp = (i8 *) (&q->data[0] + q->elsize * q->tail);
283 clib_memcpy (tailp, elem2, q->elsize);
288 if (q->tail == q->maxsize)
293 (void) pthread_cond_broadcast (&q->condvar);
294 if (q->signal_when_queue_non_empty)
295 kill (q->consumer_pid, q->signal_when_queue_non_empty);
297 pthread_mutex_unlock (&q->mutex);
303 * unix_shared_memory_queue_sub
306 unix_shared_memory_queue_sub (unix_shared_memory_queue_t * q,
307 u8 * elem, int nowait)
310 int need_broadcast = 0;
314 /* zero on success */
315 if (pthread_mutex_trylock (&q->mutex))
321 pthread_mutex_lock (&q->mutex);
323 if (PREDICT_FALSE (q->cursize == 0))
327 pthread_mutex_unlock (&q->mutex);
330 while (q->cursize == 0)
332 (void) pthread_cond_wait (&q->condvar, &q->mutex);
336 headp = (i8 *) (&q->data[0] + q->elsize * q->head);
337 clib_memcpy (elem, headp, q->elsize);
340 /* $$$$ JFC shouldn't this be == 0? */
341 if (q->cursize == q->maxsize)
346 if (q->head == q->maxsize)
350 (void) pthread_cond_broadcast (&q->condvar);
352 pthread_mutex_unlock (&q->mutex);
358 unix_shared_memory_queue_sub_raw (unix_shared_memory_queue_t * q, u8 * elem)
362 if (PREDICT_FALSE (q->cursize == 0))
364 while (q->cursize == 0)
368 headp = (i8 *) (&q->data[0] + q->elsize * q->head);
369 clib_memcpy (elem, headp, q->elsize);
374 if (q->head == q->maxsize)
380 * fd.io coding-style-patch-verification: ON
383 * eval: (c-set-style "gnu")