From 2750b11e181a691363f0660b197dd0a57f9accc2 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Tue, 7 Sep 2004 14:31:42 +0000
Subject: [PATCH] Fix places where WaitForxxx can block, to eliminate failure
 to detect deadlock on Win32.  Magnus Hagander

---
 src/backend/port/win32/sema.c   |  6 +++---
 src/backend/port/win32/socket.c | 12 ++++++------
 2 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/src/backend/port/win32/sema.c b/src/backend/port/win32/sema.c
index 71ab24c35c5..7676f2d5352 100644
--- a/src/backend/port/win32/sema.c
+++ b/src/backend/port/win32/sema.c
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/port/win32/sema.c,v 1.8 2004/08/29 05:06:46 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/port/win32/sema.c,v 1.9 2004/09/07 14:31:42 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -233,7 +233,7 @@ semop(int semId, struct sembuf * sops, int nsops)
 		wh[0] = cur_handle;
 		wh[1] = pgwin32_signal_event;
 
-		ret = WaitForMultipleObjects(2, wh, FALSE, (sops[0].sem_flg & IPC_NOWAIT) ? 0 : INFINITE);
+		ret = WaitForMultipleObjectsEx(2, wh, FALSE, (sops[0].sem_flg & IPC_NOWAIT) ? 0 : INFINITE, TRUE);
 
 		if (ret == WAIT_OBJECT_0)
 		{
@@ -241,7 +241,7 @@ semop(int semId, struct sembuf * sops, int nsops)
 			sem_counts[sops[0].sem_num]--;
 			return 0;
 		}
-		else if (ret == WAIT_OBJECT_0 + 1)
+		else if (ret == WAIT_OBJECT_0 + 1 || ret == WAIT_IO_COMPLETION)
 		{
 			/* Signal event is set - we have a signal to deliver */
 			pgwin32_dispatch_queued_signals();
diff --git a/src/backend/port/win32/socket.c b/src/backend/port/win32/socket.c
index 4835f4eab3e..9be5a7ae134 100644
--- a/src/backend/port/win32/socket.c
+++ b/src/backend/port/win32/socket.c
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/port/win32/socket.c,v 1.5 2004/08/30 02:54:38 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/port/win32/socket.c,v 1.6 2004/09/07 14:31:42 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -93,7 +93,7 @@ TranslateSocketError(void)
 static int
 pgwin32_poll_signals(void)
 {
-	if (WaitForSingleObject(pgwin32_signal_event, 0) == WAIT_OBJECT_0)
+	if (WaitForSingleObjectEx(pgwin32_signal_event, 0, TRUE) == WAIT_OBJECT_0)
 	{
 		pgwin32_dispatch_queued_signals();
 		errno = EINTR;
@@ -130,9 +130,9 @@ pgwin32_waitforsinglesocket(SOCKET s, int what)
 
 	events[0] = pgwin32_signal_event;
 	events[1] = waitevent;
-	r = WaitForMultipleObjects(2, events, FALSE, INFINITE);
+	r = WaitForMultipleObjectsEx(2, events, FALSE, INFINITE, TRUE);
 
-	if (r == WAIT_OBJECT_0)
+	if (r == WAIT_OBJECT_0 || r == WAIT_IO_COMPLETION)
 	{
 		pgwin32_dispatch_queued_signals();
 		errno = EINTR;
@@ -419,8 +419,8 @@ pgwin32_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, c
 	}
 
 	events[numevents] = pgwin32_signal_event;
-	r = WaitForMultipleObjectsEx(numevents + 1, events, FALSE, timeoutval, FALSE);
-	if (r != WSA_WAIT_TIMEOUT && r != (WAIT_OBJECT_0 + numevents))
+	r = WaitForMultipleObjectsEx(numevents + 1, events, FALSE, timeoutval, TRUE);
+	if (r != WAIT_TIMEOUT && r != WAIT_IO_COMPLETION && r != (WAIT_OBJECT_0 + numevents))
 	{
 		/*
 		 * We scan all events, even those not signalled, in case more than
-- 
GitLab