#include <stdio.h>
#include <stdlib.h>
#include <thread.h>
#include <synch.h>
#include <syslog.h>
#include <slp.h>
#include <slp-internal.h>
struct queue_entry {
void *msg;
struct queue_entry *next;
};
typedef struct queue_entry slp_queue_entry_t;
struct queue {
slp_queue_entry_t *head;
slp_queue_entry_t *tail;
mutex_t *lock;
cond_t *wait;
int count;
};
slp_queue_t *slp_new_queue(SLPError *err) {
mutex_t *lock;
cond_t *wait;
struct queue *q;
*err = SLP_OK;
if ((lock = calloc(1, sizeof (*lock))) == NULL) {
*err = SLP_MEMORY_ALLOC_FAILED;
slp_err(LOG_CRIT, 0, "slp_new_queue", "out of memory");
return (NULL);
}
if (!(wait = calloc(1, sizeof (*wait)))) {
*err = SLP_MEMORY_ALLOC_FAILED;
slp_err(LOG_CRIT, 0, "slp_new_queue", "out of memory");
return (NULL);
}
(void) cond_init(wait, USYNC_THREAD, NULL);
if ((q = malloc(sizeof (*q))) == NULL) {
*err = SLP_MEMORY_ALLOC_FAILED;
slp_err(LOG_CRIT, 0, "slp_new_queue", "out of memory");
return (NULL);
}
q->head = NULL;
q->lock = lock;
q->wait = wait;
q->count = 0;
return (q);
}
SLPError slp_enqueue(slp_queue_t *qa, void *msg) {
slp_queue_entry_t *qe;
struct queue *q = qa;
if ((qe = malloc(sizeof (*qe))) == NULL) {
slp_err(LOG_CRIT, 0, "slp_enqueue", "out of memory");
return (SLP_MEMORY_ALLOC_FAILED);
}
(void) mutex_lock(q->lock);
qe->msg = msg;
qe->next = NULL;
if (q->head != NULL) {
q->tail->next = qe;
q->tail = qe;
} else {
q->head = q->tail = qe;
}
q->count++;
(void) mutex_unlock(q->lock);
(void) cond_signal(q->wait);
return (SLP_OK);
}
SLPError slp_enqueue_at_head(slp_queue_t *qa, void *msg) {
slp_queue_entry_t *qe;
struct queue *q = qa;
if ((qe = malloc(sizeof (*qe))) == NULL) {
slp_err(LOG_CRIT, 0, "slp_enqueue", "out of memory");
return (SLP_MEMORY_ALLOC_FAILED);
}
(void) mutex_lock(q->lock);
qe->msg = msg;
qe->next = q->head;
q->head = qe;
q->count++;
(void) mutex_unlock(q->lock);
(void) cond_signal(q->wait);
return (SLP_OK);
}
static void *dequeue_nolock(struct queue *q) {
void *msg;
slp_queue_entry_t *qe = q->head;
if (!qe)
return (NULL);
msg = qe->msg;
if (!qe->next)
q->head = q->tail = NULL;
else
q->head = qe->next;
free(qe);
q->count--;
return (msg);
}
void *slp_dequeue_timed(slp_queue_t *qa, timestruc_t *to, SLPBoolean *etimed) {
int err;
void *ans;
struct queue *q = qa;
if (etimed)
*etimed = SLP_FALSE;
(void) mutex_lock(q->lock);
if (q->count > 0) {
goto msg_available;
}
while (q->count == 0) {
if (to) {
err = cond_timedwait(q->wait, q->lock, to);
} else {
err = cond_wait(q->wait, q->lock);
}
if (err == ETIME) {
(void) mutex_unlock(q->lock);
*etimed = SLP_TRUE;
return (NULL);
}
}
msg_available:
ans = dequeue_nolock(q);
(void) mutex_unlock(q->lock);
return (ans);
}
void *slp_dequeue(slp_queue_t *qa) {
return (slp_dequeue_timed(qa, NULL, NULL));
}
void slp_flush_queue(slp_queue_t *qa, void (*free_f)(void *)) {
slp_queue_entry_t *p, *pn;
struct queue *q = qa;
for (p = q->head; p; p = pn) {
pn = p->next;
free_f(p);
}
}
void slp_destroy_queue(slp_queue_t *qa) {
struct queue *q = qa;
(void) mutex_destroy(q->lock);
(void) cond_destroy(q->wait);
free(q->lock);
free(q->wait);
free(q);
}