Readers/writer locks exist only within the tis interface. There are no equivalent pthread or cma interfaces. That's ok, since you can use tis even if you build your program with DECthreads. Keep in mind that readers/writer locks are not currently portable-although the IEEE POSIX 1003.1j standard, currently in balloting, proposes a very similar set of functions that will eventually be made available in DECthreads.
Readers/writer locks routines can be used to control access to any shared resource and can be executed by a thread or program. Readers /writer locks are appropriate to protect information that needs to be read frequently and written only occasionally. For example, in a cache of recently accessed information, many threads may simultaneously examine the cache without conflict. When a thread needs to update the cache, it must have exclusive access.
A readers/writer lock can be locked for a shared read access, or for exclusive write access. A read access lock will block when any thread or program has an active write access. A write access will block when another thread currently has either write access or read access.
When both readers and writers are waiting for access at the same time, the readers are given precedence when the write lock is released. Read precedence favors concurrency because it potentially allows many threads to accomplish work simultaneously. Figure 4-1 illustrates the lock behavior on three threads (one writer and two readers) competing for the same memory object.
The tis_rwlock_init initializes a readers/writer lock by allocating and initializing the tis_rwlock_t structure.
You call tis_read_lock or tis_write_lock when access to the shared resource is required. tis_read_trylock and tis_write_trylock can also be called to acquire a lock, but if the resource is already locked by another caller, the trylock routine immediately returns EBUSY, rather than waiting.
Call tis_rwlock_destroy when you are finished using a readers/writer lock. tis_rwlock_destroy frees up readers/writer lock internal resources.
Use the following routines to manipulate the readers/writer locks:
Routine Description tis_rwlock_ Initializes a readers/writer lock. init tis_rwlock_ Destroys a readers/writer lock. destroy tis_read_ Acquires a read lock. lock tis_write_ Acquires a write lock. lock tis_read_ Tries to acquire a read lock without waiting. trylock tis_write_ Tries to acquire a write lock without waiting. trylock tis_read_ Unlocks the read lock. unlock tis_write_ Unlocks the write lock. unlock
For more information about each readers/writer tis routine, see Part III.