From patchwork Wed Sep 15 17:11:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Bugaev X-Patchwork-Id: 45055 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 68A2E3857809 for ; Wed, 15 Sep 2021 17:12:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 68A2E3857809 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1631725950; bh=RagJ9WJYAlEM95dqXUgGGIXN/xgtMTx0wGpc6Z5g1Gk=; 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=GCjVt4P1VyC1JLp6fU6/EhTbrLbMPKMTxQa57qpfyStOqfVKUeckmiyXbzVUDUjyd jqLDiU1mfvuLxzxpdrRPq2sZZcXa1M1yvNqT8rJDKDir8zHg84BnwoaYtPTmc7CzyC 6vI7K5qlipWi9Ta5GF+pQ4T9QBYx1xKBhetNbzRc= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-lf1-x131.google.com (mail-lf1-x131.google.com [IPv6:2a00:1450:4864:20::131]) by sourceware.org (Postfix) with ESMTPS id 5DCEF3858413 for ; Wed, 15 Sep 2021 17:11:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5DCEF3858413 Received: by mail-lf1-x131.google.com with SMTP id h16so7380968lfk.10 for ; Wed, 15 Sep 2021 10:11:29 -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:in-reply-to :references:mime-version:content-transfer-encoding; bh=RagJ9WJYAlEM95dqXUgGGIXN/xgtMTx0wGpc6Z5g1Gk=; b=RA/7fBeUjP15IcqTjudYjlQgWp276FV/vAbA47sU3C0qs07MBggmbUjg84gJ8s9h8a amIfMJkWArpQxWEhRD9QjwR9FHT0ZxJTat9eIXafjIox40I6Q/jOavaAWUgNDQJFJ/8I U3SaZXpr2BUDfeQF2G8hHa0zYckl6zOD5tY3rbpAhU7bUeXq9tPQNkdmtJqStWMA25F6 H81r6Ft1UBE6XdIoA7O3aGIaHxYtaYKEObYr76wSt++Kr/CwdpIgIPxwR8ZQPXlu1rhI 5QaWSiaT6S0AbnngXWsIV4gl08UDWdMh58GffLbQ22mA8GcZ7HBDddVU7NScTbdogx3I n3fQ== X-Gm-Message-State: AOAM533WfJ1N3FQhs1BUAgsJwJ2TRWgVpLRjf6PI99HX6PVrZhahiFoH mhOgApILKyvklZjUFijAjQDkdWn62dDpNY59 X-Google-Smtp-Source: ABdhPJzQSJilHhFz/eLR1Is9iX1k4NXgohJErgx53WcVNvYI+wJ5UTYEP17Bx4BwgtFx6hAaZENliA== X-Received: by 2002:a05:6512:e85:: with SMTP id bi5mr752956lfb.287.1631725887801; Wed, 15 Sep 2021 10:11:27 -0700 (PDT) Received: from badwolf.office.smartdec.ru ([81.23.5.115]) by smtp.googlemail.com with ESMTPSA id i12sm31095lfb.301.2021.09.15.10.11.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Sep 2021 10:11:27 -0700 (PDT) To: libc-alpha@sourceware.org, bug-hurd@gnu.org Subject: [PATCH htl v4 3/4] htl: Reimplement GSCOPE Date: Wed, 15 Sep 2021 20:11:09 +0300 Message-Id: <20210915171110.226187-3-bugaevc@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210915171110.226187-1-bugaevc@gmail.com> References: <20210915171110.226187-1-bugaevc@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-9.6 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_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: Sergey Bugaev via Libc-alpha From: Sergey Bugaev Reply-To: Sergey Bugaev Cc: samuel.thibault@gnu.org, Sergey Bugaev Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" This is a new implementation of GSCOPE which largely mirrors its NPTL counterpart. Same as in NPTL, instead of a global flag shared between threads, there is now a per-thread GSCOPE flag stored in each thread's TCB. This makes entering and exiting a GSCOPE faster at the expense of making THREAD_GSCOPE_WAIT () slower. The largest win is the elimination of many redundant gsync_wake () RPC calls; previously, even simplest programs would make dozens of fully redundant gsync_wake () calls. Signed-off-by: Sergey Bugaev --- sysdeps/htl/dl-thread_gscope_wait.c | 55 +++++++++++++++++++++++++++++ sysdeps/mach/hurd/i386/tls.h | 21 +++++++++++ sysdeps/mach/hurd/tls.h | 20 ----------- 3 files changed, 76 insertions(+), 20 deletions(-) create mode 100644 sysdeps/htl/dl-thread_gscope_wait.c diff --git a/sysdeps/htl/dl-thread_gscope_wait.c b/sysdeps/htl/dl-thread_gscope_wait.c new file mode 100644 index 0000000000..7557e77aab --- /dev/null +++ b/sysdeps/htl/dl-thread_gscope_wait.c @@ -0,0 +1,55 @@ +/* Out-of-line notification function for the GSCOPE locking mechanism. + Copyright (C) 2007-2021 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 + . */ + +#include +#include +#include + +void +__thread_gscope_wait (void) +{ + size_t i; + struct __pthread *t; + int *gscope_flagp; + + __libc_rwlock_rdlock (GL (dl_pthread_threads_lock)); + + /* Iterate over the list of threads. */ + for (i = 0; i < GL (dl_pthread_num_threads); ++i) + { + t = GL (dl_pthread_threads[i]); + if (t == NULL || t->tcb->gscope_flag == THREAD_GSCOPE_FLAG_UNUSED) + continue; + + gscope_flagp = &t->tcb->gscope_flag; + + /* We have to wait until this thread is done with the global + scope. First tell the thread that we are waiting and + possibly have to be woken. */ + if (atomic_compare_and_exchange_bool_acq (gscope_flagp, + THREAD_GSCOPE_FLAG_WAIT, + THREAD_GSCOPE_FLAG_USED)) + continue; + + do + lll_wait (gscope_flagp, THREAD_GSCOPE_FLAG_WAIT, LLL_PRIVATE); + while (*gscope_flagp == THREAD_GSCOPE_FLAG_WAIT); + } + + __libc_rwlock_unlock (GL (dl_pthread_threads_lock)); +} diff --git a/sysdeps/mach/hurd/i386/tls.h b/sysdeps/mach/hurd/i386/tls.h index 057b2613f3..2ac65df921 100644 --- a/sysdeps/mach/hurd/i386/tls.h +++ b/sysdeps/mach/hurd/i386/tls.h @@ -369,6 +369,27 @@ _hurd_tls_new (thread_t child, struct i386_thread_state *state, tcbhead_t *tcb) return err; } +/* Global scope switch support. */ +# define THREAD_GSCOPE_IN_TCB 1 + +# define THREAD_GSCOPE_FLAG_UNUSED 0 +# define THREAD_GSCOPE_FLAG_USED 1 +# define THREAD_GSCOPE_FLAG_WAIT 2 + +# define THREAD_GSCOPE_SET_FLAG() \ + THREAD_SETMEM (THREAD_SELF, gscope_flag, THREAD_GSCOPE_FLAG_USED) + +# define THREAD_GSCOPE_RESET_FLAG() \ + ({ \ + int __flag; \ + asm volatile ("xchgl %0, %%gs:%P1" \ + : "=r" (__flag) \ + : "i" (offsetof (tcbhead_t, gscope_flag)), \ + "0" (THREAD_GSCOPE_FLAG_UNUSED)); \ + if (__flag == THREAD_GSCOPE_FLAG_WAIT) \ + lll_wake (THREAD_SELF->gscope_flag, LLL_PRIVATE); \ + }) + #endif /* !__ASSEMBLER__ */ #endif /* i386/tls.h */ diff --git a/sysdeps/mach/hurd/tls.h b/sysdeps/mach/hurd/tls.h index f83956d3d7..8e66d5ff53 100644 --- a/sysdeps/mach/hurd/tls.h +++ b/sysdeps/mach/hurd/tls.h @@ -52,26 +52,6 @@ # define GET_DTV(descr) \ (((tcbhead_t *) (descr))->dtv) -/* Global scope switch support. */ -#define THREAD_GSCOPE_IN_TCB 0 -#define THREAD_GSCOPE_GLOBAL -#define THREAD_GSCOPE_SET_FLAG() \ - atomic_exchange_and_add_acq (&GL(dl_thread_gscope_count), 1) -#define THREAD_GSCOPE_RESET_FLAG() \ - do \ - if (atomic_exchange_and_add_rel (&GL(dl_thread_gscope_count), -1) == 1) \ - lll_wake (GL(dl_thread_gscope_count), 0); \ - while (0) -#define THREAD_GSCOPE_WAIT() \ - do \ - { \ - int count; \ - atomic_write_barrier (); \ - while ((count = GL(dl_thread_gscope_count))) \ - lll_wait (GL(dl_thread_gscope_count), count, 0); \ - } \ - while (0) - #endif /* !ASSEMBLER */