From 8adaf0da869a17ba37ab7d5cf4fddbf5d878978f Mon Sep 17 00:00:00 2001 From: dzwdz Date: Wed, 21 Feb 2024 21:08:12 +0100 Subject: kernel: integrate the proc_ns_next fixes into proc_next --- src/kernel/proc.c | 49 ++++++++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 27 deletions(-) (limited to 'src/kernel/proc.c') diff --git a/src/kernel/proc.c b/src/kernel/proc.c index dc17af2..26b9e3a 100644 --- a/src/kernel/proc.c +++ b/src/kernel/proc.c @@ -145,34 +145,26 @@ Proc *proc_ns_byid(Proc *ns, uint32_t id) { return NULL; } +/** Used for iterating over all processes in ns. */ Proc *proc_ns_next(Proc *ns, Proc *p) { - Proc *ret = NULL; - /* see comments in proc_next */ + /* analogous to proc_next */ + assert(ns); + assert(ns->pns == ns); /* is ns the namespace root? */ + assert(p); + assert(proc_ns_contains(ns, p)); - if (!p) goto end; - /* descend into children who own their own namespace, but no further */ if (p->child && proc_ns_contains(ns, p->child)) { - ret = p->child; - goto end; - } - // TODO diverged from proc_next, integrate this fix into it - // also once you do that do regression tests - this behaviour is buggy - if (p == ns) { - /* don't escape the root */ - goto end; + return p->child; } + while (!p->sibling) { + if (p == ns) return NULL; p = p->parent; assert(p); - if (p == ns) goto end; - } - ret = p->sibling; - -end: - if (ret != NULL) { - assert(proc_ns_contains(ns, ret)); + assert(proc_ns_contains(ns, p)); } - return ret; + if (p == ns) return NULL; + return p->sibling; } void proc_ns_create(Proc *proc) { @@ -447,7 +439,7 @@ _Noreturn void proc_switch_any(void) { if (proc_cur && proc_cur->state == PS_RUNNING) { p = proc_cur; } else { - for (p = proc_first; p; p = proc_next(p, NULL)) { + for (p = proc_first; p; p = proc_next(proc_first, p)) { if (p->state == PS_RUNNING) { break; } @@ -468,24 +460,27 @@ _Noreturn void proc_switch_any(void) { } } -Proc *proc_next(Proc *p, Proc *root) { - /* depth-first search, the order is: +/** Used for iterating over all descendants of root. */ +Proc *proc_next(Proc *root, Proc *p) { + /* depth-first traversal, in this order: * 1 * / \ * 2 5 * /| |\ * 3 4 6 7 */ - if (!p) return NULL; + assert(root); + assert(p); + if (p->child) return p->child; - if (p->sibling) return p->sibling; /* looking at the diagram above - we're at 4, want to find 5 */ while (!p->sibling) { + if (p == root) return NULL; p = p->parent; - if (root) assert(p); - if (!p || p == root) return NULL; + assert(p); } + if (p == root) return NULL; return p->sibling; } -- cgit v1.2.3