Compare commits

...

2 Commits

Author SHA1 Message Date
Yourtion
44e51ca946 使用“哨兵”简化程序 2016-04-20 14:46:59 +08:00
Yourtion
dab5bd893a 加快中断处理(4) 2016-04-20 14:26:18 +08:00
2 changed files with 45 additions and 40 deletions

View File

@@ -169,13 +169,14 @@ void sheet_free(struct SHEET *sht);
/* timer.c */
#define MAX_TIMER 500
struct TIMER {
struct TIMER *next;
unsigned int timeout, flags;
struct FIFO32 *fifo;
int data;
};
struct TIMERCTL {
unsigned int count, next, using;
struct TIMER *timers[MAX_TIMER];
unsigned int count, next;
struct TIMER *t0;
struct TIMER timers0[MAX_TIMER];
};
extern struct TIMERCTL timerctl;

View File

@@ -13,15 +13,20 @@ struct TIMERCTL timerctl;
void init_pit(void)
{
int i;
struct TIMER *t;
io_out8(PIT_CTRL, 0x34);
io_out8(PIT_CNT0, 0x9c);
io_out8(PIT_CNT0, 0x2e);
timerctl.count = 0;
timerctl.next = 0xffffffff; /* 因为最初没有正在运行的定时器 */
timerctl.using = 0;
for (i = 0; i < MAX_TIMER; i++) {
timerctl.timers0[i].flags = 0; /* 使用 */
timerctl.timers0[i].flags = 0; /* 没有使用 */
}
t = timer_alloc(); /* 取得一个 */
t->timeout = 0xffffffff;
t->flags = TIMER_FLAGS_USING;
t->next = 0; /* 末尾 */
timerctl.t0 = t; /* 因为现在只有哨兵,所以他就在最前面*/
timerctl.next = 0xffffffff; /* 因为只有哨兵,所以下一个超时时刻就是哨兵的时刻 */
return;
}
@@ -52,55 +57,54 @@ void timer_init(struct TIMER *timer, struct FIFO32 *fifo, int data)
void timer_settime(struct TIMER *timer, unsigned int timeout)
{
int e, i, j;
int e;
struct TIMER *t, *s;
timer->timeout = timeout + timerctl.count;
timer->flags = TIMER_FLAGS_USING;
e = io_load_eflags();
io_cli();
/* 搜索注册位置 */
for (i = 0; i < timerctl.using; i++) {
if (timerctl.timers[i]->timeout >= timer->timeout) {
break;
t = timerctl.t0;
if (timer->timeout <= t->timeout) {
/* 插入最前面的情况 */
timerctl.t0 = timer;
timer->next = t; /* 下面是设定t */
timerctl.next = timer->timeout;
io_store_eflags(e);
return;
}
for (;;) {
s = t;
t = t->next;
if (timer->timeout <= t->timeout) {
/* 插入s和t之间的情况 */
s->next = timer; /* s下一个是timer */
timer->next = t; /* timer的下一个是t */
io_store_eflags(e);
return;
}
}
/* i号之后全部后移一位 */
for (j = timerctl.using; j > i; j--) {
timerctl.timers[j] = timerctl.timers[j - 1];
}
timerctl.using++;
/* 插入到空位上 */
timerctl.timers[i] = timer;
timerctl.next = timerctl.timers[0]->timeout;
io_store_eflags(e);
return;
}
void inthandler20(int *esp)
{
int i, j;
io_out8(PIC0_OCW2, 0x60); /* 把IRQ-00信号接收完了的信息通知给PIC */
struct TIMER *timer;
io_out8(PIC0_OCW2, 0x60); /* 把IRQ-00接收信号结束的信息通知给PIC */
timerctl.count++;
if (timerctl.next > timerctl.count) {
return; /* 还不到下一个时刻,所以结束*/
return;
}
for (i = 0; i < timerctl.using; i++) {
/* timers的定时器都处于动作中所以不确认flags */
if (timerctl.timers[i]->timeout > timerctl.count) {
timer = timerctl.t0; /* 首先把最前面的地址赋给timer */
for (;;) {
/* 因为timers的定时器都处于运行状态所以不确认flags */
if (timer->timeout > timerctl.count) {
break;
}
/* 超时*/
timerctl.timers[i]->flags = TIMER_FLAGS_ALLOC;
fifo32_put(timerctl.timers[i]->fifo, timerctl.timers[i]->data);
}
/* 正好有i个定时器超时了。其余的进行移位。 */
timerctl.using -= i;
for (j = 0; j < timerctl.using; j++) {
timerctl.timers[j] = timerctl.timers[i + j];
}
if (timerctl.using > 0) {
timerctl.next = timerctl.timers[0]->timeout;
} else {
timerctl.next = 0xffffffff;
/* 超时 */
timer->flags = TIMER_FLAGS_ALLOC;
fifo32_put(timer->fifo, timer->data);
timer = timer->next; /* 将下一个定时器的地址赋给timer*/
}
timerctl.t0 = timer;
timerctl.next = timer->timeout;
return;
}
}