@@ -924,7 +924,22 @@ ModifyWaitEvent(WaitEventSet *set, int pos, uint32 events, Latch *latch)
924
924
925
925
if (events == WL_LATCH_SET )
926
926
{
927
+ if (latch && latch -> owner_pid != MyProcPid )
928
+ elog (ERROR , "cannot wait on a latch owned by another process" );
927
929
set -> latch = latch ;
930
+ /*
931
+ * On Unix, we don't need to modify the kernel object because the
932
+ * underlying pipe is the same for all latches so we can return
933
+ * immediately. On Windows, we need to update our array of handles,
934
+ * but we leave the old one in place and tolerate spurious wakeups if
935
+ * the latch is disabled.
936
+ */
937
+ #if defined(WAIT_USE_WIN32 )
938
+ if (!latch )
939
+ return ;
940
+ #else
941
+ return ;
942
+ #endif
928
943
}
929
944
930
945
#if defined(WAIT_USE_EPOLL )
@@ -1386,7 +1401,7 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
1386
1401
/* There's data in the self-pipe, clear it. */
1387
1402
drainSelfPipe ();
1388
1403
1389
- if (set -> latch -> is_set )
1404
+ if (set -> latch && set -> latch -> is_set )
1390
1405
{
1391
1406
occurred_events -> fd = PGINVALID_SOCKET ;
1392
1407
occurred_events -> events = WL_LATCH_SET ;
@@ -1536,7 +1551,7 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
1536
1551
/* There's data in the self-pipe, clear it. */
1537
1552
drainSelfPipe ();
1538
1553
1539
- if (set -> latch -> is_set )
1554
+ if (set -> latch && set -> latch -> is_set )
1540
1555
{
1541
1556
occurred_events -> fd = PGINVALID_SOCKET ;
1542
1557
occurred_events -> events = WL_LATCH_SET ;
@@ -1645,7 +1660,7 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
1645
1660
/* There's data in the self-pipe, clear it. */
1646
1661
drainSelfPipe ();
1647
1662
1648
- if (set -> latch -> is_set )
1663
+ if (set -> latch && set -> latch -> is_set )
1649
1664
{
1650
1665
occurred_events -> fd = PGINVALID_SOCKET ;
1651
1666
occurred_events -> events = WL_LATCH_SET ;
@@ -1812,7 +1827,7 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
1812
1827
if (!ResetEvent (set -> latch -> event ))
1813
1828
elog (ERROR , "ResetEvent failed: error code %lu" , GetLastError ());
1814
1829
1815
- if (set -> latch -> is_set )
1830
+ if (set -> latch && set -> latch -> is_set )
1816
1831
{
1817
1832
occurred_events -> fd = PGINVALID_SOCKET ;
1818
1833
occurred_events -> events = WL_LATCH_SET ;
0 commit comments