A portable way to determine is current context a task or ISR?

Hello to all Realtime developers!

In my firmware I need to implement a function that uses some FreeRTOS features (e.g. push to queue) and that could be executed either in a task or in ISR. FreeRTOS has distinct API functions for task (xQueueSend) and for ISR (xQueueSendFromISR). So my function needs to determine is it called from a task or from ISR.

There is STM32 CMSIS-RTOS API where API functions are suitable to use both in task in ISR. They use the following function to determine current context:

/* Determine whether we are in thread mode or handler mode. */
static int inHandlerMode (void)
return __get_IPSR() != 0;

but this is obviously not portable and will not work on cores other than Cortex M.

Is there a more portable way to determine is the current context a task or ISR?

No, there is not, because not all MCUs support this detection in the first place.

The closest is probably the function xPortIsInsideInterrupt() which is defined in port macro.h in many of the ports that have that capability.