From f395f95fd3325b62d909ac5772395c41ec449d7f Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Wed, 29 Dec 2010 11:35:02 -0500 Subject: [PATCH] Split out queue data structure --- Makefile | 2 +- dataq.c | 32 ++++++++++++++++++++++++++++++++ dataq.h | 14 ++++++++++++++ harness.c | 43 ++++++++----------------------------------- harness.h | 6 ------ 5 files changed, 55 insertions(+), 42 deletions(-) create mode 100644 dataq.c create mode 100644 dataq.h diff --git a/Makefile b/Makefile index b84df12..52b5979 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ TARGET = cmsg -OBJECTS = main.o harness.o net.o util.o relay.o +OBJECTS = main.o harness.o net.o util.o relay.o dataq.o CFLAGS = -D_XOPEN_SOURCE=600 -Wall -O0 -g #CFLAGS = -D_XOPEN_SOURCE=600 -Wall -O3 diff --git a/dataq.c b/dataq.c new file mode 100644 index 0000000..c90eb32 --- /dev/null +++ b/dataq.c @@ -0,0 +1,32 @@ +#include +#include +#include + +#include "dataq.h" + +#define QLINK(q,x) (*((void **)(((char *) x) + (q)->link_offset))) + +void enqueue(queue_t *q, void *x) { + QLINK(q, x) = NULL; + if (q->head == NULL) { + q->head = x; + } else { + QLINK(q, q->tail) = x; + } + q->tail = x; + q->count++; +} + +void *dequeue(queue_t *q) { + if (q->head == NULL) { + return NULL; + } else { + void *x = q->head; + q->head = QLINK(q, x); + if (q->head == NULL) { + q->tail = NULL; + } + q->count--; + return x; + } +} diff --git a/dataq.h b/dataq.h new file mode 100644 index 0000000..a193b01 --- /dev/null +++ b/dataq.h @@ -0,0 +1,14 @@ +#ifndef cmsg_dataq_h +#define cmsg_dataq_h + +typedef struct queue_t_ { + size_t link_offset; + int count; + void *head; + void *tail; +} queue_t; + +extern void enqueue(queue_t *q, void *x); +extern void *dequeue(queue_t *q); + +#endif diff --git a/harness.c b/harness.c index 1f15119..efa44df 100644 --- a/harness.c +++ b/harness.c @@ -3,6 +3,7 @@ #include #include #include +#include #include @@ -13,6 +14,7 @@ typedef unsigned char u_char; #include "cmsg_private.h" #include "harness.h" +#include "dataq.h" #include @@ -33,40 +35,11 @@ typedef unsigned char u_char; Process *current_process = NULL; +#define EMPTY_PROCESS_QUEUE ((queue_t) { offsetof(Process, link), 0, NULL, NULL }) + static ucontext_t scheduler; -static ProcessQueue runlist = { 0, NULL, NULL }; -static ProcessQueue deadlist = { 0, NULL, NULL }; - -static void zero_queue(ProcessQueue *pq) { - pq->count = 0; - pq->head = NULL; - pq->tail = NULL; -} - -static void enqueue(ProcessQueue *pq, Process *p) { - p->link = NULL; - if (pq->head == NULL) { - pq->head = p; - } else { - pq->tail->link = p; - } - pq->tail = p; - pq->count++; -} - -static Process *dequeue(ProcessQueue *pq) { - if (pq->head == NULL) { - return NULL; - } else { - Process *p = pq->head; - pq->head = p->link; - if (pq->head == NULL) { - pq->tail = NULL; - } - pq->count--; - return p; - } -} +static queue_t runlist = EMPTY_PROCESS_QUEUE; +static queue_t deadlist = EMPTY_PROCESS_QUEUE; static void enqueue_runlist(Process *p) { p->state = PROCESS_RUNNING; @@ -253,8 +226,8 @@ void boot_harness(void) { ICHECK(getcontext(&scheduler), "boot_harness getcontext"); while (1) { while (runlist.count) { - ProcessQueue work = runlist; - zero_queue(&runlist); + queue_t work = runlist; + runlist = EMPTY_PROCESS_QUEUE; info("Processing %d jobs\n", work.count); while ((current_process = dequeue(&work)) != NULL) { //info("entering %p\n", current_process); diff --git a/harness.h b/harness.h index 5d5cda6..f03359c 100644 --- a/harness.h +++ b/harness.h @@ -25,12 +25,6 @@ typedef struct IOHandle { short error_kind; } IOHandle; -typedef struct ProcessQueue { - int count; - Process *head; - Process *tail; -} ProcessQueue; - extern Process *current_process; extern void yield(void);