<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>ARM MicroLIB and malloc() thread safety with FreeRTOS</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/93459/arm-microlib-and-malloc-thread-safety-with-freertos</link><description>MCU: nRF52840 
 IDE: Keil uVision 5.17 
 SDK: 15.2.0 
 Board: Custom 
 OS: FreeRTOS 
 Using MicroLIB 
 
 Hello, 
 I am currently working on a FreeRTOS project that leverages a third party data processing library that internally uses malloc(), realloc</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 03 Nov 2022 17:09:01 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/93459/arm-microlib-and-malloc-thread-safety-with-freertos" /><item><title>RE: ARM MicroLIB and malloc() thread safety with FreeRTOS</title><link>https://devzone.nordicsemi.com/thread/394002?ContentTypeID=1</link><pubDate>Thu, 03 Nov 2022 17:09:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:78318f2b-99dd-445a-a05e-65fbaf621a4e</guid><dc:creator>droberson</dc:creator><description>&lt;p&gt;Ahhh, I overlooked that. Thanks for finding that! Makes sense. Other than increased RAM usage, are there any caveats or gotchas to not using MicroLIB? I notice that all the Nordic Keil uVision example projects use MicroLIB.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Edit&lt;/strong&gt;: I found my answer:&amp;nbsp;&amp;nbsp;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/18990/program-hangs-on-startup-if-not-using-microlib"&gt;program hangs on startup if not using microlib&lt;/a&gt;&amp;nbsp; and&amp;nbsp;&lt;a href="https://devzone.nordicsemi.com/nordic/nordic-blog/b/blog/posts/rom-and-ram-management"&gt;devzone.nordicsemi.com/.../rom-and-ram-management&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I guess the only way to make malloc() thread safe with MicroLIB is to wrap it with a function that uses a mutex.&lt;/p&gt;
&lt;p&gt;Thanks for your help!&lt;/p&gt;
&lt;p&gt;Derek&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: ARM MicroLIB and malloc() thread safety with FreeRTOS</title><link>https://devzone.nordicsemi.com/thread/393803?ContentTypeID=1</link><pubDate>Thu, 03 Nov 2022 08:28:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:81b4f972-9886-4946-8a5e-4b5c4b1c8318</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;Hmm, seems like &lt;a href="https://developer.arm.com/documentation/dui0475/m/the-arm-c-micro-library/differences-between-microlib-and-the-default-c-library"&gt;microlib does not support mutex locks to guards agains the code that is not already thread safe&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;Microlib does not provide mutex locks to guard against code that is not thread safe.&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: ARM MicroLIB and malloc() thread safety with FreeRTOS</title><link>https://devzone.nordicsemi.com/thread/393740?ContentTypeID=1</link><pubDate>Wed, 02 Nov 2022 16:51:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8a246102-9d5d-4cd7-974d-7f3efdcc7920</guid><dc:creator>droberson</dc:creator><description>&lt;p&gt;So this is what I have discovered after implementing the following functions as test code:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;volatile SemaphoreHandle_t testMutex = (SemaphoreHandle_t) 0x00000012;

int _mutex_initialize(SemaphoreHandle_t *sid)
{
   *sid = testMutex;
   return 1;
   
   // /* Create a mutex semaphore */
   // *sid = CreateLock(...);
   // return 1;
}

void _mutex_acquire(SemaphoreHandle_t *sid)
{
   testMutex = * sid;
   
   /* Task sleep until get semaphore */
   //AcquireLock(*sid, ...);
}

void _mutex_release(SemaphoreHandle_t *sid)
{
   testMutex = * sid;
   
   /* Release the semaphore. */
   //ReleaseLock(*sid);
}

