From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-qk1-x731.google.com with SMTP id k12so119814qkj.8 for ; Wed, 28 Sep 2022 20:14:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=53EvLFewEr8m5QqPpnKnwLmrdIiTLkw/MEqA5/FqDRg=; b=lZx68BnDmIifBAdeMBlOcnpZRIC894NeJw+5ZIo9neC7XRzI9hJtxLBAtoO4Yws5BI Rrku2lYbrf4bgI3tUoORK51yKFdleJs/XGIk+H/ndDrpfp2jC+6+7ufD76EHjVKDMP9m v0K2L0FqxR61AMQtVexzuO7rEvbUZ3I/5m1mQIWhxMApagCQ7n0KXj6sxn148+4rI1vO RHkmeaLgFfspX7fLXtk7u6hv1Ray/0Rnx5KRU5sVvOsJzfi/M+oT9w5I4rmNDkfqZ6M6 8mtBXyEOYapw4hcGWpEBEvfpFAvG2apdrsq3G9V4vitdcrtqmmjw/fM9ogCIpFWdIBoE WI1g== 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) From: Noah Goldstein To: libc-alpha@sourceware.org Cc: goldstein.w.n@gmail.com, hjl.tools@gmail.com, carlos@systemhalted.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 Content-Transfer-Encoding: 8bit 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 List-Id: 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; } -- 2.34.1