* 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 Kernel configuration parameters
#include "cfg/cfg_arch.h" /* ARCH_EMUL */
/**
- * Multithreading kernel.
+ * Enable the multithreading kernel.
*/
#define CONFIG_KERNEL 1
/**
- * \name Modules activation
+ * \name Optional kernel features
*
* \{
*/
/*\}*/
/* EXPERIMENTAL */
-#define CONFIG_KERN_PREEMPTIVE (0 && CONFIG_KERN_SCHED && CONFIG_KERN_TIMER)
+#define CONFIG_KERN_PREEMPT (1 && CONFIG_KERN_SCHED && CONFIG_KERN_TIMER)
+
+/* OBSOLETE */
+#define CONFIG_KERN_PREEMPTIVE CONFIG_KERN_PREEMPT
/// Time sharing quantum in timer ticks.
#define CONFIG_KERN_QUANTUM 50
bertos/mware/observer.c \
bertos/mware/resource.c \
bertos/mware/sprintf.c \
- bertos/kern/coop.c \
bertos/kern/proc.c \
bertos/kern/proc_test.c \
+ bertos/kern/preempt.c \
bertos/kern/sem.c \
bertos/kern/signal.c \
bertos/kern/monitor.c \
#include "cfg/cfg_arch.h" /* ARCH_EMUL */
/**
- * Multithreading kernel.
+ * Enable the multithreading kernel.
*/
#define CONFIG_KERNEL 0
/**
- * \name Modules activation
+ * \name Optional kernel features
*
* \{
*/
/*\}*/
/* EXPERIMENTAL */
-#define CONFIG_KERN_PREEMPTIVE (0 && CONFIG_KERN_SCHED && CONFIG_KERN_TIMER)
+#define CONFIG_KERN_PREEMPT (0 && CONFIG_KERN_SCHED && CONFIG_KERN_TIMER)
+
+/* OBSOLETE */
+#define CONFIG_KERN_PREEMPTIVE CONFIG_KERN_PREEMPT
/// Time sharing quantum in timer ticks.
#define CONFIG_KERN_QUANTUM 50
#endif
#if CONFIG_KERNEL
- #if CONFIG_KERN_PREEMPTIVE
- #include <hw/switch.h>
- #endif
#if CONFIG_KERN_SIGNALS
#include <kern/signal.h> /* sig_wait(), sig_check() */
#include <kern/proc.h> /* proc_current() */
#include "proc_p.h"
#include "proc.h"
+#include <cpu/frame.h> // CPU_IDLE
+
+#include <unistd.h> // XXX alarm()
+
/*
* The time sharing scheduler forces a task switch when the current
/* No need to protect against interrupts here. */
--CurrentProcess->forbid_cnt;
}
+
+static void (*irq_handlers[100])(void); // FIXME
+
+
+void proc_preempt(void)
+{
+ TRACE;
+
+ ATOMIC(LIST_ASSERT_VALID(&ProcReadyList));
+
+ IRQ_DISABLE;
+ /* Poll on the ready queue for the first ready process */
+ while (!(CurrentProcess = (struct Process *)list_remHead(&ProcReadyList)))
+ {
+ /*
+ * Make sure we physically reenable interrupts here, no matter what
+ * the current task status is. This is important because if we
+ * are idle-spinning, we must allow interrupts, otherwise no
+ * process will ever wake up.
+ *
+ * During idle-spinning, an interrupt can occur and it may
+ * modify \p ProcReadyList. To ensure that compiler reload this
+ * variable every while cycle we call CPU_MEMORY_BARRIER.
+ * The memory barrier ensure that all variables used in this context
+ * are reloaded.
+ */
+ IRQ_ENABLE;
+ CPU_IDLE;
+ MEMORY_BARRIER;
+ IRQ_DISABLE;
+ }
+ IRQ_ENABLE;
+}
+
+void proc_preempt_timer(void)
+{
+ // TODO: check Quantum
+
+ alarm(1);
+ ATOMIC(SCHED_ENQUEUE(CurrentProcess));
+ proc_schedule();
+}
+
+void proc_schedule(void)
+{
+ kill(0, SIGUSR1);
+}
+
+void proc_yield(void)
+{
+ ATOMIC(SCHED_ENQUEUE(CurrentProcess));
+
+ proc_schedule();
+}
+
+/* signal handler */
+void irq_entry(int signum)
+{
+ Process *old_process;
+
+ TRACEMSG("storing %p:%s", CurrentProcess, CurrentProcess->monitor.name);
+ CurrentProcess->leaving = false;
+ getcontext(&CurrentProcess->context);
+ /* We get here in two ways: directly, and after setcontext() below */
+
+ if (CurrentProcess->leaving)
+ {
+ TRACEMSG("leaving to %p:%s", CurrentProcess, CurrentProcess->monitor.name);
+ return;
+ }
+
+ old_process = CurrentProcess;
+
+ irq_handlers[signum]();
+
+ if (old_process != CurrentProcess)
+ {
+ TRACEMSG("launching %p:%s", CurrentProcess, CurrentProcess->monitor.name);
+ CurrentProcess->leaving = true;
+ setcontext(&CurrentProcess->context);
+ /* not reached */
+ }
+
+ TRACEMSG("keeping %p:%s", CurrentProcess, CurrentProcess->monitor.name);
+}
+
+void preempt_init(void)
+{
+ struct sigaction act;
+ act.sa_handler = irq_entry;
+ sigemptyset(&act.sa_mask);
+ sigaddset(&act.sa_mask, SIGUSR1);
+ sigaddset(&act.sa_mask, SIGALRM);
+ act.sa_flags = SA_RESTART; /* | SA_SIGINFO; */
+
+ irq_handlers[SIGUSR1] = proc_preempt;
+ irq_handlers[SIGALRM] = proc_preempt_timer;
+ sigaction(SIGUSR1, &act, NULL);
+ sigaction(SIGALRM, &act, NULL);
+
+ alarm(1);
+}
* 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.
#include "proc.h"
#include "cfg/cfg_arch.h" /* ARCH_EMUL */
+#include "cfg/cfg_kern.h"
#include <cfg/module.h>
#include <cpu/irq.h>
#include <string.h> /* memset() */
+#if CONFIG_KERN_PREEMPT
+#include "preempt.h"
+#endif
+
/*
* The scheduer tracks ready processes by enqueuing them in the
* ready list.
monitor_add(CurrentProcess, "main");
#endif
+#if CONFIG_KERN_PREEMPTIVE
+ preempt_init();
+#endif
+
MOD_INIT(proc);
}
#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 */
#include <struct/list.h>
+#if CONFIG_KERN_PREEMPTIVE
+ #include <ucontext.h> // XXX
+#endif
+
typedef struct Process
{
Node link; /**< Link Process into scheduler lists */
#if CONFIG_KERN_PREEMPTIVE
int forbid_cnt; /**< Nesting count for proc_forbid()/proc_permit(). */
+ bool leaving; /**< XXX: maybe global? */
+ ucontext_t context;
#endif
#if CONFIG_KERN_HEAP