Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h72JR-0005tA-Dt for pgsql-hackers@arkaria.postgresql.org; Thu, 21 Mar 2019 18:21:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1h72JP-0002yX-O6 for pgsql-hackers@arkaria.postgresql.org; Thu, 21 Mar 2019 18:21:07 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h72JP-0002yP-Gd for pgsql-hackers@lists.postgresql.org; Thu, 21 Mar 2019 18:21:07 +0000 Received: from smtp-fw-33001.amazon.com ([207.171.190.10]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h72JM-0005s2-J0 for pgsql-hackers@postgresql.org; Thu, 21 Mar 2019 18:21:06 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1553192464; x=1584728464; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=9RIJX6KoHJtrjmHvEDk0Thu5i45feSdkVngs1ZkJQuc=; b=AFrrB0KKmLeVx8E8t1NllLwLsJ4urX0Xlsrl8Ac/E5bwPEL+4RFnXCUh B+sfx21ZnIjovBLi0SM+ngIy2ZyrAGNXUKU519Gr9mJL48hV7ejI0vgpW HUdtqtralhBbkL40tOJ3egF0BovD0nVXgRr1Iveg7zCF3ycD/1qaQUtWk Y=; X-Amazon-filename: 0001-Introduce-timeout-capability-for-ConditionVariableSleep-v4.patch X-IronPort-AV: E=Sophos;i="5.60,253,1549929600"; d="scan'208,223";a="790124411" Received: from sea3-co-svc-lb6-vlan2.sea.amazon.com (HELO email-inbound-relay-2a-c5104f52.us-west-2.amazon.com) ([10.47.22.34]) by smtp-border-fw-out-33001.sea14.amazon.com with ESMTP/TLS/DHE-RSA-AES256-SHA; 21 Mar 2019 18:20:55 +0000 Received: from EX13MTAUWC001.ant.amazon.com (pdx1-ws-svc-p6-lb9-vlan3.pdx.amazon.com [10.236.137.198]) by email-inbound-relay-2a-c5104f52.us-west-2.amazon.com (8.14.7/8.14.7) with ESMTP id x2LIKrYJ036689 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=FAIL); Thu, 21 Mar 2019 18:20:55 GMT Received: from EX13D05UWC002.ant.amazon.com (10.43.162.92) by EX13MTAUWC001.ant.amazon.com (10.43.162.135) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Thu, 21 Mar 2019 18:20:52 +0000 Received: from f01898859afd.ant.amazon.com (10.43.160.69) by EX13D05UWC002.ant.amazon.com (10.43.162.92) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Thu, 21 Mar 2019 18:20:52 +0000 Date: Thu, 21 Mar 2019 14:20:50 -0400 From: Shawn Debnath To: Kyotaro HORIGUCHI CC: , Subject: Re: Introduce timeout capability for ConditionVariableSleep Message-ID: <20190321182049.GA95415@f01898859afd.ant.amazon.com> References: <20190313005342.GA8301@f01898859afd.ant.amazon.com> <20190313.172415.156658364.horiguchi.kyotaro@lab.ntt.co.jp> <20190315002611.GA1119@f01898859afd.ant.amazon.com> <20190315.141517.229371570.horiguchi.kyotaro@lab.ntt.co.jp> <20190316222717.GA2139@f01898859afd.ant.amazon.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="HlL+5n6rz5pIUxbD" Content-Disposition: inline In-Reply-To: <20190316222717.GA2139@f01898859afd.ant.amazon.com> User-Agent: Mutt/1.11.3 (2019-02-01) X-Originating-IP: [10.43.160.69] X-ClientProxiedBy: EX13D17UWB004.ant.amazon.com (10.43.161.132) To EX13D05UWC002.ant.amazon.com (10.43.162.92) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --HlL+5n6rz5pIUxbD Content-Type: text/plain; charset="us-ascii" Content-Disposition: inline On Sat, Mar 16, 2019 at 03:27:17PM -0700, Shawn Debnath wrote: > > + * Track the current time so that we can calculate the > > remaining timeout > > + * if we are woken up spuriously. > > > > I think tha "track" means chasing a moving objects. So it might > > be bettter that it is record or something? > > > > > * Wait for the given condition variable to be signaled or till timeout. > > > * > > > * Returns -1 when timeout expires, otherwise returns 0. > > > * > > > * See ConditionVariableSleep() for general usage. > > > > > > > +ConditionVariableTimedSleep(ConditionVariable *cv, long timeout, > > > > > > > > Counldn't the two-state return value be a boolean? > > I will change it to Record in the next iteration of the patch. Posting rebased and updated patch. Changed the word 'Track' to 'Record' and also changed variable name rem_timeout to cur_timeout to match naming in other use cases. -- Shawn Debnath Amazon Web Services (AWS) --HlL+5n6rz5pIUxbD Content-Type: text/plain; charset="us-ascii" Content-Disposition: attachment; filename="0001-Introduce-timeout-capability-for-ConditionVariableSleep-v4.patch" From 537e879a66f7d59333ede62e8f7c42fec4006a31 Mon Sep 17 00:00:00 2001 From: Shawn Debnath Date: Tue, 12 Mar 2019 23:21:41 +0000 Subject: [PATCH] Introduce timeout capability for ConditionVariableSleep This patch introduces ConditionVariableTimedSleep to enable timing out of waiting for a condition variable to get signalled. --- src/backend/storage/lmgr/condition_variable.c | 63 ++++++++++++++++++++++++--- src/include/storage/condition_variable.h | 2 + 2 files changed, 59 insertions(+), 6 deletions(-) diff --git a/src/backend/storage/lmgr/condition_variable.c b/src/backend/storage/lmgr/condition_variable.c index 58b7b51472..390072451c 100644 --- a/src/backend/storage/lmgr/condition_variable.c +++ b/src/backend/storage/lmgr/condition_variable.c @@ -19,6 +19,7 @@ #include "postgres.h" #include "miscadmin.h" +#include "portability/instr_time.h" #include "storage/condition_variable.h" #include "storage/ipc.h" #include "storage/proc.h" @@ -122,8 +123,23 @@ ConditionVariablePrepareToSleep(ConditionVariable *cv) void ConditionVariableSleep(ConditionVariable *cv, uint32 wait_event_info) { - WaitEvent event; - bool done = false; + (void) ConditionVariableTimedSleep(cv, -1 /* no timeout */, wait_event_info); +} + +/* + * Wait for the given condition variable to be signaled or till timeout. + * + * Returns true when timeout expires, otherwise returns false. + * + * See ConditionVariableSleep() for general usage. + */ +bool +ConditionVariableTimedSleep(ConditionVariable *cv, long timeout, + uint32 wait_event_info) +{ + long cur_timeout = -1; + instr_time start_time; + instr_time cur_time; /* * If the caller didn't prepare to sleep explicitly, then do so now and @@ -143,23 +159,42 @@ ConditionVariableSleep(ConditionVariable *cv, uint32 wait_event_info) if (cv_sleep_target != cv) { ConditionVariablePrepareToSleep(cv); - return; + return false; } - do + /* + * Record the current time so that we can calculate the remaining timeout + * if we are woken up spuriously. + */ + if (timeout >= 0) { + INSTR_TIME_SET_CURRENT(start_time); + Assert(timeout >= 0 && timeout <= INT_MAX); + cur_timeout = timeout; + } + + while (true) + { + WaitEvent event; + bool done = false; + int rc; + CHECK_FOR_INTERRUPTS(); /* * Wait for latch to be set. (If we're awakened for some other * reason, the code below will cope anyway.) */ - (void) WaitEventSetWait(cv_wait_event_set, -1, &event, 1, + rc = WaitEventSetWait(cv_wait_event_set, cur_timeout, &event, 1, wait_event_info); /* Reset latch before examining the state of the wait list. */ ResetLatch(MyLatch); + /* Timed out */ + if (rc == 0) + return true; + /* * If this process has been taken out of the wait list, then we know * that it has been signaled by ConditionVariableSignal (or @@ -182,7 +217,23 @@ ConditionVariableSleep(ConditionVariable *cv, uint32 wait_event_info) proclist_push_tail(&cv->wakeup, MyProc->pgprocno, cvWaitLink); } SpinLockRelease(&cv->mutex); - } while (!done); + + /* We were signaled, so return */ + if (done) + return false; + + /* If we're not done, update cur_timeout for next iteration */ + if (timeout >= 0) + { + INSTR_TIME_SET_CURRENT(cur_time); + INSTR_TIME_SUBTRACT(cur_time, start_time); + cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time); + + /* Have we crossed the timeout threshold? */ + if (cur_timeout <= 0) + return true; + } + } } /* diff --git a/src/include/storage/condition_variable.h b/src/include/storage/condition_variable.h index 2a0249392c..ee06e051ce 100644 --- a/src/include/storage/condition_variable.h +++ b/src/include/storage/condition_variable.h @@ -43,6 +43,8 @@ extern void ConditionVariableInit(ConditionVariable *cv); * the condition variable. */ extern void ConditionVariableSleep(ConditionVariable *cv, uint32 wait_event_info); +extern bool ConditionVariableTimedSleep(ConditionVariable *cv, long timeout, + uint32 wait_event_info); extern void ConditionVariableCancelSleep(void); /* -- 2.16.5 --HlL+5n6rz5pIUxbD--