From patchwork Wed Nov 10 18:41:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 47422 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DA2133857817 for ; Wed, 10 Nov 2021 18:43:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DA2133857817 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1636569821; bh=W0Ct5TDF9L4m8yRlpYWKFp7IiRLl28T0/ZAdxktyRmQ=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=SB13Z0ePZQy5dTTVQIPPY8l/A4ASHmoW8ZV/1OLzsNfZka7TZYMt2G9awE/9ayNKk u7t+H7w723bUMqB6WcM3G3dxVfOTRL5kWheNcS6BqdF2tcLj57RfO2utNaCpvJKfAP +n0r/jvis3pRcsE/aGJCx/HtxMD5JfftNaE1YALU= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com [IPv6:2607:f8b0:4864:20::1032]) by sourceware.org (Postfix) with ESMTPS id 0AC4D385840C for ; Wed, 10 Nov 2021 18:41:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0AC4D385840C Received: by mail-pj1-x1032.google.com with SMTP id np3so2293304pjb.4 for ; Wed, 10 Nov 2021 10:41:56 -0800 (PST) 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=W0Ct5TDF9L4m8yRlpYWKFp7IiRLl28T0/ZAdxktyRmQ=; b=Cz7FTC/U+qdEksHUTmRhBLtGF5h2n7qT15mw2lPZ/HJnGJZsp92noAVWk315BHz0ru cbh/GNu6u2Pe+HMFbGVzdGVbq8odm/YV/1xCNjCIL9T4vUQ00fve802rvrpz7b8dynHU /tgp6aA9S1abqxgyo40nBnPD5nxFXryhBcRR3FJQkZWJE4jrxXkHk9vUsoJfaTdrPWcp 8LJWhi5fGNDxWSnpr3HjIuRuGhBAU+oBk0EB4Ymb2FJ3bQnT42Exv466CsnlhEVSQ1gO +QwVFFOcDU7NDp560CUaMfA3R4GPsnwZAxX2/mj1wPrR95iJIbO4O8wciCtcA1bd1O+6 JGVw== X-Gm-Message-State: AOAM533mklHAVoJWHfVl68uvoreTzescF6GXu/+H6CeN0OHfWc94fm38 eCoIhrV69KppSodgc4Ghqs8= X-Google-Smtp-Source: ABdhPJzesP06D93MsufP+bYU52zgXqzpu0HzOLJNqPysxTS9B8N6rKhozHjxc1OIH6cwMoeqE3aRpg== X-Received: by 2002:a17:902:e84e:b0:141:e3f2:36c5 with SMTP id t14-20020a170902e84e00b00141e3f236c5mr1197813plg.74.1636569716154; Wed, 10 Nov 2021 10:41:56 -0800 (PST) Received: from gnu-cfl-2.localdomain ([172.58.35.133]) by smtp.gmail.com with ESMTPSA id d12sm377615pfh.165.2021.11.10.10.41.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Nov 2021 10:41:54 -0800 (PST) Received: from gnu-cfl-2.lan (localhost [IPv6:::1]) by gnu-cfl-2.localdomain (Postfix) with ESMTP id 535B01A0EC3; Wed, 10 Nov 2021 10:41:53 -0800 (PST) To: libc-alpha@sourceware.org Subject: [PATCH v5 3/3] Optimize CAS in __pthread_mutex_lock_full [BZ #28537] Date: Wed, 10 Nov 2021 10:41:53 -0800 Message-Id: <20211110184153.2269857-4-hjl.tools@gmail.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211110184153.2269857-1-hjl.tools@gmail.com> References: <20211110184153.2269857-1-hjl.tools@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-3029.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: "H.J. Lu via Libc-alpha" From: "H.J. Lu" Reply-To: "H.J. Lu" Cc: Florian Weimer , Andreas Schwab , "Paul A . Clarke" , Arjan van de Ven Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" 1. Do an atomic load and skip CAS if compare may fail to reduce cache line bouncing on contended locks. 2. Replace atomic_compare_and_exchange_bool_acq with atomic_compare_and_exchange_val_acq to avoid the extra load. --- nptl/pthread_mutex_lock.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/nptl/pthread_mutex_lock.c b/nptl/pthread_mutex_lock.c index d7e8efedd2..24ff1772cd 100644 --- a/nptl/pthread_mutex_lock.c +++ b/nptl/pthread_mutex_lock.c @@ -297,12 +297,13 @@ __pthread_mutex_lock_full (pthread_mutex_t *mutex) meantime. */ if ((oldval & FUTEX_WAITERS) == 0) { - if (atomic_compare_and_exchange_bool_acq (&mutex->__data.__lock, - oldval | FUTEX_WAITERS, - oldval) - != 0) + int val = atomic_load_relaxed (&mutex->__data.__lock); + if (val != oldval + || ((val = atomic_compare_and_exchange_val_acq + (&mutex->__data.__lock, oldval | FUTEX_WAITERS, + oldval)) != oldval)) { - oldval = mutex->__data.__lock; + oldval = val; continue; } oldval |= FUTEX_WAITERS;