X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=bertos%2Fkern%2Fsignal.c;h=2b3a191fe06b7a94194d45e9adc142b511cffcf2;hb=e8b0472be10fba4ca6baa62d8d483db90e28c06e;hp=c21de17875e32d38b582d00824ab87ec8615a1b2;hpb=4cc44c9888a0336b9d01121ec0b7ad95f4a76195;p=bertos.git diff --git a/bertos/kern/signal.c b/bertos/kern/signal.c index c21de178..2b3a191f 100644 --- a/bertos/kern/signal.c +++ b/bertos/kern/signal.c @@ -26,9 +26,8 @@ * invalidate any other reasons why the executable file might be covered by * the GNU General Public License. * - * Copyright 2004 Develer S.r.l. (http://www.develer.com/) + * Copyright 2004, 2008 Develer S.r.l. (http://www.develer.com/) * Copyright 1999, 2000, 2001 Bernie Innocenti - * * --> * * \brief IPC signals implementation. @@ -66,10 +65,38 @@ * particular event has occurred, because the same signal may be * delivered twice before the process can notice. * - * Any execution context, including an interrupt handler, can deliver - * a signal to a process using sig_signal(). Multiple independent signals - * may be delivered at once with a single invocation of sig_signal(), - * although this is rarely useful. + * Signals can be delivered synchronously via sig_send() or asynchronously via + * sig_post(). + * + * In the synchronous case the process is awakened if it was waiting for any + * signal and immediately dispatched for execution via a direct context switch, + * if its priority is greater than the running process. + * + *
+ * - Synchronous-signal delivery:
+ *
+ *     [P1]____sig_send()____proc_wakeup()____[P2]
+ * 
+ * + * In the asynchronous case, the process is scheduled for execution as a + * consequence of the delivery, but it will be dispatched by the scheduler as + * usual, according to the scheduling policy. + * + *
+ * - Asynchronous-signal delivery:
+ *
+ *     [P1]____sig_post()____[P1]____proc_schedule()____[P2]
+ * 
+ * + * In this way, any execution context, including an interrupt handler, can + * deliver a signal to a process. However, synchronous signal delivery from a + * non-sleepable context (like an interrupt handler) is forbidden in order to + * avoid potential deadlock conditions. Instead, sig_post() can be used from + * any context, expecially from interrupt context or when the preemption is + * disabled. + * + * Multiple independent signals may be delivered at once with a single + * invocation of sig_send() or sig_post(), although this is rarely useful. * * \section signal_allocation Signal Allocation * @@ -93,126 +120,143 @@ * - Do not sleep between starting the asynchronous task that will fire * SIG_SINGLE, and the call to sig_wait(). * - Do not call system functions that may implicitly sleep, such as - * timer_delayTickes(). - * - * \version $Id$ + * timer_delayTicks(). * * \author Bernie Innocenti */ #include "signal.h" +#include "cfg/cfg_timer.h" #include -#include +#include + +#include #include #include - -#if CONFIG_KERN_SIGNALS - -/** - * Check if any of the signals in \a sigs has occurred and clear them. - * \return the signals that have occurred. - */ -sigmask_t sig_check(sigmask_t sigs) -{ - sigmask_t result; - cpuflags_t flags; - IRQ_SAVE_DISABLE(flags); - result = CurrentProcess->sig_recv & sigs; - CurrentProcess->sig_recv &= ~sigs; - IRQ_RESTORE(flags); - - return result; -} +#if CONFIG_KERN_SIGNALS +// Check config dependencies +CONFIG_DEPEND(CONFIG_KERN_SIGNALS, CONFIG_KERN); -/** - * Sleep until any of the signals in \a sigs occurs. - * \return the signal(s) that have awoken the process. - */ -sigmask_t sig_wait(sigmask_t sigs) +sigmask_t sig_waitSignal(Signal *s, sigmask_t sigs) { sigmask_t result; - cpuflags_t flags; - IRQ_SAVE_DISABLE(flags); + /* Sleeping with IRQs disabled or preemption forbidden is illegal */ + IRQ_ASSERT_ENABLED(); + ASSERT(proc_preemptAllowed()); + + /* + * This is subtle: there's a race condition where a concurrent process + * or an interrupt may call sig_send()/sig_post() to set a bit in + * Process.sig_recv just after we have checked for it, but before we've + * set Process.sig_wait to let them know we want to be awaken. + * + * In this case, we'd deadlock with the signal bit already set and the + * process never being reinserted into the ready list. + */ + IRQ_DISABLE; /* Loop until we get at least one of the signals */ - while (!(result = CurrentProcess->sig_recv & sigs)) + while (!(result = s->recv & sigs)) { - /* go to sleep and proc_schedule() another process */ - CurrentProcess->sig_wait = sigs; - proc_schedule(); + /* + * Tell "them" that we want to be awaken when any of these + * signals arrives. + */ + s->wait = sigs; - /* When we come back here, a signal must be arrived */ - ASSERT(!CurrentProcess->sig_wait); - ASSERT(CurrentProcess->sig_recv); + /* Go to sleep and proc_switch() to another process. */ + proc_switch(); + /* + * When we come back here, the wait mask must have been + * cleared by someone through sig_send()/sig_post(), and at + * least one of the signals we were expecting must have been + * delivered to us. + */ + ASSERT(!s->wait); + ASSERT(s->recv & sigs); } /* Signals found: clear them and return */ - CurrentProcess->sig_recv &= ~sigs; + s->recv &= ~sigs; - IRQ_RESTORE(flags); + IRQ_ENABLE; return result; } -/** - * Sleep until any of the signals in \a sigs or \a timeout ticks elapse. - * If the timeout elapse a SIG_TIMEOUT is added to the received signal(s). - * \return the signal(s) that have awoken the process. - * \note Caller must check return value to check which signal awoke the process. - */ -sigmask_t sig_waitTimeout(sigmask_t sigs, ticks_t timeout) +#if CONFIG_TIMER_EVENTS + +#include + +sigmask_t sig_waitTimeoutSignal(Signal *s, sigmask_t sigs, ticks_t timeout, + Hook func, iptr_t data) { Timer t; sigmask_t res; - cpuflags_t flags; + cpu_flags_t flags; - ASSERT(!sig_check(SIG_TIMEOUT)); + ASSERT(!sig_checkSignal(s, SIG_TIMEOUT)); ASSERT(!(sigs & SIG_TIMEOUT)); /* IRQ are needed to run timer */ ASSERT(IRQ_ENABLED()); - timer_set_event_signal(&t, proc_current(), SIG_TIMEOUT); + if (func) + timer_setSoftint(&t, func, data); + else + timer_set_event_signal(&t, proc_current(), SIG_TIMEOUT); timer_setDelay(&t, timeout); timer_add(&t); - res = sig_wait(SIG_TIMEOUT | sigs); + res = sig_waitSignal(s, SIG_TIMEOUT | sigs); IRQ_SAVE_DISABLE(flags); /* Remove timer if sigs occur before timer signal */ - if (!(res & SIG_TIMEOUT) && !sig_check(SIG_TIMEOUT)) + if (!(res & SIG_TIMEOUT) && !sig_checkSignal(s, SIG_TIMEOUT)) timer_abort(&t); IRQ_RESTORE(flags); return res; } +#endif // CONFIG_TIMER_EVENTS -/** - * Send the signals \a sigs to the process \a proc. - * The process will be awoken if it was waiting for any of them. - * - * \note This call is interrupt safe. - */ -void sig_signal(Process *proc, sigmask_t sigs) +INLINE void __sig_signal(Signal *s, Process *proc, sigmask_t sigs, bool wakeup) { - cpuflags_t flags; + cpu_flags_t flags; + IRQ_SAVE_DISABLE(flags); /* Set the signals */ - proc->sig_recv |= sigs; + s->recv |= sigs; /* Check if process needs to be awoken */ - if (proc->sig_recv & proc->sig_wait) + if (s->recv & s->wait) { - /* Wake up process and enqueue in ready list */ - proc->sig_wait = 0; - SCHED_ENQUEUE(proc); - } + ASSERT(proc != current_process); + s->wait = 0; + if (wakeup) + proc_wakeup(proc); + else + SCHED_ENQUEUE_HEAD(proc); + } IRQ_RESTORE(flags); } -#endif /* CONFIG_KERN_SIGNALS */ +void sig_sendSignal(Signal *s, Process *proc, sigmask_t sigs) +{ + ASSERT_USER_CONTEXT(); + IRQ_ASSERT_ENABLED(); + ASSERT(proc_preemptAllowed()); + __sig_signal(s, proc, sigs, true); +} + +void sig_postSignal(Signal *s, Process *proc, sigmask_t sigs) +{ + __sig_signal(s, proc, sigs, false); +} + +#endif /* CONFIG_KERN_SIGNALS */