Authenticate GSS context in separate thread. (Not fully working yet.)
[trust_router.git] / common / tr_mq.c
1 #include <talloc.h>
2 #include <pthread.h>
3
4 #include <tr_mq.h>
5
6 /* Messages */
7 static int tr_mq_msg_destructor(void *object)
8 {
9   TR_MQ_MSG *msg=talloc_get_type_abort(object, TR_MQ_MSG);
10   if ( (msg->p!=NULL) && (msg->p_free!=NULL))
11     msg->p_free(msg->p);
12   return 0;
13 }
14
15 TR_MQ_MSG *tr_mq_msg_new(TALLOC_CTX *mem_ctx)
16 {
17   TR_MQ_MSG *msg=talloc(mem_ctx, TR_MQ_MSG);
18   if (msg!=NULL) {
19     msg->next=NULL;
20     msg->p=NULL;
21     talloc_set_destructor((void *)msg, tr_mq_msg_destructor);
22   }
23   return msg;
24 }
25
26 void tr_mq_msg_free(TR_MQ_MSG *msg)
27 {
28   if (msg!=NULL)
29     talloc_free(msg);
30 }
31
32 void *tr_mq_msg_get_payload(TR_MQ_MSG *msg)
33 {
34   return msg->p;
35 }
36
37 /* call with a pointer to the payload and a function to free it later */
38 void tr_mq_msg_set_payload(TR_MQ_MSG *msg, void *p, void (*p_free)(void *))
39 {
40   msg->p=p;
41   msg->p_free=p_free;
42 }
43
44
45 static TR_MQ_MSG *tr_mq_msg_get_next(TR_MQ_MSG *msg)
46 {
47   return msg->next;
48 }
49
50 static void tr_mq_msg_set_next(TR_MQ_MSG *msg, TR_MQ_MSG *next)
51 {
52   msg->next=next;
53 }
54
55 /* Message Queues */
56 TR_MQ *tr_mq_new(TALLOC_CTX *mem_ctx)
57 {
58   TR_MQ *mq=talloc(mem_ctx, TR_MQ);
59   if (mq!=NULL) {
60     pthread_mutex_init(&(mq->mutex), 0);
61     mq->head=NULL;
62     mq->tail=NULL;
63   }
64   return mq;
65 }
66
67 void tr_mq_free(TR_MQ *mq)
68 {
69   if (mq!=NULL) {
70     tr_mq_lock(mq); /* don't pull the rug out from under someone */
71     talloc_free(mq);
72   }
73 }
74
75 int tr_mq_lock(TR_MQ *mq)
76 {
77   return pthread_mutex_lock(&(mq->mutex));
78 }
79
80 int tr_mq_unlock(TR_MQ *mq)
81 {
82   return pthread_mutex_lock(&(mq->mutex));
83 }
84
85 static TR_MQ_MSG *tr_mq_get_head(TR_MQ *mq)
86 {
87   return mq->head;
88 }
89
90 static void tr_mq_set_head(TR_MQ *mq, TR_MQ_MSG *msg)
91 {
92   mq->head=msg;
93 }
94
95 static TR_MQ_MSG *tr_mq_get_tail(TR_MQ *mq)
96 {
97   return mq->tail;
98 }
99
100 static void tr_mq_set_tail(TR_MQ *mq, TR_MQ_MSG *msg)
101 {
102   mq->tail=msg;
103 }
104
105 void tr_mq_set_notify_cb(TR_MQ *mq, TR_MQ_NOTIFY_FN cb, void *arg)
106 {
107   mq->notify_cb=cb;
108   mq->notify_cb_arg=arg;
109 }
110
111
112 /* puts msg in mq's talloc context */
113 void tr_mq_append(TR_MQ *mq, TR_MQ_MSG *msg)
114 {
115   int was_empty=0;
116   TR_MQ_NOTIFY_FN notify_cb=NULL;
117   void *notify_cb_arg=NULL;
118
119   tr_mq_lock(mq);
120   if (tr_mq_get_head(mq)==NULL) {
121     was_empty=1;
122     tr_mq_set_head(mq, msg);
123     tr_mq_set_tail(mq, msg);
124   } else {
125     tr_mq_msg_set_next(tr_mq_get_tail(mq), msg); /* add to list */
126     tr_mq_set_tail(mq, msg); /* update tail of list */
127   }
128   talloc_steal(mq, msg);
129   /* before releasing the mutex, get notify_cb data out of mq */
130   notify_cb=mq->notify_cb;
131   notify_cb_arg=mq->notify_cb_arg;
132   tr_mq_unlock(mq);
133
134   /* see if we need to tell someone we became non-empty */
135   if (was_empty && (notify_cb!=NULL))
136     mq->notify_cb(mq, notify_cb_arg);
137 }
138
139 /* caller must free msg via tr_mq_msg_free */
140 TR_MQ_MSG *tr_mq_pop(TR_MQ *mq)
141 {
142   TR_MQ_MSG *popped=NULL;
143
144   tr_mq_lock(mq);
145   if (tr_mq_get_head(mq)!=NULL) {
146     popped=tr_mq_get_head(mq);
147     tr_mq_msg_set_next(popped, tr_mq_msg_get_next(popped)); /* popped is the old head */
148     if (tr_mq_get_head(mq)==NULL)
149       tr_mq_set_tail(mq, NULL); /* just popped the last element */
150   }
151   tr_mq_unlock(mq);
152   if (popped!=NULL)
153     tr_mq_msg_set_next(popped, NULL); /* disconnect from list */
154   return popped;
155 }