We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hello all,
I want to use dynamic memory allocation functions of the Realview C-Libary(MDK-ARM V3.11) together with the RTX-Kernel in a multithreaded application.
To make some functions threadsafe I will have to implement the mutex functions of the libary. Because the mutexes of the RTX-Kernel are larger than a 32-Bit Word, I have to define those as global variables and use the 32-Bit Word as pointer.
Now I needed to now how many mutexes are used by the C-Libary to reserve enough memory at compiletime. I couldn't find any information about that in the documentation of the libary. So I made some tests and recognized that the mutex_initialize function was called 5 times at the startup. So I assume that only 5 mutexes are necessary.
Is the number of neccessary mutexes constant or does it depent on the functions used?
I would be greatful for some hints.
Thanks in advance
Rainer
the C library does not need any mutex.
Ohh that sounds interesting. Rheinhardt could you explain me the reason for differnce of your statement and the one from the documentation, please?
I don't understand it. According to the documentation of the C-Libary, some functions (e.g. malloc, free, printf) are not threadsafe. Due to the use of the RTX-Kernel several tasks may use the functions for dynamic memory allocation. Because the point of the taskswitch can not be predicted, it's necessary to prevent tasks from "simultaneously" calling memory allocation functions of the C-Libary.
The C-Library needs no mutexs and will NEVER call mutex init or aquire a mutex.
YOUR code MAY NEED TO use some sort of mutex mechanism to prevent YOUR code from simulataneously calling SOME C-library functions.
You have listed a few functions that may cause problems
malloc, free, printf.
1 mechanism could be....
Create 1 (and only 1) global muxtex and init it once before starting your tasks.
around any C-Libray function you would like to protect do the following.
Aquire_mutex malloc Release_mutex
Aquire_mutex free Release_mutex
Aquire_mutex printf Release_mutex
@Robert: Thank you for your hints. You described a safe way to do the mutex locking manually. I'm sure that will make the it threadsafe, but it's a lot of work.
In the Complete User's Guide Selection that is included in the MDK-ARM V3.11 is a Section about using the libary in a multithreaded enviroment. It could be found in the following path: --> RealView Libaries and Floating Point Support Guide --> The C and C++ Libaries --> Writing reentrant and threadsafe code
In these section are serveral points to read. The point with the mutex locking is called "Managing locks in mulithreaded applications". There is Description what functions have to be reimplemented. So that this specific C Libary would be more threadsafe.
I a little bit condused now and would appreciate some comments. Did I understand this section wrong?
No, It appears as though I never read that section (It is nice when you learn something new). It does appear as though you are on the right track re-implementing these 3 functions. I do not have the answer to what I believe the original question was, which was "how many mutexes do you need". Depending on how separate the C library functions are, you may be able to count the references to _mutex_initialize in the map file to determine how many you need, then again, maybe not. Sorry for any mis-information I have given you.
(note - you could use only 1 mutex object, returning this same on on each _mutex_initialize [make sure you only initialize the mutex once, not each time _mutex_initialize is called], this limits the number of mutexes that you need, with the draw back of not allowing more than 1 task to be in ANY mutexed section at the same time)