I’m really new to RTOS programming and with limited knowledge about the internal operations. Right now I’m struggling with semaphores. I understand that the a semaphore taken by a task cannot be given by another task. I think that means also that different tasks can use the same semaphore as long as it is used inside the task. The question I have is: does that apply to a semaphore taken from a non-task, i.e. for example a timer call-back?? Can a semaphore taken by a timer callback be given from a task?? Are there other similar situations??
First, the restriction of the give needing to be from the same task as the take is for MUTEXES, not just general semaphores. The Mutex API definition needs to know the “owner” of a taken Mutex to support the Priority Inheritance operation. This make Mutexes prefered for mutual exclusion (and thus their name) while Semaphores are more often used for general syncronization between tasks.
Next, Timer-Callbacks are run as part of a task, the Timer Service task, so they can use Semaphores just fine, but Timer-Callbacks generally should not block, so can’t wait on a semaphore, but they could check if it is free (and then take it) with a 0 block time. This is because if a Timer-Callback blocks, it block the whole Timer task, which can often cause problems.
The one non-task entity that exists are ISRs, and they can use the FromISR routines on semaphores, and none of these allow blocking, but can try to take if the semaphore is available.