void schedule();
+void set_need_schedule();
+void clear_need_schedule();
+bool need_schedule();
+
extern task_t root_task;
extern void load_cr3(task_t* tsk);
int ticks;
int priority;
- int64_t jiffies;
-
- volatile int need_resched;
pid_t pid;
pid_t ppid;
void clk_handler(unsigned int irq, pt_regs_t* regs, void* dev_id) {
jiffies++;
- current->jiffies = jiffies;
-
#if ENABLE_CLOCK_IRQ_WAIT
if (enable_clock_irq_delay) {
return;
current->ticks--;
+ if (current->ticks <= 0) {
+ set_need_schedule();
+ }
+
if (reenter == 0) {
add_irq_bh_handler(clk_bh_handler, NULL);
}
tsk->priority = current->priority;
tsk->ticks = tsk->priority;
- tsk->need_resched = 0;
tsk->sched_cnt = 0;
tsk->sched_keep_cnt = 0;
#include <system.h>
#include <task.h>
#include <clock.h>
+#include <sched.h>
irq_desc_t irq_desc[NR_IRQS];
irq_bh_action_t* irq_bh_actions = NULL;
reenter--;
// 考察如果不需要调度程序,直接退出
- // if (current->need_resched == 0) {
- // return;
- // }
-
- // if (irq != 0) {
- // return;
- // }
+ if (!need_schedule()) {
+ return;
+ }
enable_irq();
root_task.priority = 7;
root_task.ticks = root_task.priority;
root_task.vma_list = NULL;
- root_task.need_resched = 0;
root_task.sched_cnt = 0;
root_task.sched_keep_cnt = 0;
root_task.magic = TASK_MAGIC;
unsigned long eflags;
irq_save(eflags);
+ // 把自己挂到就绪队列尾部
if (prev->state == TASK_READY || prev->state == TASK_RUN) {
task_set_ready(prev);
}
prev->ticks = prev->priority;
}
+ clear_need_schedule();
+
if (prev != next) {
next->sched_cnt++;
context_switch(prev, next);
void task_set_run(task_t* t) {
assert(t != NULL);
- if (t == &root_task) {
- t->state = TASK_RUN;
- return;
- }
+ // if (t == &root_task) {
+ // t->state = TASK_RUN;
+ // return;
+ // }
assert(t->state == TASK_READY);
INIT_LIST_HEAD(&t->list);
INIT_LIST_HEAD(&t->ready_list);
INIT_LIST_HEAD(&t->waitq_list);
+}
+
+///
+
+static volatile bool _need_schedule = false;
+
+void set_need_schedule() {
+ _need_schedule = true;
+}
+
+void clear_need_schedule() {
+ _need_schedule = false;
+}
+
+bool need_schedule() {
+ return _need_schedule;
}
\ No newline at end of file
.global syscall_entry
.global ret_from_fork_krnl
.extern reenter
+.extern sysc_check_resched
//.global syscall_exit
syscall_entry:
// 保存返回值
movl %eax, PT_REGS_EAX(%esp)
+ call sysc_check_resched
+
RESTORE_REGS
return -1;
}
+
+void sysc_check_resched() {
+ unsigned long eflags;
+ irq_save(eflags);
+ if (need_schedule()) {
+ if (!IN_CRITICAL_ZONE()) {
+ schedule();
+ }
+ }
+ irq_restore(eflags);
+}
\ No newline at end of file
assert(waitq != NULL);
assert(cnt >= 0);
+ // 是否唤醒了任务,如果唤醒了任务就将当前任务标记为需要调度,以便新任务更快可以被调度
+ bool woken_task = false;
+
for (int i = 0; ((i < cnt) || (cnt == 0)); i++) {
if (list_empty(&waitq->list)) {
break;
list_del_init(&task->waitq_list);
task_set_ready(task);
+
+ woken_task = true;
+ }
+
+ if (woken_task) {
+ set_need_schedule();
}
}