From patchwork Thu Feb 26 21:35:46 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 444073 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 20F2F1400B6 for ; Fri, 27 Feb 2015 08:36:04 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass reason="1024-bit key; unprotected key" header.d=sourceware.org header.i=@sourceware.org header.b=IMVenUst; dkim-adsp=none (unprotected policy); dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:message-id:date:from:mime-version:to:subject :content-type:content-transfer-encoding; q=dns; s=default; b=ani h07+AHm99A7BAZ9nNNLLHeH/OR8NgkKBTR8n28H8YgR8ZMulSCWDzl+JvQJyr7js njUMNCWOiz7ac8ocyLvbnN8mdaFZCtPOecRlcj5Gl5AfrYnLnqZSw5mR1T9i+wjg VZyTBxs1e6vztLBvJ17dbZACbodZEI9ZCAJGAUF0= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:message-id:date:from:mime-version:to:subject :content-type:content-transfer-encoding; s=default; bh=pDXsuFjll YDzvZSr2OleG0NW3vw=; b=IMVenUstVAEyKOJa2pzPqUY09JTRWJzz4QAFZ+GhF NbqBbQUdXsHZDhKnJiJiSfIerF//NBsNRROZf0de9Op6j0gkUBvagm/8Qd3kf9G+ T5Nba8F7mzjYMxNyF1jijbJT9tpam3zvk6pvvvM6ij3WlM1towMKjpoqU4qNQkyk 6Q= Received: (qmail 126497 invoked by alias); 26 Feb 2015 21:35:58 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 126487 invoked by uid 89); 26 Feb 2015 21:35:57 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.3 required=5.0 tests=AWL, BAYES_00, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: e24smtp01.br.ibm.com Message-ID: <54EF91B2.3020704@linux.vnet.ibm.com> Date: Thu, 26 Feb 2015 18:35:46 -0300 From: Adhemerval Zanella User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0 MIME-Version: 1.0 To: "GNU C. Library" Subject: [PATCH] powerpc: Spinlock optimization and cleanup X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 15022621-1524-0000-0000-000001EB5579 This patch optimizes powerpc spinlock implementation by: * Current algorithm spin over a lwzx, but only after issuing a lwarx. first. The optimization for such case is to avoid the first lwarx in contention case (which is much more costly than normal loads). * Use the correct EH hint bit on the larx for supported ISA. For lock acquisition, the thread that acquired the lock with a successful stcx do not want to give away the write ownership on the cacheline. The idea is to make the load reservation "sticky" about retaining write authority to the line. That way, the store that must inevitably come to release the lock can succeed quickly and not contend with other threads issuing lwarx. If another thread does a store to the line (false sharing), the winning thread must give up write authority to The proper value of EH for the larx for a lock acquisition is 1. It also adds some cleanup to use the defined acquire semantic instructions and function prototype using default C style. Tested on powerpc32-fpu and powerpc64. --- * sysdeps/powerpc/nptl/pthread_spin_lock.c (pthread_spin_lock): Optimize first check for contention case and add lwarx hint. * sysdeps/powerpc/nptl/pthread_spin_trylock.c (pthread_spin_trylock): Use ANSI prototype. * sysdep/unix/sysv/linux/powerpc/pthread_spin_unlock.c: Move to ... * sysdeps/powerpc/nptl/pthread_spin_unlock.c: ... here. --- diff --git a/sysdeps/powerpc/nptl/pthread_spin_lock.c b/sysdeps/powerpc/nptl/pthread_spin_lock.c index cc081f8..8a39da9 100644 --- a/sysdeps/powerpc/nptl/pthread_spin_lock.c +++ b/sysdeps/powerpc/nptl/pthread_spin_lock.c @@ -19,24 +19,26 @@ #include "pthreadP.h" int -pthread_spin_lock (lock) - pthread_spinlock_t *lock; +pthread_spin_lock (pthread_spinlock_t *lock) { unsigned int __tmp; asm volatile ( - "1: lwarx %0,0,%1\n" + "0: lwzx %0,0,%1\n" + " cmpwi 0,%0,0\n" + " bne 0b\n" + "1: lwarx %0,0,%1" MUTEX_HINT_ACQ "\n" " cmpwi 0,%0,0\n" " bne- 2f\n" " stwcx. %2,0,%1\n" " bne- 2f\n" - " isync\n" - " .subsection 1\n" - "2: lwzx %0,0,%1\n" - " cmpwi 0,%0,0\n" - " bne 2b\n" - " b 1b\n" - " .previous" + __ARCH_ACQ_INSTR "\n" + " .subsection 1\n" + "2: lwzx %0,0,%1\n" + " cmpwi 0,%0,0\n" + " bne 2b\n" + " b 1b\n" + " .previous" : "=&r" (__tmp) : "r" (lock), "r" (1) : "cr0", "memory"); diff --git a/sysdeps/powerpc/nptl/pthread_spin_trylock.c b/sysdeps/powerpc/nptl/pthread_spin_trylock.c index 77a4615..c485aa4 100644 --- a/sysdeps/powerpc/nptl/pthread_spin_trylock.c +++ b/sysdeps/powerpc/nptl/pthread_spin_trylock.c @@ -20,8 +20,7 @@ #include "pthreadP.h" int -pthread_spin_trylock (lock) - pthread_spinlock_t *lock; +pthread_spin_trylock (pthread_spinlock_t *lock) { unsigned int old; int err = EBUSY; diff --git a/sysdeps/powerpc/nptl/pthread_spin_unlock.c b/sysdeps/powerpc/nptl/pthread_spin_unlock.c new file mode 100644 index 0000000..7af694f --- /dev/null +++ b/sysdeps/powerpc/nptl/pthread_spin_unlock.c @@ -0,0 +1,28 @@ +/* pthread_spin_unlock -- unlock a spin lock. PowerPC version. + Copyright (C) 2007-2015 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include "pthreadP.h" +#include + +int +pthread_spin_unlock (pthread_spinlock_t *lock) +{ + __asm __volatile (__ARCH_REL_INSTR ::: "memory"); + *lock = 0; + return 0; +} diff --git a/sysdeps/unix/sysv/linux/powerpc/pthread_spin_unlock.c b/sysdeps/unix/sysv/linux/powerpc/pthread_spin_unlock.c deleted file mode 100644 index 7af694f..0000000 --- a/sysdeps/unix/sysv/linux/powerpc/pthread_spin_unlock.c +++ /dev/null @@ -1,28 +0,0 @@ -/* pthread_spin_unlock -- unlock a spin lock. PowerPC version. - Copyright (C) 2007-2015 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, see - . */ - -#include "pthreadP.h" -#include - -int -pthread_spin_unlock (pthread_spinlock_t *lock) -{ - __asm __volatile (__ARCH_REL_INSTR ::: "memory"); - *lock = 0; - return 0; -}