diff -ruN kozos25/extintr.c kozos27/extintr.c --- kozos25/extintr.c Sun Dec 9 22:19:16 2007 +++ kozos27/extintr.c Sun Dec 9 22:46:18 2007 @@ -125,7 +125,8 @@ { fd_set fds; char *p, *buffer; - int fd, id, ret, cnt_fd = -1, size, s, len; + int fd, id, ret, cnt_fd = -1, size, s; + socklen_t len; struct timeval tm = {0, 0}; intrbuf *ibp; int fildes[2]; diff -ruN kozos25/i386-stub.c kozos27/i386-stub.c --- kozos25/i386-stub.c Sun Dec 9 22:19:16 2007 +++ kozos27/i386-stub.c Sun Dec 9 22:26:41 2007 @@ -1075,17 +1075,12 @@ if (mode & TAG_DISPLAY) { ptr = intNToHex(ptr, TAG_DISPLAY, 4); /* mode */ ptr = intNToHex(ptr, 3, 1); /* length */ - { - kz_thread *thp2; + if (thp->flags & KZ_THREAD_FLAG_RUNNING) { + strcpy(ptr, "RUN"); + } else { strcpy(ptr, "SLP"); - for (thp2 = readyque[thp->pri]; thp2; thp2 = thp2->next) { - if (thp == thp2) { - strcpy(ptr, "RUN"); - break; - } - } - ptr += strlen(ptr); } + ptr += strlen(ptr); } if (mode & TAG_THREADNAME) { ptr = intNToHex(ptr, TAG_THREADNAME, 4); /* mode */ diff -ruN kozos25/stubd.c kozos27/stubd.c --- kozos25/stubd.c Sun Dec 9 22:19:16 2007 +++ kozos27/stubd.c Sun Dec 9 22:46:55 2007 @@ -14,7 +14,8 @@ int stubd_main(int argc, char *argv[]) { - int sockt, s, ret, len; + int sockt, s, ret; + socklen_t len; char hostname[256]; struct hostent *host; struct sockaddr_in address; diff -ruN kozos25/thread.c kozos27/thread.c --- kozos25/thread.c Sun Dec 9 22:19:16 2007 +++ kozos27/thread.c Mon Dec 10 20:23:28 2007 @@ -20,7 +20,10 @@ } kz_timebuf; kz_thread threads[THREAD_NUM]; -kz_thread *readyque[PRI_NUM]; +static struct { + kz_thread *head; + kz_thread *tail; +} readyque[PRI_NUM]; static kz_timebuf *timers; static kz_thread *sigcalls[SIG_NUM]; static int debug_sockt = 0; @@ -31,18 +34,28 @@ static void getcurrent() { - readyque[current->pri] = current->next; + readyque[current->pri].head = current->next; + if (readyque[current->pri].head == NULL) + readyque[current->pri].tail = NULL; + current->flags &= ~KZ_THREAD_FLAG_RUNNING; current->next = NULL; } static int putcurrent() { - kz_thread **thpp; - for (thpp = &readyque[current->pri]; *thpp; thpp = &((*thpp)->next)) { + if (current->flags & KZ_THREAD_FLAG_RUNNING) { /* すでに有る場合は無視 */ - if (*thpp == current) return -1; + return -1; + } + + if (readyque[current->pri].tail) { + readyque[current->pri].tail->next = current; + } else { + readyque[current->pri].head = current; } - *thpp = current; + readyque[current->pri].tail = current; + current->flags |= KZ_THREAD_FLAG_RUNNING; + return 0; } @@ -153,8 +166,10 @@ { kz_membuf *mp; - mp = current->messages; - current->messages = mp->next; + mp = current->messages.head; + current->messages.head = mp->next; + if (current->messages.head == NULL) + current->messages.tail = NULL; mp->next = NULL; current->syscall.param->un.recv.ret = mp->size; @@ -168,7 +183,6 @@ static void sendmsg(kz_thread *thp, int id, int size, char *p) { kz_membuf *mp; - kz_membuf **mpp; current = thp; @@ -181,9 +195,13 @@ mp->size = size; mp->id = id; mp->p = p; - for (mpp = ¤t->messages; *mpp; mpp = &((*mpp)->next)) - ; - *mpp = mp; + + if (current->messages.tail) { + current->messages.tail->next = mp; + } else { + current->messages.head = mp; + } + current->messages.tail = mp; if (putcurrent() == 0) { /* 受信する側がブロック中の場合には受信処理を行う */ @@ -200,7 +218,7 @@ static int thread_recv(int *idp, char **pp) { - if (current->messages == NULL) { + if (current->messages.head == NULL) { /* メッセージが無いのでブロックする */ return -1; } @@ -218,6 +236,7 @@ kz_timebuf *tmp; struct timeval tm; int diffmsec; + struct itimerval itm; tmp = malloc(sizeof(*tmp)); tmp->next = NULL; @@ -247,8 +266,13 @@ *tmpp = tmp; alarm_tm = tm; - if (tmpp == &timers) - ualarm(timers->msec * 1000, 0); + if (tmpp == &timers) { + itm.it_interval.tv_sec = 0; + itm.it_interval.tv_usec = 0; + itm.it_value.tv_sec = timers->msec / 1000; + itm.it_value.tv_usec = (timers->msec % 1000) * 1000; + setitimer(ITIMER_REAL, &itm, NULL); + } putcurrent(); return 0; @@ -257,6 +281,7 @@ static void alarm_handler() { kz_timebuf *tmp; + struct itimerval itm; sendmsg(timers->thp, 0, 0, NULL); tmp = timers; @@ -264,14 +289,18 @@ free(tmp); if (timers) { gettimeofday(&alarm_tm, NULL); - ualarm(timers->msec * 1000, 0); + itm.it_interval.tv_sec = 0; + itm.it_interval.tv_usec = 0; + itm.it_value.tv_sec = timers->msec / 1000; + itm.it_value.tv_usec = (timers->msec % 1000) * 1000; + setitimer(ITIMER_REAL, &itm, NULL); } } static int thread_pending() { putcurrent(); - return current->messages ? 1 : 0; + return current->messages.head ? 1 : 0; } static int thread_setsig(int signo) @@ -373,13 +402,13 @@ { int i; for (i = 0; i < PRI_NUM; i++) { - if (readyque[i]) break; + if (readyque[i].head) break; } if (i == PRI_NUM) { /* 実行可能なスレッドが存在しないので,終了する */ exit(0); } - current = readyque[i]; + current = readyque[i].head; } static void thread_intrvec(int signo) diff -ruN kozos25/thread.h kozos27/thread.h --- kozos25/thread.h Sun Dec 9 22:19:16 2007 +++ kozos27/thread.h Mon Dec 10 20:19:06 2007 @@ -26,13 +26,18 @@ kz_func func; int pri; char *stack; + unsigned int flags; +#define KZ_THREAD_FLAG_RUNNING (1 << 0) struct { kz_syscall_type_t type; kz_syscall_param_t *param; } syscall; - kz_membuf *messages; + struct { + kz_membuf *head; + kz_membuf *tail; + } messages; struct { ucontext_t uap; @@ -40,7 +45,6 @@ } kz_thread; extern kz_thread threads[THREAD_NUM]; -extern kz_thread *readyque[PRI_NUM]; extern kz_thread *current; extern sigset_t block;