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 /* Note on mq priorities: High priority messages are guaranteed to be
43 * processed before any normal priority messages. Otherwise, messages
44 * will be processed in the order they are added to the queue. */
46 typedef enum tr_mq_priority {
51 /* msg for inter-thread messaging */
52 typedef struct tr_mq_msg TR_MQ_MSG;
57 void *p; /* payload */
58 void (*p_free)(void *); /* function to free payload */
61 /* message queue for inter-thread messaging */
63 typedef struct tr_mq TR_MQ;
64 typedef void (*TR_MQ_NOTIFY_FN)(TR_MQ *, void *);
66 pthread_mutex_t mutex;
67 pthread_cond_t have_msg_cond;
70 TR_MQ_MSG *last_hi_prio;
71 TR_MQ_NOTIFY_FN notify_cb; /* callback when queue becomes non-empty */
75 /* message string for sending trpc messages */
76 #define TR_MQMSG_TRPC_SEND "trpc send msg"
78 TR_MQ_MSG *tr_mq_msg_new(TALLOC_CTX *mem_ctx, const char *msg, TR_MQ_PRIORITY prio);
79 void tr_mq_msg_free(TR_MQ_MSG *msg);
80 TR_MQ_PRIORITY tr_mq_msg_get_prio(TR_MQ_MSG *msg);
81 const char *tr_mq_msg_get_message(TR_MQ_MSG *msg);
82 void *tr_mq_msg_get_payload(TR_MQ_MSG *msg);
83 void tr_mq_msg_set_payload(TR_MQ_MSG *msg, void *p, void (*p_free)(void *));
86 TR_MQ *tr_mq_new(TALLOC_CTX *mem_ctx);
87 void tr_mq_free(TR_MQ *mq);
88 int tr_mq_lock(TR_MQ *mq);
89 int tr_mq_unlock(TR_MQ *mq);
90 void tr_mq_set_notify_cb(TR_MQ *mq, TR_MQ_NOTIFY_FN cb, void *arg);
91 void tr_mq_add(TR_MQ *mq, TR_MQ_MSG *msg);
92 int tr_mq_pop_timeout(time_t seconds, struct timespec *ts);
93 TR_MQ_MSG *tr_mq_pop(TR_MQ *mq, struct timespec *ts_abort);
94 void tr_mq_clear(TR_MQ *mq);