2002-09-19 02:15:27 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2002 Jonathan Mini (mini@freebsd.org).
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
2002-06-29 17:39:07 +00:00
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
2002-09-19 02:15:27 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/signal.h>
|
|
|
|
#include <sys/signalvar.h>
|
|
|
|
#include <sys/sysctl.h>
|
2002-06-29 17:39:07 +00:00
|
|
|
#include <sys/kse.h>
|
2002-09-19 02:15:27 +00:00
|
|
|
#include <sys/ucontext.h>
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sysexits.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2002-09-23 06:18:51 +00:00
|
|
|
#include "simplelock.h"
|
2002-09-19 02:15:27 +00:00
|
|
|
|
|
|
|
#undef TRACE_UTS
|
2002-09-23 06:18:51 +00:00
|
|
|
//#define TRACE_KSE
|
2002-09-19 02:15:27 +00:00
|
|
|
|
|
|
|
#ifdef TRACE_UTS
|
|
|
|
#define UPFMT(fmt...) pfmt(#fmt)
|
|
|
|
#define UPSTR(s) pstr(s)
|
|
|
|
#define UPCHAR(c) pchar(c)
|
2002-06-29 17:39:07 +00:00
|
|
|
#else
|
2002-09-19 02:15:27 +00:00
|
|
|
#define UPFMT(fmt...) /* Nothing. */
|
|
|
|
#define UPSTR(s) /* Nothing. */
|
|
|
|
#define UPCHAR(c) /* Nothing. */
|
2002-06-29 17:39:07 +00:00
|
|
|
#endif
|
|
|
|
|
2002-09-19 02:15:27 +00:00
|
|
|
#define MAIN_STACK_SIZE (1024 * 1024)
|
|
|
|
#define THREAD_STACK_SIZE (32 * 1024)
|
2002-06-29 17:39:07 +00:00
|
|
|
|
2002-09-23 06:18:51 +00:00
|
|
|
struct uts_runq {
|
2002-09-25 18:14:38 +00:00
|
|
|
struct kse_thr_mailbox *head;
|
2002-09-23 06:18:51 +00:00
|
|
|
struct simplelock lock;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct uts_data {
|
|
|
|
struct kse_mailbox mb;
|
|
|
|
struct uts_runq *runq;
|
2002-09-25 18:14:38 +00:00
|
|
|
struct kse_thr_mailbox *cur_thread;
|
2002-09-23 06:18:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct uts_runq runq1;
|
|
|
|
static struct uts_data data1, data2;
|
|
|
|
static struct uts_runq runq2;
|
|
|
|
static struct uts_data data3, data4;
|
2002-09-25 18:14:38 +00:00
|
|
|
static struct kse_thr_mailbox *aa;
|
2002-06-29 17:39:07 +00:00
|
|
|
|
2002-10-02 17:07:39 +00:00
|
|
|
#ifdef TRACE_UTS
|
2002-09-19 02:15:27 +00:00
|
|
|
static int progress = 0;
|
2002-10-02 17:07:39 +00:00
|
|
|
#endif
|
2002-06-29 17:39:07 +00:00
|
|
|
|
2002-09-23 06:18:51 +00:00
|
|
|
static void init_uts(struct uts_data *data, struct uts_runq *q);
|
|
|
|
static void start_uts(struct uts_data *data, int newgrp);
|
|
|
|
static void enter_uts(struct uts_data *);
|
2002-09-19 02:15:27 +00:00
|
|
|
static void pchar(char c);
|
|
|
|
static void pfmt(const char *fmt, ...);
|
|
|
|
static void pstr(const char *s);
|
2002-09-23 06:18:51 +00:00
|
|
|
static void runq_init(struct uts_runq *q);
|
2002-09-25 18:14:38 +00:00
|
|
|
static void runq_insert(struct uts_runq *q, struct kse_thr_mailbox *tm);
|
2002-10-02 17:07:39 +00:00
|
|
|
static struct kse_thr_mailbox *runq_remove(struct uts_runq *q);
|
|
|
|
static struct kse_thr_mailbox *runq_remove_nolock(struct uts_runq *q);
|
2002-09-23 06:18:51 +00:00
|
|
|
static void thread_start(struct uts_data *data, const void *func, int arg);
|
2002-09-19 02:15:27 +00:00
|
|
|
static void uts(struct kse_mailbox *km);
|
2002-06-29 17:39:07 +00:00
|
|
|
|
2002-10-02 17:07:39 +00:00
|
|
|
/* Functions implemented in assembly */
|
|
|
|
extern int uts_to_thread(struct kse_thr_mailbox *tdp,
|
|
|
|
struct kse_thr_mailbox **curthreadp);
|
|
|
|
extern int thread_to_uts(struct kse_thr_mailbox *tm,
|
|
|
|
struct kse_mailbox *km);
|
2002-06-29 17:39:07 +00:00
|
|
|
|
2002-09-19 02:15:27 +00:00
|
|
|
static void
|
|
|
|
nano(int len)
|
2002-06-29 17:39:07 +00:00
|
|
|
{
|
2002-09-19 02:15:27 +00:00
|
|
|
struct timespec time_to_sleep;
|
|
|
|
struct timespec time_remaining;
|
2002-06-29 17:39:07 +00:00
|
|
|
|
2002-09-19 02:15:27 +00:00
|
|
|
time_to_sleep.tv_sec = 0;
|
|
|
|
time_to_sleep.tv_nsec = len * 10000;
|
|
|
|
nanosleep(&time_to_sleep, &time_remaining);
|
|
|
|
}
|
2002-06-29 17:39:07 +00:00
|
|
|
|
2002-09-19 02:15:27 +00:00
|
|
|
void
|
|
|
|
aaaa(int c)
|
2002-06-29 17:39:07 +00:00
|
|
|
{
|
2002-09-19 02:15:27 +00:00
|
|
|
for (;;) {
|
|
|
|
pchar(c);
|
|
|
|
nano(1);
|
|
|
|
}
|
2002-06-29 17:39:07 +00:00
|
|
|
}
|
|
|
|
|
2002-09-19 02:15:27 +00:00
|
|
|
static void
|
|
|
|
foof(int sig)
|
2002-06-29 17:39:07 +00:00
|
|
|
{
|
2002-09-19 02:15:27 +00:00
|
|
|
pfmt("\n[%d]\n", sig);
|
2002-09-23 06:18:51 +00:00
|
|
|
// thread_start(aaaa, '0' + progress++);
|
2002-06-29 17:39:07 +00:00
|
|
|
}
|
|
|
|
|
2002-09-23 06:18:51 +00:00
|
|
|
static void
|
|
|
|
newkse(int v)
|
|
|
|
{
|
|
|
|
start_uts(&data4, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2002-09-19 02:15:27 +00:00
|
|
|
void
|
|
|
|
spin(int arg)
|
2002-06-29 17:39:07 +00:00
|
|
|
{
|
2002-09-19 02:15:27 +00:00
|
|
|
for (;;) enter_uts(); sched_yield();
|
2002-06-29 17:39:07 +00:00
|
|
|
}
|
2002-09-23 06:18:51 +00:00
|
|
|
#endif
|
2002-09-19 02:15:27 +00:00
|
|
|
/*
|
|
|
|
* Test Userland Thread Scheduler (UTS) suite for KSE.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
main(void)
|
2002-06-29 17:39:07 +00:00
|
|
|
{
|
2002-09-19 02:15:27 +00:00
|
|
|
int i;
|
2002-06-29 17:39:07 +00:00
|
|
|
|
2002-09-23 06:18:51 +00:00
|
|
|
runq_init(&runq1);
|
|
|
|
init_uts(&data1, &runq1);
|
|
|
|
init_uts(&data2, &runq1);
|
|
|
|
thread_start(&data1, aaaa, '+');
|
|
|
|
thread_start(&data1, aaaa, '-');
|
|
|
|
start_uts(&data1, 0);
|
|
|
|
start_uts(&data2, 0);
|
|
|
|
|
|
|
|
// start second ksegrp
|
|
|
|
runq_init(&runq2);
|
|
|
|
init_uts(&data3, &runq2);
|
|
|
|
init_uts(&data4, &runq2);
|
|
|
|
thread_start(&data3, newkse, 0);
|
|
|
|
thread_start(&data3, aaaa, '*');
|
|
|
|
thread_start(&data3, aaaa, '.');
|
|
|
|
start_uts(&data3, 1);
|
|
|
|
|
2002-09-19 02:15:27 +00:00
|
|
|
for (i = 0;1;i++) {
|
|
|
|
// if (i < 1000)
|
|
|
|
// thread_start(aaaa, 'a' + (i % 26));
|
|
|
|
pchar('A' + (i % 26));
|
|
|
|
nano(5);
|
|
|
|
}
|
|
|
|
pstr("\n** main() exiting **\n");
|
|
|
|
return (EX_OK);
|
2002-06-29 17:39:07 +00:00
|
|
|
}
|
|
|
|
|
2002-09-19 02:15:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Enter the UTS from a thread.
|
|
|
|
*/
|
|
|
|
static void
|
2002-09-23 06:18:51 +00:00
|
|
|
enter_uts(struct uts_data *data)
|
2002-06-29 17:39:07 +00:00
|
|
|
{
|
2002-09-25 18:14:38 +00:00
|
|
|
struct kse_thr_mailbox *td;
|
2002-09-19 02:15:27 +00:00
|
|
|
|
|
|
|
/* XXX: We should atomically exchange these two. */
|
2002-09-23 06:18:51 +00:00
|
|
|
td = data->mb.km_curthread;
|
|
|
|
data->mb.km_curthread = NULL;
|
2002-09-19 02:15:27 +00:00
|
|
|
|
2002-09-23 06:18:51 +00:00
|
|
|
thread_to_uts(td, &data->mb);
|
2002-06-29 17:39:07 +00:00
|
|
|
}
|
|
|
|
|
2002-09-19 02:15:27 +00:00
|
|
|
/*
|
|
|
|
* Initialise threading.
|
|
|
|
*/
|
|
|
|
static void
|
2002-09-23 06:18:51 +00:00
|
|
|
init_uts(struct uts_data *data, struct uts_runq *q)
|
2002-06-29 17:39:07 +00:00
|
|
|
{
|
2002-09-25 18:14:38 +00:00
|
|
|
struct kse_thr_mailbox *tm;
|
2002-09-19 02:15:27 +00:00
|
|
|
int mib[2];
|
|
|
|
char *p;
|
2002-10-02 17:07:39 +00:00
|
|
|
#if 0
|
2002-09-19 02:15:27 +00:00
|
|
|
size_t len;
|
2002-10-02 17:07:39 +00:00
|
|
|
#endif
|
2002-09-19 02:15:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create initial thread.
|
|
|
|
*/
|
2002-09-25 18:14:38 +00:00
|
|
|
tm = (struct kse_thr_mailbox *)calloc(1, sizeof(struct kse_thr_mailbox));
|
2002-09-19 02:15:27 +00:00
|
|
|
|
|
|
|
/* Throw us into its context. */
|
|
|
|
getcontext(&tm->tm_context);
|
|
|
|
|
|
|
|
/* Find our stack. */
|
|
|
|
mib[0] = CTL_KERN;
|
|
|
|
mib[1] = KERN_USRSTACK;
|
2002-09-23 06:18:51 +00:00
|
|
|
#if 0
|
2002-09-19 02:15:27 +00:00
|
|
|
len = sizeof(p);
|
|
|
|
if (sysctl(mib, 2, &p, &len, NULL, 0) == -1)
|
|
|
|
pstr("sysctl(CTL_KER.KERN_USRSTACK) failed.\n");
|
2002-09-23 06:18:51 +00:00
|
|
|
#endif
|
|
|
|
p = (char *)malloc(MAIN_STACK_SIZE) + MAIN_STACK_SIZE;
|
2002-09-19 02:15:27 +00:00
|
|
|
pfmt("main() : 0x%x\n", tm);
|
|
|
|
pfmt("eip -> 0x%x\n", tm->tm_context.uc_mcontext.mc_eip);
|
|
|
|
tm->tm_context.uc_stack.ss_sp = p - MAIN_STACK_SIZE;
|
|
|
|
tm->tm_context.uc_stack.ss_size = MAIN_STACK_SIZE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create KSE mailbox.
|
|
|
|
*/
|
|
|
|
p = (char *)malloc(THREAD_STACK_SIZE);
|
2002-09-23 06:18:51 +00:00
|
|
|
bzero(&data->mb, sizeof(struct kse_mailbox));
|
|
|
|
data->mb.km_stack.ss_sp = p;
|
|
|
|
data->mb.km_stack.ss_size = THREAD_STACK_SIZE;
|
|
|
|
data->mb.km_func = (void *)uts;
|
|
|
|
data->mb.km_udata = data;
|
|
|
|
data->cur_thread = tm;
|
|
|
|
data->runq = q;
|
2002-09-19 02:15:27 +00:00
|
|
|
pfmt("uts() at : 0x%x\n", uts);
|
|
|
|
pfmt("uts stack at : 0x%x - 0x%x\n", p, p + THREAD_STACK_SIZE);
|
2002-09-23 06:18:51 +00:00
|
|
|
}
|
2002-09-19 02:15:27 +00:00
|
|
|
|
2002-09-23 06:18:51 +00:00
|
|
|
static void
|
|
|
|
start_uts(struct uts_data *data, int newgrp)
|
|
|
|
{
|
2002-09-19 02:15:27 +00:00
|
|
|
/*
|
|
|
|
* Start KSE scheduling.
|
|
|
|
*/
|
2002-09-25 18:14:38 +00:00
|
|
|
pfmt("kse_create() -> %d\n", kse_create(&data->mb, newgrp));
|
2002-09-23 06:18:51 +00:00
|
|
|
data->mb.km_curthread = data->cur_thread;
|
2002-09-19 02:15:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Arrange to deliver signals via KSE.
|
|
|
|
*/
|
|
|
|
signal(SIGURG, foof);
|
2002-06-29 17:39:07 +00:00
|
|
|
}
|
|
|
|
|
2002-09-19 02:15:27 +00:00
|
|
|
/*
|
|
|
|
* Write a single character to stdout, in a thread-safe manner.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
pchar(char c)
|
2002-06-29 17:39:07 +00:00
|
|
|
{
|
2002-09-19 02:15:27 +00:00
|
|
|
|
|
|
|
write(STDOUT_FILENO, &c, 1);
|
2002-06-29 17:39:07 +00:00
|
|
|
}
|
|
|
|
|
2002-09-19 02:15:27 +00:00
|
|
|
/*
|
|
|
|
* Write formatted output to stdout, in a thread-safe manner.
|
|
|
|
*
|
|
|
|
* Recognises the following conversions:
|
|
|
|
* %c -> char
|
|
|
|
* %d -> signed int (base 10)
|
|
|
|
* %s -> string
|
|
|
|
* %u -> unsigned int (base 10)
|
|
|
|
* %x -> unsigned int (base 16)
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
pfmt(const char *fmt, ...)
|
2002-06-29 17:39:07 +00:00
|
|
|
{
|
2002-09-19 02:15:27 +00:00
|
|
|
static const char digits[16] = "0123456789abcdef";
|
|
|
|
va_list ap;
|
|
|
|
char buf[10];
|
|
|
|
char *s;
|
|
|
|
unsigned r, u;
|
|
|
|
int c, d;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
while ((c = *fmt++)) {
|
|
|
|
if (c == '%') {
|
|
|
|
c = *fmt++;
|
|
|
|
switch (c) {
|
|
|
|
case 'c':
|
|
|
|
pchar(va_arg(ap, int));
|
|
|
|
continue;
|
|
|
|
case 's':
|
|
|
|
pstr(va_arg(ap, char *));
|
|
|
|
continue;
|
|
|
|
case 'd':
|
|
|
|
case 'u':
|
|
|
|
case 'x':
|
|
|
|
r = ((c == 'u') || (c == 'd')) ? 10 : 16;
|
|
|
|
if (c == 'd') {
|
|
|
|
d = va_arg(ap, unsigned);
|
|
|
|
if (d < 0) {
|
|
|
|
pchar('-');
|
|
|
|
u = (unsigned)(d * -1);
|
|
|
|
} else
|
|
|
|
u = (unsigned)d;
|
|
|
|
} else
|
|
|
|
u = va_arg(ap, unsigned);
|
|
|
|
s = buf;
|
|
|
|
do {
|
|
|
|
*s++ = digits[u % r];
|
|
|
|
} while (u /= r);
|
|
|
|
while (--s >= buf)
|
|
|
|
pchar(*s);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pchar(c);
|
2002-06-29 17:39:07 +00:00
|
|
|
}
|
2002-09-19 02:15:27 +00:00
|
|
|
va_end(ap);
|
2002-06-29 17:39:07 +00:00
|
|
|
}
|
|
|
|
|
2002-09-19 02:15:27 +00:00
|
|
|
static void
|
|
|
|
pstr(const char *s)
|
|
|
|
{
|
2002-06-29 17:39:07 +00:00
|
|
|
|
2002-09-19 02:15:27 +00:00
|
|
|
write(STDOUT_FILENO, s, strlen(s));
|
|
|
|
}
|
2002-06-29 17:39:07 +00:00
|
|
|
|
2002-09-23 06:18:51 +00:00
|
|
|
static void
|
|
|
|
runq_init(struct uts_runq *q)
|
|
|
|
{
|
|
|
|
q->head = NULL;
|
|
|
|
simplelock_init(&q->lock);
|
|
|
|
}
|
|
|
|
|
2002-09-19 02:15:27 +00:00
|
|
|
/*
|
|
|
|
* Insert a thread into the run queue.
|
|
|
|
*/
|
|
|
|
static void
|
2002-09-25 18:14:38 +00:00
|
|
|
runq_insert(struct uts_runq *q, struct kse_thr_mailbox *tm)
|
2002-06-29 17:39:07 +00:00
|
|
|
{
|
2002-09-23 06:18:51 +00:00
|
|
|
simplelock_lock(&q->lock);
|
|
|
|
tm->tm_next = q->head;
|
|
|
|
q->head = tm;
|
|
|
|
simplelock_unlock(&q->lock);
|
2002-09-19 02:15:27 +00:00
|
|
|
}
|
2002-06-29 17:39:07 +00:00
|
|
|
|
2002-09-19 02:15:27 +00:00
|
|
|
/*
|
|
|
|
* Select and remove a thread from the run queue.
|
|
|
|
*/
|
2002-09-25 18:14:38 +00:00
|
|
|
static struct kse_thr_mailbox *
|
2002-09-23 06:18:51 +00:00
|
|
|
runq_remove(struct uts_runq *q)
|
2002-09-19 02:15:27 +00:00
|
|
|
{
|
2002-09-25 18:14:38 +00:00
|
|
|
struct kse_thr_mailbox *tm;
|
2002-09-19 02:15:27 +00:00
|
|
|
|
2002-09-23 06:18:51 +00:00
|
|
|
simplelock_lock(&q->lock);
|
|
|
|
tm = runq_remove_nolock(q);
|
|
|
|
simplelock_unlock(&q->lock);
|
|
|
|
return tm;
|
|
|
|
}
|
|
|
|
|
2002-09-25 18:14:38 +00:00
|
|
|
static struct kse_thr_mailbox *
|
2002-09-23 06:18:51 +00:00
|
|
|
runq_remove_nolock(struct uts_runq *q)
|
|
|
|
{
|
2002-09-25 18:14:38 +00:00
|
|
|
struct kse_thr_mailbox *p, *p1;
|
2002-09-23 06:18:51 +00:00
|
|
|
|
|
|
|
if (q->head == NULL)
|
2002-09-19 02:15:27 +00:00
|
|
|
return (NULL);
|
|
|
|
p1 = NULL;
|
2002-09-23 06:18:51 +00:00
|
|
|
for (p = q->head; p->tm_next != NULL; p = p->tm_next)
|
2002-09-19 02:15:27 +00:00
|
|
|
p1 = p;
|
|
|
|
if (p1 == NULL)
|
2002-09-23 06:18:51 +00:00
|
|
|
q->head = NULL;
|
2002-09-19 02:15:27 +00:00
|
|
|
else
|
|
|
|
p1->tm_next = NULL;
|
|
|
|
return (p);
|
|
|
|
}
|
2002-06-29 17:39:07 +00:00
|
|
|
|
2002-09-19 02:15:27 +00:00
|
|
|
/*
|
|
|
|
* Userland thread scheduler.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
uts(struct kse_mailbox *km)
|
|
|
|
{
|
2002-10-02 17:07:39 +00:00
|
|
|
#ifdef TRACE_KSE
|
2002-09-23 06:18:51 +00:00
|
|
|
static struct uts_data *prev_data;
|
2002-10-02 17:07:39 +00:00
|
|
|
#endif
|
2002-09-25 18:14:38 +00:00
|
|
|
struct kse_thr_mailbox *tm, *p;
|
2002-09-23 06:18:51 +00:00
|
|
|
struct uts_data *data;
|
2002-10-02 17:07:39 +00:00
|
|
|
int i;
|
2002-09-19 02:15:27 +00:00
|
|
|
|
|
|
|
UPSTR("\n--uts() start--\n");
|
|
|
|
UPFMT("mailbox -> %x\n", km);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Insert any processes back from being blocked
|
|
|
|
* in the kernel into the run queue.
|
|
|
|
*/
|
2002-09-23 06:18:51 +00:00
|
|
|
data = km->km_udata;
|
2002-09-19 02:15:27 +00:00
|
|
|
p = km->km_completed;
|
2002-09-23 06:18:51 +00:00
|
|
|
km->km_completed = NULL;
|
2002-09-19 02:15:27 +00:00
|
|
|
UPFMT("km_completed -> 0x%x", p);
|
2002-09-23 06:18:51 +00:00
|
|
|
#ifdef TRACE_KSE
|
|
|
|
if (data != prev_data) {
|
|
|
|
prev_data = data;
|
|
|
|
pfmt("uts data: 0x%x\n", data);
|
|
|
|
}
|
|
|
|
#endif
|
2002-09-19 02:15:27 +00:00
|
|
|
while ((tm = p) != NULL) {
|
|
|
|
p = tm->tm_next;
|
|
|
|
UPFMT(" 0x%x", p);
|
2002-09-23 06:18:51 +00:00
|
|
|
runq_insert(data->runq, tm);
|
2002-09-19 02:15:27 +00:00
|
|
|
}
|
|
|
|
UPCHAR('\n');
|
|
|
|
|
2002-09-23 06:18:51 +00:00
|
|
|
simplelock_lock(&data->runq->lock);
|
2002-09-19 02:15:27 +00:00
|
|
|
/*
|
2011-12-30 00:04:11 +00:00
|
|
|
* Process any signals we've received (but only if we have
|
2002-09-19 02:15:27 +00:00
|
|
|
* somewhere to deliver them to).
|
|
|
|
*/
|
2002-09-23 06:18:51 +00:00
|
|
|
if ((data->runq->head != NULL) && SIGNOTEMPTY(km->km_sigscaught)) {
|
2002-09-19 02:15:27 +00:00
|
|
|
for (i = 0;i < _SIG_MAXSIG;i++)
|
|
|
|
if (SIGISMEMBER(km->km_sigscaught, i)) {
|
2002-09-23 06:18:51 +00:00
|
|
|
signalcontext(&data->runq->head->tm_context,
|
|
|
|
i, foof);
|
2002-09-19 02:15:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
bzero(&km->km_sigscaught, sizeof(sigset_t));
|
2002-06-29 17:39:07 +00:00
|
|
|
}
|
|
|
|
|
2002-09-19 02:15:27 +00:00
|
|
|
/*
|
|
|
|
* Pull a thread off the run queue.
|
|
|
|
*/
|
2002-09-23 06:18:51 +00:00
|
|
|
p = runq_remove_nolock(data->runq);
|
|
|
|
simplelock_unlock(&data->runq->lock);
|
2002-09-19 02:15:27 +00:00
|
|
|
#if 0
|
|
|
|
if ((p == aa) && (progress > 0)) {
|
|
|
|
--progress;
|
|
|
|
signalcontext(&p->tm_context, 1, foof);
|
|
|
|
}
|
|
|
|
#endif
|
2002-06-29 17:39:07 +00:00
|
|
|
|
2002-09-19 02:15:27 +00:00
|
|
|
/*
|
|
|
|
* Either schedule a thread, or idle if none ready to run.
|
|
|
|
*/
|
|
|
|
if (p != NULL) {
|
|
|
|
UPFMT("\n-- uts() scheduling 0x%x--\n", p);
|
|
|
|
UPFMT("eip -> 0x%x progress -> %d\n",
|
|
|
|
p->tm_context.uc_mcontext.mc_eip, progress);
|
|
|
|
UPSTR("curthread set\n");
|
|
|
|
uts_to_thread(p, &km->km_curthread);
|
|
|
|
UPSTR("\n-- uts_to_thread() failed --\n");
|
|
|
|
}
|
2003-02-20 08:24:22 +00:00
|
|
|
kse_release(NULL);
|
2002-09-19 02:15:27 +00:00
|
|
|
pstr("** uts() exiting **\n");
|
|
|
|
exit(EX_SOFTWARE);
|
2002-06-29 17:39:07 +00:00
|
|
|
}
|
|
|
|
|
2002-09-19 02:15:27 +00:00
|
|
|
/*
|
|
|
|
* Start a thread.
|
|
|
|
*/
|
2002-09-25 18:14:38 +00:00
|
|
|
static struct kse_thr_mailbox *
|
2002-09-23 06:18:51 +00:00
|
|
|
thread_create(const void *func, int arg)
|
2002-09-19 02:15:27 +00:00
|
|
|
{
|
2002-09-25 18:14:38 +00:00
|
|
|
struct kse_thr_mailbox *tm;
|
2002-09-19 02:15:27 +00:00
|
|
|
char *p;
|
|
|
|
|
2002-09-25 18:14:38 +00:00
|
|
|
aa = tm = (struct kse_thr_mailbox *)calloc(1, sizeof(struct kse_thr_mailbox));
|
2002-09-19 02:15:27 +00:00
|
|
|
getcontext(&tm->tm_context);
|
|
|
|
p = (char *)malloc(THREAD_STACK_SIZE);
|
|
|
|
tm->tm_context.uc_stack.ss_sp = p;
|
|
|
|
tm->tm_context.uc_stack.ss_size = THREAD_STACK_SIZE;
|
|
|
|
makecontext(&tm->tm_context, func, 2, arg);
|
|
|
|
// setcontext(&tm->tm_context);
|
2002-09-23 06:18:51 +00:00
|
|
|
return tm;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
thread_start(struct uts_data *data, const void *func, int arg)
|
|
|
|
{
|
2002-09-25 18:14:38 +00:00
|
|
|
struct kse_thr_mailbox *tm;
|
|
|
|
struct kse_thr_mailbox *tm2;
|
2002-09-23 06:18:51 +00:00
|
|
|
|
|
|
|
tm = thread_create(func, arg);
|
|
|
|
tm2 = thread_create(enter_uts, (int)data);
|
|
|
|
tm->tm_context.uc_link = &tm2->tm_context;
|
|
|
|
runq_insert(data->runq, tm);
|
|
|
|
pfmt("thread_start() : 0x%x %x\n", tm, &tm->tm_context);
|
2002-09-19 02:15:27 +00:00
|
|
|
}
|