From 982542cde6b037cea35f7bc788837b03164d9581 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Wed, 22 Oct 2008 18:29:34 +0200 Subject: [PATCH] New approach for the empty packet queue issue. Now we just wait until it's no longer empty. --- src/packet-queue.c | 43 ++++++++++++++++++------------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/src/packet-queue.c b/src/packet-queue.c index 7906ffaff1..b98c9fc8da 100644 --- a/src/packet-queue.c +++ b/src/packet-queue.c @@ -22,36 +22,29 @@ void PacketEnqueue (PacketQueue *q, Packet *p) { #endif /* DBG_PERF */ } -static u_int32_t dbg_packetdequeue = 0; - Packet *PacketDequeue (PacketQueue *q) { - Packet *p = q->bot; - if (p == NULL) { - /* queue empty, alloc a new packet */ - p = malloc(sizeof(Packet)); - if (p == NULL) { - printf("ERROR: malloc failed: %s\n", strerror(errno)); - return NULL; - } + /* if the queue is empty there are no packets left. + * In that case we sleep and try again. */ + if (q->len == 0) { + printf("PacketDequeue: queue is empty, waiting...\n"); + usleep(100000); /* sleep 100ms */ + return PacketDequeue(q); + } - CLEAR_TCP_PACKET(p); - CLEAR_PACKET(p); + /* pull the bottom packet from the queue */ + Packet *p = q->bot; - dbg_packetdequeue++; - printf("PacketDequeue: alloced a new packet. MAX_PENDING %u, extra alloc: %u\n", MAX_PENDING, dbg_packetdequeue); + /* more packets in queue */ + if (q->bot->prev != NULL) { + q->bot = q->bot->prev; + q->bot->next = NULL; + /* just the one we remove, so now empty */ } else { - /* more packets in queue */ - if (q->bot->prev != NULL) { - q->bot = q->bot->prev; - q->bot->next = NULL; - /* just the one we remove, so now empty */ - } else { - q->top = NULL; - q->bot = NULL; - } - - q->len--; + q->top = NULL; + q->bot = NULL; } + q->len--; + p->next = NULL; p->prev = NULL; return p;