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: AS22989 209.51.188.0/24 X-Spam-Status: No, score=-4.2 required=3.0 tests=AWL,BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 9AF2E1F45E for ; Sun, 16 Feb 2020 20:56:00 +0000 (UTC) Received: from localhost ([::1]:36308 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j3QxL-0003Wt-98 for normalperson@yhbt.net; Sun, 16 Feb 2020 15:55:59 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:42291) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j3QxG-0003W0-Sb for bug-gnulib@gnu.org; Sun, 16 Feb 2020 15:55:56 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j3QxE-00031A-G0 for bug-gnulib@gnu.org; Sun, 16 Feb 2020 15:55:54 -0500 Received: from zimbra.cs.ucla.edu ([131.179.128.68]:58500) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j3QxE-0002xU-4g for bug-gnulib@gnu.org; Sun, 16 Feb 2020 15:55:52 -0500 Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id 4AA92160098; Sun, 16 Feb 2020 12:55:49 -0800 (PST) Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id 37euolrCkgCf; Sun, 16 Feb 2020 12:55:48 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id 0975E1600AF; Sun, 16 Feb 2020 12:55:48 -0800 (PST) X-Virus-Scanned: amavisd-new at zimbra.cs.ucla.edu Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id XY0ikWqkGplq; Sun, 16 Feb 2020 12:55:47 -0800 (PST) Received: from [192.168.1.9] (cpe-23-242-74-103.socal.res.rr.com [23.242.74.103]) by zimbra.cs.ucla.edu (Postfix) with ESMTPSA id D6742160098; Sun, 16 Feb 2020 12:55:47 -0800 (PST) Subject: Re: [PATCH 0/1] xnanosleep: Use pause for infinite duration. To: Vladimir Panteleev References: <20200210122613.397937-1-git@thecybershadow.net> From: Paul Eggert Organization: UCLA Computer Science Department Message-ID: <6737dc31-cb51-69ea-e1e7-dbac3997f0b8@cs.ucla.edu> Date: Sun, 16 Feb 2020 12:55:47 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.4.1 MIME-Version: 1.0 In-Reply-To: <20200210122613.397937-1-git@thecybershadow.net> Content-Type: multipart/mixed; boundary="------------F155D630C0CEDE52E3278821" Content-Language: en-US X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 131.179.128.68 X-BeenThere: bug-gnulib@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Gnulib discussion list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: bug-gnulib@gnu.org Errors-To: bug-gnulib-bounces+normalperson=yhbt.net@gnu.org Sender: "bug-gnulib" This is a multi-part message in MIME format. --------------F155D630C0CEDE52E3278821 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit On 2/10/20 4:26 AM, Vladimir Panteleev wrote: > I'm not sure if isfinite is the best way to check for infinity, as far > as the tradeoff between portability and dependencies goes. We can use the same test that dtotimespec uses, since we want to sleep forever not only if the argument is infinity, but also if it exceeds the maximum possible timespec value. So there's no need for the isfinite portability baggage. > By the way, shouldn't xnanosleep be using the second nanosleep > parameter to track how much time there's left to sleep in case of an > interruption? I vaguely recall not doing that because some old kernels messed up when setting the time remaining. They're long obsolete now, so it's probably not worth worrying about. If I'm wrong and it is an issue it should be fixed in the nanosleep module anyway. I installed the attached, which is a bit more conservative than your suggestion, as it doesn't assume that 'pause' exists or that it indeed sleeps forever (though I know of no counterexamples, there's little difficulty in relaxing those assumptions). Thanks for the bug report. --------------F155D630C0CEDE52E3278821 Content-Type: text/x-patch; charset=UTF-8; name="0001-xnanosleep-prefer-pause-and-get-remaining-time.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="0001-xnanosleep-prefer-pause-and-get-remaining-time.patch" >From f3243f20107435f620338aa15e1fdd5779d639fe Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sun, 16 Feb 2020 12:47:52 -0800 Subject: [PATCH] xnanosleep: prefer pause, and get remaining time Problem reported by Vladimir Panteleev in: https://lists.gnu.org/r/bug-gnulib/2020-02/msg00052.html * lib/xnanosleep.c: Include intprops.h, unistd.h. (xnanosleep) [HAVE_PAUSE]: Prefer pause when sleeping infinitely. (xnanosleep): Obtain remaining time when nanosleep is interrupted. * m4/xnanosleep.m4 (gl_XNANOSLEEP): Check for 'pause'. * modules/xnanosleep (Depends-on): Add intprops, unistd. --- ChangeLog | 11 +++++++++++ lib/xnanosleep.c | 22 ++++++++++++++++++++-- m4/xnanosleep.m4 | 4 ++-- modules/xnanosleep | 2 ++ 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index beacc0926..4e3f51ba2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2020-02-16 Paul Eggert + + xnanosleep: prefer pause, and get remaining time + Problem reported by Vladimir Panteleev in: + https://lists.gnu.org/r/bug-gnulib/2020-02/msg00052.html + * lib/xnanosleep.c: Include intprops.h, unistd.h. + (xnanosleep) [HAVE_PAUSE]: Prefer pause when sleeping infinitely. + (xnanosleep): Obtain remaining time when nanosleep is interrupted. + * m4/xnanosleep.m4 (gl_XNANOSLEEP): Check for 'pause'. + * modules/xnanosleep (Depends-on): Add intprops, unistd. + 2020-02-16 Bruno Haible lchmod: Improve cross-compilation guess. diff --git a/lib/xnanosleep.c b/lib/xnanosleep.c index 5774f75f3..8619c6df6 100644 --- a/lib/xnanosleep.c +++ b/lib/xnanosleep.c @@ -22,10 +22,12 @@ #include "xnanosleep.h" +#include #include #include #include +#include /* Sleep until the time (call it WAKE_UP_TIME) specified as SECONDS seconds after the time this function is called. @@ -37,6 +39,17 @@ int xnanosleep (double seconds) { +#if HAVE_PAUSE + if (1.0 + TYPE_MAXIMUM (time_t) <= seconds) + { + do + pause (); + while (errno == EINTR); + + /* pause failed (!); fall back on repeated nanosleep calls. */ + } +#endif + struct timespec ts_sleep = dtotimespec (seconds); for (;;) @@ -46,9 +59,14 @@ xnanosleep (double seconds) set errno to EINTR. nanosleep from linux-2.6.10, as well as implementations by (all?) other vendors, doesn't return -1 in that case; either it continues sleeping (if time remains) - or it returns zero (if the wake-up time has passed). */ + or it returns zero (if the wake-up time has passed). + + Gnulib's replacement nanosleep sometimes does not update + TS_SLEEP, and it is possible some kernels have a similar bug. + However, this merely causes xnanosleep to sleep longer than + necessary, which is not a correctness bug. */ errno = 0; - if (nanosleep (&ts_sleep, NULL) == 0) + if (nanosleep (&ts_sleep, &ts_sleep) == 0) break; if (errno != EINTR && errno != 0) return -1; diff --git a/m4/xnanosleep.m4 b/m4/xnanosleep.m4 index 5f305f864..88454ae01 100644 --- a/m4/xnanosleep.m4 +++ b/m4/xnanosleep.m4 @@ -1,4 +1,4 @@ -#serial 5 +#serial 6 dnl Copyright (C) 2005-2006, 2009-2020 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -8,5 +8,5 @@ dnl Written by Paul Eggert. AC_DEFUN([gl_XNANOSLEEP], [ - : + AC_CHECK_FUNCS_ONCE([pause]) ]) diff --git a/modules/xnanosleep b/modules/xnanosleep index 0a2b373ea..cc9069cb2 100644 --- a/modules/xnanosleep +++ b/modules/xnanosleep @@ -8,8 +8,10 @@ m4/xnanosleep.m4 Depends-on: dtotimespec +intprops nanosleep time +unistd configure.ac: gl_XNANOSLEEP -- 2.17.1 --------------F155D630C0CEDE52E3278821--