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

RTX with CMSIS RTOS : dynamically create MUTEX

hi

i'm searching how to dynamically create mutex in CMSIS-RTOS/KEIL.

i saw the osMutexCreate only accept const osMutexDef_t *

do you know how i can manage to create a mutex, runtime and dynamically allocated in memory please ?

i'm porting a code where there is Mutex_Create(); everywhere so i didn't found other possibilities, yet.

thank you

best regards
KArl

  • I would have to assume that the "const" there is a lie, and that they internally type cast away the const attribute. Because the OS needs write-access to some memory to implement a mutex.

    Most probably, the "const" is there just to make sure end-user code that tries to directly modify the osMutexDef_t object will get a compilation error.

    The problem with dynamic creation would be the following text:
    "Define a mutex object that is referenced by osMutex.

    Parameters
    name name of the mutex object.
    Note
    CAN BE CHANGED: The parameter to osMutexDef shall be consistent but the macro body is implementation specific in every CMSIS-RTOS."

    With the macro body implementation specific, you can't know what actions that must be taken to get your hands on the block of data that will then be given to osMutexCreate()

    What you might be able to do, is have a pool of mutex objects that you then allocate from and osMutexCreate(). But there are no osMutexDestroy(), and you can't know if osMutexCreate() may be called multiple times for the same object. So you might have to define and create your pool of mutexes once, and then just allocate a osMutexId from the pool instead of allocating a osMutexDef.

  • Hi,

    I thank you for your answer. My difficulty with a pool of mutex is the following one : I'm porting a library which use a lot of mutex allocation.

    - I don't know how many mutex I need in advance, so i'll need to create a big pool and try to profile with a variable how many mutex creation is done in runtime.

    - the other drawback of the CMSIS-RTOS/RTX implementation is the initialization process : - first osMutexDef(mutex_name) as a global variable. - then mutex_id=osMutexCreate(osMutex (mutex_name)); this is hard to automatize when there is a important number of semaphore, like a pool described before. This process is very static.

    - it seems that some other os have a more adapted (dynamic) process like FREERTOS (xSemaphoreCreateMutex( void ) or some proprietary ones like TI SYSBIOS or even Linux.

    best regards
    Etienne

  • Don't forget that what Linux does is irrelevant when you get to embedded systems with often very limited amount of RAM.

    The design is made on the assumption that you should be able to write applications with zero involvement of malloc/free, which means there will not be any issues with any fragmented heap making the software fail after having been run for a longer time. The smaller heap you have, the more danger you have of fragmentation killing the application.

    So what is done is a general-purpose OS is irrelevant when discussing an implementation for smaller microcontrollers.

    It's possible to abuse the original macros a bit to make it easy to set up a pool of mutex as an array.

  • I had the same issue. I posted a long explanation and a relatively short fix at another
    thread, here: http://www.keil.com/forum/61105/#/msg200445