@ -1458,9 +1458,9 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
int rc ;
int rc ;
/*
/*
* Check if the latch is set already . If so , leave the loop
* Check if the latch is set already first . If so , we either exit
* immediately , avoid blocking again . We don ' t attempt to report any
* immediately or ask the kernel for further events available right
* other events that might also be satisfied .
* now without waiting , depending on how many events the caller wants .
*
*
* If someone sets the latch between this and the
* If someone sets the latch between this and the
* WaitEventSetWaitBlock ( ) below , the setter will write a byte to the
* WaitEventSetWaitBlock ( ) below , the setter will write a byte to the
@ -1505,7 +1505,16 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
/* could have been set above */
/* could have been set above */
set - > latch - > maybe_sleeping = false ;
set - > latch - > maybe_sleeping = false ;
break ;
if ( returned_events = = nevents )
break ; /* output buffer full already */
/*
* Even though we already have an event , we ' ll poll just once with
* zero timeout to see what non - latch events we can fit into the
* output buffer at the same time .
*/
cur_timeout = 0 ;
timeout = 0 ;
}
}
/*
/*
@ -1514,18 +1523,16 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
* to retry , everything > = 1 is the number of returned events .
* to retry , everything > = 1 is the number of returned events .
*/
*/
rc = WaitEventSetWaitBlock ( set , cur_timeout ,
rc = WaitEventSetWaitBlock ( set , cur_timeout ,
occurred_events , nevents ) ;
occurred_events , nevents - returned_events ) ;
if ( set - > latch )
if ( set - > latch & &
{
set - > latch - > maybe_sleeping )
Assert ( set - > latch - > maybe_sleeping ) ;
set - > latch - > maybe_sleeping = false ;
set - > latch - > maybe_sleeping = false ;
}
if ( rc = = - 1 )
if ( rc = = - 1 )
break ; /* timeout occurred */
break ; /* timeout occurred */
else
else
returned_events = rc ;
returned_events + = rc ;
/* If we're not done, update cur_timeout for next iteration */
/* If we're not done, update cur_timeout for next iteration */
if ( returned_events = = 0 & & timeout > = 0 )
if ( returned_events = = 0 & & timeout > = 0 )
@ -1613,7 +1620,7 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
/* Drain the signalfd. */
/* Drain the signalfd. */
drain ( ) ;
drain ( ) ;
if ( set - > latch & & set - > latch - > is_set )
if ( set - > latch & & set - > latch - > maybe_sleeping & & set - > latch - > is_set )
{
{
occurred_events - > fd = PGINVALID_SOCKET ;
occurred_events - > fd = PGINVALID_SOCKET ;
occurred_events - > events = WL_LATCH_SET ;
occurred_events - > events = WL_LATCH_SET ;
@ -1772,7 +1779,7 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
if ( cur_event - > events = = WL_LATCH_SET & &
if ( cur_event - > events = = WL_LATCH_SET & &
cur_kqueue_event - > filter = = EVFILT_SIGNAL )
cur_kqueue_event - > filter = = EVFILT_SIGNAL )
{
{
if ( set - > latch & & set - > latch - > is_set )
if ( set - > latch & & set - > latch - > maybe_sleeping & & set - > latch - > is_set )
{
{
occurred_events - > fd = PGINVALID_SOCKET ;
occurred_events - > fd = PGINVALID_SOCKET ;
occurred_events - > events = WL_LATCH_SET ;
occurred_events - > events = WL_LATCH_SET ;
@ -1897,7 +1904,7 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
/* There's data in the self-pipe, clear it. */
/* There's data in the self-pipe, clear it. */
drain ( ) ;
drain ( ) ;
if ( set - > latch & & set - > latch - > is_set )
if ( set - > latch & & set - > latch - > maybe_sleeping & & set - > latch - > is_set )
{
{
occurred_events - > fd = PGINVALID_SOCKET ;
occurred_events - > fd = PGINVALID_SOCKET ;
occurred_events - > events = WL_LATCH_SET ;
occurred_events - > events = WL_LATCH_SET ;
@ -2113,7 +2120,7 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
if ( ! ResetEvent ( set - > handles [ cur_event - > pos + 1 ] ) )
if ( ! ResetEvent ( set - > handles [ cur_event - > pos + 1 ] ) )
elog ( ERROR , " ResetEvent failed: error code %lu " , GetLastError ( ) ) ;
elog ( ERROR , " ResetEvent failed: error code %lu " , GetLastError ( ) ) ;
if ( set - > latch & & set - > latch - > is_set )
if ( set - > latch & & set - > latch - > maybe_sleeping & & set - > latch - > is_set )
{
{
occurred_events - > fd = PGINVALID_SOCKET ;
occurred_events - > fd = PGINVALID_SOCKET ;
occurred_events - > events = WL_LATCH_SET ;
occurred_events - > events = WL_LATCH_SET ;