Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Sun, 19 Apr 2015 08:50:18 +0300 (MSK)
From: Alexander Monakov <amonakov@...ras.ru>
To: musl@...ts.openwall.com
Subject: Re: [PATCH] Use CAS instead of atomic swap to implement
 spinlock

On Sat, 18 Apr 2015, Rich Felker wrote:

> On Wed, Apr 15, 2015 at 01:44:53AM +0300, Alexander Monakov wrote:
> > This should allow spinning without constantly dirtying cache lines holding the
> > spinlock value.  On architectures without native atomic swap, musl implement
> > a_swap by looping around a_cas.
> > ---
> > If I'm not mistaken this was also suggested by nsz on IRC.
> > 
> >  src/thread/pthread_spin_lock.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/src/thread/pthread_spin_lock.c b/src/thread/pthread_spin_lock.c
> > index df575f0..dabcb31 100644
> > --- a/src/thread/pthread_spin_lock.c
> > +++ b/src/thread/pthread_spin_lock.c
> > @@ -2,6 +2,6 @@
> >  
> >  int pthread_spin_lock(pthread_spinlock_t *s)
> >  {
> > -	while (a_swap(s, 1)) a_spin();
> > +	while (a_cas(s, 0, 1)) a_spin();
> >  	return 0;
> >  }
> 
> Would it perhaps be better to do something like this?
> 
> while (*(volatile int *)s || a_cas(s, 0, 1)) a_spin();

I think so, Yes.  Is the cast required, or is it possible to change the
pthread_spinlock_t typedef to 'volatile int'?

Thanks.
Alexander

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.