From patchwork Wed Nov 23 15:09:13 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Liebler X-Patchwork-Id: 698368 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 3tP5NP1V5Bz9t80 for ; Thu, 24 Nov 2016 02:09:40 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b="nycCOKbc"; 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:from:to:cc:subject:date:in-reply-to:references :message-id; q=dns; s=default; b=wgZ37clWxyU+oh1AmeEkA2PXSQErBVR TRcu6pAuixOJ/lCtDvXXj6sW2+jsX924beeYt2STVPX0FPqTM4NYEeYmVc2WNkjp 5cPIK076W+JRxBLdSM0jbQYqhoOjjMxDlSZ9Il2tdClauZcVV3sMYCbwo4Xa17yC CkiZMKTDvjdI= 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:from:to:cc:subject:date:in-reply-to:references :message-id; s=default; bh=SN/7xkFlHVgk928o5Y96dWJNcaA=; b=nycCO KbckYazd3vCxFdlO6piaiAwFTXPtDKDuNyQqHSCCYlOm6BmZAjSrSd++29f+jWIj sULUQrGurlKxr/MFsd7OVoH85cwwuRfmLHYXz3EmXiJl66MY3q0H733S/bYyCxQW rINjoo9a7ANr0ypO0yms8OZK+PwTqdEQQ7g1bA= Received: (qmail 4239 invoked by alias); 23 Nov 2016 15:09:33 -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 4221 invoked by uid 89); 23 Nov 2016 15:09:32 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.0 required=5.0 tests=AWL, BAYES_00, KAM_LAZY_DOMAIN_SECURITY, RCVD_IN_DNSWL_LOW, RCVD_IN_SEMBACKSCATTER autolearn=no version=3.3.2 spammy=lhi, held X-HELO: mx0a-001b2d01.pphosted.com From: Stefan Liebler To: libc-alpha@sourceware.org Cc: Stefan Liebler Subject: [PATCH 2/2] S390: Test if lock is free before using atomic instruction in spin-lock. Date: Wed, 23 Nov 2016 16:09:13 +0100 In-Reply-To: <1479913753-20506-1-git-send-email-stli@linux.vnet.ibm.com> References: <1479913753-20506-1-git-send-email-stli@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 X-Content-Scanned: Fidelis XPS MAILER x-cbid: 16112315-0020-0000-0000-000002853A30 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 16112315-0021-0000-0000-00003F28EE38 Message-Id: <1479913753-20506-2-git-send-email-stli@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2016-11-23_02:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=3 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1609300000 definitions=main-1611230255 This patch changes the behaviour of pthread_spin_lock on s390: Instead of spinning on the lock with compare-and-swap instruction, the atomic_compare_and_exchange_bool_acq macro is used. The s390 atomic_compare_and_exchange_bool_acq macro called with constant zero as oldval first compares the lock with normal instructions. If it is free the compare-and-swap instruction is used to aquire the lock. While the lock is held by one cpu another cpu will not exclusively lock the memory of the lock in a loop. If the lock is unlocked by another cpu it is observed by the normal instruction and the lock is acquired with a compare-and-swap instruction. ChangeLog: * sysdeps/s390/nptl/pthread_spin_lock.c: Use atomic_compare_and_exchange_bool_acq macro instead of inline assembly. * sysdeps/s390/nptl/pthread_spin_trylock.c: Likewise. --- sysdeps/s390/nptl/pthread_spin_lock.c | 13 +++++++------ sysdeps/s390/nptl/pthread_spin_trylock.c | 12 +++++------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/sysdeps/s390/nptl/pthread_spin_lock.c b/sysdeps/s390/nptl/pthread_spin_lock.c index def6a24..9c06949 100644 --- a/sysdeps/s390/nptl/pthread_spin_lock.c +++ b/sysdeps/s390/nptl/pthread_spin_lock.c @@ -21,12 +21,13 @@ int pthread_spin_lock (pthread_spinlock_t *lock) { - int oldval; + /* The s390 atomic_compare_and_exchange_bool_acq macro called with constant + zero as oldval first compares the lock with normal instructions. If it is + free the compare-and-swap instruction is used to aquire the lock. While + the lock is held by one cpu another cpu will not exclusively lock the + memory of the lock in a loop. */ + while (atomic_compare_and_exchange_bool_acq (lock, 1, 0)) + ; - __asm__ __volatile__ ("0: lhi %0,0\n" - " cs %0,%2,%1\n" - " jl 0b" - : "=&d" (oldval), "=Q" (*lock) - : "d" (1), "m" (*lock) : "cc" ); return 0; } diff --git a/sysdeps/s390/nptl/pthread_spin_trylock.c b/sysdeps/s390/nptl/pthread_spin_trylock.c index 4c00e08..d9e88ef 100644 --- a/sysdeps/s390/nptl/pthread_spin_trylock.c +++ b/sysdeps/s390/nptl/pthread_spin_trylock.c @@ -22,11 +22,9 @@ int pthread_spin_trylock (pthread_spinlock_t *lock) { - int old; - - __asm__ __volatile__ ("cs %0,%3,%1" - : "=d" (old), "=Q" (*lock) - : "0" (0), "d" (1), "m" (*lock) : "cc" ); - - return old != 0 ? EBUSY : 0; + /* The s390 atomic_compare_and_exchange_bool_acq macro called with constant + zero as oldval first compares the lock with normal instructions. If it is + free the compare-and-swap instruction is used to aquire the lock. */ + return __glibc_unlikely (atomic_compare_and_exchange_bool_acq (lock, 1, 0)) + ? EBUSY : 0; }