Skip to content

Commit 215cbc9

Browse files
committed
Add emulation of non-blocking sockets to the win32 socket/signal layer,
and use this in pq_getbyte_if_available. It's only a limited implementation which swithes the whole emulation layer no non-blocking mode, but that's enough as long as non-blocking is only used during a short period of time, and only one socket is accessed during this time.
1 parent 492eaef commit 215cbc9

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

src/backend/libpq/pqcomm.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
3131
* Portions Copyright (c) 1994, Regents of the University of California
3232
*
33-
* $PostgreSQL: pgsql/src/backend/libpq/pqcomm.c,v 1.202 2010/01/15 09:19:02 heikki Exp $
33+
* $PostgreSQL: pgsql/src/backend/libpq/pqcomm.c,v 1.203 2010/02/16 19:26:02 mha Exp $
3434
*
3535
*-------------------------------------------------------------------------
3636
*/
@@ -837,9 +837,13 @@ pq_getbyte_if_available(unsigned char *c)
837837
}
838838

839839
/* Temporarily put the socket into non-blocking mode */
840+
#ifdef WIN32
841+
pgwin32_noblock = 1;
842+
#else
840843
if (!pg_set_noblock(MyProcPort->sock))
841844
ereport(ERROR,
842845
(errmsg("couldn't put socket to non-blocking mode: %m")));
846+
#endif
843847
MyProcPort->noblock = true;
844848
PG_TRY();
845849
{
@@ -851,16 +855,24 @@ pq_getbyte_if_available(unsigned char *c)
851855
* The rest of the backend code assumes the socket is in blocking
852856
* mode, so treat failure as FATAL.
853857
*/
858+
#ifdef WIN32
859+
pgwin32_noblock = 0;
860+
#else
854861
if (!pg_set_block(MyProcPort->sock))
855862
ereport(FATAL,
856863
(errmsg("couldn't put socket to blocking mode: %m")));
864+
#endif
857865
MyProcPort->noblock = false;
858866
PG_RE_THROW();
859867
}
860868
PG_END_TRY();
869+
#ifdef WIN32
870+
pgwin32_noblock = 0;
871+
#else
861872
if (!pg_set_block(MyProcPort->sock))
862873
ereport(FATAL,
863874
(errmsg("couldn't put socket to blocking mode: %m")));
875+
#endif
864876
MyProcPort->noblock = false;
865877

866878
return r;

src/backend/port/win32/socket.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,26 @@
66
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
77
*
88
* IDENTIFICATION
9-
* $PostgreSQL: pgsql/src/backend/port/win32/socket.c,v 1.23 2010/01/02 16:57:50 momjian Exp $
9+
* $PostgreSQL: pgsql/src/backend/port/win32/socket.c,v 1.24 2010/02/16 19:26:02 mha Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
1313

1414
#include "postgres.h"
1515

16+
/*
17+
* Indicate if pgwin32_recv() should operate in non-blocking mode.
18+
*
19+
* Since the socket emulation layer always sets the actual socket to
20+
* non-blocking mode in order to be able to deliver signals, we must
21+
* specify this in a separate flag if we actually need non-blocking
22+
* operation.
23+
*
24+
* This flag changes the behaviour *globally* for all socket operations,
25+
* so it should only be set for very short periods of time.
26+
*/
27+
int pgwin32_noblock = 0;
28+
1629
#undef socket
1730
#undef accept
1831
#undef connect
@@ -310,6 +323,16 @@ pgwin32_recv(SOCKET s, char *buf, int len, int f)
310323
return -1;
311324
}
312325

326+
if (pgwin32_noblock)
327+
{
328+
/*
329+
* No data received, and we are in "emulated non-blocking mode", so return
330+
* indicating thta we'd block if we were to continue.
331+
*/
332+
errno = EWOULDBLOCK;
333+
return -1;
334+
}
335+
313336
/* No error, zero bytes (win2000+) or error+WSAEWOULDBLOCK (<=nt4) */
314337

315338
for (n = 0; n < 5; n++)

src/include/port/win32.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $PostgreSQL: pgsql/src/include/port/win32.h,v 1.92 2010/02/13 02:34:14 tgl Exp $ */
1+
/* $PostgreSQL: pgsql/src/include/port/win32.h,v 1.93 2010/02/16 19:26:02 mha Exp $ */
22

33
#if defined(_MSC_VER) || defined(__BORLANDC__)
44
#define WIN32_ONLY_COMPILER
@@ -283,6 +283,8 @@ int pgwin32_send(SOCKET s, char *buf, int len, int flags);
283283
const char *pgwin32_socket_strerror(int err);
284284
int pgwin32_waitforsinglesocket(SOCKET s, int what, int timeout);
285285

286+
extern int pgwin32_noblock;
287+
286288
/* in backend/port/win32/security.c */
287289
extern int pgwin32_is_admin(void);
288290
extern int pgwin32_is_service(void);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy