# Mutex "give" occurs when another task already owns mutex. V9.0

**URL:** <https://forums.freertos.org/t/mutex-give-occurs-when-another-task-already-owns-mutex-v9-0/6789>\
**Category:** Kernel\
**Created:** [September 25, 2017, 10:10pm UTC](https://forums.freertos.org/t/mutex-give-occurs-when-another-task-already-owns-mutex-v9-0/6789 "2017-09-25T22:10:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 25, 2017, 10:10pm UTC](https://forums.freertos.org/t/mutex-give-occurs-when-another-task-already-owns-mutex-v9-0/6789/1 "2017-09-25T22:10:24Z")

</div>

**[zempter](https://sourceforge.net/u/zempter/)** wrote on [Monday, September 25, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/ddc19f7d/#0965):

I have checked to make sure that all of the tasks that take ownership of the mutex give it back imediately after the important function call that interacts with a non-thread-safe libary. However, when The tasks are operating, not very long into startup (but not imediately, so it works for a little while) My “periodics” task tries to give up my mutex that says it is currently owned by my “matrix” task.

xTaskPriorityDisinherit then thows at configASSERT( pxTCB == pxCurrentTCB ); because the current owner is wrong.

I want to understand what could be causing it, but my boss wants me to just toss everything that interracts with that library into one task to avoid the problem from happening. It is bugging me like crazy though, and I will have to do a decent amount of restructuring to my code that I feel like would be unfortunate.

What could be causing this? I am operating on a single core Cypress chip. My config header looks like this:

## 

###ifndef FREERTOS\_CONFIG\_H  
###define FREERTOS\_CONFIG\_H

## 

## /\* Here is a good place to include header files that are required across

## your application. \*/

###include “project.h”

## 

###define configUSE\_PREEMPTION 1  
###define configUSE\_PORT\_OPTIMISED\_TASK\_SELECTION 0  
###define configUSE\_TICKLESS\_IDLE 0

## // PSoC Creator creates a #define macro for the clock settings from the DWR

###define configCPU\_CLOCK\_HZ ( ( unsigned long ) CYDEV\_BCLKSYSCLKHZ )

## // SysTick Defaults to 1ms

###define configTICK\_RATE\_HZ 1000  
###define configMAX\_PRIORITIES 5  
###define configMINIMAL\_STACK\_SIZE 128  
###define configMAX\_TASK\_NAME\_LEN 16  
###define configUSE\_16\_BIT\_TICKS 0  
###define configIDLE\_SHOULD\_YIELD 1  
###define configUSE\_TASK\_NOTIFICATIONS 1  
###define configUSE\_MUTEXES 1  
###define configUSE\_RECURSIVE\_MUTEXES 0  
###define configUSE\_COUNTING\_SEMAPHORES 1  
###define configUSE\_ALTERNATIVE\_API 0 /\* Deprecated! \*/  
###define configQUEUE\_REGISTRY\_SIZE 10  
###define configUSE\_QUEUE\_SETS 1  
###define configUSE\_TIME\_SLICING 0  
###define configUSE\_NEWLIB\_REENTRANT 0  
###define configENABLE\_BACKWARD\_COMPATIBILITY 0  
###define configNUM\_THREAD\_LOCAL\_STORAGE\_POINTERS 5

## 

## /\* Memory allocation related definitions. \*/

###define configSUPPORT\_STATIC\_ALLOCATION 0  
###define configSUPPORT\_DYNAMIC\_ALLOCATION 1  
###define configTOTAL\_HEAP\_SIZE 7000  
###define configAPPLICATION\_ALLOCATED\_HEAP 0

## 

## /\* Hook function related definitions. \*/

###define configUSE\_IDLE\_HOOK 0  
###define configUSE\_TICK\_HOOK 0  
###define configCHECK\_FOR\_STACK\_OVERFLOW 0  
###define configUSE\_MALLOC\_FAILED\_HOOK 0  
###define configUSE\_DAEMON\_TASK\_STARTUP\_HOOK 0

## 

## /\* Run time and task stats gathering related definitions. \*/

###define configGENERATE\_RUN\_TIME\_STATS 0  
###define configUSE\_TRACE\_FACILITY 0  
###define configUSE\_STATS\_FORMATTING\_FUNCTIONS 0

## 

## /\* Co-routine related definitions. \*/

###define configUSE\_CO\_ROUTINES 0  
###define configMAX\_CO\_ROUTINE\_PRIORITIES 1

## 

## /\* Software timer related definitions. \*/

###define configUSE\_TIMERS 1  
###define configTIMER\_TASK\_PRIORITY 3  
###define configTIMER\_QUEUE\_LENGTH 10  
###define configTIMER\_TASK\_STACK\_DEPTH configMINIMAL\_STACK\_SIZE

## 

## /\* Interrupt nesting behaviour configuration. \*/

###define configKERNEL\_INTERRUPT\_PRIORITY [dependent of processor]  
###define configMAX\_SYSCALL\_INTERRUPT\_PRIORITY [dependent on processor and application]  
###define configMAX\_API\_CALL\_INTERRUPT\_PRIORITY [dependent on processor and application]

## 

## /\* Define to trap errors during development. \*/

###define configASSERT( x ) if( ( x ) == 0 ) { taskDISABLE\_INTERRUPTS(); for( ;; ); }

## 

## /\* FreeRTOS MPU specific definitions. \*/

###define configINCLUDE\_APPLICATION\_DEFINED\_PRIVILEGED\_FUNCTIONS 0

## 

## /\* Optional functions - most linkers will remove unused functions anyway. \*/

###define INCLUDE\_vTaskPrioritySet 1  
###define INCLUDE\_uxTaskPriorityGet 1  
###define INCLUDE\_vTaskDelete 1  
###define INCLUDE\_vTaskSuspend 1  
###define INCLUDE\_xResumeFromISR 1  
###define INCLUDE\_vTaskDelayUntil 1  
###define INCLUDE\_vTaskDelay 1  
###define INCLUDE\_xTaskGetSchedulerState 1  
###define INCLUDE\_xTaskGetCurrentTaskHandle 1  
###define INCLUDE\_uxTaskGetStackHighWaterMark 0  
###define INCLUDE\_xTaskGetIdleTaskHandle 0  
###define INCLUDE\_eTaskGetState 0  
###define INCLUDE\_xEventGroupSetBitFromISR 1  
###define INCLUDE\_xTimerPendFunctionCall 0  
###define INCLUDE\_xTaskAbortDelay 0  
###define INCLUDE\_xTaskGetHandle 0  
###define INCLUDE\_xTaskResumeFromISR 1

## 

## /\* A header file that defines trace macro can be included here. \*/

## 

###endif /\* FREERTOS\_CONFIG\_H _/_\*

---

<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:** [September 25, 2017, 10:20pm UTC](https://forums.freertos.org/t/mutex-give-occurs-when-another-task-already-owns-mutex-v9-0/6789/2 "2017-09-25T22:20:30Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Monday, September 25, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/ddc19f7d/#0965/dcd8):

Are you giving the mutex from an interrupt at all? That can really mess  
up what the system thinks is the mutex owner.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 25, 2017, 10:22pm UTC](https://forums.freertos.org/t/mutex-give-occurs-when-another-task-already-owns-mutex-v9-0/6789/3 "2017-09-25T22:22:40Z")

</div>

**[zempter](https://sourceforge.net/u/zempter/)** wrote on [Monday, September 25, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/ddc19f7d/#2793):

Nevermind, I figured it out, I was using the wrong delay value.

xSemaphoreTake(MATRIX\_MUTEX,0);

when I really wanted:

xSemaphoreTake(MATRIX\_MUTEX,portMAX\_DELAY );

I was thinking that 0 meant it didn’t have a timeout.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 25, 2017, 10:23pm UTC](https://forums.freertos.org/t/mutex-give-occurs-when-another-task-already-owns-mutex-v9-0/6789/4 "2017-09-25T22:23:12Z")

</div>

**[zempter](https://sourceforge.net/u/zempter/)** wrote on [Monday, September 25, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/ddc19f7d/#0965/dcd8/f110):

I wasn’t ever calling from an interrupt.

Thanks for the fast response.

---

<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:** [September 26, 2017, 2:02am UTC](https://forums.freertos.org/t/mutex-give-occurs-when-another-task-already-owns-mutex-v9-0/6789/5 "2017-09-26T02:02:08Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Tuesday, September 26, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/ddc19f7d/#9241):

You also should be testing the return value of the take to make sure you got it. That would have cought the error.
