Skip to content

Commit

Permalink
取消定时器
Browse files Browse the repository at this point in the history
  • Loading branch information
yourtion committed May 11, 2016
1 parent 9475d3d commit efb6028
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
5 changes: 4 additions & 1 deletion 24_day/bootpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand All @@ -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 /*最大任务数量*/
Expand Down
2 changes: 2 additions & 0 deletions 24_day/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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) {
Expand Down
51 changes: 51 additions & 0 deletions 24_day/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}
Expand Down Expand Up @@ -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;
}

0 comments on commit efb6028

Please sign in to comment.