Add copyright statement missing from recently added files.
[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
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. */
44
45 typedef enum tr_mq_priority {
46   TR_MQ_PRIO_NORMAL=0,
47   TR_MQ_PRIO_HIGH
48 } TR_MQ_PRIORITY;
49
50 /* msg for inter-thread messaging */
51 typedef struct tr_mq_msg TR_MQ_MSG;
52 struct tr_mq_msg {
53   TR_MQ_MSG *next;
54   TR_MQ_PRIORITY prio;
55   char *message;
56   void *p; /* payload */
57   void (*p_free)(void *); /* function to free payload */
58 };
59
60 /* message queue for inter-thread messaging */
61
62 typedef struct tr_mq TR_MQ;
63 typedef void (*TR_MQ_NOTIFY_FN)(TR_MQ *, void *);
64 struct tr_mq {
65   pthread_mutex_t mutex;
66   TR_MQ_MSG *head;
67   TR_MQ_MSG *tail;
68   TR_MQ_MSG *last_hi_prio;
69   TR_MQ_NOTIFY_FN notify_cb; /* callback when queue becomes non-empty */
70   void *notify_cb_arg;
71 };
72
73 /* message string for sending trpc messages */
74 #define TR_MQMSG_TRPC_SEND "trpc send msg"
75
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 *));
82
83
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);
92  
93 #endif /*_TR_MQ_H_ */