All synchronization objects subtype from $LOCK. In addition to primitive $LOCK classes, some synchronization classes return $LOCK objects to allow different kinds of locking. The concrete type of the returned object is dependent on the pSather implementation.
MUTEX is a simple mutual exclusion lock. Two threads may not simultaneously lock a MUTEX.
RW_LOCK is used to manage reader-writer synchronization, and defines two methods 'reader' and 'writer'. These return $LOCK objects. If 'rw' is an object of type RW_LOCK, then a lock on 'rw.reader' or 'rw.writer' blocks until no thread is locking on 'rw.writer', although multiple threads can simultaneously hold 'rw.reader'. Readers are granted priority over writers. Attempting to obtain a writer lock while holding the corresponding reader lock causes deadlock.
WR_LOCK and FRW_LOCK also manage reader-writer synchronization. WR_LOCK gives writes priority over reads, while FRW_LOCK grants readers and writers equal priority.
Classes under $ATTACH and $ATTACH{T} (See $ATTACH classes) also subtype from $LOCK.
Example 17-3. This code implements five dining philosophers. The philosophers are seated at a round table and forced to share a single chopstick with each neighbor. They alternate between eating and thinking, but eating requires both chopsticks.
chopsticks ::= #ARRAY{MUTEX}(5); loop chopsticks.set!(#MUTEX) end; parloop i::= 0.upto!(4); do loop think; lock when chopsticks[i], chopsticks[(i+1).mod(5)] then eat end; end; end; |