Split out queue data structure
This commit is contained in:
parent
78fc0a40a0
commit
f395f95fd3
2
Makefile
2
Makefile
|
@ -1,5 +1,5 @@
|
||||||
TARGET = cmsg
|
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 -O0 -g
|
||||||
#CFLAGS = -D_XOPEN_SOURCE=600 -Wall -O3
|
#CFLAGS = -D_XOPEN_SOURCE=600 -Wall -O3
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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
|
43
harness.c
43
harness.c
|
@ -3,6 +3,7 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
|
|
||||||
|
@ -13,6 +14,7 @@ typedef unsigned char u_char;
|
||||||
|
|
||||||
#include "cmsg_private.h"
|
#include "cmsg_private.h"
|
||||||
#include "harness.h"
|
#include "harness.h"
|
||||||
|
#include "dataq.h"
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
|
@ -33,40 +35,11 @@ typedef unsigned char u_char;
|
||||||
|
|
||||||
Process *current_process = NULL;
|
Process *current_process = NULL;
|
||||||
|
|
||||||
|
#define EMPTY_PROCESS_QUEUE ((queue_t) { offsetof(Process, link), 0, NULL, NULL })
|
||||||
|
|
||||||
static ucontext_t scheduler;
|
static ucontext_t scheduler;
|
||||||
static ProcessQueue runlist = { 0, NULL, NULL };
|
static queue_t runlist = EMPTY_PROCESS_QUEUE;
|
||||||
static ProcessQueue deadlist = { 0, NULL, NULL };
|
static queue_t deadlist = EMPTY_PROCESS_QUEUE;
|
||||||
|
|
||||||
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 void enqueue_runlist(Process *p) {
|
static void enqueue_runlist(Process *p) {
|
||||||
p->state = PROCESS_RUNNING;
|
p->state = PROCESS_RUNNING;
|
||||||
|
@ -253,8 +226,8 @@ void boot_harness(void) {
|
||||||
ICHECK(getcontext(&scheduler), "boot_harness getcontext");
|
ICHECK(getcontext(&scheduler), "boot_harness getcontext");
|
||||||
while (1) {
|
while (1) {
|
||||||
while (runlist.count) {
|
while (runlist.count) {
|
||||||
ProcessQueue work = runlist;
|
queue_t work = runlist;
|
||||||
zero_queue(&runlist);
|
runlist = EMPTY_PROCESS_QUEUE;
|
||||||
info("Processing %d jobs\n", work.count);
|
info("Processing %d jobs\n", work.count);
|
||||||
while ((current_process = dequeue(&work)) != NULL) {
|
while ((current_process = dequeue(&work)) != NULL) {
|
||||||
//info("entering %p\n", current_process);
|
//info("entering %p\n", current_process);
|
||||||
|
|
|
@ -25,12 +25,6 @@ typedef struct IOHandle {
|
||||||
short error_kind;
|
short error_kind;
|
||||||
} IOHandle;
|
} IOHandle;
|
||||||
|
|
||||||
typedef struct ProcessQueue {
|
|
||||||
int count;
|
|
||||||
Process *head;
|
|
||||||
Process *tail;
|
|
||||||
} ProcessQueue;
|
|
||||||
|
|
||||||
extern Process *current_process;
|
extern Process *current_process;
|
||||||
|
|
||||||
extern void yield(void);
|
extern void yield(void);
|
||||||
|
|
Loading…
Reference in New Issue