Redlib malloc - thread safe?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Redlib malloc - thread safe?

760 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by jharwood on Tue Apr 26 12:30:28 MST 2011
I would like to use the stdlib functions malloc and friends, printf and friends in a RTOS environment.

It appears that malloc is not thread safe, I tried declaring a function
void __malloc_lock(void)
but it was not called. I also stepped through the malloc code to see if there were any 'cpsie i' and 'cpsid i' instructions in there.

So I guess that I have to wrap every call to the standard library functions with the appropriate OS guard calls.

Is there any possibility that the __malloc_lock and __malloc_unlock stubs could be added to the Redlib library?

Thanks
0 Kudos
Reply
4 Replies

673 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Tue Apr 26 23:57:32 MST 2011

Quote: jharwood

Is there any possibility that the __malloc_lock and __malloc_unlock stubs could be added to the Redlib library?


At present we have no plans to make Redlib thread safe - we have seen little or no demand for this (to date). Our recommendation if you want thread safe is to use Newlib, or wrap the Redlib functions yourself (as you have already suggested).

Regards,
CodeRedSupport
0 Kudos
Reply

673 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by larryvc on Tue Apr 26 15:14:32 MST 2011

Quote: jharwood
Each thread of execution holds its own copy of the _reent structure which takes up 240 bytes of memory.



That's a lot considering we were trying to get a simple blinky to weigh in at around 200 bytes.:) I know, slightly off topic, and I'm a bit bored.:D
0 Kudos
Reply

673 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by jharwood on Tue Apr 26 14:45:34 MST 2011

Quote: TheFallGuy
Have you tried Newlib instead? IIRC, that provides the lock functions.



Thanks for the suggestion TheFallGuy.

It's a much larger library, as CodeRed points out. Each of the function calls need to be changed to, for example; _printf_r() and the first argument is a pointer to a struct _reent.  Each thread of execution holds its own copy of the _reent structure which takes up 240 bytes of memory.
0 Kudos
Reply

673 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by TheFallGuy on Tue Apr 26 13:20:12 MST 2011
Have you tried Newlib instead? IIRC, that provides the lock functions.
0 Kudos
Reply