From e924b8c36c63d005684bca2b47e3ca6fdac5cce9 Mon Sep 17 00:00:00 2001 From: Yourtion Date: Mon, 25 Apr 2016 16:41:00 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E5=AE=9A=E4=BB=BB=E5=8A=A1=E4=BC=98?= =?UTF-8?q?=E5=85=88=E7=BA=A7=EF=BC=882=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 16_day/bootpack.c | 3 +- 16_day/bootpack.h | 17 ++++-- 16_day/fifo.c | 2 +- 16_day/mtask.c | 151 +++++++++++++++++++++++++++++++--------------- 4 files changed, 119 insertions(+), 54 deletions(-) diff --git a/16_day/bootpack.c b/16_day/bootpack.c index 190a247..26592fb 100644 --- a/16_day/bootpack.c +++ b/16_day/bootpack.c @@ -51,6 +51,7 @@ void HariMain(void) shtctl = shtctl_init(memman, binfo->vram, binfo->scrnx, binfo->scrny); task_a = task_init(memman); fifo.task = task_a; + task_run(task_a, 1, 0); /* sht_back */ sht_back = sheet_alloc(shtctl); @@ -75,7 +76,7 @@ void HariMain(void) task_b[i]->tss.fs = 1 * 8; task_b[i]->tss.gs = 1 * 8; *((int *) (task_b[i]->tss.esp + 4)) = (int) sht_win_b[i]; - task_run(task_b[i], i + 1); + task_run(task_b[i], 2, i + 1); } /* sht_win */ diff --git a/16_day/bootpack.h b/16_day/bootpack.h index e518272..86a2dd1 100644 --- a/16_day/bootpack.h +++ b/16_day/bootpack.h @@ -188,6 +188,8 @@ void inthandler20(int *esp); /* mtask.c */ #define MAX_TASKS 1000 /*最大任务数量*/ #define TASK_GDT0 3 /*定义从GDT的几号开始分配给TSS */ +#define MAX_TASKS_LV 100 +#define MAX_TASKLEVELS 10 struct TSS32 { int backlink, esp0, ss0, esp1, ss1, esp2, ss2, cr3; int eip, eflags, eax, ecx, edx, ebx, esp, ebp, esi, edi; @@ -196,18 +198,23 @@ struct TSS32 { }; struct TASK { int sel, flags; /* sel用来存放GDT的编号*/ - int priority; /* 优先级 */ + int level, priority; /* 优先级 */ struct TSS32 tss; }; +struct TASKLEVEL { + int running; /*正在运行的任务数量*/ + int now; /*这个变量用来记录当前正在运行的是哪个任务*/ + struct TASK *tasks[MAX_TASKS_LV]; +}; struct TASKCTL { - int running; /*正在运行的任务数量*/ - int now; /*这个变量用来记录当前正在运行的是哪个任务*/ - struct TASK *tasks[MAX_TASKS]; + int now_lv; /*现在活动中的LEVEL */ + char lv_change; /*在下次任务切换时是否需要改变LEVEL */ + struct TASKLEVEL level[MAX_TASKLEVELS]; struct TASK tasks0[MAX_TASKS]; }; extern struct TIMER *task_timer; struct TASK *task_init(struct MEMMAN *memman); struct TASK *task_alloc(void); -void task_run(struct TASK *task, int priority); +void task_run(struct TASK *task, int level, int priority); void task_switch(void); void task_sleep(struct TASK *task); diff --git a/16_day/fifo.c b/16_day/fifo.c index 0c51e4c..8f28f4b 100644 --- a/16_day/fifo.c +++ b/16_day/fifo.c @@ -33,7 +33,7 @@ int fifo32_put(struct FIFO32 *fifo, int data) fifo->free--; if (fifo->task != 0) { if (fifo->task->flags != 2) { /*如果任务处于休眠状态*/ - task_run(fifo->task, 0); /*将任务唤醒*/ + task_run(fifo->task, -1, 0); /*将任务唤醒*/ } } return 0; diff --git a/16_day/mtask.c b/16_day/mtask.c index 8fe0221..148a59c 100644 --- a/16_day/mtask.c +++ b/16_day/mtask.c @@ -5,6 +5,65 @@ struct TASKCTL *taskctl; struct TIMER *task_timer; +struct TASK *task_now(void) +{ + struct TASKLEVEL *tl = &taskctl->level[taskctl->now_lv]; + return tl->tasks[tl->now]; +} + +void task_add(struct TASK *task) +{ + struct TASKLEVEL *tl = &taskctl->level[task->level]; + tl->tasks[tl->running] = task; + tl->running++; + task->flags = 2; /*活动中*/ + return; +} + +void task_remove(struct TASK *task) +{ + int i; + struct TASKLEVEL *tl = &taskctl->level[task->level]; + + /*寻找task所在的位置*/ + for (i = 0; i < tl->running; i++) { + if (tl->tasks[i] == task) { + /*在这里 */ + break; + } + } + + tl->running--; + if (i < tl->now) { + tl->now--; /*需要移动成员,要相应地处理 */ + } + if (tl->now >= tl->running) { + /*如果now的值出现异常,则进行修正*/ + tl->now = 0; + } + task->flags = 1; /* 休眠中 */ + + /* 移动 */ + for (; i < tl->running; i++) { + tl->tasks[i] = tl->tasks[i + 1]; + } + return; +} + +void task_switchsub(void) +{ + int i; + /*寻找最上层的LEVEL */ + for (i = 0; i < MAX_TASKLEVELS; i++) { + if (taskctl->level[i].running > 0) { + break; /*找到了*/ + } + } + taskctl->now_lv = i; + taskctl->lv_change = 0; + return; +} + struct TASK *task_init(struct MEMMAN *memman) { int i; @@ -17,13 +76,16 @@ struct TASK *task_init(struct MEMMAN *memman) taskctl->tasks0[i].sel = (TASK_GDT0 + i) * 8; set_segmdesc(gdt + TASK_GDT0 + i, 103, (int) &taskctl->tasks0[i].tss, AR_TSS32); } - + for (i = 0; i < MAX_TASKLEVELS; i++) { + taskctl->level[i].running = 0; + taskctl->level[i].now = 0; + } task = task_alloc(); task->flags = 2; /*活动中标志*/ - task->priority = 2; /* 0.02秒 */ - taskctl->running = 1; - taskctl->now = 0; - taskctl->tasks[0] = task; + task->priority = 2; /* 0.02秒*/ + task->level = 0; /*最高LEVEL */ + task_add(task); + task_switchsub(); /* LEVEL 设置*/ load_tr(task->sel); task_timer = timer_alloc(); timer_settime(task_timer, task->priority); @@ -58,64 +120,59 @@ struct TASK *task_alloc(void) return 0; /*全部正在使用*/ } -void task_run(struct TASK *task, int priority) +void task_run(struct TASK *task, int level, int priority) { + if (level < 0) { + level = task->level; /*不改变LEVEL */ + } if (priority > 0) { task->priority = priority; } + if (task->flags == 2 && task->level != level) { + /*改变活动中的LEVEL */ + task_remove(task); /*这里执行之后flag的值会变为1,于是下面的if语句块也会被执行*/ + } if (task->flags != 2) { - task->flags = 2; /*活动中标志*/ - taskctl->tasks[taskctl->running] = task; - taskctl->running++; + /*从休眠状态唤醒的情形*/ + task->level = level; + task_add(task); } + taskctl->lv_change = 1; /*下次任务切换时检查LEVEL */ return; } -void task_switch(void){ - struct TASK *task; - taskctl->now++; - if (taskctl->now == taskctl->running) { - taskctl->now = 0; +void task_switch(void) +{ + struct TASKLEVEL *tl = &taskctl->level[taskctl->now_lv]; + struct TASK *new_task, *now_task = tl->tasks[tl->now]; + tl->now++; + if (tl->now == tl->running) { + tl->now = 0; } - task = taskctl->tasks[taskctl->now]; - timer_settime(task_timer, task->priority); - if (taskctl->running >= 2) { - farjmp(0, task->sel); + if (taskctl->lv_change != 0) { + task_switchsub(); + tl = &taskctl->level[taskctl->now_lv]; + } + new_task = tl->tasks[tl->now]; + timer_settime(task_timer, new_task->priority); + if (new_task != now_task) { + farjmp(0, new_task->sel); } 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); + struct TASK *now_task; + if (task->flags == 2) { + /*如果处于活动状态*/ + now_task = task_now(); + task_remove(task); /*执行此语句的话flags将变为1 */ + if (task == now_task) { + /*如果是让自己休眠,则需要进行任务切换*/ + task_switchsub(); + now_task = task_now(); /*在设定后获取当前任务的值*/ + farjmp(0, now_task->sel); } } return;