Difficulty understanding mutexes and context switching
Posted: Wed Apr 24, 2019 4:00 pm
Ultimately my goal is to send messages from ISRs AND threads to a thread, but in my effort to understand how to do this, I've found myself pretty confused by the mutex example and it seems to indicate there's a lot I don't understand.
First of all, I don't understand the idea of rescheduling in the middle of a critical section. I get that interrupts are re-enabled, but I don't understand the point of the critical section then. Let's suppose I have something like
To reschedule, chCondSignalS needs to re-enable interrupts. Does this not mean any number of threads could have been scheduled between A and B? Does this not destroy the atomicity of the section? When you come back from chCondSignalS, are we still in a critical section? If not, what's the point of calling chSysUnlock()? In the FreeRTOS manual, for comparison, it says "the task that called taskENTER_CRITICAL() is guaranteed to remain in the Running state until the critical section is exited, unless the task explicitly attempts to block or yield (which it should not do from inside a critical section)." Is calling chCondSignalS not yielding?
Next, I also don't really understand the producer/consumer example with ISRs from the mutexes section. Suppose we qConsume with an empty queue. Now that thread blocks... but it blocks from inside a critical section, right? Isn't that bad? I guess this ties in with the previous question. Ultimately what I'd like to know if it's possible to use mutexes and condition variables to implement a qConsume that can be ignorant of whether or not we have qProduced from inside an ISR or not. I maybe also don't understand why exactly qConsume needs to sysLock, too. I'm most curious whether it's possible to produce a message from an ISR context and then defer processing of this message to after the ISR returns so a low priority thread can drain the message queue.
Thanks for the help!
First of all, I don't understand the idea of rescheduling in the middle of a critical section. I get that interrupts are re-enabled, but I don't understand the point of the critical section then. Let's suppose I have something like
Code: Select all
chSysLock()
A()
chCondSignalS(...)
B()
chSysUnlock()
To reschedule, chCondSignalS needs to re-enable interrupts. Does this not mean any number of threads could have been scheduled between A and B? Does this not destroy the atomicity of the section? When you come back from chCondSignalS, are we still in a critical section? If not, what's the point of calling chSysUnlock()? In the FreeRTOS manual, for comparison, it says "the task that called taskENTER_CRITICAL() is guaranteed to remain in the Running state until the critical section is exited, unless the task explicitly attempts to block or yield (which it should not do from inside a critical section)." Is calling chCondSignalS not yielding?
Next, I also don't really understand the producer/consumer example with ISRs from the mutexes section. Suppose we qConsume with an empty queue. Now that thread blocks... but it blocks from inside a critical section, right? Isn't that bad? I guess this ties in with the previous question. Ultimately what I'd like to know if it's possible to use mutexes and condition variables to implement a qConsume that can be ignorant of whether or not we have qProduced from inside an ISR or not. I maybe also don't understand why exactly qConsume needs to sysLock, too. I'm most curious whether it's possible to produce a message from an ISR context and then defer processing of this message to after the ISR returns so a low priority thread can drain the message queue.
Thanks for the help!