01.06.2013 Views

IPC@CHIP Documentation - SC12 @CHIP-RTOS V1.10

IPC@CHIP Documentation - SC12 @CHIP-RTOS V1.10

IPC@CHIP Documentation - SC12 @CHIP-RTOS V1.10

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Semaphore Services<br />

Top of list<br />

Index page<br />

Semaphores are used to guarantee a task mutually exclusive access to a critical resource. Semaphores<br />

synchronize asynchronous occurring activities. They are an essential part of a multitasking system. A<br />

good description of multitasking systems and semaphores is available in the book "Operating systems"<br />

from Andrew Tanenbaum.<br />

The <strong>@CHIP</strong>-<strong>RTOS</strong> API provides two types of semaphores:<br />

❍ A counting semaphore is a semaphore with an associated counter, which can be incremented<br />

(signal) and decremented (wait). The resource controlled by the semaphore is free (available)<br />

when the counter is greater than 0.<br />

❍ A resource semaphore is a counting semaphore with a maximum of count of one. It can be<br />

used to provide mutually exclusive access to a single resource. A resource semaphore is also<br />

called a binary semaphore. It differs from a counting semaphore in one significant feature: The<br />

resource ownership is tied to a specific task. No other task except the task owning the resource<br />

is allowed to signal the associated semaphore to release the resource.<br />

The counting and resource semaphores provide automatic timeout. Tasks can specify the maximum time<br />

for waiting on a semaphore. The tasks wait in FIFO order for a resource. A semaphore is created with<br />

RTX_CREATE_SEMservice call. The <strong>@CHIP</strong>-<strong>RTOS</strong> needs a unique four byte semaphore name and on<br />

success returns a new semaphore ID (or handle) to the caller. This handle is needed for the other<br />

semaphore services.<br />

Using a counting semaphore:<br />

A counting semaphore is created by specifying an initial count greater or equal to zero in the call to<br />

RTX_CREATE_SEM. If a semaphore is initialized with a value n, it can be used to control access to n<br />

resources, e.g. a counting semaphore with the initial value three assures that no more than three tasks<br />

can own a resource at any one time. Access to a resource controlled by a counting semaphore is<br />

acquired with a call to RTX_WAIT_SEMor RTX_GET_SEM. If the resource is available the <strong>@CHIP</strong>-<br />

<strong>RTOS</strong> gives it to the task immediately. When the task is finished using the resource, it signals its release<br />

by calling RTX_SIGNAL_SEM.<br />

Using a resource semaphore:<br />

A resource semaphore is created by specifying an initial count of -1 in the call of<br />

RTX_CREATE_SEM. The <strong>@CHIP</strong>-<strong>RTOS</strong> creates a resource semaphore and automatically gives it an<br />

initial value of one indicating that the resource is free. A resource is reserved by calling<br />

RTX_RESERVE_RESwith the semaphore ID returned by RTX_CREATE_SEM. The resource is released<br />

with a call of RTX_RELEASE_SEM.<br />

Semaphore Services:<br />

RTX_CREATE_SEM Create a semaphore<br />

RTX_DELETE_SEM Delete a semaphore<br />

RTX_FREE_RES Free a resource semaphore<br />

RTX_GET_SEM Get use of a counting semaphore(no wait)<br />

RTX_RELEASE_SEM Release a resource semaphore<br />

RTX_RESERVE_RES Reserve a resource semaphore<br />

RTX_SIGNAL_SEM Signal a counting semaphore<br />

RTX_WAIT_SEM Wait on a counting semaphore (optional timeout)<br />

Page 232 / 400

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!