|
Like Win32 semaphore objects, the RTSS 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. RTSS semaphores perform this test-and-set operation atomically; that is, when you request a resource from an RTSS 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 an RTSS semaphore's resource count, an RTSS semaphore, unlike an RTSS mutex, cannot be owned by a thread. This means that waiting for semaphore objects:
On systems where all waiting threads are scheduled to run on the same RTSS processor, the RTSS semaphore objects support a waiting priority. That is, when there are several threads with different priority waiting for an RTSS semaphore object, the system guarantees that the order of threads obtaining the semaphore object is the order of thread priority when the RTSS semaphore object is signaled.
NOTE: This behavior differs from the behavior of Win32 semaphore objects, which do not support waiting priority.
In dedicated mode, 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.
The RTSS semaphore object is always maintained in the RTSS environment. However, Win32 programs may create, open, release, and wait for RTSS semaphores. This allows cooperation between RTSS and Win32 processes. The RTSS semaphore name space is separate from the Win32 semaphore name space.
To synchronize threads running in multiple processes, including RTSS processes and Win32 processes, a thread in each process must have its own process-relative handle to a single RTSS semaphore object. These handles can be obtained by calling either RtCreateSemaphore or RtOpenSemaphore.