Page 1 of 1
ISR waking thread question
Posted: Wed Oct 17, 2012 4:12 am
by phaist
First off I'm new to the RTOS world so forgive me if the answer to this question should be obvious.
I'm looking at the example of an ISR waking up a sleeping thread here:
http://chibios.org/dokuwiki/doku.php?id=chibios:howtos:wakeupThe thread calls chSysLock() before going to sleep, where it waits for the ISR to wake it. My understanding of chSysLock() is that it disables interrupts. If that's the case, how can the ISR be serviced and wake up the sleeping thread?
Just trying to understand what's going on. Is there a book/site that explains these concepts that you'd recommend?
Also, regardless of my understanding of RTOS concepts, this code is very easy to work with! Thanks for the hard work Giovanni!
Re: ISR waking thread question
Posted: Wed Oct 17, 2012 7:48 am
by Giovanni
Hi,
This is tricky code:
Code: Select all
void mythread(void *p) {
while (TRUE) {
msg_t msg;
/* 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.*/
}
}
It is true that chSysLock() disables interrupts but then chSchGoSleepS() is called that causes a context switch, the current thread is put to sleep and another one begins to execute and immediately re-enables interrupts. Keep in mind that execution never really stops, if a thread goes into a sleep state another one somewhere immediately restarts, there is also an "idle" thread that is executed when none of the user threads are ready to run.
The reason for the chSysLock()/chSysUnlock() block is to make what is inside atomic respect to interrupt sources. Note that chSchGoSleepS() is an "S-class" function, those function must be called within lock/unlock, the part of the documentation explaining this is the section "Kernel Concepts" inside the various kernel manuals.
Don't make the RTOS thing stop you, some things can be not obvious initially, if in doubt just ask.
Giovanni
Re: ISR waking thread question
Posted: Sat Oct 20, 2012 5:28 pm
by phaist
Makes sense.
Is there a way to know if a call will enable interrupts? Also, when the thread starts execution again are the interrupts disabled again?
Thanks for your help.
Re: ISR waking thread question
Posted: Sat Oct 20, 2012 6:40 pm
by Giovanni
Hi,
Context switch is always performed with interrupts disabled, interrupts are always immediately re-enabled on context switch exit. There is no function keeping interrupts disabled on exit except chSysLock().
You just have to keep stuff between chSysLock() and chSysUnlock() as short as possible regardless of what is being called inside.
Giovanni
Re: ISR waking thread question
Posted: Sat Oct 20, 2012 6:55 pm
by phaist
Thanks again

I'm sure I'll have other questions later, but for now I can continue working.