1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
#include "copyright.h"
#include "qt.h"
#include "stp.h"
#ifndef NULL
#define NULL 0
#endif
#define STP_STKSIZE (0x1000)
/* `alignment' must be a power of 2. */
#define STP_STKALIGN(sp, alignment) \
((void *)((((qt_word_t)(sp)) + (alignment) - 1) & ~((alignment)-1)))
/* The notion of a thread is merged with the notion of a queue.
Thread stuff: thread status (sp) and stuff to use during
(re)initialization. Queue stuff: next thread in the queue
(next). */
struct stp_t {
qt_t *sp; /* QuickThreads handle. */
void *sto; /* `malloc'-allocated stack. */
struct stp_t *next; /* Next thread in the queue. */
};
/* A queue is a circular list of threads. The queue head is a
designated list element. If this is a uniprocessor-only
implementation we can store the `main' thread in this, but in a
multiprocessor there are several `heavy' threads but only one run
queue. A fancier implementation might have private run queues,
which would lead to a simpler (trivial) implementation */
typedef struct stp_q_t {
stp_t t;
stp_t *tail;
} stp_q_t;
/* Helper functions. */
extern void *malloc (unsigned size);
extern void perror (char const *msg);
extern void free (void *sto);
void *
xmalloc (unsigned size)
{
void *sto;
sto = malloc (size);
if (!sto) {
perror ("malloc");
exit (1);
}
return (sto);
}
/* Queue access functions. */
static void
stp_qinit (stp_q_t *q)
{
q->t.next = q->tail = &q->t;
}
static stp_t *
stp_qget (stp_q_t *q)
{
stp_t *t;
t = q->t.next;
q->t.next = t->next;
if (t->next == &q->t) {
if (t == &q->t) { /* If it was already empty .. */
return (NULL); /* .. say so. */
}
q->tail = &q->t; /* Else now it is empty. */
}
return (t);
}
static void
stp_qput (stp_q_t *q, stp_t *t)
{
q->tail->next = t;
t->next = &q->t;
q->tail = t;
}
/* Thread routines. */
static stp_q_t stp_global_runq; /* A queue of runable threads. */
static stp_t stp_global_main; /* Thread for the process. */
static stp_t *stp_global_curr; /* Currently-executing thread. */
static void *stp_starthelp (qt_t *old, void *ignore0, void *ignore1);
static void stp_only (void *pu, void *pt, qt_userf_t *f);
static void *stp_aborthelp (qt_t *sp, void *old, void *null);
static void *stp_yieldhelp (qt_t *sp, void *old, void *blockq);
void
stp_init()
{
stp_qinit (&stp_global_runq);
}
void
stp_start()
{
stp_t *next;
while ((next = stp_qget (&stp_global_runq)) != NULL) {
stp_global_curr = next;
QT_BLOCK (stp_starthelp, 0, 0, next->sp);
}
}
static void *
stp_starthelp (qt_t *old, void *ignore0, void *ignore1)
{
stp_global_main.sp = old;
stp_qput (&stp_global_runq, &stp_global_main);
/* return (garbage); */
}
void
stp_create (stp_userf_t *f, void *pu)
{
stp_t *t;
void *sto;
t = xmalloc (sizeof(stp_t));
t->sto = xmalloc (STP_STKSIZE);
sto = STP_STKALIGN (t->sto, QT_STKALIGN);
t->sp = QT_SP (sto, STP_STKSIZE - QT_STKALIGN);
t->sp = QT_ARGS (t->sp, pu, t, (qt_userf_t *)f, stp_only);
stp_qput (&stp_global_runq, t);
}
static void
stp_only (void *pu, void *pt, qt_userf_t *f)
{
stp_global_curr = (stp_t *)pt;
(*(stp_userf_t *)f)(pu);
stp_abort();
/* NOTREACHED */
}
void
stp_abort (void)
{
stp_t *old, *newthread;
newthread = stp_qget (&stp_global_runq);
old = stp_global_curr;
stp_global_curr = newthread;
QT_ABORT (stp_aborthelp, old, (void *)NULL, newthread->sp);
}
static void *
stp_aborthelp (qt_t *sp, void *old, void *null)
{
free (((stp_t *)old)->sto);
free (old);
/* return (garbage); */
}
void
stp_yield()
{
stp_t *old, *newthread;
newthread = stp_qget (&stp_global_runq);
old = stp_global_curr;
stp_global_curr = newthread;
QT_BLOCK (stp_yieldhelp, old, &stp_global_runq, newthread->sp);
}
static void *
stp_yieldhelp (qt_t *sp, void *old, void *blockq)
{
((stp_t *)old)->sp = sp;
stp_qput ((stp_q_t *)blockq, (stp_t *)old);
/* return (garbage); */
}
|