timer: Amend previous commit (and Subversion sucks)
[bertos.git] / bertos / kern / proc.c
index 9aa04546c35eb02e5ee425f02914a5c7839a68cb..3f2548923dbc1527660cdf3138a091c285f00aee 100644 (file)
@@ -26,9 +26,8 @@
  * invalidate any other reasons why the executable file might be covered by
  * the GNU General Public License.
  *
- * Copyright 2001,2004 Develer S.r.l. (http://www.develer.com/)
- * Copyright 1999,2000,2001 Bernie Innocenti <bernie@codewiz.org>
- *
+ * Copyright 2001, 2004 Develer S.r.l. (http://www.develer.com/)
+ * Copyright 1999, 2000, 2001, 2008 Bernie Innocenti <bernie@codewiz.org>
  * -->
  *
  * \brief Simple realtime multitasking scheduler.
@@ -43,6 +42,7 @@
 #include "proc.h"
 
 #include "cfg/cfg_arch.h"  /* ARCH_EMUL */
+#include "cfg/cfg_kern.h"
 #include <cfg/module.h>
 
 #include <cpu/irq.h>
@@ -52,6 +52,7 @@
 
 #include <string.h>           /* memset() */
 
+
 /*
  * The scheduer tracks ready processes by enqueuing them in the
  * ready list.
@@ -117,6 +118,10 @@ void proc_init(void)
        monitor_add(CurrentProcess, "main");
 #endif
 
+#if CONFIG_KERN_PREEMPTIVE
+       preempt_init();
+#endif
+
        MOD_INIT(proc);
 }
 
@@ -164,8 +169,7 @@ struct Process *proc_new_with_name(UNUSED(const char *, name), void (*entry)(voi
 
 #if CONFIG_KERN_MONITOR
        /* Fill-in the stack with a special marker to help debugging */
-#warning size incorrect
-       memset(stack_base, CONFIG_KERN_STACKFILLCODE, stack_size / sizeof(cpustack_t));
+       memset(stack_base, CONFIG_KERN_STACKFILLCODE, stack_size);
 #endif
 
        /* Initialize the process control block */
@@ -196,6 +200,15 @@ struct Process *proc_new_with_name(UNUSED(const char *, name), void (*entry)(voi
        #endif
 #endif
 
+#if CONFIG_KERN_PREEMPT
+       // FIXME: proc_exit
+       getcontext(&proc->context);
+       proc->context.uc_stack.ss_sp = stack_base;
+       proc->context.uc_stack.ss_size = stack_size;
+       proc->context.uc_link = NULL;
+       makecontext(&proc->context, (void (*)(void))proc_entry, 1, entry);
+
+#else // !CONFIG_KERN_PREEMPT
        /* Initialize process stack frame */
        CPU_PUSH_CALL_FRAME(proc->stack, proc_exit);
        CPU_PUSH_CALL_FRAME(proc->stack, entry);
@@ -207,6 +220,7 @@ struct Process *proc_new_with_name(UNUSED(const char *, name), void (*entry)(voi
        /* Add to ready list */
        ATOMIC(SCHED_ENQUEUE(proc));
        ATOMIC(LIST_ASSERT_VALID(&ProcReadyList));
+#endif // CONFIG_KERN_PREEMPT
 
 #if CONFIG_KERN_MONITOR
        monitor_add(proc, name);
@@ -231,7 +245,7 @@ void proc_rename(struct Process *proc, const char *name)
  */
 void proc_exit(void)
 {
-       TRACE;
+       TRACEMSG("%p:%s", CurrentProcess, CurrentProcess->monitor.name);
 
 #if CONFIG_KERN_MONITOR
        monitor_remove(CurrentProcess);