Skip to content

Commit

Permalink
设定任务优先级(2)
Browse files Browse the repository at this point in the history
  • Loading branch information
yourtion committed Apr 25, 2016
1 parent e467f40 commit e924b8c
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 54 deletions.
3 changes: 2 additions & 1 deletion 16_day/bootpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 */
Expand Down
17 changes: 12 additions & 5 deletions 16_day/bootpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
2 changes: 1 addition & 1 deletion 16_day/fifo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
151 changes: 104 additions & 47 deletions 16_day/mtask.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit e924b8c

Please sign in to comment.