# High performance lock ? MutexGet MutexPut

**URL:** <https://forums.freertos.org/t/high-performance-lock-mutexget-mutexput/2529>\
**Category:** Kernel\
**Created:** [July 17, 2009, 7:37am UTC](https://forums.freertos.org/t/high-performance-lock-mutexget-mutexput/2529 "2009-07-17T07:37:04Z")\
**Posts on this page:** 3\
**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 17, 2009, 7:37am UTC](https://forums.freertos.org/t/high-performance-lock-mutexget-mutexput/2529/1 "2009-07-17T07:37:04Z")

</div>

**[enginayd](https://sourceforge.net/u/enginayd/)** wrote on [Friday, July 17, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/70400152/#eb6b):

Hello there!

For Cortex-M3 port, I wonder if the mutex implementation in the grlib will make any use. Because it performs a lock without masking out the interrupts and could perform better ?

What do you think ?

Below is the code for MutexGet and MutexPut for your convenience (taken from widget.c of grlib of Luminary);

unsigned long \_\_attribute\_\_((naked))  
WidgetMutexGet(unsigned char \*pcMutex)  
{  
&nbsp;&nbsp;&nbsp; unsigned long ulRet;

&nbsp;&nbsp;&nbsp; //  
&nbsp;&nbsp;&nbsp; // Acquire the mutex if possible.  
&nbsp;&nbsp;&nbsp; //  
&nbsp;&nbsp;&nbsp; \_\_asm("&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r1, #1\n"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "&nbsp;&nbsp;&nbsp; ldrexb&nbsp;&nbsp; r2, [r0]\n"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "&nbsp;&nbsp;&nbsp; cmp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r2, #0\n"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "&nbsp;&nbsp;&nbsp; it&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; eq\n"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "&nbsp;&nbsp;&nbsp; strexbeq r2, r1, [r0]\n"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r0, r2\n"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "&nbsp;&nbsp;&nbsp; bx&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lr\n"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : "=r" (ulRet));

&nbsp;&nbsp;&nbsp; //  
&nbsp;&nbsp;&nbsp; // The return is handled in the inline assembly, but the compiler will  
&nbsp;&nbsp;&nbsp; // still complain if there is not an explicit return here (despite the fact  
&nbsp;&nbsp;&nbsp; // that this does not result in any code being produced because of the  
&nbsp;&nbsp;&nbsp; // naked attribute).  
&nbsp;&nbsp;&nbsp; //  
&nbsp;&nbsp;&nbsp; return(ulRet);  
}

void  
WidgetMutexPut(unsigned char \*pcMutex)  
{  
&nbsp;&nbsp;&nbsp; //  
&nbsp;&nbsp;&nbsp; // Release the mutex.  
&nbsp;&nbsp;&nbsp; //  
&nbsp;&nbsp;&nbsp; \*pcMutex = 0;  
}

Kind regards,

Engin

---

<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 17, 2009, 2:29pm UTC](https://forums.freertos.org/t/high-performance-lock-mutexget-mutexput/2529/2 "2009-07-17T14:29:31Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, July 17, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/70400152/#eb6b/714f):

Most semi complex architectures have instructions that can be used to implement mutexes, and indeed you could optimise individual ports this way.&nbsp; The mutex code as it stands is in the common code and not in the portable layer, so it would require some major changes and a support nightmare.&nbsp; Also, the mutex structures contain all the event control information, so tasks can block and be unblocked as semaphores are taken and given - all in strict priority order.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 17, 2009, 3:20pm UTC](https://forums.freertos.org/t/high-performance-lock-mutexget-mutexput/2529/3 "2009-07-17T15:20:03Z")

</div>

**[enginayd](https://sourceforge.net/u/enginayd/)** wrote on [Friday, July 17, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/70400152/#eb6b/714f/d207):

Hmm, so FreeRTOS is currently designed in a way that it cannot itself take advantage of this. i.e. it has [Enter|Exit]CriticalSection in portable layer and uses that as locks.

Besides it wouldn’t support priority levels.

Even though it is not appropriate for FreeRTOS as of today, is it suitable for simple usage in user programs ? i.e. it wouldn’t break anything and it would function as expected, right ?

Kind regards,

Engin
