linux 读写锁
来源:互联网 发布:微融通网络借贷 编辑:程序博客网 时间:2024/05/16 15:21
This is the mail archive of the libc-alpha@sourceware.cygnus.commailing list for theglibc project.
Re: Another LinuxThreads bug.
- To: Xavier Leroy <Xavier dot Leroy at inria dot fr>
- Subject: Re: Another LinuxThreads bug.
- From: Kaz Kylheku <kaz at ashi dot footprints dot net>
- Date: Mon, 10 Jan 2000 11:06:22 -0800 (PST)
- cc: libc-alpha at sourceware dot cygnus dot com
On Mon, 10 Jan 2000, Xavier Leroy wrote:> Date: Mon, 10 Jan 2000 13:27:20 +0100> From: Xavier Leroy <Xavier.Leroy@inria.fr>> To: Kaz Kylheku <kaz@ashi.footprints.net>> Cc: Ulrich Drepper <drepper@cygnus.com>, Andreas Jaeger <aj@suse.de>> Subject: Re: Another LinuxThreads bug.> > > The problem is that the condition ``there is no write lock'' is> > taken as sufficient for placing another read lock. To correctly give> > precedence to writers, the condition should be ``there is no write> > lock, and there are no waiting writers''.> > > > The count of existing read locks need not be consulted at all, even> > if the lock prefers readers. A lock that prefers readers simply> > allows a read lock to be placed whenever there is no write lock, and> > ignores any waiting writers. This is reflected in the new logic,> > which tests the lock's type and breaks the loop.> > Right. The patch looks good. I think we can put it in 2.1.3.Unfortunately, upon reading the Single Unix Specification's descriptionof pthread_rwlock_rdlock, I have uncovered a problem.** The patch should *not* be applied, because it gives rise to another bug.*Here is the relevant wording: A thread may hold multiple concurrent read locks on rwlock (that is, successfully call the pthread_rwlock_rdlock() function n times). If so, the thread must perform matching unlocks (that is, it must call the pthread_rwlock_unlock() function n times).By making write-priority work correctly, I broke the above requirement, becauseI had no clue that recursive read locks are permissible.If a thread which holds a read lock tries to acquire another read lock, and nowone or more writers is waiting for a write lock, then the algorithm will leadto an obvious deadlock. The reader will be suspended, waiting for the writersto acquire and release the lock, and the writers will be suspended waitingfor every existing read lock to be released.Correctly implementing write-priority locks in LinuxThreads in the face of theabove requirement doesn't seem easy. The read lock function must distinguishwhether the calling thread owns a read lock. Since many threads can own a readlock, and one thread can own read locks on many objects, it appears that bethat a structure of size O(M * N) is needed track the ownership.The alternatives are: 1. Abandon support for writer-priority. Make this decision visible to the programmer, so that they aren't led into false expectation. 2. Get it working somehow. This requires a much more sophisticated change than my naive patch which does more harm then good.I suspect that it may be necessary to go with option 1 for release 2.1.3. However read-write locks that avoid writer starvation are nice to have, soresearch on 2 should begin immediately. Perhaps an elegant solution for thisexists already that can be adapted for LinuxThreads.A compromise might be to make read-priority locking default, and guide users touse the non-portable attribute for writer-priority if they want it, on thecondition that they can't use recursive read locks (which are brain-damagedanyway!) In that case, the patch *can* be applied, with the additional changethat the text in pthread.h which reads: #ifdef __USE_UNIX98 enum { PTHREAD_RWLOCK_PREFER_READER_NP, PTHREAD_RWLOCK_PREFER_WRITER_NP, PTHREAD_RWLOCK_DEFAULT_NP = PTHREAD_RWLOCK_PREFER_WRITER_NP }; #endif/* Unix98 */is changed to PTHREAD_RWLOCK_DEFAULT_NP = PTHREAD_RWLOCK_PREFER_READER_NPAccording to The Single Unix Specification, the behavior is unspecified when areader tries to place a lock, and there is no write lock but writers arewaiting. That is, there is no requirement that priority be given to writers.(But the unspecified behavior does not extend to permitting deadlock,I presume!!!).URL:http://www.opengroup.org/onlinepubs/007908799/xsh/pthread_rwlock_rdlock.html
- Follow-Ups:
- Re: Another LinuxThreads bug.
- From: Xavier Leroy
- Re: Another LinuxThreads bug.
=================================================================================================================
And there won't be any implementation.
Uli I think this question deserves a more complete explanation. This seems like a reasonable request. If you disagree then it would not hurt to explain why.There seems to be a valid concern related to reliable implementation of recursive read locks with writer priority. As in this discusion:http://sources.redhat.com/ml/libc-alpha/2000-01/msg00055.htmlIf this your concern then saying so would help resolve/close this issue.
Just a few questions on Read/Write
lock stuff since man pages don't exist (yet)
for linux.
1) Where can I find documentation, sample code,
or anything else that will help (eg. URLs etc.)
(Is it worth telneting to a machine with some
other OS and try the man pages there ???)
2) Can I treat the rwlock stuff same as a mutex
in terms of init/destroy/lock/unlock/trylock ???
I had a look at pthread.h and all the calls look
the same... (Is it basically a mutex that allows
multiple locks for readers?)
3) What's the story with overhead if you start using
r/w locks?
4) If you have many readers could that mean that the
writer will never get a chance to lock, or are the
locks first-come-first-serve ??? I'm thinking
(I know it's probably dim but...) if a reader can
always lock, there might be a case where there is
always at least one reader on the mutex. What
happnes if a writer comes along and rwlocks ???
(Assume default sched and priority settings).
Cheers,
---
Kostas Kostiadis.
[email : kko...@essex.ac.uk]
[WebPage : http://privatewww.essex.ac.uk/~kkosti/]
"A good traveller has no fixed plans
and is not intent on arriving."
Lao Tzu (570-490 BC)