b15081c54da0c642efb31422a17cefea6e3eb4e7
[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 /* msg for inter-thread messaging */
43 typedef struct tr_mq_msg TR_MQ_MSG;
44 struct tr_mq_msg {
45   TR_MQ_MSG *next;
46   char *message;
47   void *p; /* payload */
48   void (*p_free)(void *); /* function to free payload */
49 };
50
51 /* message queue for inter-thread messaging */
52
53 typedef struct tr_mq TR_MQ;
54 typedef void (*TR_MQ_NOTIFY_FN)(TR_MQ *, void *);
55 struct tr_mq {
56   pthread_mutex_t mutex;
57   pthread_cond_t have_msg_cond;
58   TR_MQ_MSG *head;
59   TR_MQ_MSG *tail;
60   TR_MQ_NOTIFY_FN notify_cb; /* callback when queue becomes non-empty */
61   void *notify_cb_arg;
62 };
63
64 /* message string for sending trpc messages */
65 #define TR_MQMSG_TRPC_SEND "trpc send msg"
66
67 TR_MQ_MSG *tr_mq_msg_new(TALLOC_CTX *mem_ctx, const char *msg);
68 void tr_mq_msg_free(TR_MQ_MSG *msg);
69 const char *tr_mq_msg_get_message(TR_MQ_MSG *msg);
70 void *tr_mq_msg_get_payload(TR_MQ_MSG *msg);
71 void tr_mq_msg_set_payload(TR_MQ_MSG *msg, void *p, void (*p_free)(void *));
72
73
74 TR_MQ *tr_mq_new(TALLOC_CTX *mem_ctx);
75 void tr_mq_free(TR_MQ *mq);
76 int tr_mq_lock(TR_MQ *mq);
77 int tr_mq_unlock(TR_MQ *mq);
78 void tr_mq_set_notify_cb(TR_MQ *mq, TR_MQ_NOTIFY_FN cb, void *arg);
79 void tr_mq_add(TR_MQ *mq, TR_MQ_MSG *msg);
80 int tr_mq_pop_timeout(time_t seconds, struct timespec *ts);
81 TR_MQ_MSG *tr_mq_pop(TR_MQ *mq, struct timespec *ts_abort);
82 void tr_mq_clear(TR_MQ *mq);
83  
84 #endif /*_TR_MQ_H_ */