2 #include <stdlib.h> /* for malloc() */
3 #include <pthread.h> /* for mutexes */
8 struct queue
*queue_create(void)
11 pthread_mutexattr_t attr
;
13 q
= malloc(sizeof(struct queue
));
21 pthread_mutexattr_init(&attr
);
22 pthread_mutexattr_settype(&attr
, PTHREAD_MUTEX_NORMAL
);
23 pthread_mutex_init(&(q
->lock
), &attr
);
24 pthread_mutexattr_destroy(&attr
);
26 pthread_cond_init(&(q
->cond
), NULL
);
31 void queue_free(struct queue
*q
)
33 struct queue_entry
*e
;
35 /* We know when we're called there is no other possible queue user;
36 * however, we don't have any sane way to tell sparse about this, so
37 * fake the acquisition of the lock to comply with the operations
38 * performed inside. Obviously, it would be completely safe to do the
39 * queue_lock()/unlock(), but it'd be misleading to the reader */
48 pthread_mutex_destroy(&(q
->lock
));
55 void queue_lock(struct queue
*q
)
57 pthread_mutex_lock(&(q
->lock
));
60 void queue_unlock(struct queue
*q
)
62 pthread_mutex_unlock(&(q
->lock
));
65 void queue_signal(struct queue
*q
)
67 pthread_cond_signal(&(q
->cond
));
70 int queue_timedwait(struct queue
*q
, struct timespec
*ts
)
72 return pthread_cond_timedwait(&(q
->cond
), &(q
->lock
), ts
);
76 struct queue_entry
*queue_entry_create(void)
78 struct queue_entry
*e
;
80 e
= malloc(sizeof(struct queue_entry
));
96 void queue_entry_free(struct queue_entry
*e
) {
112 void queue_put(struct queue
*q
, struct queue_entry
*e
)
114 if (q
->top
== NULL
) {
115 q
->top
= q
->bottom
= e
;
124 struct queue_entry
*queue_get(struct queue
*q
)
126 struct queue_entry
*e
, *t
;
128 if (q
->bottom
== NULL
)
142 int queue_isempty(struct queue
*q
)
144 return (q
->size
== 0);