From patchwork Tue Jul 12 20:31:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Noah Goldstein X-Patchwork-Id: 1655632 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.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=lONm+rkD; dkim-atps=neutral Authentication-Results: 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=) 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 RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LjC8b1T4mz9s07 for ; Wed, 13 Jul 2022 06:31:43 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0394238560B3 for ; Tue, 12 Jul 2022 20:31:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0394238560B3 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1657657901; bh=TlmVqh9wW3iN4i9JayqJE3nZpfq1e2Raig8S0WYIbGY=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=lONm+rkDDl8JB02G8t3GzaQnUePqGFbj/4ss3wurfOIW3OlX3iZdUW5MJZvA+NKAX 0x653oP3dsQxcs2AX+W2guKEAyRIk9FvT+35YjfZWacfEujNmnRhyJLJ/G7gcSDTWf or5geW3HMvIsGWsIaqQbmBFQrI4Q7wixN8+UTmvs= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pg1-x52d.google.com (mail-pg1-x52d.google.com [IPv6:2607:f8b0:4864:20::52d]) by sourceware.org (Postfix) with ESMTPS id 98A513857BA1 for ; Tue, 12 Jul 2022 20:31:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 98A513857BA1 Received: by mail-pg1-x52d.google.com with SMTP id 23so8605871pgc.8 for ; Tue, 12 Jul 2022 13:31:26 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=TlmVqh9wW3iN4i9JayqJE3nZpfq1e2Raig8S0WYIbGY=; b=KCgjiZBi9rVklfkxV1wtXVPoIUGnp1Y10SwzwfbZtEO9jc+dz7Wl+wnsgQRHn+mJKi 4J7ceSIfEuprADIaMPEyX3xy0izAbxsrwugh/fnouE6BRr2RAyfduMyZbnfY9Mqjg/Ib 44hWyJKriFCLOJzHK3AQDkvihzXGxWzyssEO5XZ0Ht//263/qNTFZEcDrwvGjKeVH1kt LtuICWtA7EdPclBNAHytqszBqrcf77e5ZufyaOQL+KR1odhviqy7N0OqlJUfNiMiatDK PAIbHSCS/Nl0E+m1uXsXdcTcKEOL/U6RfcY7FpXTtd71vLGCmBPWMIoUQSz65FqVrpL8 v7uA== X-Gm-Message-State: AJIora+1pVAYs820AVQXIu2oC4QeReEyuoflipe0x2eTRdg6T1RqgL3g xs3OgUMIXPNA6Ykw+UE3NUeXKAT7cqM= X-Google-Smtp-Source: AGRyM1vnE9qOmY+2zaDhPlO9i9cD8iOpWwAKIgp7aGzogOTBWbO6AC08EVH01DRjSOu2MP6GbwPY9w== X-Received: by 2002:a62:b60d:0:b0:528:99aa:da09 with SMTP id j13-20020a62b60d000000b0052899aada09mr25671058pff.86.1657657885375; Tue, 12 Jul 2022 13:31:25 -0700 (PDT) Received: from noah-tgl.. ([192.55.60.37]) by smtp.gmail.com with ESMTPSA id k1-20020a6555c1000000b0041966f97af8sm1731536pgs.36.2022.07.12.13.31.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Jul 2022 13:31:24 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v1] nptl: Continue use arch prefered atomic exchange in spinlock loop Date: Tue, 12 Jul 2022 13:31:22 -0700 Message-Id: <20220712203122.2545976-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, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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`). Tested in x86-64 with `ATOMIC_EXCHANGE_USE_CAS` as `0` and `1`. --- 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..4b3003377e 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; }