In some RT kernels, if you want to post to a semaphore or flag from an ISR, you need to call an special version of the method, instead of the regular one called from a normal task. Examples:
xSemaphoreGiveFromISR()
instead ofxSemaphoreGive()
in FreeRTOSisr_PostSem()
instead ofCoPostSem()
in CoOS.
However there are other kernels where this is not needed. For instance:
OSSemPost()
in uC/OS can be called both from tasks and ISRs.release()
in mbed-rtos, can be called from Threads and ISRs.semGive()
in VxWorks seems to be callable from an ISR according to the docs, and at first glance no special method exists for ISRs.
Why would you need to create special versions? I understand that the posting of a semaphore or flag might cause a blocked task of higher priority to be awaken. And that's why the scheduler is most likely going to run when the ISR exit callback is called (I'm talking about those functions needed to be called when you want to use kernel code inside the ISR, like portEXIT_SWITCHING_ISR()
in FreeRTOS, or OSIntExit
in uC/OS). So why the special post method in addition to the ISRexit call?
Best Answer
I've been reading the source code of both FreeRTOS and CoOS.
In FreeRTOS (the port I found first in Google), the
xSemaphoreGiveFromISR()
function calls toxQueueGenericSendFromISR()
, and this function had this comment:As for CoOS, the function
isr_PostSem
called from an ISR just enqueues a request to the so called "Service request queue", using the functionInsertInSRQ
. I have no idea about what this queue is, probably is some system task to offload work from ISRs and thus let interrupts complete quickly.As for mbed, which only has 1 version of the method, it is based on CMSIS RTOS, and there's a call to the function
osSemaphoreRelease()
. There's a comment about this and similar functions:So it seems to be just kernel design, some designers chose to have separate functions and let the programmer make the distinction, and some others prefered to have just one method and internally check if the call was made from an ISR.