extern void load_cr3(task_union *tsk);
extern list_head_t all_tasks;
-extern list_head_t delay_tasks;
#define set_current_state(st) \
do { \
#include <printk.h>
#include <sched.h>
#include <system.h>
+#include <wait.h>
-static unsigned int jiffies = 0;
+volatile unsigned int jiffies = 0;
unsigned int sys_clock() { return jiffies; }
printl(MPL_CLOCK, "clock irq: %d", jiffies);
}
- // unsigned long iflags;
- // irq_save(iflags);
+ unsigned long iflags;
+ irq_save(iflags);
task_union *p = 0;
- task_union *t = 0;
- list_for_each_entry_safe(p, t, &delay_tasks, pend) {
- p->delay_cnt -= p->delay_cnt == 0 ? 0 : 1;
-
- if (0 == p->delay_cnt) {
+ list_head_t *t = 0;
+ list_head_t *pos = 0;
+ list_for_each_safe(pos, t, &all_tasks) {
+ p = list_entry(pos, task_union, list);
+ if (0 != p->delay_cnt && jiffies > p->delay_cnt && p->state == TASK_WAIT) {
+ p->delay_cnt = 0;
p->state = TASK_READY;
- list_del(&p->pend);
}
}
- // irq_restore(iflags);
+ irq_restore(iflags);
}
// printl(MPL_ROOT, "root:%d [%08x] cnt %u", root_task.pid, &root_task, root_task.cnt);
#if 1
+ bool need_reset_weight = true;
list_for_each_safe(pos, t, &all_tasks) {
p = list_entry(pos, task_union, list);
if (p->state != TASK_READY) {
continue;
}
- if (p->weight >= p->priority) {
+ if (p->weight < p->priority) {
+ need_reset_weight = false;
+ break;
+ }
+ }
+
+ if (need_reset_weight) {
+ list_for_each_safe(pos, t, &all_tasks) {
+ p = list_entry(pos, task_union, list);
+ if (p->state != TASK_READY) {
+ continue;
+ }
p->weight = 0;
+ }
+ }
+
+ list_for_each_safe(pos, t, &all_tasks) {
+ p = list_entry(pos, task_union, list);
+ if (p->state != TASK_READY) {
+ continue;
+ }
+
+ if (p->weight > p->priority) {
continue;
}
list_for_each_safe(pos, t, &all_tasks) {
p = list_entry(pos, task_union, list);
printl(MPL_TASK_0 + p->pid, " "); // 清掉上一次显示的 '>'
- printl(MPL_TASK_0 + p->pid, "%s%4s:%d [%08x] state %s weight %03d sched %u", next == p ? ">" : " ", p->name,
- p->pid, p, task_state(p->state), p->weight, p->sched_cnt);
+ printl(MPL_TASK_0 + p->pid, "%s%4s:%d [%08x] state %s weight %03d %03d sched %u", next == p ? ">" : " ",
+ p->name, p->pid, p, task_state(p->state), p->weight, p->priority, p->sched_cnt);
}
context_switch(prev, next);
}
int sysc_pause(unsigned long tick) { return 0; }
int sysc_test() {
+#if 0
static unsigned int cnt = 0;
{ // 这一段仅是测试代码
// cnt++, current, cnt);
// printk("systest cnt %u current %08x cnt %u\n",cnt++, current, cnt);
return 0;
+#endif
}
int sysc_debug(unsigned int v) {
printk("kernel[%s] task pid is %d\n", name, pid);
}
+void taskA_entry() {
+ current->priority = 99;
+
+ while (1) {
+ sysc_wait(600);
+
+ for (int i = 0; i < 200; i++) {
+ asm("hlt;");
+ }
+ }
+}
+
+void taskB_entry() {
+ current->priority = 99;
+
+ while (1) {
+ sysc_wait(200);
+
+ for (int i = 0; i < 100; i++) {
+ asm("hlt;");
+ }
+ }
+}
+
+void taskC_entry() {
+ current->priority = 99;
+
+ while (1) {
+ sysc_wait(2);
+
+ for (int i = 0; i < 2; i++) {
+ asm("hlt;");
+ }
+ }
+}
+
// 从multiboot.S进入这里
void root_task_entry() {
sti();
kernel_task("disk", disk_task_entry);
kernel_task("user", user_task_entry);
+ kernel_task("tskA", taskA_entry);
+ kernel_task("tskB", taskB_entry);
+ kernel_task("tskC", taskC_entry);
+
while (1) {
asm("hlt;");
}
wake_up(&debug_wq);
}
+DECLARE_WAIT_QUEUE_HEAD(sysc_wait_queue_head);
+
+#if 1
+extern unsigned int jiffies;
+int sysc_wait(unsigned long cnt) {
+ unsigned long flags;
+ irq_save(flags);
+ current->state = TASK_WAIT;
+ current->delay_cnt = jiffies + cnt;
+ irq_restore(flags);
+
+ schedule();
+}
+#else
int sysc_wait(unsigned long pid) {
task_union *p = find_task(pid);
- if (p == 0) return 0;
+ if (p == 0) {
+ return 0;
+ }
- if (p->state == TASK_EXITING) return 0;
+ if (p->state == TASK_EXITING) {
+ return 0;
+ }
task_union *task = current;
DECLARE_WAIT_QUEUE(wait, task);
return 0;
}
+#endif