From a1142b3a5fb49504543460ac6a2765e50d872a9f Mon Sep 17 00:00:00 2001 From: Yourtion Date: Tue, 19 Apr 2016 11:17:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=BF=AB=E4=B8=AD=E6=96=AD=E5=A4=84?= =?UTF-8?q?=E7=90=86=EF=BC=882=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 12_day/bootpack.h | 2 +- 12_day/timer.c | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/12_day/bootpack.h b/12_day/bootpack.h index f7a1a01..d42a313 100644 --- a/12_day/bootpack.h +++ b/12_day/bootpack.h @@ -176,7 +176,7 @@ struct TIMER { unsigned char data; }; struct TIMERCTL { - unsigned int count; + unsigned int count, next; struct TIMER timer[MAX_TIMER]; }; extern struct TIMERCTL timerctl; diff --git a/12_day/timer.c b/12_day/timer.c index 77ad20c..e936ffb 100644 --- a/12_day/timer.c +++ b/12_day/timer.c @@ -16,6 +16,7 @@ void init_pit(void) io_out8(PIT_CNT0, 0x9c); io_out8(PIT_CNT0, 0x2e); timerctl.count = 0; + timerctl.next = 0xffffffff; /* 因为最初没有正在运行的定时器 */ for (i = 0; i < MAX_TIMER; i++) { timerctl.timer[i].flags = 0; /* 未使用 */ } @@ -51,6 +52,10 @@ void timer_settime(struct TIMER *timer, unsigned int timeout) { timer->timeout = timeout + timerctl.count; timer->flags = TIMER_FLAGS_USING; + if (timerctl.next > timer->timeout) { + /* 更新下一次的时刻 */ + timerctl.next = timer->timeout; + } return; } @@ -59,12 +64,22 @@ void inthandler20(int *esp) int i; io_out8(PIC0_OCW2, 0x60); /* 把IRQ-00信号接收完了的信息通知给PIC */ timerctl.count++; + if (timerctl.next > timerctl.count) { + return; /* 还不到下一个时刻,所以结束*/ + } + timerctl.next = 0xffffffff; for (i = 0; i < MAX_TIMER; i++) { if (timerctl.timer[i].flags == TIMER_FLAGS_USING) { if (timerctl.timer[i].timeout <= timerctl.count) { + /* 超时 */ timerctl.timer[i].flags = TIMER_FLAGS_ALLOC; fifo8_put(timerctl.timer[i].fifo, timerctl.timer[i].data); } + } else { + /* 还没有超时 */ + if (timerctl.next > timerctl.timer[i].timeout) { + timerctl.next = timerctl.timer[i].timeout; + } } } return;