This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Implementing mutex locking of the C-libary

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

Parents
  • @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?

Reply
  • @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?

Children
  • 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)