CMSIS-RTOS RTX: Semaphore Management

| CMSIS-RTOS RTX Version 4.80 CMSIS-RTOS RTX: Real-Time Operating System for Cortex-M processor-based devices

- Main Page
- Usage and Description
- Reference

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Macros | Functions
Semaphore Management
CMSIS-RTOS API (RTX)

Control access to shared resources. More...

## Macros
#define | osFeature_Semaphore 65535
| Maximum count for osSemaphoreCreate function.
#define | osSemaphoreDef(name)
| Define a Semaphore object.
#define | osSemaphore(name) &os_semaphore_def_##name
| Access a Semaphore definition.

## Functions
osSemaphoreId | osSemaphoreCreate (const osSemaphoreDef_t *semaphore_def, int32_t count)
| Create and Initialize a Semaphore object used for managing resources.
int32_t | osSemaphoreWait (osSemaphoreId semaphore_id, uint32_t millisec)
| Wait until a Semaphore token becomes available.
osStatus | osSemaphoreRelease (osSemaphoreId semaphore_id)
| Release a Semaphore token.
osStatus | osSemaphoreDelete (osSemaphoreId semaphore_id)
| Delete a Semaphore that was created by osSemaphoreCreate.

## Description

The Semaphore Management function group is used to manage and protect access to shared resources. For example, with a Semaphore the access to a group of identical peripherals can be managed. The number of available resources is specified as parameter of the osSemaphoreCreate function.

Each time a Semaphore token is obtained with osSemaphoreWait the semaphore count is decremented. When the semaphore count is 0, no Semaphore token can be obtained. Semaphores are released with osSemaphoreRelease; this function increments the semaphore count.

CMSIS-RTOS Semaphore

## Macro Definition Documentation

#define osFeature_Semaphore 65535

CMSIS-RTOS RTX supports an index count up to 65535 for a semaphore .

#define osSemaphore | ( |  | name | ) | &os_semaphore_def_##name

Access to semaphore object for the functions osSemaphoreCreate.

Parameters

  name | name of the semaphore object.

#define osSemaphoreDef | ( |  | name | ) |

Define a semaphore object that is referenced by osSemaphore.

Parameters

  name | name of the semaphore object.

## Function Documentation

osSemaphoreId osSemaphoreCreate | ( | const osSemaphoreDef_t * | semaphore_def,
|  | int32_t | count
| ) |  |

Parameters

  [in] | semaphore_def | semaphore definition referenced with osSemaphore.
  [in] | count | number of available resources.

Returns
  semaphore ID for reference by other functions or NULL in case of error.

Create and initialize a Semaphore object that is used to manage access to shared resources. The parameter count specifies the number of available resources. The count value 1 creates a binary semaphore.

Note
  - The value count is only used to set the initial token count. The initial token count is not stored in memory (to save RAM as the RTX implementation tries to use as little resource as possible). Therefore no error is generated when osSemaphoreRelease is used to release more tokens than specified with the initial token count.
  - Cannot be called from Interrupt Service Routines.

Example

#include "cmsis_os.h"
osThreadId tid_thread1; // ID for thread 1
osThreadId tid_thread2; // ID for thread 2
osSemaphoreId semaphore; // Semaphore ID
osSemaphoreDef(semaphore); // Semaphore definition
//
// Thread 1 - High Priority - Active every 3ms
//
void thread1 (void const *argument) {
int32_t value;
while (1) {
osDelay(3); // Pass control to other tasks for 3ms
val = osSemaphoreWait (semaphore, 1); // Wait 1ms for the free semaphore
if (val > 0) {
// If there was no time-out the semaphore was acquired
: // OK, the interface is free now, use it.
osSemaphoreRelease (semaphore); // Return a token back to a semaphore
}
}
}
//
// Thread 2 - Normal Priority - looks for a free semaphore and uses
// the resource whenever it is available
//
void thread2 (void const *argument) {
while (1) {
osSemaphoreWait (semaphore, osWaitForever); // Wait indefinitely for a free semaphore
// OK, the interface is free now, use it.
:
osSemaphoreRelease (semaphore); // Return a token back to a semaphore.
}
}
// Thread definitions
osThreadDef(thread1, osPriorityHigh, 1, 0);
osThreadDef(thread2, osPriorityNormal, 1, 0);
void StartApplication (void) {
semaphore = osSemaphoreCreate(osSemaphore(semaphore), 1);
tid_thread1 = osThreadCreate(osThread(thread1), NULL);
tid_thread2 = osThreadCreate(osThread(thread2), NULL);
:
}

osStatus osSemaphoreDelete | ( | osSemaphoreId | semaphore_id | ) |

Parameters

  [in] | semaphore_id | semaphore object referenced with osSemaphoreCreate.

Returns
  status code that indicates the execution status of the function.

Delete a Semaphore object. The function releases internal memory obtained for Semaphore handling. After this call the semaphore_id is no longer valid and cannot be used. The Semaphore may be created again using the function osSemaphoreCreate.

Status and Error Codes

- osOK: the semaphore object has been deleted.
- osErrorISR: osSemaphoreDelete cannot be called from interrupt service routines.
- osErrorResource: the semaphore object could not be deleted.
- osErrorParameter: the parameter semaphore_id is incorrect.

Note
  Cannot be called from Interrupt Service Routines.

osStatus osSemaphoreRelease | ( | osSemaphoreId | semaphore_id | ) |

Parameters

  [in] | semaphore_id | semaphore object referenced with osSemaphoreCreate.

Returns
  status code that indicates the execution status of the function.

Release a Semaphore token. This increments the count of available semaphore tokens.

Note
  Interrupt Service Routines can call this function.

Status and Error Codes

- osOK: the semaphore has been released.
- osErrorResource: all tokens have already been released.
- osErrorParameter: the parameter semaphore_id is incorrect.

int32_t osSemaphoreWait | ( | osSemaphoreId | semaphore_id,
|  | uint32_t | millisec
| ) |  |

Parameters

  [in] | semaphore_id | semaphore object referenced with osSemaphoreCreate.
  [in] | millisec | Timout Value or 0 in case of no time-out.

Returns
  number of available tokens, or -1 in case of incorrect parameters.

Wait until a Semaphore token becomes available. When no Semaphore token is available, the function waits for the time specified with the parameter millisec.

The argument millisec specifies how long the system waits for a Semaphore token to become available. While the system waits the thread that is calling this function is put into the state WAITING. The millisec timeout can have the following values:

- when millisec is 0, the function returns instantly.
- when millisec is set to osWaitForever the function will wait for an infinite time until the Semaphore token becomes available.
- all other values specify a time in millisecond for a timeout.

The return value indicates the number of available tokens (the semaphore count value). If 0 is returned, then no semaphore was available.

Note
  Cannot be called from Interrupt Service Routines.