Skip to content

Commit 829f414

Browse files
author
Maksim Milyutin
committed
Fix shm_mq_receive_with_timeout
1 parent d79e373 commit 829f414

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

contrib/pg_query_state/pg_query_state.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -718,31 +718,32 @@ shm_mq_receive_with_timeout(shm_mq_handle *mqh,
718718
void **datap,
719719
long timeout)
720720
{
721-
722-
#ifdef HAVE_INT64_TIMESTAMP
723-
#define GetNowFloat() ((float8) GetCurrentTimestamp() / 1000.0)
724-
#else
725-
#define GetNowFloat() 1000.0 * GetCurrentTimestamp()
726-
#endif
727-
728-
float8 endtime = GetNowFloat() + timeout;
729-
int rc = 0;
721+
int rc = 0;
722+
long delay = timeout;
730723

731724
for (;;)
732725
{
733-
long delay;
726+
instr_time start_time;
727+
instr_time cur_time;
734728
shm_mq_result mq_receive_result;
735729

736-
mq_receive_result = shm_mq_receive(mqh, nbytesp, datap, true);
730+
elog(INFO, "%ld", delay);
731+
732+
INSTR_TIME_SET_CURRENT(start_time);
737733

734+
mq_receive_result = shm_mq_receive(mqh, nbytesp, datap, true);
738735
if (mq_receive_result != SHM_MQ_WOULD_BLOCK)
739736
return mq_receive_result;
740-
741-
if (rc & WL_TIMEOUT)
737+
if (rc & WL_TIMEOUT || delay <= 0)
742738
return SHM_MQ_WOULD_BLOCK;
743739

744-
delay = (long) (endtime - GetNowFloat());
745740
rc = WaitLatch(MyLatch, WL_LATCH_SET | WL_TIMEOUT, delay);
741+
742+
INSTR_TIME_SET_CURRENT(cur_time);
743+
INSTR_TIME_SUBTRACT(cur_time, start_time);
744+
745+
delay = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
746+
746747
CHECK_FOR_INTERRUPTS();
747748
ResetLatch(MyLatch);
748749
}

0 commit comments

Comments
 (0)