Skip to content

Commit

Permalink
让任务休眠
Browse files Browse the repository at this point in the history
  • Loading branch information
yourtion committed Apr 25, 2016
1 parent ec1f016 commit 7bd6635
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 8 deletions.
12 changes: 7 additions & 5 deletions 16_day/bootpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ void HariMain(void)
0, 0, 0, 0, 0, 0, 0, '7', '8', '9', '-', '4', '5', '6', '+', '1',
'2', '3', '0', '.'
};
struct TASK *task_b;
struct TASK *task_a, *task_b;

init_gdtidt();
init_pic();
io_sti(); /* IDT/PIC的初始化已经完成,于是开放CPU的中断 */
fifo32_init(&fifo, 128, fifobuf);
fifo32_init(&fifo, 128, fifobuf, 0);
init_pit();
init_keyboard(&fifo, 256);
enable_mouse(&fifo, 512, &mdec);
Expand Down Expand Up @@ -88,7 +88,8 @@ void HariMain(void)
memtotal / (1024 * 1024), memman_total(memman) / 1024);
putfonts8_asc_sht(sht_back, 0, 32, COL8_FFFFFF, COL8_008484, s, 40);

task_init(memman);
task_a = task_init(memman);
fifo.task = task_a;
task_b = task_alloc();
task_b->tss.esp = memman_alloc_4k(memman, 64 * 1024) + 64 * 1024 - 8;
task_b->tss.eip = (int) &task_b_main;
Expand All @@ -104,7 +105,8 @@ void HariMain(void)
for (;;) {
io_cli();
if (fifo32_status(&fifo) == 0) {
io_stihlt();
task_sleep(task_a);
io_sti();
} else {
i = fifo32_get(&fifo);
io_sti();
Expand Down Expand Up @@ -262,7 +264,7 @@ void task_b_main(struct SHEET *sht_back)
int i, fifobuf[128], count = 0, count0 = 0;
char s[12];

fifo32_init(&fifo, 128, fifobuf);
fifo32_init(&fifo, 128, fifobuf, 0);
timer_put = timer_alloc();
timer_init(timer_put, &fifo, 1);
timer_settime(timer_put, 1);
Expand Down
4 changes: 3 additions & 1 deletion 16_day/bootpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ void farjmp(int eip, int cs);
struct FIFO32 {
int *buf;
int p, q, size, free, flags;
struct TASK *task;
};
void fifo32_init(struct FIFO32 *fifo, int size, int *buf);
void fifo32_init(struct FIFO32 *fifo, int size, int *buf, struct TASK *task);
int fifo32_put(struct FIFO32 *fifo, int data);
int fifo32_get(struct FIFO32 *fifo);
int fifo32_status(struct FIFO32 *fifo);
Expand Down Expand Up @@ -208,3 +209,4 @@ struct TASK *task_init(struct MEMMAN *memman);
struct TASK *task_alloc(void);
void task_run(struct TASK *task);
void task_switch(void);
void task_sleep(struct TASK *task);
8 changes: 7 additions & 1 deletion 16_day/fifo.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#define FLAGS_OVERRUN 0x0001

void fifo32_init(struct FIFO32 *fifo, int size, int *buf)
void fifo32_init(struct FIFO32 *fifo, int size, int *buf, struct TASK *task)
/* FIFO缓冲区的初始化*/
{
fifo->size = size;
Expand All @@ -13,6 +13,7 @@ void fifo32_init(struct FIFO32 *fifo, int size, int *buf)
fifo->flags = 0;
fifo->p = 0; /*写入位置*/
fifo->q = 0; /*读取位置*/
fifo->task = task; /*有数据写入时需要唤醒的任务*/
return;
}

Expand All @@ -30,6 +31,11 @@ int fifo32_put(struct FIFO32 *fifo, int data)
fifo->p = 0;
}
fifo->free--;
if (fifo->task != 0) {
if (fifo->task->flags != 2) { /*如果任务处于休眠状态*/
task_run(fifo->task); /*将任务唤醒*/
}
}
return 0;
}

Expand Down
36 changes: 36 additions & 0 deletions 16_day/mtask.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,39 @@ void task_switch(void)
}
return;
}

void task_sleep(struct TASK *task)
{
int i;
char ts = 0;
if (task->flags == 2) { /*如果指定任务处于唤醒状态*/
if (task == taskctl->tasks[taskctl->now]) {
ts = 1; /*让自己休眠的话,稍后需要进行任务切换*/
}
/*寻找task所在的位置*/
for (i = 0; i < taskctl->running; i++) {
if (taskctl->tasks[i] == task) {
/*在这里*/
break;
}
}
taskctl->running--;
if (i < taskctl->now) {
taskctl->now--; /*需要移动成员,要相应地处理*/
}
/*移动成员*/
for (; i < taskctl->running; i++) {
taskctl->tasks[i] = taskctl->tasks[i + 1];
}
task->flags = 1; /*不工作的状态*/
if (ts != 0) {
/*任务切换*/
if (taskctl->now >= taskctl->running) {
/*如果now的值出现异常,则进行修正*/
taskctl->now = 0;
}
farjmp(0, taskctl->tasks[taskctl->now]->sel);
}
}
return;
}
2 changes: 1 addition & 1 deletion 16_day/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,4 @@ void inthandler20(int *esp)
task_switch();
}
return;
}
}

0 comments on commit 7bd6635

Please sign in to comment.