From 7bd6635921cfd8a2fac35862f2ca457ebfb14fff Mon Sep 17 00:00:00 2001 From: Yourtion Date: Mon, 25 Apr 2016 11:14:49 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A9=E4=BB=BB=E5=8A=A1=E4=BC=91=E7=9C=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 16_day/bootpack.c | 12 +++++++----- 16_day/bootpack.h | 4 +++- 16_day/fifo.c | 8 +++++++- 16_day/mtask.c | 36 ++++++++++++++++++++++++++++++++++++ 16_day/timer.c | 2 +- 5 files changed, 54 insertions(+), 8 deletions(-) diff --git a/16_day/bootpack.c b/16_day/bootpack.c index 22dc57f..7ad30dc 100644 --- a/16_day/bootpack.c +++ b/16_day/bootpack.c @@ -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); @@ -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; @@ -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(); @@ -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); diff --git a/16_day/bootpack.h b/16_day/bootpack.h index 5622ba7..8a42a78 100644 --- a/16_day/bootpack.h +++ b/16_day/bootpack.h @@ -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); @@ -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); diff --git a/16_day/fifo.c b/16_day/fifo.c index e9d69cd..12beeb5 100644 --- a/16_day/fifo.c +++ b/16_day/fifo.c @@ -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; @@ -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; } @@ -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; } diff --git a/16_day/mtask.c b/16_day/mtask.c index a382f22..ac996b3 100644 --- a/16_day/mtask.c +++ b/16_day/mtask.c @@ -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; +} diff --git a/16_day/timer.c b/16_day/timer.c index f2138aa..2c795f8 100644 --- a/16_day/timer.c +++ b/16_day/timer.c @@ -115,4 +115,4 @@ void inthandler20(int *esp) task_switch(); } return; -} \ No newline at end of file +}