From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 2110 invoked by alias); 11 Jul 2012 08:25:25 -0000 Received: (qmail 2082 invoked by uid 22791); 11 Jul 2012 08:25:23 -0000 X-SWARE-Spam-Status: No, hits=-2.6 required=5.0 tests=AWL,BAYES_00,KHOP_THREADED X-Spam-Check-By: sourceware.org Received: from shards.monkeyblade.net (HELO shards.monkeyblade.net) (149.20.54.216) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 11 Jul 2012 08:25:10 +0000 Received: from localhost (74-93-104-98-Washington.hfc.comcastbusiness.net [74.93.104.98]) by shards.monkeyblade.net (Postfix) with ESMTPSA id 11647586A27; Wed, 11 Jul 2012 01:25:12 -0700 (PDT) Date: Wed, 11 Jul 2012 08:25:00 -0000 Message-Id: <20120711.012509.1325789838255235021.davem@davemloft.net> To: roland@hack.frob.com Cc: maxim@codesourcery.com, joseph@codesourcery.com, rdsandiford@googlemail.com, libc-ports@sourceware.org, libc-alpha@sourceware.org Subject: Re: [PATCH] Unify pthread_spin_[try]lock implementations. From: David Miller In-Reply-To: <20120711081441.73BB22C093@topped-with-meat.com> References: <65B470D2-4D01-4BA1-AEC5-A72C0006EA22@codesourcery.com> <20120711081441.73BB22C093@topped-with-meat.com> Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Mailing-List: contact libc-ports-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Post: List-Help: , Sender: libc-ports-owner@sourceware.org X-SW-Source: 2012-07/txt/msg00022.txt.bz2 From: Roland McGrath Date: Wed, 11 Jul 2012 01:14:41 -0700 (PDT) >> +int >> +pthread_spin_lock (pthread_spinlock_t *lock) >> +{ >> + while (atomic_compare_and_exchange_val_acq (lock, 1, 0) != 0) >> + while (*lock != 0) >> + ; > > What's the inner loop for? I guess the idea is to spin with non-atomic reads when the lock is contended so we don't do expensive bus cycles grabbing the cache line in exclusive state over and over again. If we spun using only the atomic it would be very expensive.