From patchwork Thu Sep 29 03:14:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Noah Goldstein X-Patchwork-Id: 1684218 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=pDbmE7Sd; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4MdJQB38Hwz20Vm for ; Thu, 29 Sep 2022 13:15:14 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 483E73858289 for ; Thu, 29 Sep 2022 03:15:12 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 483E73858289 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1664421312; bh=BtnD/RrXqMLa/AJzpnVBYXdjrjOgK5nUHQP3A3hdb/o=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=pDbmE7SduGekr5SD7c4sp9xb5DkpTXLcL76AjxETa9x/zPv218IlmkfR8+RZV7HGd pJzZgxzgZem7nzMGGdg7jANbTUWrUdtR9E+qoAS44CrzLrweQZkQCltkIOH/yAl80T MQOpXCy1E7G7TkERnNh6wSwXqkWRJoFNZiJiBtLM= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qv1-xf30.google.com (mail-qv1-xf30.google.com [IPv6:2607:f8b0:4864:20::f30]) by sourceware.org (Postfix) with ESMTPS id F1E013858D28 for ; Thu, 29 Sep 2022 03:14:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org F1E013858D28 Received: by mail-qv1-xf30.google.com with SMTP id m18so180976qvo.12 for ; Wed, 28 Sep 2022 20:14:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date; bh=BtnD/RrXqMLa/AJzpnVBYXdjrjOgK5nUHQP3A3hdb/o=; b=PC/KO1JPfMtLYRgMO5sgBKVwpNYr1/kxw2VvGGU19F4/nQZTHWnnIp94fnmaU4REW7 F54RjAcXDtzWI/9MrNdtCya34PhJgYeXb59Dm9hbFO/F359FQfNF4sV4scV2vhUfXaMw 6wTi9mW1nV2XxqlkXRbZa3m/4sSaqeBaQ/w6YkDGSzgY62vcYYaOC3f9uxycIpJcZ41Y pDELhJTaZLPuucQ05/ufR2bZT5yC+njeO3fJImyhzcC9zxG0Z/EPf2S3QnTy85aJ2deq XkVBu8usfaN+bIoYBPtCQryWiv8yU7QB36rWCSCQmMSr73cHaPjsa7w1tYphttojhZq9 KtBg== X-Gm-Message-State: ACrzQf3v/1VXuakBudq4O9Vsfxm+qP7AuvGAJ+kPOg09RluYRyymGqY5 XY6TCD++h19pyTCS0NGrDhKs2KiFWfY= X-Google-Smtp-Source: AMsMyM6IHc/nlnSgxKvXtxqoE4LOibVPUCUTvVfzD2pkGB6NLRl32ivPyLo+jprFDI78OymBGxXv3A== X-Received: by 2002:a0c:a950:0:b0:4aa:e2ac:922a with SMTP id z16-20020a0ca950000000b004aae2ac922amr922600qva.119.1664421294845; Wed, 28 Sep 2022 20:14:54 -0700 (PDT) Received: from noah-tgl.. (cpe-74-64-106-10.nyc.res.rr.com. [74.64.106.10]) by smtp.gmail.com with ESMTPSA id t18-20020a05622a181200b0035cf5edefa6sm4882137qtc.56.2022.09.28.20.14.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Sep 2022 20:14:54 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v1 1/4] Benchtests: Add benchtests for pthread_spin_lock and mutex_trylock Date: Wed, 28 Sep 2022 20:14:51 -0700 Message-Id: <20220929031452.2551219-1-goldstein.w.n@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Noah Goldstein via Libc-alpha From: Noah Goldstein Reply-To: Noah Goldstein Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" Reuses infrastructure from previous pthread_mutex_lock benchmarks to test other performance sensitive functions. --- benchtests/Makefile | 8 +++- ...utex-locks.c => bench-pthread-lock-base.c} | 20 +++++----- benchtests/bench-pthread-mutex-lock.c | 32 ++++++++++++++++ benchtests/bench-pthread-mutex-trylock.c | 37 +++++++++++++++++++ benchtests/bench-pthread-spinlock.c | 30 +++++++++++++++ 5 files changed, 115 insertions(+), 12 deletions(-) rename benchtests/{bench-pthread-mutex-locks.c => bench-pthread-lock-base.c} (93%) create mode 100644 benchtests/bench-pthread-mutex-lock.c create mode 100644 benchtests/bench-pthread-mutex-trylock.c create mode 100644 benchtests/bench-pthread-spinlock.c diff --git a/benchtests/Makefile b/benchtests/Makefile index d99771be74..841997e0a9 100644 --- a/benchtests/Makefile +++ b/benchtests/Makefile @@ -103,11 +103,17 @@ endif bench-pthread := \ pthread-locks \ - pthread-mutex-locks \ + pthread-mutex-lock \ + pthread-mutex-trylock \ + pthread-spinlock \ pthread_once \ thread_create \ # bench-pthread +LDLIBS-bench-pthread-mutex-lock += -lm +LDLIBS-bench-pthread-mutex-trylock += -lm +LDLIBS-bench-pthread-spinlock += -lm + bench-string := \ ffs \ ffsll \ diff --git a/benchtests/bench-pthread-mutex-locks.c b/benchtests/bench-pthread-lock-base.c similarity index 93% rename from benchtests/bench-pthread-mutex-locks.c rename to benchtests/bench-pthread-lock-base.c index 1685b9dd1f..fac8a12b52 100644 --- a/benchtests/bench-pthread-mutex-locks.c +++ b/benchtests/bench-pthread-lock-base.c @@ -1,4 +1,4 @@ -/* Measure mutex_lock for different threads and critical sections. +/* Measure lock functions for different threads and critical sections. Copyright (C) 2022 Free Software Foundation, Inc. This file is part of the GNU C Library. @@ -17,7 +17,6 @@ . */ #define TEST_MAIN -#define TEST_NAME "pthread-mutex-locks" #define TIMEOUT (20 * 60) #include @@ -31,8 +30,8 @@ #include "bench-timing.h" #include "json-lib.h" -static pthread_mutex_t lock; -static pthread_mutexattr_t attr; +static bench_lock_t lock; +static bench_lock_attr_t attr; static pthread_barrier_t barrier; #define START_ITERS 1000 @@ -104,9 +103,9 @@ worker (void *v) TIMING_NOW (start); while (iters--) { - pthread_mutex_lock (&lock); + LOCK (&lock); critical_section (crt_len); - pthread_mutex_unlock (&lock); + UNLOCK (&lock); non_critical_section (non_crt_len); } TIMING_NOW (stop); @@ -123,7 +122,7 @@ do_one_test (int num_threads, int crt_len, int non_crt_len, long iters) Worker_Params *p, params[num_threads]; pthread_t threads[num_threads]; - pthread_mutex_init (&lock, &attr); + LOCK_INIT (&lock, &attr); pthread_barrier_init (&barrier, NULL, num_threads); for (i = 0; i < num_threads; i++) @@ -137,7 +136,7 @@ do_one_test (int num_threads, int crt_len, int non_crt_len, long iters) for (i = 0; i < num_threads; i++) pthread_join (threads[i], NULL); - pthread_mutex_destroy (&lock); + LOCK_DESTROY (&lock); pthread_barrier_destroy (&barrier); mean = 0; @@ -246,7 +245,7 @@ do_bench (void) char name[128]; json_init (&json_ctx, 2, stdout); - json_attr_object_begin (&json_ctx, "pthread_mutex_locks"); + json_attr_object_begin (&json_ctx, TEST_NAME); /* The thread config begins from 1, and increases by 2x until nprocs. We also wants to test over-saturation case (1.25*nprocs). */ @@ -260,8 +259,7 @@ do_bench (void) threads[th_conf++] = nprocs; threads[th_conf++] = nprocs + nprocs / 4; - pthread_mutexattr_init (&attr); - pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ADAPTIVE_NP); + LOCK_ATTR_INIT (&attr); snprintf (name, sizeof name, "type=adaptive"); for (k = 0; k < (sizeof (non_crt_lens) / sizeof (int)); k++) diff --git a/benchtests/bench-pthread-mutex-lock.c b/benchtests/bench-pthread-mutex-lock.c new file mode 100644 index 0000000000..16556d4116 --- /dev/null +++ b/benchtests/bench-pthread-mutex-lock.c @@ -0,0 +1,32 @@ +/* Measure mutex_lock for different threads and critical sections. + Copyright (C) 2022 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 + . */ + +#define LOCK(lock) pthread_mutex_lock (lock) +#define UNLOCK(lock) pthread_mutex_unlock (lock) +#define LOCK_INIT(lock, attr) pthread_mutex_init (lock, attr) +#define LOCK_DESTROY(lock) pthread_mutex_destroy (lock) +#define LOCK_ATTR_INIT(attr) \ + pthread_mutexattr_init (attr); \ + pthread_mutexattr_settype (attr, PTHREAD_MUTEX_ADAPTIVE_NP); + +#define bench_lock_t pthread_mutex_t +#define bench_lock_attr_t pthread_mutexattr_t + +#define TEST_NAME "pthread-mutex-lock" + +#include "bench-pthread-lock-base.c" diff --git a/benchtests/bench-pthread-mutex-trylock.c b/benchtests/bench-pthread-mutex-trylock.c new file mode 100644 index 0000000000..66318f499f --- /dev/null +++ b/benchtests/bench-pthread-mutex-trylock.c @@ -0,0 +1,37 @@ +/* Measure mutex_trylock for different threads and critical sections. + Copyright (C) 2022 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 + . */ + +#define LOCK(lock) \ + while (pthread_mutex_trylock (lock) != 0) \ + { \ + non_critical_section (non_crt_len); \ + } + +#define UNLOCK(lock) pthread_mutex_unlock (lock) +#define LOCK_INIT(lock, attr) pthread_mutex_init (lock, attr) +#define LOCK_DESTROY(lock) pthread_mutex_destroy (lock) +#define LOCK_ATTR_INIT(attr) \ + pthread_mutexattr_init (attr); \ + pthread_mutexattr_settype (attr, PTHREAD_MUTEX_ADAPTIVE_NP); + +#define bench_lock_t pthread_mutex_t +#define bench_lock_attr_t pthread_mutexattr_t + +#define TEST_NAME "pthread-mutex-trylock" + +#include "bench-pthread-lock-base.c" diff --git a/benchtests/bench-pthread-spinlock.c b/benchtests/bench-pthread-spinlock.c new file mode 100644 index 0000000000..2174933d6b --- /dev/null +++ b/benchtests/bench-pthread-spinlock.c @@ -0,0 +1,30 @@ +/* Measure mutex_trylock for different threads and critical sections. + Copyright (C) 2022 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 + . */ + +#define LOCK(lock) pthread_spin_lock (lock) +#define UNLOCK(lock) pthread_spin_unlock (lock) +#define LOCK_INIT(lock, attr) pthread_spin_init (lock, *(attr)) +#define LOCK_DESTROY(lock) pthread_spin_destroy (lock) +#define LOCK_ATTR_INIT(attr) *(attr) = 0 + +#define bench_lock_t pthread_spinlock_t +#define bench_lock_attr_t int + +#define TEST_NAME "pthread-spin-lock" + +#include "bench-pthread-lock-base.c" From patchwork Thu Sep 29 03:14:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Noah Goldstein X-Patchwork-Id: 1684217 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=n7sX10Gn; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4MdJQ96rbwz1ypH for ; Thu, 29 Sep 2022 13:15:13 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 092503858C33 for ; Thu, 29 Sep 2022 03:15:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 092503858C33 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1664421311; bh=53EvLFewEr8m5QqPpnKnwLmrdIiTLkw/MEqA5/FqDRg=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=n7sX10Gn6nz1/y9VZpQxVnEjSlbKDBrpWMoGKkywRxMD4E0zJosrxxruLuFZLPX18 yBnKuVCmMKudFavZN7epEl3S97E4ysBEGnioKEtqtYNe4uWakqRUbhI7x7dc04jhgC bcrgef7rLMyMmPozDU/Fsn77d3l/i04xw6ySJyno= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x731.google.com (mail-qk1-x731.google.com [IPv6:2607:f8b0:4864:20::731]) by sourceware.org (Postfix) with ESMTPS id 5BED93858D1E for ; Thu, 29 Sep 2022 03:14:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5BED93858D1E Received: by mail-qk1-x731.google.com with SMTP id k12so119814qkj.8 for ; Wed, 28 Sep 2022 20:14:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=53EvLFewEr8m5QqPpnKnwLmrdIiTLkw/MEqA5/FqDRg=; b=Q1E0zwQRxMEp4tIApveOxI66RF8IzAsCxy0/FfJhY+rEy4drSTYZIQDlAAeB98X1aR 4/68DXn/jlN6RF43tR9kV8Ay7lfQN3OpoxC7gFQ/Cbk7EnFR9A+DPkwkcxSO+YGOjxFn RpaRnjz0O7fPiSExEceSWHYIcrqMFfnZfnMpcWNefWdjVfVFi+urIjnBrxJnG3t6yrsc GgAXW+DA3nyGY4KrFhB+CgKu+tS2fBtXb/LwJCrcanAU4mctpe3T7ZH5JWtfstA6KsAR dWJQPDCAvW1jH9+3B/kcVds7Ixwtl2xnbjzfsxrXN5DqmoXeuno5GUz1zf9YGLFaN7oH gv6A== X-Gm-Message-State: ACrzQf1S5d8Ub3P7LdEHVwFIMZIBpdjPBuYwdzWY8zx1EL2tgsdCW2bX 0r6f4DwDJXSvJW/J7ac5vwDRcS1xYBg= X-Google-Smtp-Source: AMsMyM7NK6tSYDW9KugbEjApZldzJtLFeB5cnPwWoL9Jp6PCWCcsP46bYN747PfjYTfAbClfgH+SLA== X-Received: by 2002:a37:ad12:0:b0:6cf:8235:a2a2 with SMTP id f18-20020a37ad12000000b006cf8235a2a2mr782811qkm.189.1664421296365; Wed, 28 Sep 2022 20:14:56 -0700 (PDT) Received: from noah-tgl.. (cpe-74-64-106-10.nyc.res.rr.com. [74.64.106.10]) by smtp.gmail.com with ESMTPSA id t18-20020a05622a181200b0035cf5edefa6sm4882137qtc.56.2022.09.28.20.14.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Sep 2022 20:14:56 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v1 2/4] nptl: Continue use arch prefered atomic exchange in spinlock loop Date: Wed, 28 Sep 2022 20:14:52 -0700 Message-Id: <20220929031452.2551219-2-goldstein.w.n@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220929031452.2551219-1-goldstein.w.n@gmail.com> References: <20220929031452.2551219-1-goldstein.w.n@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Noah Goldstein via Libc-alpha From: Noah Goldstein Reply-To: Noah Goldstein Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" Despite using the preferred atomic exchange in the initial check, the loop was unconditionally using CAS which is not desired on some architectures (those that didn't set `ATOMIC_EXCHANGE_USES_CAS`). No meaningful perf changes measured on broadwell but still seems like a reasonable change. Full check passes on x86-64. --- nptl/pthread_spin_lock.c | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/nptl/pthread_spin_lock.c b/nptl/pthread_spin_lock.c index 19d1759f9a..1bdd6e2048 100644 --- a/nptl/pthread_spin_lock.c +++ b/nptl/pthread_spin_lock.c @@ -20,6 +20,20 @@ #include "pthreadP.h" #include +#if ATOMIC_EXCHANGE_USES_CAS +/* Try to acquire the lock with a CAS instruction as this architecture + has no exchange instruction. The acquisition succeeds if the lock is not + acquired. */ +# define pthread_spin_lock_grab_lock(mem, val, c) \ + atomic_compare_exchange_weak_acquire (lock, &val, 1)) +#else +/* Try to acquire the lock with an exchange instruction as this architecture + has such an instruction and we assume it is faster than a CAS. + The acquisition succeeds if the lock is not in an acquired state. */ +# define pthread_spin_lock_grab_lock(mem, val, c) \ + (atomic_exchange_acquire (lock, 1) == 0) +#endif + int __pthread_spin_lock (pthread_spinlock_t *lock) { @@ -36,19 +50,8 @@ __pthread_spin_lock (pthread_spinlock_t *lock) We use acquire MO to synchronize-with the release MO store in pthread_spin_unlock, and thus ensure that prior critical sections happen-before this critical section. */ -#if ! ATOMIC_EXCHANGE_USES_CAS - /* Try to acquire the lock with an exchange instruction as this architecture - has such an instruction and we assume it is faster than a CAS. - The acquisition succeeds if the lock is not in an acquired state. */ - if (__glibc_likely (atomic_exchange_acquire (lock, 1) == 0)) + if (__glibc_likely (pthread_spin_lock_grab_lock (lock, &val, 1))) return 0; -#else - /* Try to acquire the lock with a CAS instruction as this architecture - has no exchange instruction. The acquisition succeeds if the lock is not - acquired. */ - if (__glibc_likely (atomic_compare_exchange_weak_acquire (lock, &val, 1))) - return 0; -#endif do { @@ -75,7 +78,7 @@ __pthread_spin_lock (pthread_spinlock_t *lock) /* We need acquire memory order here for the same reason as mentioned for the first try to lock the spinlock. */ } - while (!atomic_compare_exchange_weak_acquire (lock, &val, 1)); + while (!pthread_spin_lock_grab_lock (lock, &val, 1)); return 0; }