You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@subversion.apache.org by mark benedetto king <mb...@lowlatency.com> on 2003/09/08 15:26:01 UTC
Re: svn commit: rev 7004 - branches/svnserve-thread-pools/subversion/svnserve
On Mon, Sep 08, 2003 at 03:20:18AM -0500, brane@tigris.org wrote:
> Author: brane
> Date: Mon Sep 8 03:20:17 2003
> New Revision: 7004
>
> Modified:
> branches/svnserve-thread-pools/subversion/svnserve/main.c
> branches/svnserve-thread-pools/subversion/svnserve/serve.c
> Log:
> Merged changes from trunk between revisions 6668:7003.
>
I'm just wondering; is there more work to be done on this branch before it
can be merged into the trunk?
--ben
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Re: svn commit: rev 7004 - branches/svnserve-thread-pools/subversion/svnserve
Posted by Branko Čibej <br...@xbc.nu>.
Garrett Rooney wrote:
> Branko Čibej wrote:
>
>>> I don't recally any actual concerns being voiced...
>>
>>
>>
>> I simply wasn't ready to commit them, because those patches are broken.
>> Just FYI, a correct implementation of condition variables on Windows
>> doesn't look even remotely like what's in APR now, or what mbk's or my
>> patches look like. I know a couple of places where I could get such an
>> implementation, but the trouble is that the licenses they're distrbuted
>> under makes them unusable for APR. Which means that I can't even look at
>> them. I have seen the algorithm described, though, so I'm (slowly)
>> taking it from there.
>>
>
> I don't know if this implementation fixes whatever races you're
> concerned about (my win32-fu is weak), but you might want to take a
> look at the boost.threads condition class, as it seems to be under an
> acceptable license.
>
> http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/boost/boost/libs/thread/src/condition.cpp
>
Thanks, I've already done that. :-) The ones I haven't looked at are
pthreads-win32 (GPL) and ACE (some IBM license, supposed to be free but
you never know).
--
Brane Čibej <br...@xbc.nu> http://www.xbc.nu/brane/
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Re: svn commit: rev 7004 - branches/svnserve-thread-pools/subversion/svnserve
Posted by Garrett Rooney <ro...@electricjellyfish.net>.
Branko Čibej wrote:
>>I don't recally any actual concerns being voiced...
>
>
> I simply wasn't ready to commit them, because those patches are broken.
> Just FYI, a correct implementation of condition variables on Windows
> doesn't look even remotely like what's in APR now, or what mbk's or my
> patches look like. I know a couple of places where I could get such an
> implementation, but the trouble is that the licenses they're distrbuted
> under makes them unusable for APR. Which means that I can't even look at
> them. I have seen the algorithm described, though, so I'm (slowly)
> taking it from there.
>
I don't know if this implementation fixes whatever races you're
concerned about (my win32-fu is weak), but you might want to take a look
at the boost.threads condition class, as it seems to be under an
acceptable license.
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/boost/boost/libs/thread/src/condition.cpp
-garrett
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Re: [OT] Win32 condition variable implementation
Posted by Branko Čibej <br...@xbc.nu>.
Tobias Ringstrom wrote:
> Branko Čibej wrote:
>
>> Yes, but it doesn't implement the semantics of POSIX condition variables
>> -- it doesn't release and reacquire the mutex associated with the
>> condition whie waiting for the signal. The problem with these simple
>> inplementations on Windows has always been that
>>
>> release(mutex)
>> wait(event)
>> acquire(mutex)
>>
>> has a race condition because the release and wait can't be performed
>> atomically.
>
>
> Have you considered using SignalObjectAndWait?
Yes, I have. I'd prefer to do a generic implementation first, and
perhaps an optimized one for NT/2k/XP later.
--
Brane Čibej <br...@xbc.nu> http://www.xbc.nu/brane/
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Re: [OT] Win32 condition variable implementation
Posted by Tobias Ringstrom <to...@ringstrom.mine.nu>.
Branko Čibej wrote:
> Yes, but it doesn't implement the semantics of POSIX condition variables
> -- it doesn't release and reacquire the mutex associated with the
> condition whie waiting for the signal. The problem with these simple
> inplementations on Windows has always been that
>
> release(mutex)
> wait(event)
> acquire(mutex)
>
> has a race condition because the release and wait can't be performed
> atomically.
Have you considered using SignalObjectAndWait?
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/signalobjectandwait.asp
The only problem is that it's not available in Win 95/98/ME.
/Tobias
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Re: [OT] Win32 condition variable implementation
Posted by Branko Čibej <br...@xbc.nu>.
Alon Ziv wrote:
>Just a note from a lurker--
>I'm maintaining a home-grown OS, and we also have a "condition variable"
>construct. The implementation is along the lines of:
>
>wait(cond):
> e = new event
> lock(cond->mutex)
> cond->waiters = [cond->waiters, e]
> unlock(cond->mutex)
> wait(e)
>
>signal(cond):
> lock(cond->mutex)
> e = pop(cond->waiters)
> unlock(cond->mutex)
> signal(e)
>
>broadcast(cond):
> lock(cond->mutex)
> waiters = cond->waiters
> cond->waiters = []
> unlock(cond->mutex)
> for e in waiters:
> signal(e)
>
>(Sorry if I'm using too many Perl-isms in the Python-pseudocode...)
>
>The above is (as far as I know) free from any races, deadlocks, etc.
>
>
Yes, but it doesn't implement the semantics of POSIX condition variables
-- it doesn't release and reacquire the mutex associated with the
condition whie waiting for the signal. The problem with these simple
inplementations on Windows has always been that
release(mutex)
wait(event)
acquire(mutex)
has a race condition because the release and wait can't be performed
atomically.
--
Brane Čibej <br...@xbc.nu> http://www.xbc.nu/brane/
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
RE: [OT] Win32 condition variable implementation
Posted by Alon Ziv <al...@nolaviz.org>.
Just a note from a lurker--
I'm maintaining a home-grown OS, and we also have a "condition variable"
construct. The implementation is along the lines of:
wait(cond):
e = new event
lock(cond->mutex)
cond->waiters = [cond->waiters, e]
unlock(cond->mutex)
wait(e)
signal(cond):
lock(cond->mutex)
e = pop(cond->waiters)
unlock(cond->mutex)
signal(e)
broadcast(cond):
lock(cond->mutex)
waiters = cond->waiters
cond->waiters = []
unlock(cond->mutex)
for e in waiters:
signal(e)
(Sorry if I'm using too many Perl-isms in the Python-pseudocode...)
The above is (as far as I know) free from any races, deadlocks, etc.
-Alon Ziv
-----Original Message-----
From: mark benedetto king [mailto:mbk@lowlatency.com]
Sent: Wednesday, September 10, 2003 8:35 PM
To: Philip Martin
Cc: dev@subversion.tigris.org
Subject: Re: [OT] Win32 condition variable implementation
On Wed, Sep 10, 2003 at 05:18:53PM +0100, Philip Martin wrote:
>
> So a thread would do acquire(mutex) then wait(cond,mutex) and the
> wait() would immediately release(mutex)? The mutex doesn't seem to be
> protecting anything, it's a bit strange.
usage of a condition variable is generally along the lines of
enqueue(queue, item):
lock(queue->mutex)
append(list, item)
signal(queue->cond)
unlock(queue->mutex)
dequeue(queue):
lock(queue->mutex)
while list->size == 0:
wait(queue->cond, queue->mutex)
item = remove_head(list)
unlock(queue->mutex)
return item
The idea is that wait() gives up the mutex so that other threads can
act, but once wait() returns, this thread holds the mutex.
> Another concern, your algorithm has the broadcast thread blocking for
> an ack from each waiting thread. I suppose that may be acceptable
> POSIX behaviour, but it's less than ideal. Your first algorithm had a
> non-blocking broadcast, although it had other problems.
This is a valid concern. I could remove the spin-lock from the first
approach by using a second event, "cond->stable", that is initially
set, reset just before cond->event is set, and set just after
cond->event
is reset. I'm not sure that would be better, though, just different:
The first implementation had a bit of a "thundering herd" problem.
If N threads were waiting, even a single signal caused all N to wake,
and
N-1 to go back to sleep. This implementation has the opposite effect;
a single signal wakes only one waiter, but a broadcast could conceivably
cause quite a bit of context switching.
--ben
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Re: [OT] Win32 condition variable implementation
Posted by mark benedetto king <mb...@lowlatency.com>.
On Wed, Sep 10, 2003 at 05:18:53PM +0100, Philip Martin wrote:
>
> So a thread would do acquire(mutex) then wait(cond,mutex) and the
> wait() would immediately release(mutex)? The mutex doesn't seem to be
> protecting anything, it's a bit strange.
usage of a condition variable is generally along the lines of
enqueue(queue, item):
lock(queue->mutex)
append(list, item)
signal(queue->cond)
unlock(queue->mutex)
dequeue(queue):
lock(queue->mutex)
while list->size == 0:
wait(queue->cond, queue->mutex)
item = remove_head(list)
unlock(queue->mutex)
return item
The idea is that wait() gives up the mutex so that other threads can
act, but once wait() returns, this thread holds the mutex.
> Another concern, your algorithm has the broadcast thread blocking for
> an ack from each waiting thread. I suppose that may be acceptable
> POSIX behaviour, but it's less than ideal. Your first algorithm had a
> non-blocking broadcast, although it had other problems.
This is a valid concern. I could remove the spin-lock from the first
approach by using a second event, "cond->stable", that is initially
set, reset just before cond->event is set, and set just after cond->event
is reset. I'm not sure that would be better, though, just different:
The first implementation had a bit of a "thundering herd" problem.
If N threads were waiting, even a single signal caused all N to wake, and
N-1 to go back to sleep. This implementation has the opposite effect;
a single signal wakes only one waiter, but a broadcast could conceivably
cause quite a bit of context switching.
--ben
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Re: [OT] Win32 condition variable implementation
Posted by Philip Martin <ph...@codematters.co.uk>.
mark benedetto king <mb...@lowlatency.com> writes:
> On Wed, Sep 10, 2003 at 04:13:37PM +0100, Philip Martin wrote:
>> thread 1 waits
>> thread 2 waits
>> thread 3 broadcasts
>> thread 1 wakes up and runs
>> thread 1 acquires mutex and calls wait
>> thread 1 blocks waiting for cond->mutex held by thread 3
>> thread 2 tries to wake up but blocks waiting for mutex held by thread 1
>
> Aha! I knew there was something suspicious about the use of mutex.
>
> What if we were to move "release(mutex)" to the first line of wait()?
So a thread would do acquire(mutex) then wait(cond,mutex) and the
wait() would immediately release(mutex)? The mutex doesn't seem to be
protecting anything, it's a bit strange.
Another concern, your algorithm has the broadcast thread blocking for
an ack from each waiting thread. I suppose that may be acceptable
POSIX behaviour, but it's less than ideal. Your first algorithm had a
non-blocking broadcast, although it had other problems.
--
Philip Martin
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Re: [OT] Win32 condition variable implementation
Posted by mark benedetto king <mb...@lowlatency.com>.
On Wed, Sep 10, 2003 at 04:13:37PM +0100, Philip Martin wrote:
> mark benedetto king <mb...@lowlatency.com> writes:
>
> > wait(cond,mutex):
> > acquire(cond->mutex)
> > cond->num_waiters++
> > release(cond->mutex)
> > release(mutex)
> > down(cond->sem)
> > up(cond->ack)
> > acquire(mutex)
> >
> > deliver(cond,count):
> > up(cond->sem, count)
> > down(cond->ack, count)
> > count->num_waiters -= count
> >
> > signal(cond):
> > acquire(cond->mutex)
> > deliver(cond, min(1, cond->num_waiters))
> > release(cond->mutex)
> >
> > broadcast(cond):
> > acquire(cond->mutex)
> > deliver(cond, cond->num_waiters);
> > release(cond->mutex)
>
> thread 1 waits
> thread 2 waits
> thread 3 broadcasts
> thread 1 wakes up and runs
> thread 1 acquires mutex and calls wait
> thread 1 blocks waiting for cond->mutex held by thread 3
> thread 2 tries to wake up but blocks waiting for mutex held by thread 1
Aha! I knew there was something suspicious about the use of mutex.
What if we were to move "release(mutex)" to the first line of wait()?
--ben
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Re: [OT] Win32 condition variable implementation
Posted by Philip Martin <ph...@codematters.co.uk>.
mark benedetto king <mb...@lowlatency.com> writes:
> A counting semaphore keeps an internal counter; we'll call it num_avail.
> This is a spin-lock implementation:
Much as I imagined.
> Because thread 3 is still holding cond->mutex, thread 1 cannot
> actually wait until the broadcast is complete.
I see it now, thanks.
--
Philip Martin
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Re: [OT] Win32 condition variable implementation
Posted by mark benedetto king <mb...@lowlatency.com>.
On Wed, Sep 10, 2003 at 03:07:21PM +0100, Philip Martin wrote:
> mark benedetto king <mb...@lowlatency.com> writes:
>
> > On Wed, Sep 10, 2003 at 08:29:39AM -0400, mark benedetto king wrote:
> >
> > wait(cond,mutex):
> > acquire(cond->mutex)
> > cond->num_waiters++
> > release(cond->mutex)
> > release(mutex)
> > down(cond->sem)
> > up(cond->ack)
> > acquire(mutex)
> >
> > deliver(cond,count):
> > up(cond->sem, count)
> > down(cond->ack, count)
> > count->num_waiters -= count
> >
> > signal(cond):
> > acquire(cond->mutex)
> > deliver(cond, min(1, cond->num_waiters))
> > release(cond->mutex)
> >
> > broadcast(cond):
> > acquire(cond->mutex)
> > deliver(cond, cond->num_waiters);
> > release(cond->mutex)
>
> I am not familiar with the sematics of the Win32 "counting
> semaphores", your up/down in the algorithm above, but that algorithm
A binary semaphore has a single binary value, we'll call it "avail".
This is a spin-lock implementation:
down(s):
while 1:
acquire(s->mutex)
if s->avail:
s->avail = 0;
release(s->mutex)
return
release(s->mutex)
up(s):
acquire(s->mutex)
s->avail = 1;
release(s->mutex)
A counting semaphore keeps an internal counter; we'll call it num_avail.
This is a spin-lock implementation:
down(s, n):
while 1:
acquire(s->mutex)
if s->avail >= n:
s->avail -= n;
release(s->mutex)
return
release(s->mutex)
up(s,n):
acquire(s->mutex)
s->avail += n
release(s->mutex)
> doesn't look right. What behaviour you are trying to achieve, are you
> after the POSIX behaviour where broadcast wakes every waiting thread?
> What about this scenario
Yes. I accomplish that by delivering N signals, where N is the number
of waiters.
> thread 1 waits
> thread 2 waits
> thread 3 broadcasts
> thread 1 wakes up and runs
> thread 1 waits again
Because thread 3 is still holding cond->mutex, thread 1 cannot
actually wait until the broadcast is complete. Broadcast and signal
do not release cond->mutex until all waiters have been woken up.
Once thread 2 wakes and calls up(cond->ack, 1), the down() in deliver
(called from broadcast) will return, and thread 3 will release
cond->mutex. At this point, thread 1 will be allowed to wait.
--ben
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Re: [OT] Win32 condition variable implementation
Posted by Philip Martin <ph...@codematters.co.uk>.
mark benedetto king <mb...@lowlatency.com> writes:
> wait(cond,mutex):
> acquire(cond->mutex)
> cond->num_waiters++
> release(cond->mutex)
> release(mutex)
> down(cond->sem)
> up(cond->ack)
> acquire(mutex)
>
> deliver(cond,count):
> up(cond->sem, count)
> down(cond->ack, count)
> count->num_waiters -= count
>
> signal(cond):
> acquire(cond->mutex)
> deliver(cond, min(1, cond->num_waiters))
> release(cond->mutex)
>
> broadcast(cond):
> acquire(cond->mutex)
> deliver(cond, cond->num_waiters);
> release(cond->mutex)
thread 1 waits
thread 2 waits
thread 3 broadcasts
thread 1 wakes up and runs
thread 1 acquires mutex and calls wait
thread 1 blocks waiting for cond->mutex held by thread 3
thread 2 tries to wake up but blocks waiting for mutex held by thread 1
deadlock
--
Philip Martin
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Re: [OT] Win32 condition variable implementation
Posted by Philip Martin <ph...@codematters.co.uk>.
mark benedetto king <mb...@lowlatency.com> writes:
> On Wed, Sep 10, 2003 at 08:29:39AM -0400, mark benedetto king wrote:
>
> wait(cond,mutex):
> acquire(cond->mutex)
> cond->num_waiters++
> release(cond->mutex)
> release(mutex)
> down(cond->sem)
> up(cond->ack)
> acquire(mutex)
>
> deliver(cond,count):
> up(cond->sem, count)
> down(cond->ack, count)
> count->num_waiters -= count
>
> signal(cond):
> acquire(cond->mutex)
> deliver(cond, min(1, cond->num_waiters))
> release(cond->mutex)
>
> broadcast(cond):
> acquire(cond->mutex)
> deliver(cond, cond->num_waiters);
> release(cond->mutex)
I am not familiar with the sematics of the Win32 "counting
semaphores", your up/down in the algorithm above, but that algorithm
doesn't look right. What behaviour you are trying to achieve, are you
after the POSIX behaviour where broadcast wakes every waiting thread?
What about this scenario
thread 1 waits
thread 2 waits
thread 3 broadcasts
thread 1 wakes up and runs
thread 1 waits again
thread 1 wakes up and runs
thread 1 waits again
The broadcast didn't wake thread 2. That could be enough to cause
deadlock if the application relied on POSIX behaviour.
--
Philip Martin
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Re: [OT] Win32 condition variable implementation
Posted by mark benedetto king <mb...@lowlatency.com>.
On Wed, Sep 10, 2003 at 08:29:39AM -0400, mark benedetto king wrote:
> I give up for today. :-)
>
I lied. But I promise that this is the last one. :-)
wait(cond,mutex):
acquire(cond->mutex)
cond->num_waiters++
release(cond->mutex)
release(mutex)
down(cond->sem)
up(cond->ack)
acquire(mutex)
deliver(cond,count):
up(cond->sem, count)
down(cond->ack, count)
count->num_waiters -= count
signal(cond):
acquire(cond->mutex)
deliver(cond, min(1, cond->num_waiters))
release(cond->mutex)
broadcast(cond):
acquire(cond->mutex)
deliver(cond, cond->num_waiters);
release(cond->mutex)
--ben
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Re: [OT] Win32 condition variable implementation
Posted by mark benedetto king <mb...@lowlatency.com>.
On Wed, Sep 10, 2003 at 08:22:48AM -0400, mark benedetto king wrote:
>
> wait(cond,mutex):
> acquire(cond->mutex)
> cond->num_waiters++
> release(cond->mutex)
> release(mutex)
> down(cond->sem)
> up(cond->ack)
> acquire(cond->mutex)
> cond->num_waiters--
> release(cond->mutex)
> acquire(mutex)
>
> signal(cond):
> acquire(cond->mutex)
> up(cond->sem,min(1, cond->num_waiters))
> down(cond->ack,min(1, cond->num_waiters))
> release(cond->mutex)
>
> broadcast(cond):
> acquire(cond->mutex)
> up(cond->sem,cond->num_waiters)
> down(cond->ack,cond->num_waiters);
> release(cond->mutex)
>
I spoke too soon; this implementation has a problem with
Thread1 Thread2
wait()
signal()
signal()
There is an interleaving that causes the second signal() to hang, waiting
for a waiter that can never come, since Thread2 holds cond->mutex.
I give up for today. :-)
--ben
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Re: [OT] Win32 condition variable implementation
Posted by Branko Čibej <br...@xbc.nu>.
Now, before anyone breaks their back trying to reinvent the wheel, take
a look at this:
http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
--
Brane Čibej <br...@xbc.nu> http://www.xbc.nu/brane/
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
[OT] Win32 condition variable implementation
Posted by mark benedetto king <mb...@lowlatency.com>.
On Wed, Sep 10, 2003 at 04:29:52AM +0200, Branko ??ibej wrote:
> >
> >wait(cond, mutex):
> > # section 1:
> > # wait for pending signals to be delivered
> > while 1:
> > acquire(cond->mutex)
> > if !cond->num_undelivered:
> > break
> > release(cond->mutex)
> >
> >
> And here's the problem. This is a busy-wait loop that'll drive the CPU
> load to 100%, and you most definitely don't want condition variables to
> behave that way.
>
True; it assumes that the scheduler won't grant the mutex to the same
thread again and again when other threads are ready to run.
Since Win32 has "counting semaphores", another approach would be to two
semaphores and a mutex instead of an event and a mutex. This may well be
overkill.
wait(cond,mutex):
acquire(cond->mutex)
cond->num_waiters++
release(cond->mutex)
release(mutex)
down(cond->sem)
up(cond->ack)
acquire(cond->mutex)
cond->num_waiters--
release(cond->mutex)
acquire(mutex)
signal(cond):
acquire(cond->mutex)
up(cond->sem,min(1, cond->num_waiters))
down(cond->ack,min(1, cond->num_waiters))
release(cond->mutex)
broadcast(cond):
acquire(cond->mutex)
up(cond->sem,cond->num_waiters)
down(cond->ack,cond->num_waiters);
release(cond->mutex)
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Re: svn commit: rev 7004 - branches/svnserve-thread-pools/subversion/svnserve
Posted by Branko Čibej <br...@xbc.nu>.
mark benedetto king wrote:
>On Tue, Sep 09, 2003 at 10:33:06PM +0200, Branko ??ibej wrote:
>
>
>>mbk: After a quick glance at your patch, I can say that you're making
>>the same mistakes I did in my patches. :-)
>>
>>
>>
>
>I'm not offended (proud to be in such good company, even), but I am
>wondering what mistakes you saw.
>
>Error handling aside (and some minor optimizations removed), things are
>essentially:
>
>wait(cond, mutex):
> # section 1:
> # wait for pending signals to be delivered
> while 1:
> acquire(cond->mutex)
> if !cond->num_undelivered:
> break
> release(cond->mutex)
>
>
And here's the problem. This is a busy-wait loop that'll drive the CPU
load to 100%, and you most definitely don't want condition variables to
behave that way.
--
Brane Čibej <br...@xbc.nu> http://www.xbc.nu/brane/
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Re: svn commit: rev 7004 -
branches/svnserve-thread-pools/subversion/svnserve
Posted by Philip Martin <ph...@codematters.co.uk>.
mark benedetto king <mb...@lowlatency.com> writes:
> I'm not offended (proud to be in such good company, even), but I am
> wondering what mistakes you saw.
>
> Error handling aside (and some minor optimizations removed), things are
> essentially:
>
> wait(cond, mutex):
> # section 1:
> # wait for pending signals to be delivered
> while 1:
> acquire(cond->mutex)
> if !cond->num_undelivered:
> break
> release(cond->mutex)
That loop could be waiting for other threads to make progress and yet
it doesn't block, instead it relies on release/acquire being
sufficient to allow the other threads to proceed. While it probably
works, it's not particularly elegant.
--
Philip Martin
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Re: svn commit: rev 7004 - branches/svnserve-thread-pools/subversion/svnserve
Posted by mark benedetto king <mb...@lowlatency.com>.
On Tue, Sep 09, 2003 at 10:33:06PM +0200, Branko ??ibej wrote:
>
> mbk: After a quick glance at your patch, I can say that you're making
> the same mistakes I did in my patches. :-)
>
I'm not offended (proud to be in such good company, even), but I am
wondering what mistakes you saw.
Error handling aside (and some minor optimizations removed), things are
essentially:
wait(cond, mutex):
# section 1:
# wait for pending signals to be delivered
while 1:
acquire(cond->mutex)
if !cond->num_undelivered:
break
release(cond->mutex)
# section 2:
# add self as waiter
cond->num_waiters++
release(mutex)
release(cond->mutex)
# section 3:
# wait for a signal
while 1:
wait(cond->event)
acquire(cond->mutex)
if cond->num_undelivered:
cond->num_undelivered--
if !cond->num_undelivered:
reset(cond->event)
cond->num_waiters--;
release(cond->mutex)
acquire(mutex)
return
release(cond->mutex)
signal(cond):
acquire(cond->mutex)
cond->num_undelivered = min(cond->num_waiters, cond->num_undelivered + 1)
set(cond->event)
release(cond->mutex)
broadcast(cond):
acquire(cond->mutex)
cond->num_undelivered = cond->num_waiters
set(cond->event)
release(cond->mutex)
If a thread enters section 1 of wait(), it cannot proceed until all pending
signals have been delivered, and when it does, it holds cond->mutex, so
no more signals may be delivered until after it completes section 2.
Upon entering section 3, it is possible that an event has already been
signalled. If so, execution may proceed (or, obviously, be pre-empted).
If not, the thread sleeps. It does not hold cond->mutex, so signals
may occur.
Once a signal occurs, one or more threads will wake up and compete for the
signal. It is important to note that they may not all wake up immediately;
cond->event must not be reset until all signals have been delivered.
Each one of these threads will attempt to acquire cond->mutex. Once a
thread holds the mutex, it may examine cond->num_undelivered to determine
if there are, in fact, undelivered signals. If so, it may decrement
the count (and reset the event if the count has gone to zero), and remove
itself from the wait count.
--ben
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Re: svn commit: rev 7004 - branches/svnserve-thread-pools/subversion/svnserve
Posted by Branko Čibej <br...@xbc.nu>.
Garrett Rooney wrote:
> [removed svn@subversion.tigris.org, I'm tired of getting this on both
> lists]
>
> mark benedetto king wrote:
>
>> On Mon, Sep 08, 2003 at 06:24:03PM +0200, Branko ??ibej wrote:
>>
>>> Sort of. What we need is a reliable Win32 implementation of condition
>>> variables in APR -- the current one is full of holes and races -- which
>>> is far from trivial to do. Without that, svnserve would be broken on
>>> Windows if we merged this to the trunk.
>>>
>>
>>
>> Attached is a COMPLETELY UNTESTED patch against APR's HEAD. Care to
>> take
>> a glance?
>
>
> I believe Branko posted a couple of patches to dev@apr.apache.org
> which fixed the problem, but for some reason they were never committed.
>
> Was it just lack of someone to review them?
No, the problem is those patchis *didn't* fix the problem -- they still
have race conditions. A lot less than what's currently there, but this
is an all-or-nothing situation.
mbk: After a quick glance at your patch, I can say that you're making
the same mistakes I did in my patches. :-)
> I don't recally any actual concerns being voiced...
I simply wasn't ready to commit them, because those patches are broken.
Just FYI, a correct implementation of condition variables on Windows
doesn't look even remotely like what's in APR now, or what mbk's or my
patches look like. I know a couple of places where I could get such an
implementation, but the trouble is that the licenses they're distrbuted
under makes them unusable for APR. Which means that I can't even look at
them. I have seen the algorithm described, though, so I'm (slowly)
taking it from there.
--
Brane Čibej <br...@xbc.nu> http://www.xbc.nu/brane/
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Re: svn commit: rev 7004 - branches/svnserve-thread-pools/subversion/svnserve
Posted by Garrett Rooney <ro...@electricjellyfish.net>.
[removed svn@subversion.tigris.org, I'm tired of getting this on both lists]
mark benedetto king wrote:
> On Mon, Sep 08, 2003 at 06:24:03PM +0200, Branko ??ibej wrote:
>
>>Sort of. What we need is a reliable Win32 implementation of condition
>>variables in APR -- the current one is full of holes and races -- which
>>is far from trivial to do. Without that, svnserve would be broken on
>>Windows if we merged this to the trunk.
>>
>
>
> Attached is a COMPLETELY UNTESTED patch against APR's HEAD. Care to take
> a glance?
I believe Branko posted a couple of patches to dev@apr.apache.org which
fixed the problem, but for some reason they were never committed.
Was it just lack of someone to review them?
I don't recally any actual concerns being voiced...
-garrett
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Re: svn commit: rev 7004 - branches/svnserve-thread-pools/subversion/svnserve
Posted by mark benedetto king <mb...@lowlatency.com>.
On Mon, Sep 08, 2003 at 06:24:03PM +0200, Branko ??ibej wrote:
> Sort of. What we need is a reliable Win32 implementation of condition
> variables in APR -- the current one is full of holes and races -- which
> is far from trivial to do. Without that, svnserve would be broken on
> Windows if we merged this to the trunk.
>
Attached is a COMPLETELY UNTESTED patch against APR's HEAD. Care to take
a glance?
The error handling is (fundamentally) broken, because if it has an error
obtaining the condition variable mutex, it cannot update the number of
waiters in the condition variable to reflect the fact that *this* waiter
is no longer waiting. The timeout handling is also broken, because it
should be sensitive to accumulated timeslices, and it isn't. In these
respects, though, it is no worse than the existing implementation.
It may of course be broken in other ways, as well. It might not even
compile!
--ben
Log Message:
Cleanup condition variable implementation; avoid several potential race
conditions (hopefully without creating new ones!). Most importantly:
deliver exactly the right number of signals to only those threads who
were actually waiting at the time of the signal/broadcast.
* include/arch/win32/apr_arch_thread_cond.h
Remove "signal_all" member, and replace "signalled" variable with
"num_undelivered".
* locks/win32/thread_cond.c
Overhaul.
Index: include/arch/win32/apr_arch_thread_cond.h
===================================================================
RCS file: /home/cvspublic/apr/include/arch/win32/apr_arch_thread_cond.h,v
retrieving revision 1.1
diff -r1.1 apr_arch_thread_cond.h
64d63
< int signal_all;
66c65
< int signalled;
---
> int num_undelivered;
Index: locks/win32/thread_cond.c
===================================================================
RCS file: /home/cvspublic/apr/locks/win32/thread_cond.c,v
retrieving revision 1.12
diff -r1.12 thread_cond.c
78c78
< (*cond)->signal_all = 0;
---
> (*cond)->num_undelivered = 0;
87a88
> /* wait for undelivered signals to be delivered */
93c94,95
< cond->num_waiting++;
---
> if (cond->num_undelivered == 0)
> break;
94a97,99
> }
> cond->num_waiters++;
> ReleaseMutex(cond->mutex);
96c101,103
< apr_thread_mutex_unlock(mutex);
---
> /* wait for a signal */
> apr_thread_mutex_unlock(mutex);
> while (1) {
98d104
< cond->num_waiting--;
101c107,109
< ReleaseMutex(cond->mutex);
---
> apr_thread_mutex_lock(mutex);
> /* XXX: disaster! we don't hold the cond->mutex! */
> cond->num_waiters--;
104,108c112,118
< if (cond->signal_all) {
< if (cond->num_waiting == 0) {
< ResetEvent(cond->event);
< }
< break;
---
> res = WaitForSingleObject(cond->mutex, INFINITE);
> if (res != WAIT_OBJECT_0) {
> apr_status_t rv = apr_get_os_error();
> apr_thread_mutex_lock(mutex);
> /* XXX: disaster! we don't hold the cond->mutex! */
> cond->num_waiters--;
> return rv;
110,113c120,132
< if (cond->signalled) {
< cond->signalled = 0;
< ResetEvent(cond->event);
< break;
---
> switch (cond->num_undelivered) {
> case 0:
> ReleaseMutex(cond->mutex);
> continue;
> case 1:
> ResetEvent(cond->event);
> /* fall through */
> default:
> cond->num_undelivered--;
> cond->num_waiters--;
> ReleaseMutex(cond->mutex);
> apr_thread_mutex_lock(mutex);
> return APR_SUCCESS;
115d133
< ReleaseMutex(cond->mutex);
117,118d134
< apr_thread_mutex_lock(mutex);
< return APR_SUCCESS;
120a137,140
> /* XXX: the timeout is not implemented correctly at all, since there is a
> potential to wait an unbounded amount of time on a frequently signalled
> condition variable */
>
125d144
< DWORD res;
126a146
> DWORD res;
127a148
> /* wait for undelivered signals to be delivered */
131c152
< if (res == WAIT_TIMEOUT) {
---
> if (res == WAIT_TIMEOUT)
133d153
< }
136c156,157
< cond->num_waiting++;
---
> if (cond->num_undelivered == 0)
> break;
137a159,161
> }
> cond->num_waiters++;
> ReleaseMutex(cond->mutex);
139c163,165
< apr_thread_mutex_unlock(mutex);
---
> /* wait for a signal */
> apr_thread_mutex_unlock(mutex);
> while (1) {
141d166
< cond->num_waiting--;
144d168
< ReleaseMutex(cond->mutex);
146c170,172
< if (res == WAIT_TIMEOUT) {
---
> /* XXX: disaster! we don't hold the cond->mutex! */
> cond->num_waiters--;
> if (res == WAIT_TIMEOUT)
148,149c174
< }
< return apr_get_os_error();
---
> return rv;
151,155c176,184
< if (cond->signal_all) {
< if (cond->num_waiting == 0) {
< ResetEvent(cond->event);
< }
< break;
---
> res = WaitForSingleObject(cond->mutex, timeout_ms);
> if (res != WAIT_OBJECT_0) {
> apr_status_t rv = apr_get_os_error();
> apr_thread_mutex_lock(mutex);
> /* XXX: disaster! we don't hold the cond->mutex! */
> cond->num_waiters--;
> if (res == WAIT_TIMEOUT)
> return APR_TIMEUP;
> return rv;
157,160c186,198
< if (cond->signalled) {
< cond->signalled = 0;
< ResetEvent(cond->event);
< break;
---
> switch (cond->num_undelivered) {
> case 0:
> ReleaseMutex(cond->mutex);
> continue;
> case 1:
> ResetEvent(cond->event);
> /* fall through */
> default:
> cond->num_undelivered--;
> cond->num_waiters--;
> ReleaseMutex(cond->mutex);
> apr_thread_mutex_lock(mutex);
> return APR_SUCCESS;
162d199
< ReleaseMutex(cond->mutex);
164,165d200
< apr_thread_mutex_lock(mutex);
< return APR_SUCCESS;
167a203
>
177,180c213,217
< cond->signalled = 1;
< res = SetEvent(cond->event);
< if (res == 0) {
< rv = apr_get_os_error();
---
> if (cond->num_waiters) {
> cond->num_undelivered++;
> if (cond->num_undelivered > cond->num_waiters)
> cond->num_undelivered = cond->num_waiters;
> SetEvent(cond->event);
182a220
>
195,199c233,235
< cond->signalled = 1;
< cond->signal_all = 1;
< res = SetEvent(cond->event);
< if (res == 0) {
< rv = apr_get_os_error();
---
> if (cond->num_waiters) {
> cond->num_undelivered = cond->num_waiters;
> SetEvent(cond->event);
201a238
>
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Re: svn commit: rev 7004 - branches/svnserve-thread-pools/subversion/svnserve
Posted by Branko Čibej <br...@xbc.nu>.
mark benedetto king wrote:
>On Mon, Sep 08, 2003 at 03:20:18AM -0500, brane@tigris.org wrote:
>
>
>>Author: brane
>>Date: Mon Sep 8 03:20:17 2003
>>New Revision: 7004
>>
>>Modified:
>> branches/svnserve-thread-pools/subversion/svnserve/main.c
>> branches/svnserve-thread-pools/subversion/svnserve/serve.c
>>Log:
>>Merged changes from trunk between revisions 6668:7003.
>>
>>
>>
>
>I'm just wondering; is there more work to be done on this branch before it
>can be merged into the trunk?
>
>
Sort of. What we need is a reliable Win32 implementation of condition
variables in APR -- the current one is full of holes and races -- which
is far from trivial to do. Without that, svnserve would be broken on
Windows if we merged this to the trunk.
--
Brane Čibej <br...@xbc.nu> http://www.xbc.nu/brane/
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org