f60c7af78e793f6a278fdfed38ff2ec6f3aed1af
[trust_router.git] / include / tr_mq.h
1 /*
2  * Copyright (c) 2016, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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.
32  *
33  */
34
35 #ifndef _TR_MQ_H_
36 #define _TR_MQ_H_
37
38 #include <talloc.h>
39 #include <pthread.h>
40 #include <time.h>
41
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. */
45
46 typedef enum tr_mq_priority {
47   TR_MQ_PRIO_NORMAL=0,
48   TR_MQ_PRIO_HIGH
49 } TR_MQ_PRIORITY;
50
51 /* msg for inter-thread messaging */
52 typedef struct tr_mq_msg TR_MQ_MSG;
53 struct tr_mq_msg {
54   TR_MQ_MSG *next;
55   TR_MQ_PRIORITY prio;
56   char *message;
57   void *p; /* payload */
58   void (*p_free)(void *); /* function to free payload */
59 };
60
61 /* message queue for inter-thread messaging */
62
63 typedef struct tr_mq TR_MQ;
64 typedef void (*TR_MQ_NOTIFY_FN)(TR_MQ *, void *);
65 struct tr_mq {
66   pthread_mutex_t mutex;
67   pthread_cond_t have_msg_cond;
68   TR_MQ_MSG *head;
69   TR_MQ_MSG *tail;
70   TR_MQ_MSG *last_hi_prio;
71   TR_MQ_NOTIFY_FN notify_cb; /* callback when queue becomes non-empty */
72   void *notify_cb_arg;
73 };
74
75 /* message string for sending trpc messages */
76 #define TR_MQMSG_TRPC_SEND "trpc send msg"
77
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 *));
84
85
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);
95  
96 #endif /*_TR_MQ_H_ */