I have a quick design question. For a rare but time-critical event (for example, a fire sensor detecting a fire and requiring an immediate system power shutdown) in an RTOS-based system.
What would be your preferred approach?
Use an interrupt only to wake a high-priority task, and let the task decide and perform the shutdown?
Or use the interrupt to signal a high-priority task via a semaphore and let that task handle the shutdown?
The question is primarily about interrupt-only vs interrupt + semaphore. I’m aware of the option where the interrupt itself directly performs the action, but that’s not the focus here.
My first thought is you are mixing different types of terms. “Wake” is a generic action, of which using a semaphore is a specific way of implementing it.
“Interrupt-Only” would be the ISR does the actions to perform the shut down, which depending on what is actually needed to be done, might not be a bad choice if “immediate” really means immediate (but it might just mean really quick).
Things get very particular in life safety situations like your fire sensor example and you should look to the different safety standards for guidance. Without looking myself for this example, I would assume that if you are the cause of the fire situation (eg. you are powering a heater assembly or something), you should probably kill it immediately before proceeding further with emergency procedures. This would probably mean turning off the cause of the fire in the irq and then signaling the emergency task.
What I’m really trying to understand is when an interrupt by itself is the best choice versus when the preferred pattern is for the interrupt to signal a task (e.g., via a semaphore) and let the task handle the processing.
For example, if a fire sensor detects a fire:
In one case, the ISR immediately cuts power or trips a safety relay because the action is simple, deterministic, and requires the lowest possible latency. In this case, I’d prefer handling it directly in the interrupt.
In another case, the ISR uses a semaphore to wake a high-priority safety task, and that task evaluates the situation, logs the event, sends alarms, and performs an orderly shutdown sequence. In this case, I’d prefer using interrupt + semaphore
So, my question was really about understanding when interrupt-only is the better choice versus when interrupt + semaphore is the preferred pattern
Your explanation sort of answers your question. Why should you go to the extra effort to somehow notify a task, and switch to it, if you can do it quicker directly in the ISR. ISR might defer operations to tasks that will take more time to do in the ISR, then to do the switching, I’m sorry, but there isn’t a golden rule that says which is better, it is a judgement call at times. Sometimes the operation is quick enough to do in the ISR that this is the clear winner. Sometimes the is part of the processing that will take time, and should be deferred to a task, but even then, you need to decide what parts a quick enough and urgent enough) that they should be done in the ISR.
EVERYTHING comes at a cost, and you need to find the right balance to keep within your budget, and sometimes it is other trades that might affect this trade.
It mostly depends on the system you are controlling, its response time and what happens during the emergency. If the best way is to simply cut power then turning off a GPIO that controls a relay in the ISR is the obvious option. However, if you are controlling a motor via some algorithm and have to spin it down in a controlled way while also sending messages to other systems then task notifications or semaphores make more sense.