Hi Giovanni, thanks for your answer!
Yes, I supposed that the woken thread is the one which performs the unlock. But what if the woken thread never calls chSysUnlock()? For example:
Low priority thread:
Code: Select all
volatile int a;
msg_t ThreadLowPrio(void *p) {
for(;;) {
a++;
}
}
High priority thread (from the Wiki, except for the added chThdSleepMilliseconds):
Code: Select all
static Thread *tp = NULL;
void ThreadHighPrio(void *p) {
while (TRUE) {
msg_t msg;
chThdSleepMilliseconds(100);
/* Waiting for the IRQ to happen.*/
chSysLock();
tp = chThdSelf();
chSchGoSleepS(THD_STATE_SUSPENDED);
msg = chThdSelf()->p_u.rdymsg; /* Retrieving the message, optional.*/
chSysUnlock();
/* Perform processing here.*/
}
}
IRQ (directly from the Wiki):
Code: Select all
CH_IRQ_HANDLER(myIRQ) {
CH_IRQ_PROLOGUE();
/* Wakes up the thread.*/
chSysLockFromIsr();
if (tp != NULL) {
tp->p_u.rdymsg = (msg_t)55; /* Sending the message, optional.*/
chSchReadyI(tp);
tp = NULL;
}
chSysUnlockFromIsr().
CH_IRQ_EPILOGUE();
}
Suppose also that Priority(_idle_thread) < Priority(ThreadLowPrio) < Priority(ThreadHighPrio).
If I understand correctly, the sequence of actions is:
1) ThreadHighPrio goes to sleep for 100ms (chThdSleepMilliseconds).
2) ThreadHighPrio wakes up.
3) ThreadHighPrio goes to sleep (chSchGoSleepS), and the scheduler immediately wakes up the ThreadLowPrio.
4) ThreadLowPrio
never does a chSysUnlock().
5) The interrupt takes place, but the interrupts are still disabled

(forever!).
If the ThreadLowPrio never yields execution (because its priority is higher than the only other thread ready to run - _idle_thread), then the system is locked forever? Even if its timeslot expires, the scheduler will not find a thread ready to run with higher priority...
Am I missing something?
Thanks for your help!
Diego.