1 #include <sched.h> // for sched_yield
2 #include "bits/callback.h" // for Transformer
5 void queue_ini(Queue *queue)
8 queue->cnl = queue->fin = 0;
9 pthread_cond_init(&queue->cnd, NULL);
10 pthread_mutex_init(&queue->mtx, NULL);
13 void queue_dst(Queue *queue)
15 pthread_cond_destroy(&queue->cnd);
16 pthread_mutex_destroy(&queue->mtx);
19 void queue_clr(Queue *queue, void *iter, void *arg, void *trans)
21 list_clr(&queue->lst, iter, arg, trans);
24 #define ENQUEUE(queue_fun, list_fun) \
25 bool queue_fun(Queue *queue, void *dat) \
27 bool success = false; \
29 pthread_mutex_lock(&queue->mtx); \
32 list_fun(&queue->lst, dat); \
33 pthread_cond_signal(&queue->cnd); \
35 pthread_mutex_unlock(&queue->mtx); \
40 ENQUEUE(queue_enq, list_apd)
41 ENQUEUE(queue_ppd, list_ppd)
45 void *queue_deq(Queue *queue, void *trans)
49 pthread_mutex_lock(&queue->mtx);
50 while (!queue->cnl && !dat) {
51 ListNode **node = &queue->lst.fst;
54 list_nrm(&queue->lst, node);
57 dat = ((Transformer) trans)(dat);
59 pthread_cond_wait(&queue->cnd, &queue->mtx);
62 pthread_mutex_unlock(&queue->mtx);
67 void queue_cnl(Queue *queue)
69 pthread_mutex_lock(&queue->mtx);
71 pthread_cond_broadcast(&queue->cnd);
72 pthread_mutex_unlock(&queue->mtx);
75 void queue_fin(Queue *queue)
77 pthread_mutex_lock(&queue->mtx);
79 pthread_mutex_unlock(&queue->mtx);
82 pthread_mutex_lock(&queue->mtx);
83 ListNode *node = queue->lst.fst;
84 pthread_mutex_unlock(&queue->mtx);