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:wakeup
The 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!
ISR waking thread question
Moderators: RoccoMarco, lbednarz, tfAteba
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: ISR waking thread question
Hi,
This is tricky code:
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
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
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.
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.
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: ISR waking thread question
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
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
Thanks again
I'm sure I'll have other questions later, but for now I can continue working.