9os

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 328b8b2aedacb93ce99b470b6cef48902658b238
parent 333e833cab42cde776b250263306fc19edb22e6f
Author: Roberto E. Vargas Caballero <roberto.vargas@midokura.com>
Date:   Wed, 16 Nov 2022 10:33:23 +0100

os9: Merge sched.c into proc.c

Sched() is simplified and it does not need anymore a individual
file for it. Also, it moves forward to remove the process bits
that are not needed anymore.

Diffstat:
Minclude/os9/os9.h | 30++++++++----------------------
Msrc/os9/Makefile | 1-
Msrc/os9/dev/devproc.c | 2+-
Msrc/os9/proc.c | 61+++++++++++++++++++++++++++++++++++++++++++++++++++----------
Dsrc/os9/sched.c | 74--------------------------------------------------------------------------
5 files changed, 60 insertions(+), 108 deletions(-)

diff --git a/include/os9/os9.h b/include/os9/os9.h @@ -41,7 +41,6 @@ typedef struct ptable Ptable; typedef struct context Context; -typedef struct proc Proc; typedef struct task Task; typedef struct win Win; typedef struct chan Chan; @@ -80,26 +79,17 @@ enum pstates { PSYSTEM = 1 << 0, }; -enum pmodes { - PDISABLED = 'D', - PCOLD_START = 'C', - PWARM_START = 'W', - PIDLE = 'I', - PNORMAL = 'N', - PFREE = 'F', -}; - enum tflags { TLOCK_PREEMP = 1 << 0, - TENABLED = 1 << 1, }; -enum tstates { +enum tmode { TDORMANT, TREADY, TRUNNING, TWAITING, - FAULTED, + TFAULTED, + TFREE, }; enum start_reason { @@ -190,10 +180,9 @@ struct mpoint { * @capacity: * @deadline: * @deadtime: - * @state: * @flags: - * @mode: Denotes process's execution state. - * @start: Denotes the reason the process is started. + * @state: + * @start: Denotes the reason the task is started. * @ctx: * @ptable: * @text: @@ -226,10 +215,8 @@ struct task { long deadline; long deadtime; - int state; int flags; - - atomic_t mode; + int state; int start; mutex_t m; @@ -267,6 +254,7 @@ extern Task *gettask(int); extern int mapseg(Map *); extern Task *kproc(void *); extern Map *newstack(Task *); +extern void sched(void); /* alloc.c */ extern void *alloc(size_t); @@ -312,7 +300,6 @@ extern int devread(Chan *c, void *buf, int n); extern int devclose(Chan *c); /* sched.c */ -extern void sched(void); /* globals */ extern Chan *console; @@ -320,7 +307,6 @@ extern Mach mach; extern char buffertab[NR_BUFFERS][PAGESIZE]; /* per cpu globals */ -extern Proc *proc; -extern Task *task; +extern Task *proc; extern int cpuid; extern long long now; diff --git a/src/os9/Makefile b/src/os9/Makefile @@ -12,7 +12,6 @@ OBJS =\ alloc.o\ dlang.o\ proc.o\ - sched.o\ sys.o\ dev/builtin.o\ diff --git a/src/os9/dev/devproc.c b/src/os9/dev/devproc.c @@ -124,7 +124,7 @@ taskstatusread(Chan *c, void *buf, int n) [TREADY] = "ready", [TRUNNING] = "running", [TWAITING] = "waiting", - [FAULTED] = "faulted", + [TFAULTED] = "faulted", }; if ((tp = gettask(TASK(c->qid))) == NULL) diff --git a/src/os9/proc.c b/src/os9/proc.c @@ -20,9 +20,15 @@ struct fdset { static Task tasktab[NR_TASKS]; static mutex_t procm; +/* per cpu globals */ +Task *proc; +long long now; +int cpuid; + static void initfn(void) { + debug(); } void @@ -32,7 +38,7 @@ iproc(void) for (tp = tasktab; tp < &tasktab[NR_TASKS]; ++tp) { tp->tid = tp->ptid = -1; - tp->mode = PFREE; + tp->state = TFREE; } if ((tp = kproc(initfn)) == NULL) @@ -79,7 +85,7 @@ getntask(int n, Task **tpp) tp = &tasktab[n]; lock(&tp->m); - if (tp->mode == PDISABLED || tp->mode == PFREE) { + if (tp->state == TFREE) { unlock(&tp->m); return 0; } @@ -97,9 +103,7 @@ gettask(int tid) lock(&procm); for (p = tasktab; p < &tasktab[NR_TASKS]; ++p) { lock(&p->m); - if (p->tid == tid - && p->mode != PDISABLED - && p->mode != PFREE) { + if (p->tid == tid && p->state != TFREE) { tp = p; break; } @@ -178,8 +182,8 @@ newslot(void) lock(&procm); for (tp = tasktab; tp < &tasktab[NR_TASKS]; tp++) { lock(&tp->m); - if (tp->mode == PFREE) { - tp->mode = PDISABLED; + if (tp->state == TFREE) { + tp->state = TWAITING; break; } unlock(&tp->m); @@ -347,7 +351,7 @@ rfork(int flags) return -1; } - tp = parent = task; + tp = parent = proc; ns = NULL; fds = NULL; data = NULL; @@ -450,10 +454,9 @@ kproc(void *fn) tp->deadline = 0; tp->deadtime = 0; - tp->state = 0; tp->flags = 0; - tp->mode = PNORMAL; + tp->state = TRUNNING; tp->start = SKERNEL; if ((tp->kstack = allocb(1)) == NULL) @@ -468,3 +471,41 @@ err: deltask(tp); return NULL; } + +void +sched(void) +{ + Task *new, *tp; + int prio; + long long wait, mask; + + lock(&procm); + new = proc; + if (proc->state == TRUNNING && proc->flags&TLOCK_PREEMP) + goto found; + + prio = -1; + wait = -1; + mask = 1ul << cpuid; + for (tp = tasktab; tp < &tasktab[NR_TASKS]; ++tp) { + lock(&tp->m); + if (tp->state == TREADY + && (tp->affinity & mask) != 0 + && tp->prio >= prio + && (tp->prio != prio || new->wait > wait)) { + new = tp; + prio = tp->prio; + wait = tp->wait; + } + } + +found: + if (proc->state == TRUNNING) + proc->state = TREADY; + proc->wait = now; + proc = new; + proc->state = TRUNNING; + unlock(&procm); + + swtch(&proc->ctx); +} diff --git a/src/os9/sched.c b/src/os9/sched.c @@ -1,74 +0,0 @@ -#include <os9/os9.h> - -#include <errno.h> - -static Win wintab[NR_WINS]; - -/* per cpu globals */ -Task *task; -long long now; -int cpuid; - -#if 0 -void -sched(void) -{ - Task *new, *tp, **p, **tasks; - int prio; - long long wait, mask; - static mutex_t m; - - lock(&m); - new = proc->errhdl; - if (new && new->state == READY) - goto found; - - new = task; - if (task->mode == RUNNING && task->flags&LOCK_PREEMP) - goto found; - - tasks = proc->tasks; - prio = -1; - wait = -1; - mask = 1ul << cpuid; - for (p = tasks; p < &tasks[NR_TASKS]; ++p) { - tp = p; - - if (tp->mode != READY - || (tp->affinity & mask) == 0 - || tp->prio < prio - || tp->prio == prio && new->wait <= wait) { - continue; - } - - new = tp; - prio = tp->prio; - wait = tp->wait; - } - -found: - if (cur->mode == RUNNIG) - cur->mode = READY; - cur->wait = now; - cur = new; - cur->mode = RUNNING; - unlock(&m); - - swtch(&cur->ctx); -} - -void -minframe(void) -{ - static Win *wp = &wintab[0]; - - if (++wp == &wintab[nr_wins]) - wp = wintab; - - proc = wp->proc; - cur = idle; - mmap(proc); - - sched(); -} -#endif