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

Parents
  • 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.

Reply
  • 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.

Children