proc: Use a global forbid count;
[bertos.git] / bertos / kern / proc.c
index bce8ac3e8a7fcadfd4eaf4d7073c659bf0813ff6..4b8f3f65a799e0d590b8c7fb03d17440b49c1ecc 100644 (file)
@@ -52,9 +52,6 @@
 
 #include <string.h>           /* memset() */
 
-#if CONFIG_KERN_PREEMPT
-#include "preempt.h"
-#endif
 
 /*
  * The scheduer tracks ready processes by enqueuing them in the
@@ -93,10 +90,6 @@ static void proc_init_struct(Process *proc)
        proc->sig_recv = 0;
 #endif
 
-#if CONFIG_KERN_PREEMPTIVE
-       proc->forbid_cnt = 0;
-#endif
-
 #if CONFIG_KERN_HEAP
        proc->flags = 0;
 #endif
@@ -121,7 +114,7 @@ void proc_init(void)
        monitor_add(CurrentProcess, "main");
 #endif
 
-#if CONFIG_KERN_PREEMPTIVE
+#if CONFIG_KERN_PREEMPT
        preempt_init();
 #endif
 
@@ -138,7 +131,6 @@ void proc_init(void)
 struct Process *proc_new_with_name(UNUSED(const char *, name), void (*entry)(void), iptr_t data, size_t stack_size, cpustack_t *stack_base)
 {
        Process *proc;
-       size_t i;
        const size_t PROC_SIZE_WORDS = ROUND2(sizeof(Process), sizeof(cpustack_t)) / sizeof(cpustack_t);
 #if CONFIG_KERN_HEAP
        bool free_stack = false;
@@ -203,26 +195,60 @@ struct Process *proc_new_with_name(UNUSED(const char *, name), void (*entry)(voi
        #endif
 #endif
 
-       /* Initialize process stack frame */
-       CPU_PUSH_CALL_FRAME(proc->stack, proc_exit);
-       CPU_PUSH_CALL_FRAME(proc->stack, entry);
+       #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
+       {
+               size_t i;
+
+               /* Initialize process stack frame */
+               CPU_PUSH_CALL_FRAME(proc->stack, proc_exit);
+               CPU_PUSH_CALL_FRAME(proc->stack, entry);
 
-       /* Push a clean set of CPU registers for asm_switch_context() */
-       for (i = 0; i < CPU_SAVED_REGS_CNT; i++)
-               CPU_PUSH_WORD(proc->stack, CPU_REG_INIT_VALUE(i));
+               /* Push a clean set of CPU registers for asm_switch_context() */
+               for (i = 0; i < CPU_SAVED_REGS_CNT; i++)
+                       CPU_PUSH_WORD(proc->stack, CPU_REG_INIT_VALUE(i));
+       }
+       #endif // CONFIG_KERN_PREEMPT
 
        /* Add to ready list */
        ATOMIC(SCHED_ENQUEUE(proc));
-       ATOMIC(LIST_ASSERT_VALID(&ProcReadyList));
 
-#if CONFIG_KERN_MONITOR
-       monitor_add(proc, name);
-#endif
+       #if CONFIG_KERN_MONITOR
+               monitor_add(proc, name);
+       #endif
 
        return proc;
 }
 
-/** Rename a process */
+/**
+ * Return the name of the specified process.
+ *
+ * NULL is a legal argument and will return the name "<NULL>".
+ */
+const char *proc_name(struct Process *proc)
+{
+       #if CONFIG_KERN_MONITOR
+               return proc ? proc->monitor.name : "<NULL>";
+       #else
+               (void)proc;
+               return "---";
+       #endif
+}
+
+/// Return the name of the currently running process
+const char *proc_currentName(void)
+{
+       return proc_name(proc_current());
+}
+
+/// Rename a process
 void proc_rename(struct Process *proc, const char *name)
 {
 #if CONFIG_KERN_MONITOR
@@ -232,13 +258,12 @@ void proc_rename(struct Process *proc, const char *name)
 #endif
 }
 
-
 /**
  * Terminate the current process
  */
 void proc_exit(void)
 {
-       TRACE;
+       TRACEMSG("%p:%s", CurrentProcess, proc_currentName());
 
 #if CONFIG_KERN_MONITOR
        monitor_remove(CurrentProcess);
@@ -287,7 +312,7 @@ struct Process *proc_current(void)
 /**
  * Get the pointer to the user data of the current process
  */
-iptr_t proc_current_user_data(void)
+iptr_t proc_currentUserData(void)
 {
        return CurrentProcess->user_data;
 }