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.
41 /* Note on mq priorities: High priority messages are guaranteed to be
42 * processed before any normal priority messages. Otherwise, messages
43 * will be processed in the order they are added to the queue. */
45 typedef enum tr_mq_priority {
50 /* msg for inter-thread messaging */
51 typedef struct tr_mq_msg TR_MQ_MSG;
56 void *p; /* payload */
57 void (*p_free)(void *); /* function to free payload */
60 /* message queue for inter-thread messaging */
62 typedef struct tr_mq TR_MQ;
63 typedef void (*TR_MQ_NOTIFY_FN)(TR_MQ *, void *);
65 pthread_mutex_t mutex;
68 TR_MQ_MSG *last_hi_prio;
69 TR_MQ_NOTIFY_FN notify_cb; /* callback when queue becomes non-empty */
73 /* message string for sending trpc messages */
74 #define TR_MQMSG_TRPC_SEND "trpc send msg"
76 TR_MQ_MSG *tr_mq_msg_new(TALLOC_CTX *mem_ctx, const char *msg, TR_MQ_PRIORITY prio);
77 void tr_mq_msg_free(TR_MQ_MSG *msg);
78 TR_MQ_PRIORITY tr_mq_msg_get_prio(TR_MQ_MSG *msg);
79 const char *tr_mq_msg_get_message(TR_MQ_MSG *msg);
80 void *tr_mq_msg_get_payload(TR_MQ_MSG *msg);
81 void tr_mq_msg_set_payload(TR_MQ_MSG *msg, void *p, void (*p_free)(void *));
84 TR_MQ *tr_mq_new(TALLOC_CTX *mem_ctx);
85 void tr_mq_free(TR_MQ *mq);
86 int tr_mq_lock(TR_MQ *mq);
87 int tr_mq_unlock(TR_MQ *mq);
88 void tr_mq_set_notify_cb(TR_MQ *mq, TR_MQ_NOTIFY_FN cb, void *arg);
89 void tr_mq_add(TR_MQ *mq, TR_MQ_MSG *msg);
90 TR_MQ_MSG *tr_mq_pop(TR_MQ *mq);
91 void tr_mq_clear(TR_MQ *mq);