# Mutex cannot be 'given', although has the value of '0'

**URL:** <https://forums.freertos.org/t/mutex-cannot-be-given-although-has-the-value-of-0/11611>\
**Category:** Kernel\
**Created:** [January 24, 2021, 10:24pm UTC](https://forums.freertos.org/t/mutex-cannot-be-given-although-has-the-value-of-0/11611 "2021-01-24T22:24:46Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![peter1](https://avatars.discourse-cdn.com/v4/letter/p/a587f6/32.png) [@peter1](https://forums.freertos.org/u/peter1)\
**Post date:** [January 24, 2021, 10:24pm UTC](https://forums.freertos.org/t/mutex-cannot-be-given-although-has-the-value-of-0/11611/1 "2021-01-24T22:24:46Z")

</div>

I need two tasks to use a single ethernet interface in the device I am working with (MPC5748G from NXP). I decided to use a mutex to control the access to the resource. I quickly figured out that the mutex cannot be given by xSemaphoreGive(), although its value is ‘0’ just before the function call.  
Debugging showed that an assert is thrown by xQueueGenericSend() in _queue.c_, precisely speaking the application freezes on this piece of code in _devassert.h_ :  
` static inline void DevAssert(volatile bool x) { if(x) { } else { BKPT_ASM; for(;;) {} } }`

I don’t understand why this happens, the _configSUPPORT\_DYNAMIC\_ALLOCATION_ is set to 1, so creating a dynamic mutex should work.

main.c:

```
    #include "../include/CANfuncs.h"

    SemaphoreHandle_t ethernet_sem;
    int main(void)
    {
      / ***Processor Expert internal initialization. DON'T REMOVE THIS CODE!!!*** /
      #ifdef PEX_RTOS_INIT
        PEX_RTOS_INIT(); /* Initialization of the selected RTOS. Macro is defined by the RTOS component. */
      #endif

    CLOCK_SYS_Init(g_clockManConfigsArr, (uint8_t)CLOCK_MANAGER_CONFIG_CNT, g_clockManCallbacksArr, (uint8_t)CLOCK_MANAGER_CALLBACK_CNT);
    CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_AGREEMENT);

    /* Initialize pins */
    PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr);

    ethernet_sem = xSemaphoreCreateMutex();

	xTaskCreate(vTestTask, "testTask", 256U,NULL, 3, NULL);
	vTaskStartScheduler();

	#ifdef PEX_RTOS_START
	PEX_RTOS_START();                
	#endif		
	for(;;) {
	 if(exit_code != 0) {
  		break;
		}
	}
	  return exit_code;
	}

```

CANfuncs.c:

```
 void vTestTask(void* pvParameters){
  for(;;){
    	vTaskDelay( 4000 / portTICK_PERIOD_MS );
    	/* show the value of a semaphore for the debug */
    	UBaseType_t semaphore_val1;
    	semaphore_val1 = uxSemaphoreGetCount(ethernet_sem);

    	if(xSemaphoreTake(ethernet_sem, portMAX_DELAY) == pdTRUE ){
    	     retval = UDPStringSend();
	         semaphore_val1 = uxSemaphoreGetCount(ethernet_sem); //Mutex here is '0'
    	     xSemaphoreGive(ethernet_sem); //Crashes here!
    	    }
       };
  }

```

Does anybody see anything wrong in the code above? I would be grateful for any tips or advice

---

<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:** [January 24, 2021, 10:42pm UTC](https://forums.freertos.org/t/mutex-cannot-be-given-although-has-the-value-of-0/11611/2 "2021-01-24T22:42:48Z")

</div>

Which line is causing the assert failure?

---

<div class="post-metadata">

**Author:** ![peter1](https://avatars.discourse-cdn.com/v4/letter/p/a587f6/32.png) [@peter1](https://forums.freertos.org/u/peter1)\
**Post date:** [January 24, 2021, 11:03pm UTC](https://forums.freertos.org/t/mutex-cannot-be-given-although-has-the-value-of-0/11611/3 "2021-01-24T23:03:13Z")

</div>

`xSemaphoreGive()` causes the assert failure.

---

<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:** [January 24, 2021, 11:11pm UTC](https://forums.freertos.org/t/mutex-cannot-be-given-although-has-the-value-of-0/11611/4 "2021-01-24T23:11:32Z")

</div>

I think the question is which line in the semaphore code is asserting.

The code looks right (assuming CANfuncs.c has an extern SemahoreHandle\_t ethernet\_sem; statement). It could be memory corruption breaking the mutex.

---

<div class="post-metadata">

**Author:** ![peter1](https://avatars.discourse-cdn.com/v4/letter/p/a587f6/32.png) [@peter1](https://forums.freertos.org/u/peter1)\
**Post date:** [January 24, 2021, 11:15pm UTC](https://forums.freertos.org/t/mutex-cannot-be-given-although-has-the-value-of-0/11611/5 "2021-01-24T23:15:59Z")

</div>

the _CANfuncs.c_ doesn’t have the `ethernet_sem` variable as ‘extern’ - maybe that is the problem.  
I managed to compile the code tough and even got the value of the semaphore with GetCount(), so I just didn’t assume that the variable is wrongly declared…

---

<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:** [January 24, 2021, 11:28pm UTC](https://forums.freertos.org/t/mutex-cannot-be-given-although-has-the-value-of-0/11611/6 "2021-01-24T23:28:38Z")

</div>

> [@peter1](#):
>
> static inline void DevAssert(volatile bool x) { if(x) { } else { BKPT\_ASM; for( ;; ) {} } }

Telling us your code is getting stuck in this line is not helpful because it is called from every configASSERT() statement in the kernel code. We need to know which call to configASSRT() is causing you to enter the for( ;; ) loop. Please see the documentation for the configASSERT() macro: [FreeRTOS - The Free RTOS configuration constants and configuration options - FREE Open Source RTOS for small real time embedded systems](https://www.freertos.org/a00110.html#configASSERT)

---

<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:** [January 25, 2021, 1:56am UTC](https://forums.freertos.org/t/mutex-cannot-be-given-although-has-the-value-of-0/11611/7 "2021-01-25T01:56:11Z")

</div>

It has to have the variable declared somehow, perhaps in a header file that is included.

If it isn’t extern, then it will have its own copy of the handle, which would then be NULL, so it should fail earlier

---

<div class="post-metadata">

**Author:** ![NoMaY-jp](https://avatars.discourse-cdn.com/v4/letter/n/439d5e/32.png) [@NoMaY-jp](https://forums.freertos.org/u/NoMaY-jp)\
**Post date:** [January 25, 2021, 7:59am UTC](https://forums.freertos.org/t/mutex-cannot-be-given-although-has-the-value-of-0/11611/8 "2021-01-25T07:59:01Z")

</div>

Hello peter1,

You may already notice this idea… I think that it is better for you to change back the definition of configASSERT() like the followings even if the definition is provided by NXP.

Not good (the following code of your FreeRTOSConfig.h is my guess):

```auto
static inline void DevAssert(volatile bool x);
#define configASSERT(x) DevAssert((bool)(x))

```

Better:

```auto
#define configASSERT(x) do{ if(x) { } else { BKPT_ASM; for( ;; ) {} } }while(0)

```

This change makes you and the debugger to catch up the just asserted line.

Best regards,  
NoMaY

---

<div class="post-metadata">

**Author:** ![peter1](https://avatars.discourse-cdn.com/v4/letter/p/a587f6/32.png) [@peter1](https://forums.freertos.org/u/peter1)\
**Post date:** [January 25, 2021, 3:19pm UTC](https://forums.freertos.org/t/mutex-cannot-be-given-although-has-the-value-of-0/11611/9 "2021-01-25T15:19:36Z")

</div>

thanks for all of the replies.

I added the declaration of the variable as extern, however the problem is still there.  
The assert is thrown from the `xQueueGenericSend()` in the _queue.c_. This function is called directly after `xSemaphoreGive()` .  
Precisely this line is problematic:

```
configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( UBaseType_t ) 0U ) ) );

```

here, the `pvItemToQueue` is 0, and the `uxItemSize` of `pxQueue` is 1073776744. In this way the whole expression evaluates to 0 ( not (TRUE and TRUE)) and an assert is thrown.

Unfortuntelly I don’t know how to approach the problem further - should the pvItemToQueue be different from 0, or is the size of the item in the queue wrong?

---

<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:** [January 25, 2021, 3:29pm UTC](https://forums.freertos.org/t/mutex-cannot-be-given-although-has-the-value-of-0/11611/10 "2021-01-25T15:29:36Z")

</div>

uxItemSize is wrong, it should be 0 for a semaphore/mutex. This likely indicates that somewhere in your program you are corrupting memory by writing outside an array or with a wrong pointer or the like.

---

<div class="post-metadata">

**Author:** ![peter1](https://avatars.discourse-cdn.com/v4/letter/p/a587f6/32.png) [@peter1](https://forums.freertos.org/u/peter1)\
**Post date:** [January 25, 2021, 11:04pm UTC](https://forums.freertos.org/t/mutex-cannot-be-given-although-has-the-value-of-0/11611/11 "2021-01-25T23:04:27Z")

</div>

Thanks to all the tips I decided to change use a static mutex.  
I set the `configSUPPORT_STATIC_ALLOCATION` to 1, provided the default `vApplicationGetIdleTaskMemory()` and `vApplicationGetTimerTaskMemory()` functions (I used the default example from [here](https://www.freertos.org/a00110.html#configSUPPORT_STATIC_ALLOCATION), even though I don’t fully understand the consequences) and created a static mutex. I changed the mutex initialization to this:

```
SemaphoreHandle_t ethernet_sem;
StaticSemaphore_t xStaticEthernetSemaphoreBuffer;
int main(){
    ....
   ethernet_sem = xSemaphoreCreateMutexStatic( &xStaticEthernetSemaphoreBuffer );
   ....
}

```

and left the other part of the code as it was.

Unfortunatelly it still doesn’t work.  
The semaphore gets created, with a value of 1 and can get taken. It can also be given, so the code does a single iteration.  
However this time `xSemaphoreTake()` returns a semaphore that has a value of 1073776000. Suprisingly the `xSemaphoreGive()` returns no error. However, in the second iteration, when the same semaphore has to be taken it returns an error. Precisely speaking `xQueueSemaphoreTake()` fails on `configASSERT( pxQueue->uxItemSize == 0 );`, because the ItemSize is again not 0.

Does it mean that I corrupted some memory again? How can this happen if I am using a static call for a semaphore initialization?

---

<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:** [January 25, 2021, 11:39pm UTC](https://forums.freertos.org/t/mutex-cannot-be-given-although-has-the-value-of-0/11611/12 "2021-01-25T23:39:26Z")

</div>

> However this time `xSemaphoreTake()` returns a semaphore that has a value of 1073776000.

Which means that the memory is already corrupted. Is it possible for you to put a data breakpoint at the location of `pxQueue->uxItemSize` after your semaphore is created?

> How can this happen if I am using a static call for a semaphore initialization?

Most probably, some other piece of code is corrupting the memory.

Thanks.

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [January 26, 2021, 9:43am UTC](https://forums.freertos.org/t/mutex-cannot-be-given-although-has-the-value-of-0/11611/13 "2021-01-26T09:43:25Z")

</div>

Do you have [configASSERT()](https://www.freertos.org/a00110.html#configASSERT) defined and also [stack overflow checking](https://www.freertos.org/Stacks-and-stack-overflow-checking.html) enabled (for development/debugging) ?  
In the original post the test task stack size is only 256, which might be too small…  
Stack overflows are a common source of memory corruption.

---

<div class="post-metadata">

**Author:** ![JacobApium](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/jacobapium/32/2172_2.png) [@JacobApium](https://forums.freertos.org/u/JacobApium)\
**Post date:** [January 27, 2021, 3:30pm UTC](https://forums.freertos.org/t/mutex-cannot-be-given-although-has-the-value-of-0/11611/14 "2021-01-27T15:30:27Z")

</div>

When you create the semaphore does the call actually succeed?

```
   ethernet_sem = xSemaphoreCreateMutex();

   if( ethernet_sem != NULL )
   {
       /* The semaphore was created successfully and
       can be used. */
   }

```

Jacob

---

<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:** [January 27, 2021, 4:06pm UTC](https://forums.freertos.org/t/mutex-cannot-be-given-although-has-the-value-of-0/11611/15 "2021-01-27T16:06:42Z")

</div>

Are you sure the linker script and C startup code are correct for your device? If you declare a variables such as:

```auto
volatile uint32_t ulInitialisedGlobal = 0x11111111UL;
static volatile uint32_t ulInitialisedStaticGlobal = 0x22222222UL;
volatile uint32_t ulUnInitialisedGlobal;
static volatile uint32_t ulUnInitialisedStaticGlobal;

```

where the semaphore is declared, then stop the program in the debugger on entry to main and view these variables - do they all contain the expected values? If not then there is something wrong before main() is even called.

---

<div class="post-metadata">

**Author:** ![peter1](https://avatars.discourse-cdn.com/v4/letter/p/a587f6/32.png) [@peter1](https://forums.freertos.org/u/peter1)\
**Post date:** [January 29, 2021, 4:24pm UTC](https://forums.freertos.org/t/mutex-cannot-be-given-although-has-the-value-of-0/11611/16 "2021-01-29T16:24:00Z")

</div>

That was it, the stack designed for this task was too small. I changed it to 2048 and now it works.  
Thanks for all the help!
