I have a problem regarding the behaviour of the pthread function pthread_rwlock_wrlock. The specification linked above states that when one thread has locked the lock for writing and the same thread locks it again, it results in undefined behaviour (I could actually observe this in that on x86 Linux calling this function is a noop and on PowerPC Linux it stalls the thread).
The behaviour I need would be a read write lock that has the following characteristics:
- read-locking by a thread succeeds if:
- the lock is not held by any thread
- the lock is only read-locked by zero or more threads (including the calling thread) and possibly read- or write locked by the calling thread
- write-locking succeeds when:
- the lock is not held by any other thread
- only the current thread is holding the lock (for reading or writing)
With a pthread_mutex_t
, the recursiveness of the lock can be controlled via an initialization flag, but this is not possible for pthread_rwlock_t
.
What are my options? I've never actually had to implement this kind of concurrency primitive in C, and I think I'm missing some obvious solution here.