# How to make a timercallback function release a semaphore

**URL:** <https://forums.freertos.org/t/how-to-make-a-timercallback-function-release-a-semaphore/12986>\
**Category:** Kernel\
**Created:** [July 3, 2021, 8:51pm UTC](https://forums.freertos.org/t/how-to-make-a-timercallback-function-release-a-semaphore/12986 "2021-07-03T20:51:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Naderatef11](https://avatars.discourse-cdn.com/v4/letter/n/53a042/32.png) [@Naderatef11](https://forums.freertos.org/u/Naderatef11)\
**Post date:** [July 3, 2021, 8:51pm UTC](https://forums.freertos.org/t/how-to-make-a-timercallback-function-release-a-semaphore/12986/1 "2021-07-03T20:51:35Z")

</div>

I need to create a timer that when it is fired, the callback function release a specific semaphore that is blocking another task, how can I make the semaphore already taken, specially because the task will try to take the semaphore first

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [July 3, 2021, 9:23pm UTC](https://forums.freertos.org/t/how-to-make-a-timercallback-function-release-a-semaphore/12986/2 "2021-07-03T21:23:45Z")

</div>

First, after you create the semaphore, just first do a take (maybe with a 0 block time) to make sure it starts taken as part of your initialization routine.

Then your timer call back just needs to call the semaphore give function. If you want to make a general callback, you could even create it with the semaphore handle stored as the timer ID, so the callback could get the ID to get the handle for the semaphore to give.

---

<div class="post-metadata">

**Author:** ![Naderatef11](https://avatars.discourse-cdn.com/v4/letter/n/53a042/32.png) [@Naderatef11](https://forums.freertos.org/u/Naderatef11)\
**Post date:** [July 3, 2021, 9:43pm UTC](https://forums.freertos.org/t/how-to-make-a-timercallback-function-release-a-semaphore/12986/3 "2021-07-03T21:43:12Z")

</div>

i created the semaphore as global, if i did a take as global also, in the timercallback is it even possible to do the give?, I think it will produce a deadlock

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [July 3, 2021, 10:16pm UTC](https://forums.freertos.org/t/how-to-make-a-timercallback-function-release-a-semaphore/12986/4 "2021-07-03T22:16:31Z")

</div>

Right after the statement to create the semaphore, add the take (In C, you can make the handle a global, but you need some code to actually create the semaphore, so you put the take right after you create it). Note, if you use xSemaphoreCreateBinary to make the semaphore, it starts out as taken, so you don’t need to do the take. That is one reason I said use a 0 timeout, that way if the semaphore was created take, it doesn’t cause the block to happen.

Also note, xSemaphoreGive NEVER blocks, so can’t itself create a deadlock. Just as the take with a 0 block time just makes sure it is in the taken state without the ability to create a deadlock.

---

<div class="post-metadata">

**Author:** ![RAc](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rac/32/1389_2.png) [@RAc](https://forums.freertos.org/u/RAc)\
**Post date:** [July 4, 2021, 5:54am UTC](https://forums.freertos.org/t/how-to-make-a-timercallback-function-release-a-semaphore/12986/5 "2021-07-04T05:54:16Z")

</div>

> [@Naderatef11](#):
>
> i created the semaphore as global, if i did a take as global also, in the timercallback is it even possible to do the give?, I think it will produce a deadlock

You MAY be under the impression here that a semaphore is something like a mutex that tracks ownership or is under other restrictions to be “given.” It’s not. A semaphore can be released by any piece of code once it has been created (of course under the usual restrictions, that is, ISRs must use the …FromISR version of the API call and must not run on a higher pri than MAX\_Syscall), and it can be released as many times as one wishes, even if it is already fully released. You may get an error return if you try to do that, but that’s all that happens.

If you look at one of the standard applications of a semaphore usage - an ISR signalling reception of data on the ISR level for a task to do deferred input processing later - you’ll see that the ISR signalling the semaphore never bothers to see what the state of the signal is. It simply submits the signal via a give for the processor task to wait on.
