Split out queue data structure

This commit is contained in:
Tony Garnock-Jones 2010-12-29 11:35:02 -05:00
parent 78fc0a40a0
commit f395f95fd3
5 changed files with 55 additions and 42 deletions

View File

@ -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

32
dataq.c Normal file
View File

@ -0,0 +1,32 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#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;
}
}

14
dataq.h Normal file
View File

@ -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

View File

@ -3,6 +3,7 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stddef.h>
#include <sys/time.h>
@ -13,6 +14,7 @@ typedef unsigned char u_char;
#include "cmsg_private.h"
#include "harness.h"
#include "dataq.h"
#include <assert.h>
@ -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);

View File

@ -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);