From: bernie Date: Thu, 7 Aug 2008 11:05:57 +0000 (+0000) Subject: monitor: Correct preemptive locking X-Git-Tag: 2.0.0~370 X-Git-Url: https://codewiz.org/gitweb?a=commitdiff_plain;h=4108302ecc1a63518e91b3ee0ade5d50d89453b6;p=bertos.git monitor: Correct preemptive locking git-svn-id: https://src.develer.com/svnoss/bertos/trunk@1564 38d2e660-2303-0410-9eaa-f027e97ec537 --- diff --git a/bertos/kern/monitor.c b/bertos/kern/monitor.c index 362bb82d..9dcc38b0 100644 --- a/bertos/kern/monitor.c +++ b/bertos/kern/monitor.c @@ -49,6 +49,7 @@ #include +/* Access to this list must be protected against the scheduler */ static List MonitorProcs; @@ -62,13 +63,13 @@ void monitor_add(Process *proc, const char *name) { proc->monitor.name = name; - ADDTAIL(&MonitorProcs, &proc->monitor.link); + PROC_ATOMIC(ADDTAIL(&MonitorProcs, &proc->monitor.link)); } void monitor_remove(Process *proc) { - REMOVE(&proc->monitor.link); + PROC_ATOMIC(REMOVE(&proc->monitor.link)); } void monitor_rename(Process *proc, const char *name) @@ -76,10 +77,6 @@ void monitor_rename(Process *proc, const char *name) proc->monitor.name = name; } -/* TODO: use containerof() */ -#define MONITOR_NODE_TO_PROCESS(node) \ - (struct Process *)((intptr_t)(node) - offsetof(struct Process, monitor.link)) - size_t monitor_checkStack(cpustack_t *stack_base, size_t stack_size) { cpustack_t *beg; @@ -116,49 +113,47 @@ size_t monitor_checkStack(cpustack_t *stack_base, size_t stack_size) void monitor_report(void) { - struct Process* p; + Node *node; int i; - if (LIST_EMPTY(&MonitorProcs)) - { - kputs("No stacks registered in the monitor\n"); - return; - } - kprintf("%-24s%-8s%-8s%-8s%-8s\n", "Process name", "TCB", "SPbase", "SPsize", "SPfree"); - for (i=0;i<56;i++) + for (i = 0; i<56; i++) kputchar('-'); kputchar('\n'); - for (p = MONITOR_NODE_TO_PROCESS(LIST_HEAD(&MonitorProcs)); - p->monitor.link.succ; - p = MONITOR_NODE_TO_PROCESS(p->monitor.link.succ)) + proc_forbid(); + FOREACH_NODE(node, &MonitorProcs) { + Process *p = containerof(node, Process, monitor.link); size_t free = monitor_checkStack(p->stack_base, p->stack_size); kprintf("%-24s%-8p%-8p%-8lu%-8lu\n", p->monitor.name, p, p->stack_base, p->stack_size, free); } + proc_permit(); } static void NORETURN monitor(void) { - struct Process *p; + Process *p; + Node *node; - while (1) + for (;;) { - for (p = MONITOR_NODE_TO_PROCESS(LIST_HEAD(&MonitorProcs)); - p->monitor.link.succ; - p = MONITOR_NODE_TO_PROCESS(p->monitor.link.succ)) + proc_forbid(); + FOREACH_NODE(node, &MonitorProcs) { + Process *p = containerof(node, Process, monitor.link); size_t free = monitor_checkStack(p->stack_base, p->stack_size); if (free < 0x20) - kprintf("MONITOR: WARNING: Free stack for process '%s' is only %u chars\n", + kprintf("MONITOR: Free stack of process '%s' is only %u chars\n", p->monitor.name, (unsigned int)free); - - timer_delay(500); } + proc_permit(); + + /* Give some rest to the system */ + timer_delay(500); } } diff --git a/bertos/kern/proc_p.h b/bertos/kern/proc_p.h index 7dad780d..d6979410 100644 --- a/bertos/kern/proc_p.h +++ b/bertos/kern/proc_p.h @@ -98,7 +98,11 @@ extern REGISTER Process *CurrentProcess; extern REGISTER List ProcReadyList; -/** Enqueue a task in the ready list. */ +/** + * Enqueue a task in the ready list. + * + * \note This is *NOT* protected against + */ #define SCHED_ENQUEUE(proc) ADDTAIL(&ProcReadyList, &(proc)->link) /** Schedule to another process *without* adding the current to the ready list. */ diff --git a/bertos/kern/proc_test.c b/bertos/kern/proc_test.c index d59bc749..96c4a9bd 100644 --- a/bertos/kern/proc_test.c +++ b/bertos/kern/proc_test.c @@ -45,9 +45,9 @@ /** * Proc scheduling test subthread 1 */ -static void NORETURN proc_test_thread1(void) +static void proc_test_thread1(void) { - for (;;) + for (int i = 0; i < 30; ++i) { kputs(">task 1\n"); timer_delay(50); @@ -58,9 +58,9 @@ static void NORETURN proc_test_thread1(void) /** * Proc scheduling test subthread 2 */ -static void NORETURN proc_test_thread2(void) +static void proc_test_thread2(void) { - for (;;) + for (int i = 0; i < 30; ++i) { kputs(">task 2\n"); timer_delay(75); @@ -72,9 +72,9 @@ static cpustack_t proc_test_stack1[CONFIG_PROC_DEFSTACKSIZE / sizeof(cpustack_t) static cpustack_t proc_test_stack2[CONFIG_PROC_DEFSTACKSIZE / sizeof(cpustack_t)]; /** - * Proc scheduling test + * Process scheduling test */ -void NORETURN proc_test(void) +void proc_test(void) { proc_new(proc_test_thread1, NULL, sizeof(proc_test_stack1), proc_test_stack1); proc_new(proc_test_thread2, NULL, sizeof(proc_test_stack2), proc_test_stack2); @@ -87,12 +87,10 @@ void NORETURN proc_test(void) // #warning FIXME kdump(proc_test_stack2+sizeof(proc_test_stack1)-64, 64); - for (;;) + for (int i = 0; i < 30; ++i) { kputs(">main task\n"); timer_delay(93); proc_switch(); } - - ASSERT(false); }