You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
lighttpd2/src/waitqueue.c

158 lines
3.0 KiB

#include <lighttpd/base.h>
void waitqueue_init(waitqueue *queue, struct ev_loop *loop, waitqueue_cb callback, gdouble delay, gpointer data) {
ev_timer_init(&queue->timer, callback, delay, delay);
queue->timer.data = data;
queue->head = queue->tail = NULL;
queue->loop = loop;
queue->delay = delay;
}
void waitqueue_stop(waitqueue *queue) {
ev_timer_stop(queue->loop, &queue->timer);
}
void waitqueue_update(waitqueue *queue) {
ev_tstamp repeat;
ev_tstamp now = ev_now(queue->loop);
if (G_LIKELY(queue->head)) {
repeat = queue->head->ts + queue->delay - now;
if (repeat < 0.05)
repeat = 0.05;
queue->timer.repeat = repeat;
ev_timer_again(queue->loop, &queue->timer);
} else {
/* stop timer if queue empty */
ev_timer_stop(queue->loop, &queue->timer);
return;
}
}
void waitqueue_push(waitqueue *queue, waitqueue_elem *elem) {
elem->ts = ev_now(queue->loop);
if (!elem->queued) {
/* not in the queue yet, insert at the end */
elem->queued = TRUE;
if (!queue->head) {
/* queue is empty */
queue->head = elem;
queue->tail = elem;
elem->prev = NULL;
elem->next = NULL;
} else {
/* queue not empty */
elem->prev = queue->tail;
elem->next = NULL;
queue->tail->next = elem;
queue->tail = elem;
}
} else {
/* already queued, move to end */
if (elem == queue->tail)
return;
if (elem == queue->head)
queue->head = elem->next;
else
elem->prev->next = elem->next;
elem->next->prev = elem->prev;
elem->prev = queue->tail;
elem->next = NULL;
queue->tail->next = elem;
queue->tail = elem;
}
if (G_UNLIKELY(!ev_is_active(&queue->timer)))
ev_timer_start(queue->loop, &queue->timer);
}
waitqueue_elem *waitqueue_pop(waitqueue *queue) {
waitqueue_elem *elem = queue->head;
ev_tstamp now = ev_now(queue->loop);
if (!elem || (elem->ts + queue->delay) > now) {
return NULL;
}
if (elem == queue->tail)
queue->tail = NULL;
else
elem->next->prev = NULL;
queue->head = elem->next;
elem->queued = FALSE;
return elem;
}
void waitqueue_remove(waitqueue *queue, waitqueue_elem *elem) {
if (!elem->queued)
return;
if (elem == queue->head)
queue->head = elem->next;
else
elem->prev->next = elem->next;
if (elem == queue->tail)
queue->tail = elem->prev;
else
elem->next->prev = elem->prev;
elem->queued = FALSE;
elem->ts = 0;
if (G_UNLIKELY(!queue->head))
ev_timer_stop(queue->loop, &queue->timer);
}
guint waitqueue_length(waitqueue *queue) {
guint i = 0;
waitqueue_elem *elem = queue->head;
while (elem) {
i++;
elem = elem->next;
}
return i;
}
guint waitqueue_pop_ready(waitqueue *queue, waitqueue_elem **head) {
guint i = 0;
waitqueue_elem *elem = queue->head;
ev_tstamp now = ev_now(queue->loop);
*head = elem;
while (elem != NULL) {
if ((elem->ts + queue->delay) > now) {
queue->head = elem;
if (elem->prev) {
elem->prev->next = NULL;
}
return i;
}
elem->queued = FALSE;
elem = elem->next;
i++;
}
queue->head = NULL;
queue->tail = NULL;
return i;
}