Package Overview
Note: Riverbed also provides mutex KPs (described in “Mutex Sub-Package“ on page 5). These KPs have two advantages over the prg_mt functions: they provide ODB tracing and they become no-ops during sequential execution. Thus, you should use the KPs whenever possible.
In a parallel processing environment, it is possible to have multiple code sequences (processes) trying to access the same resource at the same time. If multiple processes try only to read the value of a resource, no special precautions are required. But if at least one of these processes tries to modify the value of a resource, a serialization mechanism is required to prevent concurrent writes (or a write concurrent with reads). Not preventing concurrent writes can result in incorrect modification and reading of the shared resource.
The “mutual exclusion” (mutex) serialization mechanism is used to coordinate access to a shared resource. Any process that wants to access the resource is required to first lock the associated mutex. When access to the shared resource is complete, the mutex is unlocked so it can be locked by another process.
The basic form of mutex allows at most one process to lock the mutex. Once the mutex is locked, it cannot be locked again by any process (including the one that already locked it) until it has been unlocked. Trying to lock a locked mutex will block the locking process until the mutex is unlocked. Each mutex maintains a list of requesting processes in the order in which they requested the lock. Note that a process that tries to lock a mutex it already locked would be blocked until the mutex is unlocked. Because only the process that locked the mutex can unlock it, the code would end up in a deadlocked state. The prg_mt package keeps track of which process locked which mutex and will prevent such deadlocked states.
It is also possible to create a deadlock if two processes lock the same set of mutexes in different order. Assume that processes P and Q are both trying to access a shared resource that requires locked mutexes A and B. If P locks A first then tries to lock B, while Q locks B first then tries to lock A, it can reach a state where both processes have locked one mutex and are trying to lock the other. To try to prevent this situation, the prg_mt package allows you to associate a rank with each mutex. Mutexes with associated ranks must be locked in decreasing rank order. Each process has a set of locked mutexes associated with it. If this process tries to lock mutex of rank R after locking a mutex of rank lower than R, the lock attempt will fail with a reported error message. This allows you to catch potential deadlock situations and correct them.
A variant form of mutex is the recursive mutex. It is similar to the base version except that the process that locked it can be locked many additional times without being deadlocked. For each recursive lock call, there must be a matching unlock call before the mutex can be locked by another process.
Another variant form of mutex is the reader/writer mutex. A process needs to indicate whether it is locking the mutex as a reader or a writer of the resource. While the mutex is locked by a reader, other readers can also lock the mutex and access the shared resource. On the other hand, at most one writer can access the shared resource at any time.
A built-in global mutex, with the highest possible rank, is available for times when it is undesirable or impossible to create a specific new mutex.
This package also supports a simple, non-recursive form of mutex called a spinlock. Spinlocks are a memory-efficient (but resource hungry) method for serializing access to shared data in parallel simulations. Whereas a mutex releases its CPU while blocked, a spinlock monopolizes its CPU by continuously checking for the lock to be released.