I donβt know anything 100% as your suggestion, but there are some existing interfaces that are close:
Many existing r / w locking APIs have a try lock interface, for example pthread_rwlock_trywrlock() on UN * X systems. They are not available and will only receive a lock if no one owns it and is not waiting for it.
Usually you use this rotation to block and / or artificially delay the attempt code (backtracking). That is, to have code like:
for (count = 0; count < MAX_SPINS && (locked = trywrlock(lock)); count++); if (locked) { delay(some_backoff); wrlock(lock); }
Unfortunately, this is not quite what you are asking for; it will receive the lock in order, but the low priority writer will rotate around the processor and / or receive it with a delay due to the (possibly unnecessary) delay.
The Solaris kernel has an rw_tryupgrade(9f) interface that can be used to check if the current thread is the only reader on the lock without a writer waiting, and if so, update the lock to exclusive / write, i.e. The code will look like this:
if (!rw_tryenter(lock, RW_WRITER)) { rw_enter(lock, RW_READER); if (!rw_tryupgrade(lock)) { } }
Which is a little closer to what you ask, but still not quite the same thing - if it fails, you will have to give up readlock, perhaps back out (wait), request a read, try to update. This process is twisted again.
In addition, many UNIX systems, at least in fact, execute waiter sessions in order of priority; therefore, you will need to make your thread the lowest priority (if necessary, artificially) before trying the usual pending call to wrlock() ; anyone else who wants the same log while your thread is waiting will receive it before that, due to the way the scheduler works. Although on multiprocessor / core systems that are not necessarily guaranteed ...
Finally, SymbianOS (version of Symbian ^ 3) has an RRWlock class that can be made to prioritize readers over writers, so that it deliberately starves writers if there are readers waiting / inbound. Again, the behavior you want is not entirely accurate, as it affects all authors of a given lock, not just a specific one.
I'm afraid you will have to write your own priority castle with two lines of awakening the writer.