Shared memory objects

Shared memory objects create and control the access to memory space that is used simultaneously by many tasks.

Shared memory creation and deletion

The function osCreateSharedMemory creates a shared memory object. The shared memory can be created before system start, from an interrupt handler or by a task. During shared memory creation, an optional object name may be specified. It makes tasks able to open it by osOpenSharedMemory function. During shared memory creation and opening, its handle is returned. The handle is assigned by the system and it is used to discern the system objects. All operations performed on the shared memory except to specify its handle. The non-used shared memory object should be closed with the osCloseHandle. Shared memory object will be deleted when it will be closed by all tasks that opened it. More information you can find in the system objects management section.

When shared memory objects are not used, the OS_USE_SHARED_MEM constant may be set to 0, to reduce the output code.

Using a shared memory object

When single object instance is created, it allocates a memory buffer. Its address is given back during object creation or opening. Additionally, basing on the object handle, it can be taken by using osGetSharedMemoryAddress function. This function is available only when the OS_GET_SH_MEM_ADDRESS_FUNC is set to 1.

Memory access synchronization can be performed in three ways and is described by Mode parameter during the object creation.

If as a mode flag was used OS_IPC_PROTECT_MUTEX, the memory is protected by a mutex object. It prevents the priority inversion occurrence. This feature is available only, when the OS_SH_MEM_PROTECT_MUTEX constant is set to 1. If it is a certainty that priority inversion will not occur, the memory can be protected by auto-reset event object by setting the OS_IPC_PROTECT_EVENT as a mode flag.

To acquire the shared memory, use one of wait functions. When the wait is completed, the task owns a shared memory. This time no other task will use memory. Releasing the shared memory is realized by osReleaseSharedMemory function. For more information on the specific method of synchronization, please refer to mutex and eventsection.

If the access to the memory is very short, the synchronization can be performed by disabling the interrupts with arLock and later restoring them by arRestore. In this case, the wait and the osReleaseSharedMemory functions are not to be used.

SpaceShadow documentation