2 sources that 'give' the same interrupt

biker126 wrote on Friday, September 28, 2007:

hey guys

does anyone know if it is possible to have a semaphore taken by 1 task and given back by 2 different sources?

in the demo’s semaphores are used for synchronisation between 1 task and 1 ISR. but i’d like to synchronize a task with TWO ISR’s.

so my idea was that the task does a
xSemaphoreTake( xSemaphore, WAIT_TIME);   

and ISR1 and ISR2 both do:
xSemaphoreGiveFromISR( xEMACSemaphore, pdFALSE );

but i dont know what happens if the semaphore is currently ‘free’ and the function above is called… (would be nice if there’s a function to check wether the semaphore is in use or not. but afaik there aint such a function).

if there would be event-flags available in freeRTOS i would use that but since there are none i need something different… :wink:

D. Holzwarth

rtel wrote on Friday, September 28, 2007:

There is no problem in giving from two different locations in this manner.

The xSemaphoreGiveFromISR() would have been better constructed if its return told you whether or not the give was successful.

Binary semaphores are just queues with a length of 1.  You could therefore easily write a function/macro to see if the semaphore is currently taken or not - something like the uxQueueMessageWaiting() function, but without the critical section so it can be used from an ISR.  For example (untested code):

unsigned portBASE_TYPE uxGetSemaphoreStatusFromISR( xSemaphoreHandle pxSemaphore )
    return pxSemaphore->uxMessagesWaiting;

If this returns 0 then the semaphore can be given, but not taken, and visa versa.


biker126 wrote on Sunday, September 30, 2007:

its good to know that you can release a semaphore twice :slight_smile:

I solved my current synchronizing problem with a queue tho (instead the semaphore) which is filled by the 2 sources.