Semaphore Object
Note: Internal objects are created from the system's internal allocation space (IntMSpace).
Like Windows semaphore objects, the eRTOS semaphore objects are used for resource counting. They give a thread the ability to query the number of resources available; if one or more resources are available, the count of available resources is decreased. Semaphores perform this test-and-set operation atomically; that is, when you request a resource from a semaphore, the operating system checks whether the resource is available and decrements the count of available resources without letting another thread interfere. Only after the resource count has been decreased does the system allow another thread to request a resource.
Because several threads can affect a semaphore's resource count, a semaphore, unlike a mutex, cannot be owned by a thread. This means that waiting for semaphore objects:
- Will never return WAIT_ABANDONED
- May lose semaphore resources if threads are terminating
Semaphores and Thread Priority
On systems where all waiting threads are scheduled to run on the same processor, the semaphore objects support a waiting priority. That is, when there are several threads with different priority waiting for a semaphore object, the system guarantees that the order of threads obtaining the semaphore object is the order of thread priority when the semaphore object is signaled.
Note: This behavior differs from the behavior of Windows semaphore objects, which do not support waiting priority.
When waiting threads are scheduled to run on different processors, waiting priority is not guaranteed. RtReleaseSemaphore will signal waiting threads in the order of thread priority, through an inter-processor interrupt. However, inter-processor interrupt latency is not under software control. As a result, it is possible that a lower priority thread may wake up earlier than a higher priority thread if the processor scheduled to run the lower priority thread receives the inter-processor interrupt first.
Semaphore Synchronization
To synchronize threads running in multiple processes, a thread in each process must have its own process-relative handle to a single Process semaphore object. These handles can be obtained by calling either RtCreateSemaphore or RtOpenSemaphore.
Topics: