# Using Mutex before scheduler starts

**URL:** <https://forums.freertos.org/t/using-mutex-before-scheduler-starts/6702>\
**Category:** Kernel\
**Created:** [July 27, 2017, 12:29am UTC](https://forums.freertos.org/t/using-mutex-before-scheduler-starts/6702 "2017-07-27T00:29:01Z")\
**Posts on this page:** 8\
**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:** [July 27, 2017, 12:29am UTC](https://forums.freertos.org/t/using-mutex-before-scheduler-starts/6702/1 "2017-07-27T00:29:01Z")

</div>

**[alsaleem](https://sourceforge.net/u/alsaleem/)** wrote on [Thursday, July 27, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/34ad0b10/#60da):

On STM32F4 application, I need to synchronize access to a resource (SPI/Flash).  
So, I am going to use a mutex.

However, before starting the scheduler, I need to access the resource to get some data.  
The synchronization code is there [xSemaphoreTake( h,10 ) / xSemaphoreGive( h )]

At the beginning (before scheduler starts) all accesses are serial.

Will this work as I use ticks in the call to take function?

Thanks.

---

<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:** [July 27, 2017, 12:37am UTC](https://forums.freertos.org/t/using-mutex-before-scheduler-starts/6702/2 "2017-07-27T00:37:55Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Thursday, July 27, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/34ad0b10/#60da/c853):

There is little point using the mutex before the scheduler has started,  
so I assume the reason you are doing it is because the same code runs  
both before and after the scheduler is running.

I THINK that, as long as you have a block time of 0, then you SHOULD be  
ok, but am going from memory of the source code and haven’t actually  
tried it. Note that calling [nearly] any FreeRTOS API function before  
the scheduler has started will [deliberately] leave interrupts disabled.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 27, 2017, 12:48am UTC](https://forums.freertos.org/t/using-mutex-before-scheduler-starts/6702/3 "2017-07-27T00:48:21Z")

</div>

**[alsaleem](https://sourceforge.net/u/alsaleem/)** wrote on [Thursday, July 27, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/34ad0b10/#abf4):

I thought that the ticks are effective if there is another task using the resurce (already took the mutex) and ticks used are to give the the calling code a chance to wait.  
Since scheduler has not started, and access is serial, the tick has no effect since no other code is taking the mutex.

Is this true?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 27, 2017, 1:00am UTC](https://forums.freertos.org/t/using-mutex-before-scheduler-starts/6702/4 "2017-07-27T01:00:55Z")

</div>

**[alsaleem](https://sourceforge.net/u/alsaleem/)** wrote on [Thursday, July 27, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/34ad0b10/#40b6):

> Note that calling [nearly] any FreeRTOS API function before  
> the scheduler has started will [deliberately] leave interrupts disabled.

I do have initialization code enables other interrupts.  
Does that mean if I use an API function before scheduler starts, I have to re-enable those IRQs?

---

<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:** [July 27, 2017, 1:09am UTC](https://forums.freertos.org/t/using-mutex-before-scheduler-starts/6702/5 "2017-07-27T01:09:45Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Thursday, July 27, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/34ad0b10/#40b6/258b):

Interrupts up to configMAX\_SYSCALL\_INTERRUPT\_PRIORITY will be left  
disabled. You can renable them by calling portENABLE\_INTERRUPTS()  
(again, I think that is the case, didn’t actually check the source code).

Note calling portEXIT\_CRITICAL() will NOT re-enable them.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 27, 2017, 2:42am UTC](https://forums.freertos.org/t/using-mutex-before-scheduler-starts/6702/6 "2017-07-27T02:42:32Z")

</div>

**[alsaleem](https://sourceforge.net/u/alsaleem/)** wrote on [Thursday, July 27, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/34ad0b10/#b150):

To avoid all this complexity, do you recommend to create an initialization task and start scheduler.  
Then the initialization task initializes all resources, then delete itself?

Thanks.

---

<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:** [July 27, 2017, 3:37am UTC](https://forums.freertos.org/t/using-mutex-before-scheduler-starts/6702/7 "2017-07-27T03:37:32Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Thursday, July 27, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/34ad0b10/#b150/8fb9):

That is a common technique. Recent versions of FreeRTOS also have a startup hook - a function that runs once when the scheduler starts.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 27, 2017, 3:46am UTC](https://forums.freertos.org/t/using-mutex-before-scheduler-starts/6702/8 "2017-07-27T03:46:49Z")

</div>

**[alsaleem](https://sourceforge.net/u/alsaleem/)** wrote on [Thursday, July 27, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/34ad0b10/#a988):

I am using FreeRTOS V8.2.1, do you recommend to upgrade?
