From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 25440 invoked by alias); 11 Jul 2012 09:02:43 -0000 Received: (qmail 25424 invoked by uid 22791); 11 Jul 2012 09:02:42 -0000 X-SWARE-Spam-Status: No, hits=-7.1 required=5.0 tests=AWL,BAYES_00,KHOP_RCVD_UNTRUST,KHOP_THREADED,RCVD_IN_DNSWL_HI,RCVD_IN_HOSTKARMA_W,SPF_HELO_PASS,T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 11 Jul 2012 09:02:23 +0000 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q6B92HFP030748 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 11 Jul 2012 05:02:17 -0400 Received: from zebedee.pink (ovpn-113-34.phx2.redhat.com [10.3.113.34]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id q6B92Dt5024078; Wed, 11 Jul 2012 05:02:14 -0400 Message-ID: <4FFD4114.9000806@redhat.com> Date: Wed, 11 Jul 2012 09:02:00 -0000 From: Andrew Haley User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20120430 Thunderbird/12.0.1 MIME-Version: 1.0 To: Maxim Kuvyrkov CC: David Miller , roland@hack.frob.com, joseph@codesourcery.com, rdsandiford@googlemail.com, libc-ports@sourceware.org, libc-alpha@sourceware.org Subject: Re: [PATCH] Unify pthread_spin_[try]lock implementations. References: <65B470D2-4D01-4BA1-AEC5-A72C0006EA22@codesourcery.com> <20120711081441.73BB22C093@topped-with-meat.com> <20120711.012509.1325789838255235021.davem@davemloft.net> <4FFD3CD9.4030206@redhat.com> <84304C03-6A49-4263-9016-05486EDC0E98@codesourcery.com> In-Reply-To: <84304C03-6A49-4263-9016-05486EDC0E98@codesourcery.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-IsSubscribed: yes 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/msg00026.txt.bz2 On 07/11/2012 09:53 AM, Maxim Kuvyrkov wrote: > On 11/07/2012, at 8:44 PM, Andrew Haley wrote: > >> On 07/11/2012 09:25 AM, David Miller wrote: >>> 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. >> >> Sure, but on ARM at least there's no guarantee that the local processor >> will see changes to the state of the lock when another processor frees >> it. > > Hm, but this is exactly what ARM port does and did for a while. I > guess the memory on the local processor eventually gets updated and > the loop breaks. Maybe the thread ends its time slice and gets interrupted. There aren't any guarantees. > This is an interesting point and maybe introducing a counter like > below will improve spinlocks for ARM. > > + int counter = 123456; > + while (atomic_compare_and_exchange_val_acq (lock, 1, 0) != 0) > + while (*lock != 0 && --counter) > + ; This is a much better idea than an unbounded spin. Andrew.