From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS3215 2.6.0.0/16 X-Spam-Status: No, score=-4.1 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,SPF_HELO_PASS,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 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 (4096 bits) server-digest SHA256) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 7040B1F8C6 for ; Wed, 11 Aug 2021 20:04:47 +0000 (UTC) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A4C62398582E for ; Wed, 11 Aug 2021 20:04:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A4C62398582E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1628712286; bh=yEEINSXgiTIrg8OtTz3wEf1kOJIQt61GjF/mC5CALtQ=; 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=R0Jg+sjza6ZnJasIDud5j4rmdL/L+8fLz31qlLCCP6xUBVhvymvlJb5o6x67Vjoo8 6Fbwj9V3wxB45ouKxJdjp8HSYk5D4VH3q8+/R42nPpyON8Gh/DGf0++hONp7YYLG3i MjQrWhuwAX0QTtt113vX4ii3qvNPzDxC4g6j9dio= Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by sourceware.org (Postfix) with ESMTPS id 2B2443848009 for ; Wed, 11 Aug 2021 20:01:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2B2443848009 Received: by mail-pl1-x62a.google.com with SMTP id d17so4120976plr.12 for ; Wed, 11 Aug 2021 13:01:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=yEEINSXgiTIrg8OtTz3wEf1kOJIQt61GjF/mC5CALtQ=; b=P5BD/XJzgbpZFjoRCbOGrsy+1TemTOt7JxZLsuiV5NJgyTOcXIhNlO/eL8hhnZ+e6o LR/T5q+YmxL1/aW/uW8ydIuBirjWTux4WRDPNTAk+GB6kMBfWYDi9JKY+3xmS3o+7s0m 5sG/pGBOrABfHVyNbTX3rEPfwUADqxUqyiwYwhQ6ipFrFIbUf5CNo163rS+iphRLff11 OgoaXOlM5P7QYSogmRFqvrzC0j9ONpzlUBofRQCUk+kEwrOI7Mn1TeB2jixJSzy/MYtJ bSv2wS299GA6dFo6bxvMkj+kuL5NUcpD+5f7Z7I7G9fjHuHsC18cIsvlfhZD5IX0uagL 2DsQ== X-Gm-Message-State: AOAM5312TO3K/yJCOvezTThK1KigaVSZ55lmfku6tyyZmlgPzUaQCYpy K7XOI8RiITZiBJpmAtzU+NbUfGNjIBISpw== X-Google-Smtp-Source: ABdhPJzpBCAKVFokGFUqEF9H4iRvEd8HvZFKYubqltUU9Zdqwyhs+EsA1MXEMpZqhozoYrF5peZPaQ== X-Received: by 2002:a63:204a:: with SMTP id r10mr370239pgm.365.1628712107113; Wed, 11 Aug 2021 13:01:47 -0700 (PDT) Received: from birita.. ([2804:431:c7cb:9dce:70f4:4f7a:652c:7fb]) by smtp.gmail.com with ESMTPSA id r15sm281370pjl.29.2021.08.11.13.01.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Aug 2021 13:01:46 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 4/4] nptl: Add CLOCK_MONOTONIC support for PI mutexes Date: Wed, 11 Aug 2021 17:01:33 -0300 Message-Id: <20210811200133.3869287-5-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210811200133.3869287-1-adhemerval.zanella@linaro.org> References: <20210811200133.3869287-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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: , From: Adhemerval Zanella via Libc-alpha Reply-To: Adhemerval Zanella Cc: Kurt Kanzenbach Errors-To: libc-alpha-bounces+e=80x24.org@sourceware.org Sender: "Libc-alpha" With FUTEX_LOCK_PI2 support we can now proper support CLOCK_MONOTONIC for pthread_mutex_clocklock with Priority Inheritance. If kernel does not support, EINVAL is returned instead as previously. The difference is the futex operation will be issued and the kernel will advertise the missing support. Checked on x86_64-linux-gnu and i686-linux-gnu on Linux 5.14, 5.11, and 4.15. --- nptl/pthread_mutex_timedlock.c | 7 ------ nptl/tst-mutexpi10.c | 42 +++++++++++++++++++++------------- sysdeps/pthread/tst-mutex5.c | 23 ++++++++++++------- sysdeps/pthread/tst-mutex9.c | 20 ++++++++++------ 4 files changed, 54 insertions(+), 38 deletions(-) diff --git a/nptl/pthread_mutex_timedlock.c b/nptl/pthread_mutex_timedlock.c index ca51da6f6c..a2fd1ada7b 100644 --- a/nptl/pthread_mutex_timedlock.c +++ b/nptl/pthread_mutex_timedlock.c @@ -300,13 +300,6 @@ __pthread_mutex_clocklock_common (pthread_mutex_t *mutex, case PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP: case PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP: { - /* Currently futex FUTEX_LOCK_PI operation only provides support for - CLOCK_REALTIME and trying to emulate by converting a - CLOCK_MONOTONIC to CLOCK_REALTIME will take in account possible - changes to the wall clock. */ - if (__glibc_unlikely (clockid != CLOCK_REALTIME)) - return EINVAL; - int kind, robust; { /* See concurrency notes regarding __kind in struct __pthread_mutex_s diff --git a/nptl/tst-mutexpi10.c b/nptl/tst-mutexpi10.c index da781d0d7a..34a7bea21b 100644 --- a/nptl/tst-mutexpi10.c +++ b/nptl/tst-mutexpi10.c @@ -38,29 +38,39 @@ do_test (void) PTHREAD_MUTEX_STALLED, PTHREAD_MUTEX_ROBUST }; - + const struct { + int clk; + int r; + } clocks[] = { + { CLOCK_REALTIME, 0 }, + { CLOCK_MONOTONIC, 0 }, + { CLOCK_REALTIME_COARSE, EINVAL } + }; for (int t = 0; t < array_length (types); t++) for (int r = 0; r < array_length (robust); r++) - { - pthread_mutexattr_t attr; - - xpthread_mutexattr_init (&attr); - xpthread_mutexattr_setprotocol (&attr, PTHREAD_PRIO_INHERIT); - xpthread_mutexattr_settype (&attr, types[t]); - xpthread_mutexattr_setrobust (&attr, robust[r]); + for (int c = 0; c < array_length (clocks); c++) + { + pthread_mutexattr_t attr; + xpthread_mutexattr_init (&attr); + xpthread_mutexattr_setprotocol (&attr, PTHREAD_PRIO_INHERIT); + xpthread_mutexattr_settype (&attr, types[t]); + xpthread_mutexattr_setrobust (&attr, robust[r]); - pthread_mutex_t mtx; - xpthread_mutex_init (&mtx, &attr); + pthread_mutex_t mtx; + xpthread_mutex_init (&mtx, &attr); - struct timespec tmo = timespec_add (xclock_now (CLOCK_MONOTONIC), - make_timespec (0, 100000000)); + /* Uncontended case does not trigger any futex call. */ + struct timespec tmo = timespec_add (xclock_now (clocks[c].clk), + make_timespec (0, 100000000)); - TEST_COMPARE (pthread_mutex_clocklock (&mtx, CLOCK_MONOTONIC, &tmo), - EINVAL); + TEST_COMPARE (pthread_mutex_clocklock (&mtx, clocks[c].clk, &tmo), + clocks[c].r); + if (clocks[c].r == 0) + TEST_COMPARE (pthread_mutex_unlock (&mtx), 0); - xpthread_mutex_destroy (&mtx); - } + xpthread_mutex_destroy (&mtx); + } return 0; } diff --git a/sysdeps/pthread/tst-mutex5.c b/sysdeps/pthread/tst-mutex5.c index 7dc5331cfc..5ca8a45eda 100644 --- a/sysdeps/pthread/tst-mutex5.c +++ b/sysdeps/pthread/tst-mutex5.c @@ -26,6 +26,7 @@ #include #include #include +#include #ifdef ENABLE_PP #include "tst-tpp.h" @@ -40,7 +41,7 @@ #define CLOCK_USE_TIMEDLOCK (-1) static int -do_test_clock (clockid_t clockid, const char *fnname) +do_test_clock (clockid_t clockid, const char *fnname, int tmo_result) { pthread_mutex_t m; pthread_mutexattr_t a; @@ -77,11 +78,12 @@ do_test_clock (clockid_t clockid, const char *fnname) make_timespec (2, 0)); if (clockid == CLOCK_USE_TIMEDLOCK) - TEST_COMPARE (pthread_mutex_timedlock (&m, &ts_timeout), ETIMEDOUT); + TEST_COMPARE (pthread_mutex_timedlock (&m, &ts_timeout), tmo_result); else TEST_COMPARE (pthread_mutex_clocklock (&m, clockid, &ts_timeout), - ETIMEDOUT); - TEST_TIMESPEC_BEFORE_NOW (ts_timeout, clockid_for_get); + tmo_result); + if (tmo_result == ETIMEDOUT) + TEST_TIMESPEC_BEFORE_NOW (ts_timeout, clockid_for_get); /* The following makes the ts value invalid. */ ts_timeout.tv_nsec += 1000000000; @@ -120,11 +122,16 @@ static int do_test (void) init_tpp_test (); #endif - do_test_clock (CLOCK_USE_TIMEDLOCK, "timedlock"); - do_test_clock (CLOCK_REALTIME, "clocklock(realtime)"); -#ifndef ENABLE_PI - do_test_clock (CLOCK_MONOTONIC, "clocklock(monotonic)"); + int monotonic_result = +#ifdef ENABLE_PI + xpthread_mutex_pi_support_monotonic () ? ETIMEDOUT : EINVAL; +#else + ETIMEDOUT; #endif + + do_test_clock (CLOCK_USE_TIMEDLOCK, "timedlock", ETIMEDOUT); + do_test_clock (CLOCK_REALTIME, "clocklock(realtime)", ETIMEDOUT); + do_test_clock (CLOCK_MONOTONIC, "clocklock(monotonic)", monotonic_result); return 0; } diff --git a/sysdeps/pthread/tst-mutex9.c b/sysdeps/pthread/tst-mutex9.c index 58c3a1aec2..5448e4ec16 100644 --- a/sysdeps/pthread/tst-mutex9.c +++ b/sysdeps/pthread/tst-mutex9.c @@ -29,6 +29,7 @@ #include #include #include +#include #ifdef ENABLE_PP #include "tst-tpp.h" @@ -40,7 +41,7 @@ #define CLOCK_USE_TIMEDLOCK (-1) static void -do_test_clock (clockid_t clockid) +do_test_clock (clockid_t clockid, int tmo_result) { const clockid_t clockid_for_get = (clockid == CLOCK_USE_TIMEDLOCK) ? CLOCK_REALTIME : clockid; @@ -112,9 +113,9 @@ do_test_clock (clockid_t clockid) make_timespec (0, 500000000)); if (clockid == CLOCK_USE_TIMEDLOCK) - TEST_COMPARE (pthread_mutex_timedlock (m, &ts), ETIMEDOUT); + TEST_COMPARE (pthread_mutex_timedlock (m, &ts), tmo_result); else - TEST_COMPARE (pthread_mutex_clocklock (m, clockid, &ts), ETIMEDOUT); + TEST_COMPARE (pthread_mutex_clocklock (m, clockid, &ts), tmo_result); alarm (1); @@ -142,11 +143,16 @@ do_test (void) init_tpp_test (); #endif - do_test_clock (CLOCK_USE_TIMEDLOCK); - do_test_clock (CLOCK_REALTIME); -#ifndef ENABLE_PI - do_test_clock (CLOCK_MONOTONIC); + int monotonic_result = +#ifdef ENABLE_PI + xpthread_mutex_pi_support_monotonic () ? ETIMEDOUT : EINVAL; +#else + ETIMEDOUT; #endif + + do_test_clock (CLOCK_USE_TIMEDLOCK, ETIMEDOUT); + do_test_clock (CLOCK_REALTIME, ETIMEDOUT); + do_test_clock (CLOCK_MONOTONIC, monotonic_result); return 0; } -- 2.30.2