diff -ruN kozos34/Makefile kozos35/Makefile --- kozos34/Makefile Thu Jan 15 22:56:55 2009 +++ kozos35/Makefile Mon Jan 19 21:50:03 2009 @@ -1,6 +1,6 @@ -OBJS += thread.o syscall.o memory.o outlog.o extintr.o idle.o +OBJS += thread.o syscall.o memory.o outlog.o timerd.o extintr.o idle.o OBJS += stubd.o stublib.o i386-stub.o -OBJS += main.o clock.o command.o +OBJS += main.o clock.o clock2.o clock3.o command.o TARGET ?= koz CC ?= gcc CFLAGS = diff -ruN kozos34/clock.c kozos35/clock.c --- kozos34/clock.c Sun Jan 18 23:48:35 2009 +++ kozos35/clock.c Mon Jan 19 21:51:29 2009 @@ -19,7 +19,7 @@ static void send_enable() { char *p; - p = kz_kmalloc(9); + p = kz_kmalloc(10); strcpy(p, "s1;;;0;;;"); kz_send(extintr_id, 9, p); } @@ -37,7 +37,8 @@ t = time(NULL); p = kz_kmalloc(128); - strcpy(p, ctime(&t)); + strcpy(p, "clock1 "); + strcpy(p + 7, ctime(&t)); kz_send(outlog_id, 0, p); } } diff -ruN kozos34/clock2.c kozos35/clock2.c --- kozos34/clock2.c Thu Jan 1 09:00:00 1970 +++ kozos35/clock2.c Mon Jan 19 21:51:17 2009 @@ -0,0 +1,33 @@ +#include +#include +#include +#include + +#include "kozos.h" + +int clock2_id; + +static void send_enable() +{ + char *p; + p = kz_kmalloc(7); + strcpy(p, "s1500;"); + kz_send(timerd_id, 7, p); +} + +int clock2_main(int argc, char *argv[]) +{ + time_t t; + char *p; + + while (1) { + send_enable(); + kz_recv(NULL, NULL); + + t = time(NULL); + p = kz_kmalloc(128); + strcpy(p, "clock2 "); + strcpy(p + 7, ctime(&t)); + kz_send(outlog_id, 0, p); + } +} diff -ruN kozos34/clock3.c kozos35/clock3.c --- kozos34/clock3.c Thu Jan 1 09:00:00 1970 +++ kozos35/clock3.c Mon Jan 19 21:51:24 2009 @@ -0,0 +1,33 @@ +#include +#include +#include +#include + +#include "kozos.h" + +int clock3_id; + +static void send_enable() +{ + char *p; + p = kz_kmalloc(7); + strcpy(p, "s1800;"); + kz_send(timerd_id, 7, p); +} + +int clock3_main(int argc, char *argv[]) +{ + time_t t; + char *p; + + while (1) { + send_enable(); + kz_recv(NULL, NULL); + + t = time(NULL); + p = kz_kmalloc(128); + strcpy(p, "clock3 "); + strcpy(p + 7, ctime(&t)); + kz_send(outlog_id, 0, p); + } +} diff -ruN kozos34/extintr.c kozos35/extintr.c --- kozos34/extintr.c Sun Jan 18 23:55:47 2009 +++ kozos35/extintr.c Mon Jan 19 01:29:35 2009 @@ -338,7 +338,7 @@ case 's': tbp->flags |= INTRBUF_FLAG_ENABLE; tbp->tm.tv_sec = strtol(&buf[1], NULL, 0); - tbp->tm.tv_usec = strtol(&buf[5], NULL, 0); + tbp->tm.tv_usec = strtol(&buf[5], NULL, 0) * 1000; timeadd(&tbp->tm, &now, &tbp->tm); i += 8; break; diff -ruN kozos34/kozos.h kozos35/kozos.h --- kozos34/kozos.h Thu Jan 15 22:56:55 2009 +++ kozos35/kozos.h Mon Jan 19 21:49:32 2009 @@ -16,7 +16,6 @@ int kz_chpri(int pri); int kz_send(int id, int size, char *p); int kz_recv(int *idp, char **pp); -int kz_timer(int msec); int kz_pending(); int kz_sethandler(int signo, kz_handler handler); int kz_setsig(int signo); @@ -36,18 +35,24 @@ /* general thread */ extern int outlog_id; +extern int timerd_id; extern int extintr_id; extern int idle_id; extern int stubd_id; int outlog_main(int argc, char *argv[]); +int timerd_main(int argc, char *argv[]); int extintr_main(int argc, char *argv[]); int idle_main(int argc, char *argv[]); int stubd_main(int argc, char *argv[]); /* user thread */ extern int clock_id; +extern int clock2_id; +extern int clock3_id; extern int command_id; int clock_main(int argc, char *argv[]); +int clock2_main(int argc, char *argv[]); +int clock3_main(int argc, char *argv[]); int command_main(int argc, char *argv[]); #endif diff -ruN kozos34/main.c kozos35/main.c --- kozos34/main.c Thu Jan 15 22:56:55 2009 +++ kozos35/main.c Mon Jan 19 21:49:09 2009 @@ -10,8 +10,11 @@ stubd_id = kz_run(stubd_main, "stubd", 2, 0, NULL); #endif outlog_id = kz_run(outlog_main, "outlog", 3, 0, NULL); + timerd_id = kz_run(timerd_main, "timerd", 4, 0, NULL); idle_id = kz_run(idle_main, "idle", 31, 0, NULL); clock_id = kz_run(clock_main, "clock", 7, 0, NULL); + clock2_id = kz_run(clock2_main, "clock2", 7, 0, NULL); + clock3_id = kz_run(clock3_main, "clock3", 7, 0, NULL); command_id = kz_run(command_main, "command", 8, 0, NULL); return 0; diff -ruN kozos34/syscall.c kozos35/syscall.c --- kozos34/syscall.c Thu Jan 15 22:56:55 2009 +++ kozos35/syscall.c Mon Jan 19 21:45:20 2009 @@ -89,14 +89,6 @@ return param.un.recv.ret; } -int kz_timer(int msec) -{ - kz_syscall_param_t param; - param.un.timer.msec = msec; - kz_syscall(KZ_SYSCALL_TYPE_TIMER, ¶m); - return param.un.timer.ret; -} - int kz_pending() { kz_syscall_param_t param; diff -ruN kozos34/syscall.h kozos35/syscall.h --- kozos34/syscall.h Thu Jan 15 22:56:55 2009 +++ kozos35/syscall.h Mon Jan 19 21:45:15 2009 @@ -13,7 +13,6 @@ KZ_SYSCALL_TYPE_CHPRI, KZ_SYSCALL_TYPE_SEND, KZ_SYSCALL_TYPE_RECV, - KZ_SYSCALL_TYPE_TIMER, KZ_SYSCALL_TYPE_PENDING, KZ_SYSCALL_TYPE_SETSIG, KZ_SYSCALL_TYPE_SETHANDLER, @@ -65,10 +64,6 @@ char **pp; int ret; } recv; - struct { - int msec; - int ret; - } timer; struct { int ret; } pending; diff -ruN kozos34/thread.c kozos35/thread.c --- kozos34/thread.c Thu Jan 15 22:56:55 2009 +++ kozos35/thread.c Mon Jan 19 21:47:33 2009 @@ -4,7 +4,6 @@ #include #include #include -#include #include "kozos.h" #include "syscall.h" @@ -14,19 +13,12 @@ #define SIG_NUM 32 -typedef struct _kz_timebuf { - struct _kz_timebuf *next; - int msec; - kz_thread *thp; -} kz_timebuf; - kz_thread threads[THREAD_NUM]; static struct { kz_thread *head; kz_thread *tail; } readyque[PRI_NUM]; static unsigned int readyque_bitmap; -static kz_timebuf *timers; static kz_thread *sigcalls[SIG_NUM]; static kz_handler handlers[SIG_NUM]; static int debug_sockt = 0; @@ -250,75 +242,6 @@ return current->syscall.param->un.recv.ret; } -static struct timeval alarm_tm; - -static int thread_timer(int msec) -{ - kz_timebuf **tmpp; - kz_timebuf *tmp; - struct timeval tm; - int diffmsec; - struct itimerval itm; - - tmp = kzmem_alloc(sizeof(*tmp)); - tmp->next = NULL; - tmp->thp = current; - - gettimeofday(&tm, NULL); - if (timers) { - diffmsec = (tm.tv_sec - alarm_tm.tv_sec) * 1000 + - (tm.tv_usec - alarm_tm.tv_usec) / 1000; - if (timers->msec > diffmsec) - timers->msec -= diffmsec; - else - timers->msec = 0; - } - - for (tmpp = &timers; *tmpp; tmpp = &((*tmpp)->next)) { - if (msec < (*tmpp)->msec) { - (*tmpp)->msec -= msec; - break; - } - msec -= (*tmpp)->msec; - } - - if (msec == 0) msec++; - tmp->msec = msec; - tmp->next = *tmpp; - *tmpp = tmp; - - alarm_tm = tm; - 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; -} - -static void alarm_handler() -{ - kz_timebuf *tmp; - struct itimerval itm; - - sendmsg(timers->thp, 0, 0, NULL); - tmp = timers; - timers = timers->next; - kzmem_free(tmp); - if (timers) { - gettimeofday(&alarm_tm, NULL); - 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(); @@ -433,9 +356,6 @@ case KZ_SYSCALL_TYPE_RECV: p->un.recv.ret = thread_recv(p->un.recv.idp, p->un.recv.pp); break; - case KZ_SYSCALL_TYPE_TIMER: - p->un.timer.ret = thread_timer(p->un.timer.msec); - break; case KZ_SYSCALL_TYPE_PENDING: p->un.pending.ret = thread_pending(); break; @@ -508,7 +428,7 @@ case SIGHUP: /* 外部割込み */ break; case SIGALRM: /* タイマ割込み発生 */ - alarm_handler(); + /* none */ break; case SIGBUS: /* ダウン要因発生 */ case SIGSEGV: @@ -576,7 +496,6 @@ memset(handlers, 0, sizeof(handlers)); readyque_bitmap = 0; - timers = NULL; intrstack.ss_sp = malloc(SIGSTKSZ); intrstack.ss_size = SIGSTKSZ; diff -ruN kozos34/timerd.c kozos35/timerd.c --- kozos34/timerd.c Thu Jan 1 09:00:00 1970 +++ kozos35/timerd.c Mon Jan 19 01:49:05 2009 @@ -0,0 +1,124 @@ +#include +#include +#include + +#include "kozos.h" + +int timerd_id; + +typedef struct _timebuf { + struct _timebuf *next; + int msec; + int id; +} timebuf; + +static timebuf *timers = NULL; + +static void send_use(int num) +{ + char *p; + p = kz_kmalloc(2); + p[0] = 't'; + p[1] = '0' + num; + kz_send(extintr_id, 2, p); +} + +static void send_enable(int sec, int msec) +{ + char *p; + p = kz_kmalloc(9); + p[0] = 's'; + p[1] = '0' + ( sec / 100) % 10; + p[2] = '0' + ( sec / 10) % 10; + p[3] = '0' + sec % 10; + p[4] = ';'; + p[5] = '0' + (msec / 100) % 10; + p[6] = '0' + (msec / 10) % 10; + p[7] = '0' + msec % 10; + p[8] = ';'; + kz_send(extintr_id, 9, p); +} + +static struct timeval alarm_tm; + +static int set_timer(int id, int msec) +{ + timebuf **tmpp; + timebuf *tmp; + struct timeval tm; + int diffmsec; + + tmp = kz_kmalloc(sizeof(*tmp)); + tmp->next = NULL; + tmp->id = id; + + gettimeofday(&tm, NULL); + if (timers) { + diffmsec = (tm.tv_sec - alarm_tm.tv_sec) * 1000 + + (tm.tv_usec - alarm_tm.tv_usec) / 1000; + if (timers->msec > diffmsec) + timers->msec -= diffmsec; + else + timers->msec = 0; + } + + for (tmpp = &timers; *tmpp; tmpp = &((*tmpp)->next)) { + if (msec < (*tmpp)->msec) { + (*tmpp)->msec -= msec; + break; + } + msec -= (*tmpp)->msec; + } + + if (msec == 0) msec++; + tmp->msec = msec; + tmp->next = *tmpp; + *tmpp = tmp; + + alarm_tm = tm; + if (tmpp == &timers) { + send_enable(timers->msec / 1000, timers->msec % 1000); + } + + return 0; +} + +static void expire_timer() +{ + timebuf *old; + + kz_send(timers->id, 0, NULL); + + old = timers; + timers = timers->next; + kz_kmfree(old); + if (timers) { + gettimeofday(&alarm_tm, NULL); + send_enable(timers->msec / 1000, timers->msec % 1000); + } +} + +int timerd_main(int argc, char *argv[]) +{ + int size, id, msec; + char *p; + + send_use(3); + + while (1) { + size = kz_recv(&id, &p); + if (p == NULL) { + expire_timer(); + } else { + switch (p[0]) { + case 's': /* タイマ設定 */ + msec = strtol(&p[1], NULL, 0); + set_timer(id, msec); + break; + default: + break; + } + kz_kmfree(p); + } + } +}