From patchwork Wed Nov 18 19:55:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 41117 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 CC1B1394740E; Wed, 18 Nov 2020 19:56:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CC1B1394740E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1605729389; bh=0a8vJ78BUXXokdQGTokGiiolQdawGjbHgkmTvE7glQg=; 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=XPX8qS8P3OqmCORu9cK/Si/okZkRILyxJDI65G0XBt31EzmhO6tvq+VsPaHxkt7OI dEOitpy+xHrW0dUxx5N1sLaJAPcabmoVmDBdF8TvwoOCtXMVTGIlCx5rXk318GfAI9 J6ds3LRNv0OoVgvBdRJcx0kTiQYjM7AyhyIzdqjk= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x742.google.com (mail-qk1-x742.google.com [IPv6:2607:f8b0:4864:20::742]) by sourceware.org (Postfix) with ESMTPS id 2E19039450DE for ; Wed, 18 Nov 2020 19:56:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 2E19039450DE Received: by mail-qk1-x742.google.com with SMTP id q5so3036831qkc.12 for ; Wed, 18 Nov 2020 11:56:24 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=0a8vJ78BUXXokdQGTokGiiolQdawGjbHgkmTvE7glQg=; b=CBnhInbQgnLVRxx+46BAbnNMqL3eNMFJ+vCUk3RTXwcR76sf8zVdg5U2h5wQSgbZgW tmVxNIxbPFC69/zn8IAbNk5k7xFoRqFnRTy6uwlPgkr/XgaPcF8hLGlqo28fUQdLxko+ TWbj4uwXZTTW0QZcJUoh2uOlZ8aBlj5WmXby7siHZnl+wAXfkV5Onk2PgD/HuqFF76ah KUHoTExIQC03QdKRI4vuDxdnhvgSwWG5B4bWgNr6dEb3o27HK6UAfQAZ3ZhYVWCUbA/Q 2CYk9SLFtzi4hTcXIsYTnrY8RP8FqgiKVngxa8DJRMzfk7cUsCRklaaNN0eyiML6U3HY 1C+w== X-Gm-Message-State: AOAM530dTYlUWVRCr2rVmTtae9Z9a8Q35fuRDWDQioD13Nif3iG082Wm 6k3kHM2IHrZ3her6efmq01GOG8O3RC6r6Q== X-Google-Smtp-Source: ABdhPJzwr1rGAjSJoyYWgdhfYwroyYTwMW5d+wAtL/4+Fm1i4lLXO5VpYROrxkqbyKh+9Q7LehV0oQ== X-Received: by 2002:a37:813:: with SMTP id 19mr6814223qki.438.1605729383414; Wed, 18 Nov 2020 11:56:23 -0800 (PST) Received: from localhost.localdomain ([177.194.48.209]) by smtp.googlemail.com with ESMTPSA id f27sm6538775qtv.95.2020.11.18.11.56.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Nov 2020 11:56:23 -0800 (PST) To: libc-alpha@sourceware.org Subject: [PATCH 18/28] nptl: Add futex_trylock_pi Date: Wed, 18 Nov 2020 16:55:42 -0300 Message-Id: <20201118195552.2687336-19-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201118195552.2687336-1-adhemerval.zanella@linaro.org> References: <20201118195552.2687336-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-13.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" This is similar to other futex wrappers on futex-internal. Checked on x86_64-linux-gnu. --- nptl/pthread_mutex_trylock.c | 9 +++----- sysdeps/nptl/futex-internal.h | 36 +++++++++++++++++++++++++++++++ sysdeps/nptl/lowlevellock-futex.h | 5 +++++ 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/nptl/pthread_mutex_trylock.c b/nptl/pthread_mutex_trylock.c index 2130f52529..be9d9a3bac 100644 --- a/nptl/pthread_mutex_trylock.c +++ b/nptl/pthread_mutex_trylock.c @@ -297,12 +297,9 @@ __pthread_mutex_trylock (pthread_mutex_t *mutex) int private = (robust ? PTHREAD_ROBUST_MUTEX_PSHARED (mutex) : PTHREAD_MUTEX_PSHARED (mutex)); - int e = INTERNAL_SYSCALL_CALL (futex, &mutex->__data.__lock, - __lll_private_flag (FUTEX_TRYLOCK_PI, - private), 0, 0); - - if (INTERNAL_SYSCALL_ERROR_P (e) - && INTERNAL_SYSCALL_ERRNO (e) == EWOULDBLOCK) + if (futex_trylock_pi ((unsigned int *) &mutex->__data.__lock, + private) + == -EWOULDBLOCK) { /* The kernel has not yet finished the mutex owner death. We do not need to ensure ordering wrt another memory diff --git a/sysdeps/nptl/futex-internal.h b/sysdeps/nptl/futex-internal.h index c27d0cdac8..3cda6c5762 100644 --- a/sysdeps/nptl/futex-internal.h +++ b/sysdeps/nptl/futex-internal.h @@ -483,6 +483,42 @@ futex_lock_pi64 (int *futex_word, const struct __timespec64 *abstime, } } +/* This operaration is similar to futex_lock_pi and it is used when the lock + information contains stale state (FUTEX_WAITERS and/or FUTEX_OWNER_DIED). + User space can not handle this condition in a race-free manner, but kernel + might handle it and acquire the futex. + + Returns: + + - 0 if woken by a PI unlock operation or spuriously. + - EAGAIN if the futex owner thread ID is about to exit, but has not yet + handled the state cleanup. + - EDEADLK if the futex is already locked by the caller. + - ESRCH if the thread ID int he futex does not exist. + - EINVAL is the state is corrupted or if there is a waiter on the + futex. +*/ +static __always_inline int +futex_trylock_pi (unsigned int *futex_word, int private) +{ + int err = lll_futex_trylock_pi ((int *) futex_word, private); + switch (err) + { + case 0: + case -EAGAIN: /* EWOULDBLOCK */ + case -EINTR: + case -ESRCH: + case -EDEADLK: + case -EPERM: + return -err; + + case -EINVAL: + case -EFAULT: + default: + futex_fatal_error (); + } +} + /* Wakes the top priority waiter that called a futex_lock_pi operation on the futex. diff --git a/sysdeps/nptl/lowlevellock-futex.h b/sysdeps/nptl/lowlevellock-futex.h index 2209ca76a1..0440aeb390 100644 --- a/sysdeps/nptl/lowlevellock-futex.h +++ b/sysdeps/nptl/lowlevellock-futex.h @@ -144,6 +144,11 @@ __lll_private_flag (FUTEX_LOCK_PI, private), \ 0, abstime) +#define lll_futex_trylock_pi(futexp, private) \ + lll_futex_syscall (4, futexp, \ + __lll_private_flag (FUTEX_TRYLOCK_PI, private), \ + 0, 0) + #define lll_futex_timed_unlock_pi(futexp, private) \ lll_futex_syscall (4, futexp, \ __lll_private_flag (FUTEX_UNLOCK_PI, private), \