2 * Copyright (c) 2016, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
42 static int tr_mq_msg_destructor(void *object)
44 TR_MQ_MSG *msg=talloc_get_type_abort(object, TR_MQ_MSG);
45 if ( (msg->p!=NULL) && (msg->p_free!=NULL))
50 TR_MQ_MSG *tr_mq_msg_new(TALLOC_CTX *mem_ctx, const char *message, TR_MQ_PRIORITY prio)
52 TR_MQ_MSG *msg=talloc(mem_ctx, TR_MQ_MSG);
56 msg->message=talloc_strdup(msg, message);
57 if (msg->message==NULL) {
62 talloc_set_destructor((void *)msg, tr_mq_msg_destructor);
67 void tr_mq_msg_free(TR_MQ_MSG *msg)
73 TR_MQ_PRIORITY tr_mq_msg_get_prio(TR_MQ_MSG *msg)
78 const char *tr_mq_msg_get_message(TR_MQ_MSG *msg)
83 void *tr_mq_msg_get_payload(TR_MQ_MSG *msg)
88 /* call with a pointer to the payload and a function to free it later */
89 void tr_mq_msg_set_payload(TR_MQ_MSG *msg, void *p, void (*p_free)(void *))
96 static TR_MQ_MSG *tr_mq_msg_get_next(TR_MQ_MSG *msg)
101 static void tr_mq_msg_set_next(TR_MQ_MSG *msg, TR_MQ_MSG *next)
107 TR_MQ *tr_mq_new(TALLOC_CTX *mem_ctx)
109 TR_MQ *mq=talloc(mem_ctx, TR_MQ);
111 pthread_mutex_init(&(mq->mutex), 0);
114 mq->last_hi_prio=NULL;
119 void tr_mq_free(TR_MQ *mq)
122 tr_mq_lock(mq); /* don't pull the rug out from under someone */
127 int tr_mq_lock(TR_MQ *mq)
129 return pthread_mutex_lock(&(mq->mutex));
132 int tr_mq_unlock(TR_MQ *mq)
134 return pthread_mutex_unlock(&(mq->mutex));
137 static TR_MQ_MSG *tr_mq_get_head(TR_MQ *mq)
142 static void tr_mq_set_head(TR_MQ *mq, TR_MQ_MSG *msg)
147 static TR_MQ_MSG *tr_mq_get_tail(TR_MQ *mq)
152 static void tr_mq_set_tail(TR_MQ *mq, TR_MQ_MSG *msg)
157 void tr_mq_set_notify_cb(TR_MQ *mq, TR_MQ_NOTIFY_FN cb, void *arg)
160 mq->notify_cb_arg=arg;
163 void tr_mq_clear(TR_MQ *mq)
169 m=tr_mq_get_head(mq);
171 n=tr_mq_msg_get_next(m);
175 tr_mq_set_head(mq, NULL);
176 tr_mq_set_tail(mq, NULL);
180 static int tr_mq_empty(TR_MQ *mq)
182 return tr_mq_get_head(mq)==NULL;
185 /* puts msg in mq's talloc context */
186 static void tr_mq_append(TR_MQ *mq, TR_MQ_MSG *msg)
188 if (tr_mq_get_head(mq)==NULL) {
189 tr_mq_set_head(mq, msg);
190 tr_mq_set_tail(mq, msg);
192 tr_mq_msg_set_next(tr_mq_get_tail(mq), msg); /* add to list */
193 tr_mq_set_tail(mq, msg); /* update tail of list */
195 talloc_steal(mq, msg);
198 static void tr_mq_append_high_prio(TR_MQ *mq, TR_MQ_MSG *new)
200 if (tr_mq_get_head(mq)==NULL) {
201 tr_mq_set_head(mq, new);
202 tr_mq_set_tail(mq, new);
203 } else if (mq->last_hi_prio==NULL) {
204 tr_mq_msg_set_next(new, tr_mq_get_head(mq)); /* add to front of list */
205 tr_mq_set_head(mq, new); /* update head of list */
207 tr_mq_msg_set_next(new, tr_mq_msg_get_next(mq->last_hi_prio));
208 tr_mq_msg_set_next(mq->last_hi_prio, new); /* add to end of hi prio msgs */
210 mq->last_hi_prio=new; /* in any case, this is now the last high priority msg */
211 talloc_steal(mq,new);
214 #define DEBUG_TR_MQ 0
216 static void tr_mq_print(TR_MQ *mq)
218 TR_MQ_MSG *m=mq->head;
221 tr_debug("tr_mq_print: mq contents:");
224 tr_debug("tr_mq_print: Entry %02d: %-15s (prio %d)",
225 ii, tr_mq_msg_get_message(m), tr_mq_msg_get_prio(m));
226 m=tr_mq_msg_get_next(m);
230 void tr_mq_add(TR_MQ *mq, TR_MQ_MSG *msg)
233 TR_MQ_NOTIFY_FN notify_cb=NULL;
234 void *notify_cb_arg=NULL;
238 was_empty=tr_mq_empty(mq);
239 switch (tr_mq_msg_get_prio(msg)) {
240 case TR_MQ_PRIO_HIGH:
241 tr_mq_append_high_prio(mq, msg);
244 tr_mq_append(mq, msg);
247 /* before releasing the mutex, get notify_cb data out of mq */
248 notify_cb=mq->notify_cb;
249 notify_cb_arg=mq->notify_cb_arg;
257 /* see if we need to tell someone we became non-empty */
258 if (was_empty && (notify_cb!=NULL))
259 notify_cb(mq, notify_cb_arg);
262 /* caller must free msg via tr_mq_msg_free */
263 TR_MQ_MSG *tr_mq_pop(TR_MQ *mq)
265 TR_MQ_MSG *popped=NULL;
268 if (tr_mq_get_head(mq)!=NULL) {
269 popped=tr_mq_get_head(mq);
270 tr_mq_set_head(mq, tr_mq_msg_get_next(popped)); /* popped is the old head */
272 if (popped==mq->last_hi_prio)
273 mq->last_hi_prio=NULL;
275 if (tr_mq_get_head(mq)==NULL)
276 tr_mq_set_tail(mq, NULL); /* just popped the last element */
280 tr_mq_msg_set_next(popped, NULL); /* disconnect from list */