From 5fa95ad728dce710d6cd56985f72ca156d47a8fc Mon Sep 17 00:00:00 2001 From: hinus Date: Fri, 25 Jun 2021 20:24:56 +0800 Subject: [PATCH] Title: sleep_on and wakeup. Issue: https://gitee.com/hinus/linux_kernel_011/issues/I3XPU8 Description: 'sleep_on' and 'wakeup' for process. --- kernel/sched.c | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/kernel/sched.c b/kernel/sched.c index 0320f36..e279768 100644 --- a/kernel/sched.c +++ b/kernel/sched.c @@ -61,7 +61,50 @@ void schedule() { switch_to(next); } -int clock = COUNTER; +static inline void __sleep_on(struct task_struct** p, int state) { + struct task_struct* tmp; + + if (!p) + return; + if (current == &(init_task.task)) + printk("task[0] trying to sleep"); + + tmp = *p; + *p = current; + current->state = state; + +repeat: + schedule(); + + if (*p && *p != current) { + (**p).state = 0; + current->state = TASK_UNINTERRUPTIBLE; + goto repeat; + } + + if (!*p) + printk("Warning: *P = NULL\n\r"); + if (*p = tmp) + tmp->state = 0; +} + +void interruptible_sleep_on(struct task_struct** p) { + __sleep_on(p, TASK_INTERRUPTIBLE); +} + +void sleep_on(struct task_struct** p) { + __sleep_on(p, TASK_UNINTERRUPTIBLE); +} + +void wake_up(struct task_struct **p) { + if (p && *p) { + if ((**p).state == TASK_STOPPED) + printk("wake_up: TASK_STOPPED"); + if ((**p).state == TASK_ZOMBIE) + printk("wake_up: TASK_ZOMBIE"); + (**p).state=0; + } +} void do_timer(long cpl) { if ((--current->counter)>0) return; -- Gitee