1 /**************************************************************************/
2 /* ---------------------------------------------------------------------- */
3 /* Copyright (C) 2006 - 2008 FRESCOR consortium partners: */
5 /* Universidad de Cantabria, SPAIN */
6 /* University of York, UK */
7 /* Scuola Superiore Sant'Anna, ITALY */
8 /* Kaiserslautern University, GERMANY */
9 /* Univ. Politécnica Valencia, SPAIN */
10 /* Czech Technical University in Prague, CZECH REPUBLIC */
12 /* Thales Communication S.A. FRANCE */
13 /* Visual Tools S.A. SPAIN */
14 /* Rapita Systems Ltd UK */
17 /* See http://www.frescor.org for a link to partners' websites */
19 /* FRESCOR project (FP6/2005/IST/5-034026) is funded */
20 /* in part by the European Union Sixth Framework Programme */
21 /* The European Union is not liable of any use that may be */
22 /* made of this code. */
25 /* This file is part of FWP (Frescor WLAN Protocol) */
27 /* FWP is free software; you can redistribute it and/or modify it */
28 /* under terms of the GNU General Public License as published by the */
29 /* Free Software Foundation; either version 2, or (at your option) any */
30 /* later version. FWP is distributed in the hope that it will be */
31 /* useful, but WITHOUT ANY WARRANTY; without even the implied warranty */
32 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
33 /* General Public License for more details. You should have received a */
34 /* copy of the GNU General Public License along with FWP; see file */
35 /* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave, */
36 /* Cambridge, MA 02139, USA. */
38 /* As a special exception, including FWP header files in a file, */
39 /* instantiating FWP generics or templates, or linking other files */
40 /* with FWP objects to produce an executable application, does not */
41 /* by itself cause the resulting executable application to be covered */
42 /* by the GNU General Public License. This exception does not */
43 /* however invalidate any other reasons why the executable file might be */
44 /* covered by the GNU Public License. */
45 /**************************************************************************/
50 *Initialize message queue
51 *@param[in] msgq Message queue
53 void fwp_msgq_init(struct fwp_msgq *msgq)
58 pthread_mutex_init(&msgq->lock, NULL); /* fast mutex */
59 sem_init(&msgq->msg_sem, 0, 0);
63 * Enqueue message in message queue
65 * @param[in] msgq Message queue
66 *`@param[in] msgb Message buffer which stores a message
68 * Zero on success, -1 or error.
70 int fwp_msgq_enqueue(struct fwp_msgq *msgq, struct fwp_msgb *msgb)
72 /* acquire queue mutex */
73 pthread_mutex_lock(&msgq->lock);
75 if (!(msgq->nr_pending < FWP_MSGQ_SIZE)){
76 /*if (msgq->qr_policy == NEWCOMER) {*/
78 /*release queue mutex*/
79 pthread_mutex_unlock(&msgq->lock);
83 * if (msgq->qr_policy == OLDEST)
84 msgq->first = (msgq->first++) % FWP_MSGQ_SIZE;
89 /* depends on queuing policy specifies in endpoint */
90 msgq->queue[msgq->in] = msgb;
92 msgq->in = (++msgq->in) & (FWP_MSGQ_SIZE - 1);
94 sem_post(&msgq->msg_sem);
95 /* release queue mutex */
96 pthread_mutex_unlock(&msgq->lock);
102 * Dequeue message from message queue
104 * @param[in] msgq Message queue
106 * NULL if message queue in empty
107 * else returns pointer to message buffer(msgb)
109 struct fwp_msgb* fwp_msgq_dequeue(struct fwp_msgq *msgq)
111 struct fwp_msgb* msgb;
113 sem_wait(&msgq->msg_sem);
114 /* acquire queue mutex */
115 pthread_mutex_lock(&msgq->lock);
117 msgb = msgq->queue[msgq->out];
119 msgq->out = (++msgq->out) & (FWP_MSGQ_SIZE - 1);
121 /* release queue mutex */
122 pthread_mutex_unlock(&msgq->lock);
127 struct fwp_msgb* fwp_msgq_peek(struct fwp_msgq *msgq)
129 struct fwp_msgb* msgb;
131 pthread_mutex_lock(&msgq->lock);
133 if (msgq->nr_pending > 0)
134 msgb = msgq->queue[msgq->out];
137 pthread_mutex_unlock(&msgq->lock);
143 * Dequeue all messages from message queue
145 * @param[in] msgq Message queue
147 * NULL if message queue is empty
148 * else returns pointer to message buffer(msgb)
150 void fwp_msgq_dequeue_all(struct fwp_msgq *msgq)
152 struct fwp_msgb *msgb;
154 /* acquire queue mutex */
155 pthread_mutex_lock(&msgq->lock);
157 while (msgq->in != msgq->out){
158 msgb = msgq->queue[msgq->out];
160 msgq->out = (++msgq->out) & (FWP_MSGQ_SIZE - 1);
164 sem_init(&msgq->msg_sem, 0, 0);
165 /* release queue mutex */
166 pthread_mutex_unlock(&msgq->lock);