osCreateCountSem function
Declaration:
HANDLE osCreateCountSem(
  SYSNAME Name,
  INDEX InitialCount,
  INDEX MaxCount
);
Parameters:
Name

The name of the object (can be NULL).

InitialCount

The initial value of the semaphore counter.

MaxCount

The maximum value the semaphore counter can reach.

Return value:

On success, the return value is a handle to the newly created counting semaphore object. On failure, the function returns NULL_HANDLE. To retrieve extended error information, call the osGetLastError function.

Description:

This function creates a counting semaphore object.

Counting semaphores are utilized to synchronize access to a shared resource, ensuring that only a specified number of tasks can access the resource simultaneously. While counting semaphores offer higher performance compared to standard semaphores, they do not include mechanisms to prevent priority inversion.

An optional name may be assigned to the counting semaphore during creation, allowing other tasks to obtain a handle via the osOpenCountSem function. When the counting semaphore is no longer needed, it must be closed using osCloseHandle. The object is deleted only after all tasks that opened the handle have closed it. Further details can be found in the system objects management section.

If none of the osOpen* functions are referenced in the application, the system ignores object names and the internal name management logic is excluded from the build. This optimization reduces the final footprint of the output code. For more information regarding object naming and opening, see the system objects management section.

The semaphore counter is initialized to the value specified by the InitialCount parameter, which must not exceed MaxCount. For a more detailed explanation of counting semaphore behavior, please refer to the counting semaphore objects section.

This function is available only when the OS_USE_CNT_SEM configuration constant is set to 1.

SpaceShadow documentation