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
36 * coid = consumer coid, from ChannelCreate
38 * pulse_code = pulse code consumer expects
39 * pulse_value = pulse value consumer expects
40 * consumer_prio = consumer's priority, so pulses won't change
41 * the consumer's priority.
43 * The idea is to call this function in the queue consumer,
44 * and e-mail the queue pointer to the producer(s).
46 * The spp process / main thread allocates one of these
47 * at startup; its main input queue. The spp main input queue
48 * has a pointer to it in the shared memory segment header.
50 * You probably want to be on an svm data heap before calling this
53 unix_shared_memory_queue_t *
54 unix_shared_memory_queue_init(int nels,
57 int signal_when_queue_non_empty)
59 unix_shared_memory_queue_t *q;
60 pthread_mutexattr_t attr;
61 pthread_condattr_t cattr;
63 q = clib_mem_alloc_aligned(sizeof(unix_shared_memory_queue_t)
64 + nels*elsize, CLIB_CACHE_LINE_BYTES);
65 memset(q, 0, sizeof (*q));
69 q->consumer_pid = consumer_pid;
70 q->signal_when_queue_non_empty = signal_when_queue_non_empty;
72 memset(&attr,0,sizeof(attr));
73 memset(&cattr,0,sizeof(attr));
75 if (pthread_mutexattr_init(&attr))
76 clib_unix_warning("mutexattr_init");
77 if (pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED))
78 clib_unix_warning("pthread_mutexattr_setpshared");
79 if (pthread_mutex_init(&q->mutex, &attr))
80 clib_unix_warning("mutex_init");
81 if (pthread_mutexattr_destroy(&attr))
82 clib_unix_warning("mutexattr_destroy");
83 if (pthread_condattr_init(&cattr))
84 clib_unix_warning("condattr_init");
85 /* prints funny-looking messages in the Linux target */
86 if (pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED))
87 clib_unix_warning("condattr_setpshared");
88 if (pthread_cond_init(&q->condvar, &cattr))
89 clib_unix_warning("cond_init1");
90 if(pthread_condattr_destroy(&cattr))
91 clib_unix_warning("cond_init2");
97 * unix_shared_memory_queue_free
99 void unix_shared_memory_queue_free(unix_shared_memory_queue_t *q)
101 (void) pthread_mutex_destroy(&q->mutex);
102 (void) pthread_cond_destroy(&q->condvar);
106 void unix_shared_memory_queue_lock (unix_shared_memory_queue_t *q)
108 pthread_mutex_lock(&q->mutex);
111 void unix_shared_memory_queue_unlock (unix_shared_memory_queue_t *q)
113 pthread_mutex_unlock(&q->mutex);
116 int unix_shared_memory_queue_is_full (unix_shared_memory_queue_t *q)
118 return q->cursize == q->maxsize;
122 * unix_shared_memory_queue_add_nolock
124 int 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)) {
131 while(q->cursize == q->maxsize) {
132 (void) pthread_cond_wait(&q->condvar, &q->mutex);
136 tailp = (i8 *)(&q->data[0] + q->elsize*q->tail);
137 memcpy(tailp, elem, q->elsize);
142 need_broadcast = (q->cursize == 1);
144 if (q->tail == q->maxsize)
147 if (need_broadcast) {
148 (void) pthread_cond_broadcast(&q->condvar);
149 if (q->signal_when_queue_non_empty)
150 kill (q->consumer_pid, q->signal_when_queue_non_empty);
155 int unix_shared_memory_queue_add_raw (unix_shared_memory_queue_t *q,
160 if (PREDICT_FALSE(q->cursize == q->maxsize)) {
161 while(q->cursize == q->maxsize)
165 tailp = (i8 *)(&q->data[0] + q->elsize*q->tail);
166 memcpy(tailp, elem, q->elsize);
171 if (q->tail == q->maxsize)
178 * unix_shared_memory_queue_add
180 int unix_shared_memory_queue_add (unix_shared_memory_queue_t *q,
181 u8 *elem, int nowait)
184 int need_broadcast=0;
187 /* zero on success */
188 if (pthread_mutex_trylock (&q->mutex)) {
192 pthread_mutex_lock(&q->mutex);
194 if (PREDICT_FALSE(q->cursize == q->maxsize)) {
196 pthread_mutex_unlock(&q->mutex);
199 while(q->cursize == q->maxsize) {
200 (void) pthread_cond_wait(&q->condvar, &q->mutex);
204 tailp = (i8 *)(&q->data[0] + q->elsize*q->tail);
205 memcpy(tailp, elem, q->elsize);
210 need_broadcast = (q->cursize == 1);
212 if (q->tail == q->maxsize)
215 if (need_broadcast) {
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);
226 * unix_shared_memory_queue_sub
228 int unix_shared_memory_queue_sub(unix_shared_memory_queue_t *q,
229 u8 *elem, int nowait)
232 int need_broadcast=0;
235 /* zero on success */
236 if (pthread_mutex_trylock (&q->mutex)) {
240 pthread_mutex_lock(&q->mutex);
242 if (PREDICT_FALSE(q->cursize == 0)) {
244 pthread_mutex_unlock(&q->mutex);
247 while (q->cursize == 0) {
248 (void) pthread_cond_wait(&q->condvar, &q->mutex);
252 headp = (i8 *)(&q->data[0] + q->elsize*q->head);
253 memcpy(elem, headp, q->elsize);
256 if (q->cursize == q->maxsize)
261 if(q->head == q->maxsize)
265 (void) pthread_cond_broadcast(&q->condvar);
267 pthread_mutex_unlock(&q->mutex);
272 int unix_shared_memory_queue_sub_raw (unix_shared_memory_queue_t *q,
277 if (PREDICT_FALSE(q->cursize == 0)) {
278 while (q->cursize == 0)
282 headp = (i8 *)(&q->data[0] + q->elsize*q->head);
283 memcpy(elem, headp, q->elsize);
288 if(q->head == q->maxsize)