void _mutex_free(SemaphoreHandle_t *sid)
{
   testMutex = * sid;
   
   /* Free the semaphore. */
   //FreeLock(*sid, ...);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;If I compile &lt;strong&gt;without&lt;/strong&gt; MicroLIB (box not checked on &lt;em&gt;Options for Target-&amp;gt;Target&lt;/em&gt;), I see the following in the .map file:&lt;/p&gt;
&lt;p&gt;&amp;quot;h1_free_mt.o(.text) refers (Weak) to main.o(i._mutex_acquire) for _mutex_acquire&amp;quot;&lt;/p&gt;
&lt;p&gt;So I can see my implementation overriding the weak implementations. I can also see the code execute as shown below:&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/malloc.png" /&gt;&lt;/p&gt;
&lt;p&gt;If I compile &lt;strong&gt;with&lt;/strong&gt; MicroLIB and look at the .map file, there are no weak implementations of&amp;nbsp;&lt;span&gt;_mutex_acquire() and similar functions and thus nothing is overridden. Also, my breakpoint depicted above is never hit.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Question :&lt;/strong&gt;&amp;nbsp;Does this mean that MicroLIB doesn&amp;#39;t use the _mutex_* functions? If not, then is malloc() thread safe in MicroLIB or is it inherently not thread safe with no way to make it thread safe?&lt;/p&gt;
&lt;p&gt;Thanks,&lt;/p&gt;
&lt;p&gt;Derek&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: ARM MicroLIB and malloc() thread safety with FreeRTOS</title><link>https://devzone.nordicsemi.com/thread/393734?ContentTypeID=1</link><pubDate>Wed, 02 Nov 2022 16:02:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6ab7d361-0f3d-4107-84f7-5817a2e76885</guid><dc:creator>droberson</dc:creator><description>&lt;p&gt;Per &lt;strong&gt;Question 2&lt;/strong&gt; form above, do you have any ARM documentation that details the function prototypes and expected return values for the _mutex_* functions? I can&amp;#39;t find anything.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Edit&lt;/strong&gt;: I found this:&amp;nbsp;&lt;a href="https://developer.arm.com/documentation/dui0475/m/the-arm-c-and-c---libraries/multithreaded-support-in-arm-c-libraries/management-of-locks-in-multithreaded-applications?lang=en"&gt;https://developer.arm.com/documentation/dui0475/m/the-arm-c-and-c---libraries/multithreaded-support-in-arm-c-libraries/management-of-locks-in-multithreaded-applications?lang=en&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I will try to implement per the spec above and report back after testing.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Thanks,&lt;/p&gt;
&lt;p&gt;Derek&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: ARM MicroLIB and malloc() thread safety with FreeRTOS</title><link>https://devzone.nordicsemi.com/thread/393626?ContentTypeID=1</link><pubDate>Wed, 02 Nov 2022 11:37:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9cdd5bc7-5750-4a04-b7f5-c51780041e32</guid><dc:creator>Susheel Nuguru</dc:creator><description>[quote user=""]&lt;strong&gt;Question 1&lt;/strong&gt;: Is malloc(), free(), and realloc() thread safe in MicroLib? I am not sure whether the link above applies to the default C library or MicroLIB.[/quote]
&lt;p&gt;I do not see that the weak implementation of&amp;nbsp;&lt;span&gt;_mutex_acquire&amp;nbsp;,&amp;nbsp;_mutex_free&amp;nbsp; and&amp;nbsp;_mutex_release&amp;nbsp; has been patched with freertos mutexes. What I understand from the documentation of the arm c libraries is that there is a use of these weak implementation of mutexes inside these heap functions, but these are dummy functions not doing anything and expects to be overridden by the layer above. So what you need to do is that you need to overide the weak implementation of these C library functions with freertos mutexes API and the C library for these heap functions would make sure to call these overridden calls to ensure thread safety.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;In short, you should override &lt;a href="https://developer.arm.com/documentation/dui0378/c/Chdfjddj"&gt;these &lt;/a&gt;functions using FreeRTOS API and the C library would take care of the rest to call them when needed.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>