[Prev] [Contents] [Next]
Shared read/write locks are released at thread termination
If a thread is the owner of one or more shared read locks acquired by pthread_rwlock_unlock(), pthread_rwlock_tryrdlock() or pthread_rwlock_timedrdlock_np() functions, when that thread terminates, the shared read locks will be automatically released by the system. It is expected that if a thread holds a shared read lock, it will not be modifying the resources associated with that lock. It is then safe for the runtime to unlock the read lock without indicating an error condition or causing the process to wait. For performance reasons, your application should unlock all held locks before the thread ends.
If a thread is the owner of one or more exclusive write locks acquired by pthread_rwlock_wrlock(), pthread_rwlock_trywrlock() or pthread_rwlock_timedwrlock_np(), when that thread terminates, the exclusive write locks will NOT be automatically released by the system. This is an error in the application, and indicates that the data associated with the lock is in an inconsistent state. If another thread attempts to get a shared read or exclusive write lock on the lock, that thread will block forever.
Read/write locks can be upgraded / downgraded
The OS/400 implementation of read/write locks allow a thread to effectively `upgrade' a read lock to a write lock, or `downgrade' a write lock to a read lock without an intervening unlocked and unprotected section of code. The following items describe read/write lock behavior that allows this. This behavior is outside of the definition of the Single UNIX Specification. An application written to be portable to the Single UNIX Specification should not attempt to acquire a shared read lock and a shared write lock on the same read/write lock at the same time.
- If a thread currently holds a shared read lock, an attempt by the same thread to acquire an exclusive write lock will succeed if no other threads hold a shared read lock. The thread then holds both an exclusive write lock and a shared read lock.
- If a thread currently holds an exclusive write lock, an attempt by the thread to acquire a shared read lock will succeed. The thread then holds both an exclusive write lock and a shared read lock.
- In the case that a thread holds one or more shared read locks and one or more exclusive write locks on the same read/write lock object at the same time. A call to pthread_rwlock_unlock(), will always unlock the exclusive write lock FIRST.
- When multiple exclusive write locks and multiple exclusive read locks are held by the same thread on the same read/write lock object, the behavior of pthread_rwlock_unlock() is as follows:
- A call to the pthread_rwlock_unlock() function will always unlock the most recent exclusive write lock first.
- Subsequent calls to pthread_rwlock_unlock() will first reduce the count of any outstanding exclusive write locks held by the thread until all exclusive write locks are unlocked.
- After all outstanding exclusive write locks are unlocked, and the thread holds only shared read locks on the read/write lock object, a call to pthread_rwlock_unlock() function will then unlock the most recent shared read lock.
- Subsequent calls to pthread_rwlock_unlock() will reduce the count of any outstanding shared read locks held by the thread until all shared read locks are unlocked.
In order for a thread to `upgrade' a shared read lock to an exclusive write lock, the thread should perform the following:
{
pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
pthread_rwlock_rdlock(&rwlock);
...
/* Thread holding a read lock decides it needs to upgrade to a write lock */
/* Now Upgrade to write lock */
pthread_rwlock_wrlock(&rwlock);
...
/* write lock (and read lock) are held here.*/
/* We've effectively upgraded to a write lock */
...
/* `Downgrade' back to a only the read lock */
pthread_rwlock_unlock(&rwlock);
...
/* unlock the read lock */
pthread_rwlock_unlock(&rwlock);
}
{
pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
pthread_rwlock_wrlock(&rwlock);
..
/* Thread holding the write lock decides it needs to downgrade to a read lock */
/* Get the read lock, so we're holding BOTH read and write locks */
pthread_rwlock_rdlock(&rwlock);
...
/* An unlock always unlocks the write lock first */
pthread_wrlock_unlock(&rwlock);
...
/* At this point, we're only holding the read lock. */
/* We've effectively downgraded the write lock to a read lock */
...
/* Use unlock to unlock the last read lock. */
pthread_wrlock_unlock(&rwlock);
}
[Prev] [Contents] [Next]
Copyright © 1998, IBM Corporation. All rights
reserved.
Comments? Contact rchthrds@us.ibm.com