diff --git a/24_day/bootpack.h b/24_day/bootpack.h index 4215c38..bc14ac9 100644 --- a/24_day/bootpack.h +++ b/24_day/bootpack.h @@ -176,7 +176,8 @@ void sheet_free(struct SHEET *sht); #define MAX_TIMER 500 struct TIMER { struct TIMER *next; - unsigned int timeout, flags; + unsigned int timeout; + char flags, flags2; struct FIFO32 *fifo; int data; }; @@ -192,6 +193,8 @@ void timer_free(struct TIMER *timer); void timer_init(struct TIMER *timer, struct FIFO32 *fifo, int data); void timer_settime(struct TIMER *timer, unsigned int timeout); void inthandler20(int *esp); +int timer_cancel(struct TIMER *timer); +void timer_cancelall(struct FIFO32 *fifo); /* mtask.c */ #define MAX_TASKS 1000 /*最大任务数量*/ diff --git a/24_day/console.c b/24_day/console.c index d61d65c..ab3f80a 100644 --- a/24_day/console.c +++ b/24_day/console.c @@ -308,6 +308,7 @@ int cmd_app(struct CONSOLE *cons, int *fat, char *cmdline) sheet_free(sht); /*关闭*/ } } + timer_cancelall(&task->fifo); memman_free_4k(memman, (int) q, segsiz); } else { cons_putstr0(cons, ".hrb file format error.\n"); @@ -421,6 +422,7 @@ int *hrb_api(int edi, int esi, int ebp, int esp, int ebx, int edx, int ecx, int } } else if (edx == 16) { reg[7] = (int) timer_alloc(); + ((struct TIMER *) reg[7])->flags2 = 1; /*允许自动取消*/ } else if (edx == 17) { timer_init((struct TIMER *) ebx, &task->fifo, eax + 256); } else if (edx == 18) { diff --git a/24_day/timer.c b/24_day/timer.c index 2c795f8..9018ac8 100644 --- a/24_day/timer.c +++ b/24_day/timer.c @@ -36,6 +36,7 @@ struct TIMER *timer_alloc(void) for (i = 0; i < MAX_TIMER; i++) { if (timerctl.timers0[i].flags == 0) { timerctl.timers0[i].flags = TIMER_FLAGS_ALLOC; + timerctl.timers0[i].flags2 = 0; return &timerctl.timers0[i]; } } @@ -116,3 +117,53 @@ void inthandler20(int *esp) } return; } + +int timer_cancel(struct TIMER *timer) +{ + int e; + struct TIMER *t; + e = io_load_eflags(); + io_cli(); /*在设置过程中禁止改变定时器状态*/ + if (timer->flags == TIMER_FLAGS_USING) { /*是否需要取消?*/ + if (timer == timerctl.t0) { + /*第一个定时器的取消处理*/ + t = timer->next; + timerctl.t0 = t; + timerctl.next = t->timeout; + } else { + /*非第一个定时器的取消处理*/ + /*找到timer前一个定时器*/ + t = timerctl.t0; + for (;;) { + if (t->next == timer) { + break; + } + t = t->next; + } + t->next = timer->next; + /*将之前“timer的下一个”指向“timer的下一个”*/ + } + timer->flags = TIMER_FLAGS_ALLOC; + io_store_eflags(e); + return 1; /*取消处理成功*/ + } + io_store_eflags(e); + return 0; /*不需要取消处理*/ +} + +void timer_cancelall(struct FIFO32 *fifo) +{ + int e, i; + struct TIMER *t; + e = io_load_eflags(); + io_cli(); /*在设置过程中禁止改变定时器状态*/ + for (i = 0; i < MAX_TIMER; i++) { + t = &timerctl.timers0[i]; + if (t->flags != 0 && t->flags2 != 0 && t->fifo == fifo) { + timer_cancel(t); + timer_free(t); + } + } + io_store_eflags(e); + return; +}