8 static int tr_mq_msg_destructor(void *object)
10 TR_MQ_MSG *msg=talloc_get_type_abort(object, TR_MQ_MSG);
11 if ( (msg->p!=NULL) && (msg->p_free!=NULL))
16 TR_MQ_MSG *tr_mq_msg_new(TALLOC_CTX *mem_ctx)
18 TR_MQ_MSG *msg=talloc(mem_ctx, TR_MQ_MSG);
22 talloc_set_destructor((void *)msg, tr_mq_msg_destructor);
27 void tr_mq_msg_free(TR_MQ_MSG *msg)
33 void *tr_mq_msg_get_payload(TR_MQ_MSG *msg)
38 /* call with a pointer to the payload and a function to free it later */
39 void tr_mq_msg_set_payload(TR_MQ_MSG *msg, void *p, void (*p_free)(void *))
46 static TR_MQ_MSG *tr_mq_msg_get_next(TR_MQ_MSG *msg)
51 static void tr_mq_msg_set_next(TR_MQ_MSG *msg, TR_MQ_MSG *next)
57 TR_MQ *tr_mq_new(TALLOC_CTX *mem_ctx)
59 TR_MQ *mq=talloc(mem_ctx, TR_MQ);
61 pthread_mutex_init(&(mq->mutex), 0);
68 void tr_mq_free(TR_MQ *mq)
71 tr_mq_lock(mq); /* don't pull the rug out from under someone */
76 int tr_mq_lock(TR_MQ *mq)
78 return pthread_mutex_lock(&(mq->mutex));
81 int tr_mq_unlock(TR_MQ *mq)
83 return pthread_mutex_unlock(&(mq->mutex));
86 static TR_MQ_MSG *tr_mq_get_head(TR_MQ *mq)
91 static void tr_mq_set_head(TR_MQ *mq, TR_MQ_MSG *msg)
96 static TR_MQ_MSG *tr_mq_get_tail(TR_MQ *mq)
101 static void tr_mq_set_tail(TR_MQ *mq, TR_MQ_MSG *msg)
106 void tr_mq_set_notify_cb(TR_MQ *mq, TR_MQ_NOTIFY_FN cb, void *arg)
109 mq->notify_cb_arg=arg;
113 /* puts msg in mq's talloc context */
114 void tr_mq_append(TR_MQ *mq, TR_MQ_MSG *msg)
117 TR_MQ_NOTIFY_FN notify_cb=NULL;
118 void *notify_cb_arg=NULL;
121 if (tr_mq_get_head(mq)==NULL) {
123 tr_mq_set_head(mq, msg);
124 tr_mq_set_tail(mq, msg);
126 tr_mq_msg_set_next(tr_mq_get_tail(mq), msg); /* add to list */
127 tr_mq_set_tail(mq, msg); /* update tail of list */
129 talloc_steal(mq, msg);
130 /* before releasing the mutex, get notify_cb data out of mq */
131 notify_cb=mq->notify_cb;
132 notify_cb_arg=mq->notify_cb_arg;
135 /* see if we need to tell someone we became non-empty */
136 if (was_empty && (notify_cb!=NULL))
137 notify_cb(mq, notify_cb_arg);
140 /* caller must free msg via tr_mq_msg_free */
141 TR_MQ_MSG *tr_mq_pop(TR_MQ *mq)
143 TR_MQ_MSG *popped=NULL;
146 if (tr_mq_get_head(mq)!=NULL) {
147 popped=tr_mq_get_head(mq);
148 tr_mq_set_head(mq, tr_mq_msg_get_next(popped)); /* popped is the old head */
149 if (tr_mq_get_head(mq)==NULL)
150 tr_mq_set_tail(mq, NULL); /* just popped the last element */
154 tr_mq_msg_set_next(popped, NULL); /* disconnect from list */