From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/7435 Path: news.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: [PATCH] Use CAS instead of atomic swap to implement spinlock Date: Sun, 19 Apr 2015 02:01:32 -0400 Message-ID: <20150419060132.GN6817@brightrain.aerifal.cx> References: <1429051493-2821-1-git-send-email-amonakov@ispras.ru> <20150419002433.GL6817@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1429423314 12569 80.91.229.3 (19 Apr 2015 06:01:54 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 19 Apr 2015 06:01:54 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-7448-gllmg-musl=m.gmane.org@lists.openwall.com Sun Apr 19 08:01:49 2015 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1YjiIc-0002s2-P5 for gllmg-musl@m.gmane.org; Sun, 19 Apr 2015 08:01:46 +0200 Original-Received: (qmail 10172 invoked by uid 550); 19 Apr 2015 06:01:45 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 10154 invoked from network); 19 Apr 2015 06:01:45 -0000 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:7435 Archived-At: On Sun, Apr 19, 2015 at 08:50:18AM +0300, Alexander Monakov wrote: > 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'? For C++ ABI purposes, I think switching to volatile int would be a different type. :( I wouldn't really be opposed to changing it for C and just having the ABI-compat type used when __cplusplus is defined. We already do that for pthread_t. Rich