7 static int tr_mq_msg_destructor(void *object)
9 TR_MQ_MSG *msg=talloc_get_type_abort(object, TR_MQ_MSG);
10 if ( (msg->p!=NULL) && (msg->p_free!=NULL))
15 TR_MQ_MSG *tr_mq_msg_new(TALLOC_CTX *mem_ctx)
17 TR_MQ_MSG *msg=talloc(mem_ctx, TR_MQ_MSG);
21 talloc_set_destructor((void *)msg, tr_mq_msg_destructor);
26 void tr_mq_msg_free(TR_MQ_MSG *msg)
32 void *tr_mq_msg_get_payload(TR_MQ_MSG *msg)
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 *))
45 static TR_MQ_MSG *tr_mq_msg_get_next(TR_MQ_MSG *msg)
50 static void tr_mq_msg_set_next(TR_MQ_MSG *msg, TR_MQ_MSG *next)
56 TR_MQ *tr_mq_new(TALLOC_CTX *mem_ctx)
58 TR_MQ *mq=talloc(mem_ctx, TR_MQ);
60 pthread_mutex_init(&(mq->mutex), 0);
67 void tr_mq_free(TR_MQ *mq)
70 tr_mq_lock(mq); /* don't pull the rug out from under someone */
75 int tr_mq_lock(TR_MQ *mq)
77 return pthread_mutex_lock(&(mq->mutex));
80 int tr_mq_unlock(TR_MQ *mq)
82 return pthread_mutex_lock(&(mq->mutex));
85 static TR_MQ_MSG *tr_mq_get_head(TR_MQ *mq)
90 static void tr_mq_set_head(TR_MQ *mq, TR_MQ_MSG *msg)
95 static TR_MQ_MSG *tr_mq_get_tail(TR_MQ *mq)
100 static void tr_mq_set_tail(TR_MQ *mq, TR_MQ_MSG *msg)
105 void tr_mq_set_notify_cb(TR_MQ *mq, TR_MQ_NOTIFY_FN cb, void *arg)
108 mq->notify_cb_arg=arg;
112 /* puts msg in mq's talloc context */
113 void tr_mq_append(TR_MQ *mq, TR_MQ_MSG *msg)
116 TR_MQ_NOTIFY_FN notify_cb=NULL;
117 void *notify_cb_arg=NULL;
120 if (tr_mq_get_head(mq)==NULL) {
122 tr_mq_set_head(mq, msg);
123 tr_mq_set_tail(mq, msg);
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 */
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;
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);
139 /* caller must free msg via tr_mq_msg_free */
140 TR_MQ_MSG *tr_mq_pop(TR_MQ *mq)
142 TR_MQ_MSG *popped=NULL;
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 */
153 tr_mq_msg_set_next(popped, NULL); /* disconnect from list */