Skip to content

Mutexes

Rich Neswold edited this page Apr 1, 2022 · 2 revisions

NOTE: Starting with v2.7, we introduced a nested, version namespace to enforce matching APIs when building and deploying. In the following code examples, we refer to the namespace as "VWPP". This translates to vwpp for pre-2.7 libraries and to vwpp::v2_7 for v2.7.

The Mutex API

The vwpp library defines two classes that provide serialization, VWPP::CountingSemaphore and VWPP::Mutex. These classes only allow instances to be created. To actually own and release these resources, you use Lock Objects.


Constructor

CountingSemaphore(int = 1)

The VWPP::CountingSemaphore class wraps the VxWorks' counting semaphore, which is a semaphore which allows more than one task access to a resource. The constructor parameter indicates how many tasks are allowed to own the semaphore. Further attempts to own the semaphore will block the task until any previous owner releases their ownership. The default count is 1, meaning one task can grab the semaphore at a time.

Why Deprecated?

This class is marked deprecated. After searching through front-end source code, it appears counting semaphores are perceived to be a more reliable form of synchronization than binary semaphores. The thought is that, when a task does a semTake() and expects to be woken up by a semGive() (or semFlush()), it may have just missed it and the notification would be lost. A counting semaphore would keep track of how many times the task should have been woken up. A correctly running system shouldn't see the semaphore count go above 1. Better solutions however, are to use Events or Condition Variables.


Constructor

Mutex()

A Mutex is used to protect data or hardware that can only be accessed by one task at a time. If multiple tasks are blocked on a mutex, the highest priority task will run next. A mutex protects against "priority inversion" where a low priority task prevents a high priority task from running. The mutex in this library also prevents a task from being deleted while it owns the mutex.

Clone this wiki locally