Difficulty understanding mutexes and context switching

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
ezalys
Posts: 3
Joined: Wed Apr 24, 2019 3:29 pm
Been thanked: 1 time

Difficulty understanding mutexes and context switching

Post by ezalys »

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

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!
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Difficulty understanding mutexes and context switching

Post by Giovanni »

Hi,

S-functions reschedule internally, context switch always happens in critical sections in ChibiOS.

chCondSignalS needs to re-enable interrupts


This is your wrong assumption, it does not need that. Try single stepping inside an S function in asm.

Giovanni
ezalys
Posts: 3
Joined: Wed Apr 24, 2019 3:29 pm
Been thanked: 1 time

Re: Difficulty understanding mutexes and context switching

Post by ezalys »

Okay after seeing that many non-S-class functions call the S-class variant I think I understand a bit better. Let me maybe ask about another example... Suppose I have a timing-sensitive ISR and I want to log a message through USB. The ISR stuffs a message in a mailbox that a USB-handling thread is waiting on. Is the USB-handling thread (waiting on the mailbox) guaranteed to now wake up and start doing its thing? Once this thread blocks on something and reschedules, under what condition do I end up back in the original ISR?
ezalys
Posts: 3
Joined: Wed Apr 24, 2019 3:29 pm
Been thanked: 1 time

Re: Difficulty understanding mutexes and context switching

Post by ezalys »

Well okay, chCondSignalI is documented as saying that it doesn't reschedule and that ISRs reschedule on exit, answering my question. This would have also been obvious from reading the code. This codebase really is top-notch.
Post Reply