diff options
author | dzwdz | 2024-02-21 21:08:12 +0100 |
---|---|---|
committer | dzwdz | 2024-02-21 21:08:12 +0100 |
commit | 8adaf0da869a17ba37ab7d5cf4fddbf5d878978f (patch) | |
tree | f18336efbb97c7016a170722b84c6838f7cab8ba /src/kernel/proc.c | |
parent | 4b8f771d6ec926f95ec017c427348013d4c1872c (diff) |
kernel: integrate the proc_ns_next fixes into proc_next
Diffstat (limited to 'src/kernel/proc.c')
-rw-r--r-- | src/kernel/proc.c | 49 |
1 files changed, 22 insertions, 27 deletions
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; } |