Skip to content

Commit

Permalink
feat(ex_13_1): update for timeslice
Browse files Browse the repository at this point in the history
  • Loading branch information
ludics committed Oct 28, 2023
1 parent 6dda145 commit 436aa37
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
32 changes: 25 additions & 7 deletions code/exercises/ex_13_1/sched.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ uint32_t task_timeslice[MAX_TASKS];
* _current is used to point to the context of current task
*/
static int _current = -1;
static int _cur_timeslice = 0;

extern void os_schedule();

Expand All @@ -54,11 +55,7 @@ void sched_init()
}
}

/*
* implment a simple cycle FIFO schedular
*/
void schedule()
{
int find_next_task_id() {
// 先扫描获取最高优先级
int highest_priority = 0xff;
for (int i = 0; i < MAX_TASKS; i++) {
Expand All @@ -85,13 +82,34 @@ void schedule()
}
}
}
return next_task_id;
}

/*
* implment a simple cycle FIFO schedular
*/
void schedule()
{
int next_task_id = find_next_task_id();
// 没有可调度的任务
if (next_task_id == -1) {
// 没有可调度的任务
printf("no schedulable task!\n");
switch_to(&ctx_os);
}
// 检查下个任务的优先级是否比当前任务高,如果高则切换
// 如果优先级相同,则检查当前任务的时间片是否用完
if (_current == -1 || task_status[_current] == TASK_EXITED) {
_current = next_task_id;
_cur_timeslice = 1;
} else if (task_priorities[next_task_id] < task_priorities[_current] ||
(task_priorities[next_task_id] == task_priorities[_current] &&
task_timeslice[_current] <= _cur_timeslice )) {
_current = next_task_id;
_cur_timeslice = 1;
} else {
_cur_timeslice++;
}
// 切换到下一个任务
_current = next_task_id;
struct context *next = &(ctx_tasks[_current]);
task_status[_current] = TASK_RUNNING;
switch_to(next);
Expand Down
6 changes: 3 additions & 3 deletions code/exercises/ex_13_1/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ void timer_init()
* On reset, mtime is cleared to zero, but the mtimecmp registers
* are not reset. So we have to init the mtimecmp manually.
*/
timer_load(TIMER_INTERVAL_MS * 500);
timer_load(TIMER_INTERVAL_MS * 300);

/* enable machine-mode timer interrupts. */
w_mie(r_mie() | MIE_MTIE);
}

void timer_handler()
void timer_handler()
{
_tick++;
printf("tick: %d\n", _tick);

timer_load(TIMER_INTERVAL_MS * 1000);
timer_load(TIMER_INTERVAL_MS * 300);

schedule();
}
18 changes: 9 additions & 9 deletions code/exercises/ex_13_1/user.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void user_task(void* param)
{
int id = (int) param;
printf("Task %d: Created!\n", id);
int iter_cnt = id / 1000 + 5;
int iter_cnt = id / 1000 + 10;
while (1) {
printf("Task %d: Running...\n", id);
task_delay(DELAY);
Expand All @@ -44,13 +44,13 @@ void os_main(void)
{
// task_create(user_task0, NULL, 255, 0);
// task_create(user_task1, NULL, 255, 0);
task_create(user_task, (void *)3000, 0, 0);
task_create(user_task, (void *)8003, 3, 0);
task_create(user_task, (void *)2005, 5, 0);
task_create(user_task, (void *)5000, 0, 0);
task_create(user_task, (void *)9004, 4, 0);
task_create(user_task, (void *)6001, 1, 0);
task_create(user_task, (void *)7002, 2, 0);
task_create(user_task, (void *)4000, 0, 0);
task_create(user_task, (void *)3000, 0, 2);
task_create(user_task, (void *)8003, 3, 2);
task_create(user_task, (void *)2003, 3, 3);
task_create(user_task, (void *)5000, 0, 3);
task_create(user_task, (void *)9003, 3, 4);
task_create(user_task, (void *)6001, 1, 3);
task_create(user_task, (void *)7001, 1, 2);
task_create(user_task, (void *)4000, 0, 4);
}

0 comments on commit 436aa37

Please sign in to comment.