# Why can taskA release the mutex semaphore successfully that has already been acquired by taskB?

**URL:** <https://forums.freertos.org/t/why-can-taska-release-the-mutex-semaphore-successfully-that-has-already-been-acquired-by-taskb/18668>\
**Category:** Kernel\
**Tags:** debug, tutorial\
**Created:** [December 6, 2023, 3:04pm UTC](https://forums.freertos.org/t/why-can-taska-release-the-mutex-semaphore-successfully-that-has-already-been-acquired-by-taskb/18668 "2023-12-06T15:04:12Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![perlman-fun](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/perlman-fun/32/3242_2.png) [@perlman-fun](https://forums.freertos.org/u/perlman-fun)\
**Post date:** [December 6, 2023, 3:04pm UTC](https://forums.freertos.org/t/why-can-taska-release-the-mutex-semaphore-successfully-that-has-already-been-acquired-by-taskb/18668/1 "2023-12-06T15:04:12Z")

</div>

Firstly: task B: create a mutex semaphore (called MS below) by calling xSemaphoreCreateMutex() and take it by xSemaphoreTake() successfully.  
Note that task B never release MS anytime and anywhere.  
Secondly: task A: release MS by xSemaphoreGive() and the API’s return value is pdTRUE. It means that MS is given successfully.  
Thirdly: Task A: try to acquire MS by xSemaphoreTake() and it returns pdTRUE, too! That means MS was released successfully previously in the second stage, therefore it can be acquired without any problem.

I don’t understand why the xSemaphoreGive() API is designed like this. In my mind, the mutex semaphore should only be released by its ownership task(task B), shouldn’t it?  
The code is like this:

```auto
// TaskB
SemaphoreHandle_t MS = xSemaphoreCreateMutex();
 void TaskB_proc( void * pvParameters )
 {
    if( MS != NULL )
    {
        if( xSemaphoreTake( xSemaphore ) == pdTRUE )
        {
            // always loop
            while(1) {
              // Do something-1 // run
            }
        }
        // Do something-2 // NOT run
    }
 }

```

```auto
// TaskA
extern SemaphoreHandle_t MS;
// Note: TaskA_proc would wait so long that MS can be created and taken by taskB and then to run. 
 void TaskA_proc( void * pvParameters )
 {
    if( MS != NULL )
    {
        if( xSemaphoreGive( MS ) == pdTRUE )
        {
            // Do something-3 // run

}
	if( xSemaphoreTake( MS ) == pdTRUE )
        {
            // Do something-4 // run
        }
    }
 }

```

In the end, in the code the priority of task A is higher than task B.

---

<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:** [December 6, 2023, 3:11pm UTC](https://forums.freertos.org/t/why-can-taska-release-the-mutex-semaphore-successfully-that-has-already-been-acquired-by-taskb/18668/2 "2023-12-06T15:11:02Z")

</div>

Please post your code. The behavior you describe would naturally be dead wrong, but muteces have been proven correct a perceived bazillion times.

---

<div class="post-metadata">

**Author:** ![jefftenney](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/jefftenney/32/1314_2.png) [@jefftenney](https://forums.freertos.org/u/jefftenney)\
**Post date:** [December 6, 2023, 3:45pm UTC](https://forums.freertos.org/t/why-can-taska-release-the-mutex-semaphore-successfully-that-has-already-been-acquired-by-taskb/18668/3 "2023-12-06T15:45:55Z")

</div>

Do you have `configASSERT()` defined?

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [December 7, 2023, 5:36am UTC](https://forums.freertos.org/t/why-can-taska-release-the-mutex-semaphore-successfully-that-has-already-been-acquired-by-taskb/18668/4 "2023-12-07T05:36:09Z")

</div>

It is the wrong use of the API. configASSERT should trigger - do you have it defined as @jefftenney asked?

---

<div class="post-metadata">

**Author:** ![perlman-fun](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/perlman-fun/32/3242_2.png) [@perlman-fun](https://forums.freertos.org/u/perlman-fun)\
**Post date:** [December 7, 2023, 1:51pm UTC](https://forums.freertos.org/t/why-can-taska-release-the-mutex-semaphore-successfully-that-has-already-been-acquired-by-taskb/18668/5 "2023-12-07T13:51:55Z")

</div>

Code is showed in the message. Thanks!

---

<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:** [December 7, 2023, 2:33pm UTC](https://forums.freertos.org/t/why-can-taska-release-the-mutex-semaphore-successfully-that-has-already-been-acquired-by-taskb/18668/6 "2023-12-07T14:33:59Z")

</div>

I think the issue is that the requirement for only the holding task to give the Mutex is a operational requirement on the PROGRAM, and might not be actually enforced by the code, as that would add inefficiency.

If your program breaks the rules, then it will suffer the possible consequences. This is very much in the spirit of the C language that FreeRTOS is written in.

---

<div class="post-metadata">

**Author:** ![simonjwright](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/simonjwright/32/4770_2.png) [@simonjwright](https://forums.freertos.org/u/simonjwright)\
**Post date:** [December 7, 2023, 4:18pm UTC](https://forums.freertos.org/t/why-can-taska-release-the-mutex-semaphore-successfully-that-has-already-been-acquired-by-taskb/18668/7 "2023-12-07T16:18:08Z")

</div>

> [@richard-damon](#):
>
> If your program breaks the rules, then it will suffer the possible consequences. This is very much in the spirit of the C language that FreeRTOS is written in.

But not a good look if a customer has safety or security certification requirements?

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [December 7, 2023, 4:46pm UTC](https://forums.freertos.org/t/why-can-taska-release-the-mutex-semaphore-successfully-that-has-already-been-acquired-by-taskb/18668/8 "2023-12-07T16:46:50Z")

</div>

I tried the posted code and hit [this assert](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/tasks.c#L6725).

---

<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:** [December 8, 2023, 6:58am UTC](https://forums.freertos.org/t/why-can-taska-release-the-mutex-semaphore-successfully-that-has-already-been-acquired-by-taskb/18668/9 "2023-12-08T06:58:55Z")

</div>

your code is incomplete. What is the complete parameter set to your take functions? Do you use a non-infinite-timeout? If so, task A should never get a true return from xSemaphoreTake on a mutex that B owns.

---

<div class="post-metadata">

**Author:** ![perlman-fun](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/perlman-fun/32/3242_2.png) [@perlman-fun](https://forums.freertos.org/u/perlman-fun)\
**Post date:** [December 8, 2023, 2:19pm UTC](https://forums.freertos.org/t/why-can-taska-release-the-mutex-semaphore-successfully-that-has-already-been-acquired-by-taskb/18668/10 "2023-12-08T14:19:58Z")

</div>

Not defined!  
With the help of all of you(especially [richard-damon](https://forums.freertos.org/u/richard-damon)), after checking the source code of xSemaphoreGive() once again and I mentioned the code like below:

 ![image](https://d2v6vdsk2p900z.cloudfront.net/original/2X/d/d7e1926d43a087db2002ced4dabd28a0a2b8605a.jpeg)

---

<div class="post-metadata">

**Author:** ![robert.berger](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/robert.berger/32/4739_2.png) [@robert.berger](https://forums.freertos.org/u/robert.berger)\
**Post date:** [December 18, 2023, 1:26pm UTC](https://forums.freertos.org/t/why-can-taska-release-the-mutex-semaphore-successfully-that-has-already-been-acquired-by-taskb/18668/11 "2023-12-18T13:26:23Z")

</div>

Hmm I am not quite sure about this one, since there should be some kind of enforcement.

As far as I remember I had an example somewhere to show the difference between a mutex and a binary semaphore and once a task took a mutex only that task was able to release it.

With respect to the binary semaphore: As soon as a task has access to the semaphore handle it can release it.

---

<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:** [December 18, 2023, 1:50pm UTC](https://forums.freertos.org/t/why-can-taska-release-the-mutex-semaphore-successfully-that-has-already-been-acquired-by-taskb/18668/12 "2023-12-18T13:50:20Z")

</div>

> [@robert.berger](#):
>
> Hmm I am not quite sure about this one, since there should be some kind of enforcement.
> 
> As far as I remember I had an example somewhere to show the difference between a mutex and a binary semaphore and once a task took a mutex only that task was able to release it.

Actually, from my understanding it MUST be enforced by the OS because othertwise priority inheritance management would not work. Whether failure to release by the holder needs to be propagated by the caller is a different question, but I do not think that efficiency would be an issue as the test for valid context must be performed by the release code anyways.

---

<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:** [December 18, 2023, 3:15pm UTC](https://forums.freertos.org/t/why-can-taska-release-the-mutex-semaphore-successfully-that-has-already-been-acquired-by-taskb/18668/13 "2023-12-18T15:15:44Z")

</div>

My memory of the code is that when configASSERT isn’t defined, the code just goes ahead and does what it does. If the releasing task doesn’t have inhereted priority, it may just work “fine”, but some of the defined preconditions might get violated.

The cost to check might be low, but it isn’t zero, and it may currently happen at points (when configASSERT is defined) where a clean blackout can’t happen. In this case, I seem to remember the check happens AFTER the mutex has been released, and possibly taken again by a higher priority task, so it couldn’t get undone.
