2 The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-)
3 Copyright (C) 2001,2002,2003 Aymeric MOIZARD jack@atosc.org
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <osip2/internal.h>
24 #include <osip2/osip_fifo.h>
27 /* always use this method to initiate osip_fifo_t.
30 osip_fifo_init (osip_fifo_t * ff)
33 ff->qislocked = osip_mutex_init ();
34 /*INIT SEMA TO BLOCK ON GET() WHEN QUEUE IS EMPTY */
35 ff->qisempty = osip_sem_init (0);
37 ff->queue = (osip_list_t *) osip_malloc (sizeof (osip_list_t));
38 osip_list_init (ff->queue);
44 osip_fifo_add (osip_fifo_t * ff, void *el)
47 osip_mutex_lock (ff->qislocked);
50 if (ff->etat != plein)
53 osip_list_add (ff->queue, el, -1); /* insert at end of queue */
57 OSIP_TRACE (osip_trace
58 (__FILE__, __LINE__, OSIP_WARNING, NULL,
59 "too much traffic in fifo.\n"));
61 osip_mutex_unlock (ff->qislocked);
63 return -1; /* stack is full */
65 /* if (ff->nb_elt >= MAX_LEN) */
66 if (osip_list_size (ff->queue) >= MAX_LEN)
72 osip_sem_post (ff->qisempty);
73 osip_mutex_unlock (ff->qislocked);
80 osip_fifo_insert (osip_fifo_t * ff, void *el)
83 osip_mutex_lock (ff->qislocked);
86 if (ff->etat != plein)
89 osip_list_add (ff->queue, el, 0); /* insert at end of queue */
93 OSIP_TRACE (osip_trace
94 (__FILE__, __LINE__, OSIP_WARNING, NULL,
95 "too much traffic in fifo.\n"));
97 osip_mutex_unlock (ff->qislocked);
99 return -1; /* stack is full */
101 /* if (ff->nb_elt >= MAX_LEN) */
102 if (osip_list_size (ff->queue) >= MAX_LEN)
108 osip_sem_post (ff->qisempty);
109 osip_mutex_unlock (ff->qislocked);
116 osip_fifo_size (osip_fifo_t * ff)
121 osip_mutex_lock (ff->qislocked);
124 i = osip_list_size (ff->queue);
126 osip_mutex_unlock (ff->qislocked);
134 osip_fifo_get (osip_fifo_t * ff)
137 int i = osip_sem_wait (ff->qisempty);
141 osip_mutex_lock (ff->qislocked);
143 if (ff->etat != vide)
145 el = osip_list_get (ff->queue, 0);
146 osip_list_remove (ff->queue, 0);
151 OSIP_TRACE (osip_trace
152 (__FILE__, __LINE__, OSIP_ERROR, NULL,
153 "no element in fifo.\n"));
154 osip_mutex_unlock (ff->qislocked);
155 return 0; /* pile vide */
157 /* if (ff->nb_elt <= 0) */
158 if (osip_list_size (ff->queue) <= 0)
163 osip_mutex_unlock (ff->qislocked);
170 osip_fifo_tryget (osip_fifo_t * ff)
175 if (0 != osip_sem_trywait (ff->qisempty))
176 { /* no elements... */
179 osip_mutex_lock (ff->qislocked);
181 if (ff->etat == vide)
185 if (ff->etat != vide)
187 el = osip_list_get (ff->queue, 0);
188 osip_list_remove (ff->queue, 0);
193 { /* this case MUST never happen... */
194 OSIP_TRACE (osip_trace
195 (__FILE__, __LINE__, OSIP_INFO4, NULL,
196 "no element in fifo.\n"));
197 osip_mutex_unlock (ff->qislocked);
202 /* if (ff->nb_elt <= 0) */
203 if (osip_list_size (ff->queue) <= 0)
209 osip_mutex_unlock (ff->qislocked);
215 osip_fifo_free (osip_fifo_t * ff)
220 osip_mutex_destroy (ff->qislocked);
221 /* seems that pthread_mutex_destroy does not free space by itself */
222 osip_sem_destroy (ff->qisempty);
224 osip_free (ff->queue);