Mercurial > gnulib
annotate lib/poll.c @ 17363:5a51fb7777a9
sys_select, sys_time: port 2013-01-30 Solaris 2.6 fix to Cygwin
Problem reported by Marco Atzeri in
<http://lists.gnu.org/archive/html/bug-gnulib/2013-03/msg00000.html>.
* lib/sys_select.in.h [HAVE_SYS_SELECT_H && _CYGWIN_SYS_TIME_H]:
Simply delegate to the system <sys/select.h> in this case too.
Also, pay attention to _GL_SYS_SELECT_H_REDIRECT_FROM_SYS_TIME_H only
if OSF/1, since otherwise Cygwin breaks, and it doesn't seem to
be needed on Solaris either.
* lib/sys_time.in.h [_CYGWIN_SYS_TIME_H]:
Simply delgate to the system <sys/time.h> in this case.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Tue, 19 Mar 2013 09:08:47 -0700 |
parents | e542fd46ad6f |
children | 344018b6e5d7 |
rev | line source |
---|---|
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
1 /* Emulation for poll(2) |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
2 Contributed by Paolo Bonzini. |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
3 |
17249
e542fd46ad6f
maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents:
17084
diff
changeset
|
4 Copyright 2001-2003, 2006-2013 Free Software Foundation, Inc. |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
5 |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
6 This file is part of gnulib. |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
7 |
4435 | 8 This program is free software; you can redistribute it and/or modify |
9 it under the terms of the GNU General Public License as published by | |
10 the Free Software Foundation; either version 2, or (at your option) | |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
11 any later version. |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
12 |
4435 | 13 This program is distributed in the hope that it will be useful, |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 GNU General Public License for more details. | |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
17 |
4435 | 18 You should have received a copy of the GNU General Public License along |
16366
bb182ee4a09d
maint: replace FSF snail-mail addresses with URLs
Paul Eggert <eggert@cs.ucla.edu>
parents:
16324
diff
changeset
|
19 with this program; if not, see <http://www.gnu.org/licenses/>. */ |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
20 |
11020
4494a99c424d
poll: suppress a warning
Jim Meyering <meyering@redhat.com>
parents:
11019
diff
changeset
|
21 /* Tell gcc not to warn about the (nfd < 0) tests, below. */ |
4494a99c424d
poll: suppress a warning
Jim Meyering <meyering@redhat.com>
parents:
11019
diff
changeset
|
22 #if (__GNUC__ == 4 && 3 <= __GNUC_MINOR__) || 4 < __GNUC__ |
4494a99c424d
poll: suppress a warning
Jim Meyering <meyering@redhat.com>
parents:
11019
diff
changeset
|
23 # pragma GCC diagnostic ignored "-Wtype-limits" |
4494a99c424d
poll: suppress a warning
Jim Meyering <meyering@redhat.com>
parents:
11019
diff
changeset
|
24 #endif |
4494a99c424d
poll: suppress a warning
Jim Meyering <meyering@redhat.com>
parents:
11019
diff
changeset
|
25 |
9644
42367c277240
Prefer <config.h> over "config.h". See autoconf doc for explanation.
Jim Meyering <meyering@redhat.com>
parents:
8500
diff
changeset
|
26 #include <config.h> |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
27 #include <alloca.h> |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
28 |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
29 #include <sys/types.h> |
13736 | 30 |
31 /* Specification. */ | |
32 #include <poll.h> | |
33 | |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
34 #include <errno.h> |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
35 #include <limits.h> |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
36 #include <assert.h> |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
37 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
38 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ |
16214
ec738d6aeef5
Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents:
16213
diff
changeset
|
39 # define WINDOWS_NATIVE |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
40 # include <winsock2.h> |
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
41 # include <windows.h> |
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
42 # include <io.h> |
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
43 # include <stdio.h> |
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
44 # include <conio.h> |
15752
b86e9061a6d0
New module 'msvc-nothrow'. Makes _get_osfhandle safe on MSVC 9.
Bruno Haible <bruno@clisp.org>
parents:
15353
diff
changeset
|
45 # include "msvc-nothrow.h" |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
46 #else |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
47 # include <sys/time.h> |
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
48 # include <sys/socket.h> |
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
49 # include <sys/select.h> |
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
50 # include <unistd.h> |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
51 #endif |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
52 |
7382
8eedfb060b79
2006-09-28 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
6259
diff
changeset
|
53 #ifdef HAVE_SYS_IOCTL_H |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
54 # include <sys/ioctl.h> |
7382
8eedfb060b79
2006-09-28 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
6259
diff
changeset
|
55 #endif |
8eedfb060b79
2006-09-28 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
6259
diff
changeset
|
56 #ifdef HAVE_SYS_FILIO_H |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
57 # include <sys/filio.h> |
7382
8eedfb060b79
2006-09-28 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
6259
diff
changeset
|
58 #endif |
8eedfb060b79
2006-09-28 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
6259
diff
changeset
|
59 |
7908
d41d48e822b4
* lib/poll.c: Include sys/time.h and time.h unconditionally,
Paul Eggert <eggert@cs.ucla.edu>
parents:
7776
diff
changeset
|
60 #include <time.h> |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
61 |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
62 #ifndef INFTIM |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
63 # define INFTIM (-1) |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
64 #endif |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
65 |
8500
6c3093c67379
Fix compilation error on BeOS.
Bruno Haible <bruno@clisp.org>
parents:
7921
diff
changeset
|
66 /* BeOS does not have MSG_PEEK. */ |
6c3093c67379
Fix compilation error on BeOS.
Bruno Haible <bruno@clisp.org>
parents:
7921
diff
changeset
|
67 #ifndef MSG_PEEK |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
68 # define MSG_PEEK 0 |
8500
6c3093c67379
Fix compilation error on BeOS.
Bruno Haible <bruno@clisp.org>
parents:
7921
diff
changeset
|
69 #endif |
6c3093c67379
Fix compilation error on BeOS.
Bruno Haible <bruno@clisp.org>
parents:
7921
diff
changeset
|
70 |
16214
ec738d6aeef5
Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents:
16213
diff
changeset
|
71 #ifdef WINDOWS_NATIVE |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
72 |
16213
64bc9fe46ccd
isatty: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
73 /* Optimized test whether a HANDLE refers to a console. |
64bc9fe46ccd
isatty: Support for MSVC 9.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
74 See <http://lists.gnu.org/archive/html/bug-gnulib/2009-08/msg00065.html>. */ |
16324
a37e8d313ade
select, poll, isatty: Avoid warnings on x86_64 mingw64.
Marc-André Lureau <marcandre.lureau@redhat.com>
parents:
16218
diff
changeset
|
75 #define IsConsoleHandle(h) (((intptr_t) (h) & 3) == 3) |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
76 |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
77 static BOOL |
13051 | 78 IsSocketHandle (HANDLE h) |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
79 { |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
80 WSANETWORKEVENTS ev; |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
81 |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
82 if (IsConsoleHandle (h)) |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
83 return FALSE; |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
84 |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
85 /* Under Wine, it seems that getsockopt returns 0 for pipes too. |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
86 WSAEnumNetworkEvents instead distinguishes the two correctly. */ |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
87 ev.lNetworkEvents = 0xDEADBEEF; |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
88 WSAEnumNetworkEvents ((SOCKET) h, NULL, &ev); |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
89 return ev.lNetworkEvents != 0xDEADBEEF; |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
90 } |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
91 |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
92 /* Declare data structures for ntdll functions. */ |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
93 typedef struct _FILE_PIPE_LOCAL_INFORMATION { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
94 ULONG NamedPipeType; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
95 ULONG NamedPipeConfiguration; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
96 ULONG MaximumInstances; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
97 ULONG CurrentInstances; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
98 ULONG InboundQuota; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
99 ULONG ReadDataAvailable; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
100 ULONG OutboundQuota; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
101 ULONG WriteQuotaAvailable; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
102 ULONG NamedPipeState; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
103 ULONG NamedPipeEnd; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
104 } FILE_PIPE_LOCAL_INFORMATION, *PFILE_PIPE_LOCAL_INFORMATION; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
105 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
106 typedef struct _IO_STATUS_BLOCK |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
107 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
108 union { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
109 DWORD Status; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
110 PVOID Pointer; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
111 } u; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
112 ULONG_PTR Information; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
113 } IO_STATUS_BLOCK, *PIO_STATUS_BLOCK; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
114 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
115 typedef enum _FILE_INFORMATION_CLASS { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
116 FilePipeLocalInformation = 24 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
117 } FILE_INFORMATION_CLASS, *PFILE_INFORMATION_CLASS; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
118 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
119 typedef DWORD (WINAPI *PNtQueryInformationFile) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
120 (HANDLE, IO_STATUS_BLOCK *, VOID *, ULONG, FILE_INFORMATION_CLASS); |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
121 |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
122 # ifndef PIPE_BUF |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
123 # define PIPE_BUF 512 |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
124 # endif |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
125 |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
126 /* Compute revents values for file handle H. If some events cannot happen |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
127 for the handle, eliminate them from *P_SOUGHT. */ |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
128 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
129 static int |
16214
ec738d6aeef5
Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents:
16213
diff
changeset
|
130 windows_compute_revents (HANDLE h, int *p_sought) |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
131 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
132 int i, ret, happened; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
133 INPUT_RECORD *irbuffer; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
134 DWORD avail, nbuffer; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
135 BOOL bRet; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
136 IO_STATUS_BLOCK iosb; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
137 FILE_PIPE_LOCAL_INFORMATION fpli; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
138 static PNtQueryInformationFile NtQueryInformationFile; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
139 static BOOL once_only; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
140 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
141 switch (GetFileType (h)) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
142 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
143 case FILE_TYPE_PIPE: |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
144 if (!once_only) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
145 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
146 NtQueryInformationFile = (PNtQueryInformationFile) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
147 GetProcAddress (GetModuleHandle ("ntdll.dll"), |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
148 "NtQueryInformationFile"); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
149 once_only = TRUE; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
150 } |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
151 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
152 happened = 0; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
153 if (PeekNamedPipe (h, NULL, 0, NULL, &avail, NULL) != 0) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
154 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
155 if (avail) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
156 happened |= *p_sought & (POLLIN | POLLRDNORM); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
157 } |
13565
69913822a0f7
poll, select: handle ERROR_BROKEN_PIPE.
Paolo Bonzini <pbonzini@redhat.com>
parents:
13051
diff
changeset
|
158 else if (GetLastError () == ERROR_BROKEN_PIPE) |
69913822a0f7
poll, select: handle ERROR_BROKEN_PIPE.
Paolo Bonzini <pbonzini@redhat.com>
parents:
13051
diff
changeset
|
159 happened |= POLLHUP; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
160 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
161 else |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
162 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
163 /* It was the write-end of the pipe. Check if it is writable. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
164 If NtQueryInformationFile fails, optimistically assume the pipe is |
16218
80c03bb8a948
Spell out "Windows 9x" and "Windows XP".
Paul Eggert <eggert@cs.ucla.edu>
parents:
16214
diff
changeset
|
165 writable. This could happen on Windows 9x, where |
80c03bb8a948
Spell out "Windows 9x" and "Windows XP".
Paul Eggert <eggert@cs.ucla.edu>
parents:
16214
diff
changeset
|
166 NtQueryInformationFile is not available, or if we inherit a pipe |
80c03bb8a948
Spell out "Windows 9x" and "Windows XP".
Paul Eggert <eggert@cs.ucla.edu>
parents:
16214
diff
changeset
|
167 that doesn't permit FILE_READ_ATTRIBUTES access on the write end |
80c03bb8a948
Spell out "Windows 9x" and "Windows XP".
Paul Eggert <eggert@cs.ucla.edu>
parents:
16214
diff
changeset
|
168 (I think this should not happen since Windows XP SP2; WINE seems |
80c03bb8a948
Spell out "Windows 9x" and "Windows XP".
Paul Eggert <eggert@cs.ucla.edu>
parents:
16214
diff
changeset
|
169 fine too). Otherwise, ensure that enough space is available for |
80c03bb8a948
Spell out "Windows 9x" and "Windows XP".
Paul Eggert <eggert@cs.ucla.edu>
parents:
16214
diff
changeset
|
170 atomic writes. */ |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
171 memset (&iosb, 0, sizeof (iosb)); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
172 memset (&fpli, 0, sizeof (fpli)); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
173 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
174 if (!NtQueryInformationFile |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
175 || NtQueryInformationFile (h, &iosb, &fpli, sizeof (fpli), |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
176 FilePipeLocalInformation) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
177 || fpli.WriteQuotaAvailable >= PIPE_BUF |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
178 || (fpli.OutboundQuota < PIPE_BUF && |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
179 fpli.WriteQuotaAvailable == fpli.OutboundQuota)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
180 happened |= *p_sought & (POLLOUT | POLLWRNORM | POLLWRBAND); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
181 } |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
182 return happened; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
183 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
184 case FILE_TYPE_CHAR: |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
185 ret = WaitForSingleObject (h, 0); |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
186 if (!IsConsoleHandle (h)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
187 return ret == WAIT_OBJECT_0 ? *p_sought & ~(POLLPRI | POLLRDBAND) : 0; |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
188 |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
189 nbuffer = avail = 0; |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
190 bRet = GetNumberOfConsoleInputEvents (h, &nbuffer); |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
191 if (bRet) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
192 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
193 /* Input buffer. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
194 *p_sought &= POLLIN | POLLRDNORM; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
195 if (nbuffer == 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
196 return POLLHUP; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
197 if (!*p_sought) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
198 return 0; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
199 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
200 irbuffer = (INPUT_RECORD *) alloca (nbuffer * sizeof (INPUT_RECORD)); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
201 bRet = PeekConsoleInput (h, irbuffer, nbuffer, &avail); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
202 if (!bRet || avail == 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
203 return POLLHUP; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
204 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
205 for (i = 0; i < avail; i++) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
206 if (irbuffer[i].EventType == KEY_EVENT) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
207 return *p_sought; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
208 return 0; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
209 } |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
210 else |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
211 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
212 /* Screen buffer. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
213 *p_sought &= POLLOUT | POLLWRNORM | POLLWRBAND; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
214 return *p_sought; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
215 } |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
216 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
217 default: |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
218 ret = WaitForSingleObject (h, 0); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
219 if (ret == WAIT_OBJECT_0) |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
220 return *p_sought & ~(POLLPRI | POLLRDBAND); |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
221 |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
222 return *p_sought & (POLLOUT | POLLWRNORM | POLLWRBAND); |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
223 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
224 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
225 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
226 /* Convert fd_sets returned by select into revents values. */ |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
227 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
228 static int |
16214
ec738d6aeef5
Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents:
16213
diff
changeset
|
229 windows_compute_revents_socket (SOCKET h, int sought, long lNetworkEvents) |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
230 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
231 int happened = 0; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
232 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
233 if ((lNetworkEvents & (FD_READ | FD_ACCEPT | FD_CLOSE)) == FD_ACCEPT) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
234 happened |= (POLLIN | POLLRDNORM) & sought; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
235 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
236 else if (lNetworkEvents & (FD_READ | FD_ACCEPT | FD_CLOSE)) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
237 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
238 int r, error; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
239 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
240 char data[64]; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
241 WSASetLastError (0); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
242 r = recv (h, data, sizeof (data), MSG_PEEK); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
243 error = WSAGetLastError (); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
244 WSASetLastError (0); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
245 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
246 if (r > 0 || error == WSAENOTCONN) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
247 happened |= (POLLIN | POLLRDNORM) & sought; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
248 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
249 /* Distinguish hung-up sockets from other errors. */ |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
250 else if (r == 0 || error == WSAESHUTDOWN || error == WSAECONNRESET |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
251 || error == WSAECONNABORTED || error == WSAENETRESET) |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
252 happened |= POLLHUP; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
253 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
254 else |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
255 happened |= POLLERR; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
256 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
257 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
258 if (lNetworkEvents & (FD_WRITE | FD_CONNECT)) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
259 happened |= (POLLOUT | POLLWRNORM | POLLWRBAND) & sought; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
260 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
261 if (lNetworkEvents & FD_OOB) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
262 happened |= (POLLPRI | POLLRDBAND) & sought; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
263 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
264 return happened; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
265 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
266 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
267 #else /* !MinGW */ |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
268 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
269 /* Convert select(2) returned fd_sets into poll(2) revents values. */ |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
270 static int |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
271 compute_revents (int fd, int sought, fd_set *rfds, fd_set *wfds, fd_set *efds) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
272 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
273 int happened = 0; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
274 if (FD_ISSET (fd, rfds)) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
275 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
276 int r; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
277 int socket_errno; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
278 |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
279 # if defined __MACH__ && defined __APPLE__ |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
280 /* There is a bug in Mac OS X that causes it to ignore MSG_PEEK |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
281 for some kinds of descriptors. Detect if this descriptor is a |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
282 connected socket, a server socket, or something else using a |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
283 0-byte recv, and use ioctl(2) to detect POLLHUP. */ |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
284 r = recv (fd, NULL, 0, MSG_PEEK); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
285 socket_errno = (r < 0) ? errno : 0; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
286 if (r == 0 || socket_errno == ENOTSOCK) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
287 ioctl (fd, FIONREAD, &r); |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
288 # else |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
289 char data[64]; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
290 r = recv (fd, data, sizeof (data), MSG_PEEK); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
291 socket_errno = (r < 0) ? errno : 0; |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
292 # endif |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
293 if (r == 0) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
294 happened |= POLLHUP; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
295 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
296 /* If the event happened on an unconnected server socket, |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
297 that's fine. */ |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
298 else if (r > 0 || ( /* (r == -1) && */ socket_errno == ENOTCONN)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
299 happened |= (POLLIN | POLLRDNORM) & sought; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
300 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
301 /* Distinguish hung-up sockets from other errors. */ |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
302 else if (socket_errno == ESHUTDOWN || socket_errno == ECONNRESET |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
303 || socket_errno == ECONNABORTED || socket_errno == ENETRESET) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
304 happened |= POLLHUP; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
305 |
17083
1ba58228acda
poll: fix for systems that can't recv() on a non-socket
Joachim Schmitz <jojo@schmitz-digital.de>
parents:
16869
diff
changeset
|
306 /* some systems can't use recv() on non-socket, including HP NonStop */ |
1ba58228acda
poll: fix for systems that can't recv() on a non-socket
Joachim Schmitz <jojo@schmitz-digital.de>
parents:
16869
diff
changeset
|
307 else if (socket_errno == ENOTSOCK) |
1ba58228acda
poll: fix for systems that can't recv() on a non-socket
Joachim Schmitz <jojo@schmitz-digital.de>
parents:
16869
diff
changeset
|
308 happened |= (POLLIN | POLLRDNORM) & sought; |
1ba58228acda
poll: fix for systems that can't recv() on a non-socket
Joachim Schmitz <jojo@schmitz-digital.de>
parents:
16869
diff
changeset
|
309 |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
310 else |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
311 happened |= POLLERR; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
312 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
313 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
314 if (FD_ISSET (fd, wfds)) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
315 happened |= (POLLOUT | POLLWRNORM | POLLWRBAND) & sought; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
316 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
317 if (FD_ISSET (fd, efds)) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
318 happened |= (POLLPRI | POLLRDBAND) & sought; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
319 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
320 return happened; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
321 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
322 #endif /* !MinGW */ |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
323 |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
324 int |
13741 | 325 poll (struct pollfd *pfd, nfds_t nfd, int timeout) |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
326 { |
16214
ec738d6aeef5
Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents:
16213
diff
changeset
|
327 #ifndef WINDOWS_NATIVE |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
328 fd_set rfds, wfds, efds; |
10310
b48c3c82f4d9
Further micro-optimization.
Bruno Haible <bruno@clisp.org>
parents:
10308
diff
changeset
|
329 struct timeval tv; |
10308
c3ecbe083a26
microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents:
9831
diff
changeset
|
330 struct timeval *ptv; |
7776
35d644ee8b48
2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
7382
diff
changeset
|
331 int maxfd, rc; |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
332 nfds_t i; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
333 |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
334 # ifdef _SC_OPEN_MAX |
10308
c3ecbe083a26
microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents:
9831
diff
changeset
|
335 static int sc_open_max = -1; |
c3ecbe083a26
microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents:
9831
diff
changeset
|
336 |
c3ecbe083a26
microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents:
9831
diff
changeset
|
337 if (nfd < 0 |
c3ecbe083a26
microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents:
9831
diff
changeset
|
338 || (nfd > sc_open_max |
c3ecbe083a26
microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents:
9831
diff
changeset
|
339 && (sc_open_max != -1 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
340 || nfd > (sc_open_max = sysconf (_SC_OPEN_MAX))))) |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
341 { |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
342 errno = EINVAL; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
343 return -1; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
344 } |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
345 # else /* !_SC_OPEN_MAX */ |
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
346 # ifdef OPEN_MAX |
10308
c3ecbe083a26
microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents:
9831
diff
changeset
|
347 if (nfd < 0 || nfd > OPEN_MAX) |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
348 { |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
349 errno = EINVAL; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
350 return -1; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
351 } |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
352 # endif /* OPEN_MAX -- else, no check is needed */ |
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
353 # endif /* !_SC_OPEN_MAX */ |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
354 |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
355 /* EFAULT is not necessary to implement, but let's do it in the |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
356 simplest case. */ |
17084
723cfa7546c0
poll: fix poll(0, NULL, msec)
Joachim Schmitz <jojo@schmitz-digital.de>
parents:
17083
diff
changeset
|
357 if (!pfd && nfd) |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
358 { |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
359 errno = EFAULT; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
360 return -1; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
361 } |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
362 |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
363 /* convert timeout number into a timeval structure */ |
10308
c3ecbe083a26
microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents:
9831
diff
changeset
|
364 if (timeout == 0) |
10310
b48c3c82f4d9
Further micro-optimization.
Bruno Haible <bruno@clisp.org>
parents:
10308
diff
changeset
|
365 { |
b48c3c82f4d9
Further micro-optimization.
Bruno Haible <bruno@clisp.org>
parents:
10308
diff
changeset
|
366 ptv = &tv; |
b48c3c82f4d9
Further micro-optimization.
Bruno Haible <bruno@clisp.org>
parents:
10308
diff
changeset
|
367 ptv->tv_sec = 0; |
b48c3c82f4d9
Further micro-optimization.
Bruno Haible <bruno@clisp.org>
parents:
10308
diff
changeset
|
368 ptv->tv_usec = 0; |
b48c3c82f4d9
Further micro-optimization.
Bruno Haible <bruno@clisp.org>
parents:
10308
diff
changeset
|
369 } |
10308
c3ecbe083a26
microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents:
9831
diff
changeset
|
370 else if (timeout > 0) |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
371 { |
10308
c3ecbe083a26
microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents:
9831
diff
changeset
|
372 ptv = &tv; |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
373 ptv->tv_sec = timeout / 1000; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
374 ptv->tv_usec = (timeout % 1000) * 1000; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
375 } |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
376 else if (timeout == INFTIM) |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
377 /* wait forever */ |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
378 ptv = NULL; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
379 else |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
380 { |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
381 errno = EINVAL; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
382 return -1; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
383 } |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
384 |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
385 /* create fd sets and determine max fd */ |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
386 maxfd = -1; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
387 FD_ZERO (&rfds); |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
388 FD_ZERO (&wfds); |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
389 FD_ZERO (&efds); |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
390 for (i = 0; i < nfd; i++) |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
391 { |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
392 if (pfd[i].fd < 0) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
393 continue; |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
394 |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
395 if (pfd[i].events & (POLLIN | POLLRDNORM)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
396 FD_SET (pfd[i].fd, &rfds); |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
397 |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
398 /* see select(2): "the only exceptional condition detectable |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
399 is out-of-band data received on a socket", hence we push |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
400 POLLWRBAND events onto wfds instead of efds. */ |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
401 if (pfd[i].events & (POLLOUT | POLLWRNORM | POLLWRBAND)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
402 FD_SET (pfd[i].fd, &wfds); |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
403 if (pfd[i].events & (POLLPRI | POLLRDBAND)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
404 FD_SET (pfd[i].fd, &efds); |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
405 if (pfd[i].fd >= maxfd |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
406 && (pfd[i].events & (POLLIN | POLLOUT | POLLPRI |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
407 | POLLRDNORM | POLLRDBAND |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
408 | POLLWRNORM | POLLWRBAND))) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
409 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
410 maxfd = pfd[i].fd; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
411 if (maxfd > FD_SETSIZE) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
412 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
413 errno = EOVERFLOW; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
414 return -1; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
415 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
416 } |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
417 } |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
418 |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
419 /* examine fd sets */ |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
420 rc = select (maxfd + 1, &rfds, &wfds, &efds, ptv); |
7776
35d644ee8b48
2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
7382
diff
changeset
|
421 if (rc < 0) |
35d644ee8b48
2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
7382
diff
changeset
|
422 return rc; |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
423 |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
424 /* establish results */ |
7776
35d644ee8b48
2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
7382
diff
changeset
|
425 rc = 0; |
35d644ee8b48
2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
7382
diff
changeset
|
426 for (i = 0; i < nfd; i++) |
35d644ee8b48
2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
7382
diff
changeset
|
427 if (pfd[i].fd < 0) |
35d644ee8b48
2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
7382
diff
changeset
|
428 pfd[i].revents = 0; |
35d644ee8b48
2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
7382
diff
changeset
|
429 else |
35d644ee8b48
2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
7382
diff
changeset
|
430 { |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
431 int happened = compute_revents (pfd[i].fd, pfd[i].events, |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
432 &rfds, &wfds, &efds); |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
433 if (happened) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
434 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
435 pfd[i].revents = happened; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
436 rc++; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
437 } |
7776
35d644ee8b48
2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
7382
diff
changeset
|
438 } |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
439 |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
440 return rc; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
441 #else |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
442 static struct timeval tv0; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
443 static HANDLE hEvent; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
444 WSANETWORKEVENTS ev; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
445 HANDLE h, handle_array[FD_SETSIZE + 2]; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
446 DWORD ret, wait_timeout, nhandles; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
447 fd_set rfds, wfds, xfds; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
448 BOOL poll_again; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
449 MSG msg; |
10996
724ce887ae63
poll: don't return uninitialized
Daniel P. Berrange <berrange@redhat.com>
parents:
10562
diff
changeset
|
450 int rc = 0; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
451 nfds_t i; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
452 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
453 if (nfd < 0 || timeout < -1) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
454 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
455 errno = EINVAL; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
456 return -1; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
457 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
458 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
459 if (!hEvent) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
460 hEvent = CreateEvent (NULL, FALSE, FALSE, NULL); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
461 |
15353
f3753e923660
poll: do not return 0 on timeout=-1
Erik Faye-Lund <kusmabite@gmail.com>
parents:
14079
diff
changeset
|
462 restart: |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
463 handle_array[0] = hEvent; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
464 nhandles = 1; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
465 FD_ZERO (&rfds); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
466 FD_ZERO (&wfds); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
467 FD_ZERO (&xfds); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
468 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
469 /* Classify socket handles and create fd sets. */ |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
470 for (i = 0; i < nfd; i++) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
471 { |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
472 int sought = pfd[i].events; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
473 pfd[i].revents = 0; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
474 if (pfd[i].fd < 0) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
475 continue; |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
476 if (!(sought & (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM | POLLWRBAND |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
477 | POLLPRI | POLLRDBAND))) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
478 continue; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
479 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
480 h = (HANDLE) _get_osfhandle (pfd[i].fd); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
481 assert (h != NULL); |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
482 if (IsSocketHandle (h)) |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
483 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
484 int requested = FD_CLOSE; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
485 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
486 /* see above; socket handles are mapped onto select. */ |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
487 if (sought & (POLLIN | POLLRDNORM)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
488 { |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
489 requested |= FD_READ | FD_ACCEPT; |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
490 FD_SET ((SOCKET) h, &rfds); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
491 } |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
492 if (sought & (POLLOUT | POLLWRNORM | POLLWRBAND)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
493 { |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
494 requested |= FD_WRITE | FD_CONNECT; |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
495 FD_SET ((SOCKET) h, &wfds); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
496 } |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
497 if (sought & (POLLPRI | POLLRDBAND)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
498 { |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
499 requested |= FD_OOB; |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
500 FD_SET ((SOCKET) h, &xfds); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
501 } |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
502 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
503 if (requested) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
504 WSAEventSelect ((SOCKET) h, hEvent, requested); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
505 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
506 else |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
507 { |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
508 /* Poll now. If we get an event, do not poll again. Also, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
509 screen buffer handles are waitable, and they'll block until |
16214
ec738d6aeef5
Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents:
16213
diff
changeset
|
510 a character is available. windows_compute_revents eliminates |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
511 bits for the "wrong" direction. */ |
16214
ec738d6aeef5
Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents:
16213
diff
changeset
|
512 pfd[i].revents = windows_compute_revents (h, &sought); |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
513 if (sought) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
514 handle_array[nhandles++] = h; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
515 if (pfd[i].revents) |
13574
ac05886b34b6
poll: return immediately on POLLHUP.
Erik Faye-Lund <kusmabite@gmail.com>
parents:
13565
diff
changeset
|
516 timeout = 0; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
517 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
518 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
519 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
520 if (select (0, &rfds, &wfds, &xfds, &tv0) > 0) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
521 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
522 /* Do MsgWaitForMultipleObjects anyway to dispatch messages, but |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
523 no need to call select again. */ |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
524 poll_again = FALSE; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
525 wait_timeout = 0; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
526 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
527 else |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
528 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
529 poll_again = TRUE; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
530 if (timeout == INFTIM) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
531 wait_timeout = INFINITE; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
532 else |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
533 wait_timeout = timeout; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
534 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
535 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
536 for (;;) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
537 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
538 ret = MsgWaitForMultipleObjects (nhandles, handle_array, FALSE, |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
539 wait_timeout, QS_ALLINPUT); |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
540 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
541 if (ret == WAIT_OBJECT_0 + nhandles) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
542 { |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
543 /* new input of some other kind */ |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
544 BOOL bRet; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
545 while ((bRet = PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) != 0) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
546 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
547 TranslateMessage (&msg); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
548 DispatchMessage (&msg); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
549 } |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
550 } |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
551 else |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
552 break; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
553 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
554 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
555 if (poll_again) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
556 select (0, &rfds, &wfds, &xfds, &tv0); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
557 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
558 /* Place a sentinel at the end of the array. */ |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
559 handle_array[nhandles] = NULL; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
560 nhandles = 1; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
561 for (i = 0; i < nfd; i++) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
562 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
563 int happened; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
564 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
565 if (pfd[i].fd < 0) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
566 continue; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
567 if (!(pfd[i].events & (POLLIN | POLLRDNORM | |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
568 POLLOUT | POLLWRNORM | POLLWRBAND))) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
569 continue; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
570 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
571 h = (HANDLE) _get_osfhandle (pfd[i].fd); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
572 if (h != handle_array[nhandles]) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
573 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
574 /* It's a socket. */ |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
575 WSAEnumNetworkEvents ((SOCKET) h, NULL, &ev); |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
576 WSAEventSelect ((SOCKET) h, 0, 0); |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
577 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
578 /* If we're lucky, WSAEnumNetworkEvents already provided a way |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
579 to distinguish FD_READ and FD_ACCEPT; this saves a recv later. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
580 if (FD_ISSET ((SOCKET) h, &rfds) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
581 && !(ev.lNetworkEvents & (FD_READ | FD_ACCEPT))) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
582 ev.lNetworkEvents |= FD_READ | FD_ACCEPT; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
583 if (FD_ISSET ((SOCKET) h, &wfds)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
584 ev.lNetworkEvents |= FD_WRITE | FD_CONNECT; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
585 if (FD_ISSET ((SOCKET) h, &xfds)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
586 ev.lNetworkEvents |= FD_OOB; |
10562
ebbb50a36a9f
remove trailing spaces
Jim Meyering <meyering@redhat.com>
parents:
10467
diff
changeset
|
587 |
16214
ec738d6aeef5
Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents:
16213
diff
changeset
|
588 happened = windows_compute_revents_socket ((SOCKET) h, pfd[i].events, |
ec738d6aeef5
Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents:
16213
diff
changeset
|
589 ev.lNetworkEvents); |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
590 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
591 else |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
592 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
593 /* Not a socket. */ |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
594 int sought = pfd[i].events; |
16214
ec738d6aeef5
Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents:
16213
diff
changeset
|
595 happened = windows_compute_revents (h, &sought); |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
596 nhandles++; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
597 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
598 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
599 if ((pfd[i].revents |= happened) != 0) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
600 rc++; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
601 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
602 |
15353
f3753e923660
poll: do not return 0 on timeout=-1
Erik Faye-Lund <kusmabite@gmail.com>
parents:
14079
diff
changeset
|
603 if (!rc && timeout == INFTIM) |
f3753e923660
poll: do not return 0 on timeout=-1
Erik Faye-Lund <kusmabite@gmail.com>
parents:
14079
diff
changeset
|
604 { |
16869
38e6d30d7e4d
poll/select: prevent busy-waiting
Paolo Bonzini <bonzini@gnu.org>
parents:
16366
diff
changeset
|
605 SleepEx (1, TRUE); |
15353
f3753e923660
poll: do not return 0 on timeout=-1
Erik Faye-Lund <kusmabite@gmail.com>
parents:
14079
diff
changeset
|
606 goto restart; |
f3753e923660
poll: do not return 0 on timeout=-1
Erik Faye-Lund <kusmabite@gmail.com>
parents:
14079
diff
changeset
|
607 } |
f3753e923660
poll: do not return 0 on timeout=-1
Erik Faye-Lund <kusmabite@gmail.com>
parents:
14079
diff
changeset
|
608 |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
609 return rc; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
610 #endif |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
611 } |