Mercurial > hg > octave-kai > gnulib-hg
annotate lib/poll.c @ 15353:f3753e923660
poll: do not return 0 on timeout=-1
* lib/poll.c: Loop with yield if no events occured
author | Erik Faye-Lund <kusmabite@gmail.com> |
---|---|
date | Wed, 06 Jul 2011 12:10:51 +0200 |
parents | 97fc9a21a8fb |
children | b86e9061a6d0 |
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 |
14079
97fc9a21a8fb
maint: update almost all copyright ranges to include 2011
Jim Meyering <meyering@redhat.com>
parents:
13741
diff
changeset
|
4 Copyright 2001-2003, 2006-2011 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 |
19 with this program; if not, write to the Free Software Foundation, | |
5848
a48fb0e98c8c
*** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents:
4435
diff
changeset
|
20 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
21 |
11020
4494a99c424d
poll: suppress a warning
Jim Meyering <meyering@redhat.com>
parents:
11019
diff
changeset
|
22 /* 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
|
23 #if (__GNUC__ == 4 && 3 <= __GNUC_MINOR__) || 4 < __GNUC__ |
4494a99c424d
poll: suppress a warning
Jim Meyering <meyering@redhat.com>
parents:
11019
diff
changeset
|
24 # pragma GCC diagnostic ignored "-Wtype-limits" |
4494a99c424d
poll: suppress a warning
Jim Meyering <meyering@redhat.com>
parents:
11019
diff
changeset
|
25 #endif |
4494a99c424d
poll: suppress a warning
Jim Meyering <meyering@redhat.com>
parents:
11019
diff
changeset
|
26 |
9644
42367c277240
Prefer <config.h> over "config.h". See autoconf doc for explanation.
Jim Meyering <meyering@redhat.com>
parents:
8500
diff
changeset
|
27 #include <config.h> |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
28 #include <alloca.h> |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
29 |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
30 #include <sys/types.h> |
13736 | 31 |
32 /* Specification. */ | |
33 #include <poll.h> | |
34 | |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
35 #include <errno.h> |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
36 #include <limits.h> |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
37 #include <assert.h> |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
38 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
39 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
40 # define WIN32_NATIVE |
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
41 # include <winsock2.h> |
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
42 # include <windows.h> |
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
43 # include <io.h> |
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
44 # include <stdio.h> |
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
45 # include <conio.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 |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
71 #ifdef WIN32_NATIVE |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
72 |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
73 #define IsConsoleHandle(h) (((long) (h) & 3) == 3) |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
74 |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
75 static BOOL |
13051 | 76 IsSocketHandle (HANDLE h) |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
77 { |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
78 WSANETWORKEVENTS ev; |
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 if (IsConsoleHandle (h)) |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
81 return FALSE; |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
82 |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
83 /* 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
|
84 WSAEnumNetworkEvents instead distinguishes the two correctly. */ |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
85 ev.lNetworkEvents = 0xDEADBEEF; |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
86 WSAEnumNetworkEvents ((SOCKET) h, NULL, &ev); |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
87 return ev.lNetworkEvents != 0xDEADBEEF; |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
88 } |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
89 |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
90 /* Declare data structures for ntdll functions. */ |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
91 typedef struct _FILE_PIPE_LOCAL_INFORMATION { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
92 ULONG NamedPipeType; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
93 ULONG NamedPipeConfiguration; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
94 ULONG MaximumInstances; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
95 ULONG CurrentInstances; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
96 ULONG InboundQuota; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
97 ULONG ReadDataAvailable; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
98 ULONG OutboundQuota; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
99 ULONG WriteQuotaAvailable; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
100 ULONG NamedPipeState; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
101 ULONG NamedPipeEnd; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
102 } 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
|
103 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
104 typedef struct _IO_STATUS_BLOCK |
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 union { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
107 DWORD Status; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
108 PVOID Pointer; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
109 } u; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
110 ULONG_PTR Information; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
111 } IO_STATUS_BLOCK, *PIO_STATUS_BLOCK; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
112 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
113 typedef enum _FILE_INFORMATION_CLASS { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
114 FilePipeLocalInformation = 24 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
115 } FILE_INFORMATION_CLASS, *PFILE_INFORMATION_CLASS; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
116 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
117 typedef DWORD (WINAPI *PNtQueryInformationFile) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
118 (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
|
119 |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
120 # ifndef PIPE_BUF |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
121 # define PIPE_BUF 512 |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
122 # endif |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
123 |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
124 /* 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
|
125 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
|
126 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
127 static int |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
128 win32_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
|
129 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
130 int i, ret, happened; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
131 INPUT_RECORD *irbuffer; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
132 DWORD avail, nbuffer; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
133 BOOL bRet; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
134 IO_STATUS_BLOCK iosb; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
135 FILE_PIPE_LOCAL_INFORMATION fpli; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
136 static PNtQueryInformationFile NtQueryInformationFile; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
137 static BOOL once_only; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
138 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
139 switch (GetFileType (h)) |
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 case FILE_TYPE_PIPE: |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
142 if (!once_only) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
143 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
144 NtQueryInformationFile = (PNtQueryInformationFile) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
145 GetProcAddress (GetModuleHandle ("ntdll.dll"), |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
146 "NtQueryInformationFile"); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
147 once_only = TRUE; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
148 } |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
149 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
150 happened = 0; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
151 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
|
152 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
153 if (avail) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
154 happened |= *p_sought & (POLLIN | POLLRDNORM); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
155 } |
13565
69913822a0f7
poll, select: handle ERROR_BROKEN_PIPE.
Paolo Bonzini <pbonzini@redhat.com>
parents:
13051
diff
changeset
|
156 else if (GetLastError () == ERROR_BROKEN_PIPE) |
69913822a0f7
poll, select: handle ERROR_BROKEN_PIPE.
Paolo Bonzini <pbonzini@redhat.com>
parents:
13051
diff
changeset
|
157 happened |= POLLHUP; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
158 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
159 else |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
160 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
161 /* 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
|
162 If NtQueryInformationFile fails, optimistically assume the pipe is |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
163 writable. This could happen on Win9x, where NtQueryInformationFile |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
164 is not available, or if we inherit a pipe that doesn't permit |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
165 FILE_READ_ATTRIBUTES access on the write end (I think this should |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
166 not happen since WinXP SP2; WINE seems fine too). Otherwise, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
167 ensure that enough space is available for atomic writes. */ |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
168 memset (&iosb, 0, sizeof (iosb)); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
169 memset (&fpli, 0, sizeof (fpli)); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
170 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
171 if (!NtQueryInformationFile |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
172 || NtQueryInformationFile (h, &iosb, &fpli, sizeof (fpli), |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
173 FilePipeLocalInformation) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
174 || fpli.WriteQuotaAvailable >= PIPE_BUF |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
175 || (fpli.OutboundQuota < PIPE_BUF && |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
176 fpli.WriteQuotaAvailable == fpli.OutboundQuota)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
177 happened |= *p_sought & (POLLOUT | POLLWRNORM | POLLWRBAND); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
178 } |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
179 return happened; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
180 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
181 case FILE_TYPE_CHAR: |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
182 ret = WaitForSingleObject (h, 0); |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
183 if (!IsConsoleHandle (h)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
184 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
|
185 |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
186 nbuffer = avail = 0; |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
187 bRet = GetNumberOfConsoleInputEvents (h, &nbuffer); |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
188 if (bRet) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
189 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
190 /* Input buffer. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
191 *p_sought &= POLLIN | POLLRDNORM; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
192 if (nbuffer == 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
193 return POLLHUP; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
194 if (!*p_sought) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
195 return 0; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
196 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
197 irbuffer = (INPUT_RECORD *) alloca (nbuffer * sizeof (INPUT_RECORD)); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
198 bRet = PeekConsoleInput (h, irbuffer, nbuffer, &avail); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
199 if (!bRet || avail == 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
200 return POLLHUP; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
201 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
202 for (i = 0; i < avail; i++) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
203 if (irbuffer[i].EventType == KEY_EVENT) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
204 return *p_sought; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
205 return 0; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
206 } |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
207 else |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
208 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
209 /* Screen buffer. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
210 *p_sought &= POLLOUT | POLLWRNORM | POLLWRBAND; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
211 return *p_sought; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
212 } |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
213 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
214 default: |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
215 ret = WaitForSingleObject (h, 0); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
216 if (ret == WAIT_OBJECT_0) |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
217 return *p_sought & ~(POLLPRI | POLLRDBAND); |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
218 |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
219 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
|
220 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
221 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
222 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
223 /* 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
|
224 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
225 static int |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
226 win32_compute_revents_socket (SOCKET h, int sought, long lNetworkEvents) |
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 int happened = 0; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
229 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
230 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
|
231 happened |= (POLLIN | POLLRDNORM) & sought; |
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 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
|
234 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
235 int r, error; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
236 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
237 char data[64]; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
238 WSASetLastError (0); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
239 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
|
240 error = WSAGetLastError (); |
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 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
243 if (r > 0 || error == WSAENOTCONN) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
244 happened |= (POLLIN | POLLRDNORM) & sought; |
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 /* 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
|
247 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
|
248 || error == WSAECONNABORTED || error == WSAENETRESET) |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
249 happened |= POLLHUP; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
250 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
251 else |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
252 happened |= POLLERR; |
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 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
255 if (lNetworkEvents & (FD_WRITE | FD_CONNECT)) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
256 happened |= (POLLOUT | POLLWRNORM | POLLWRBAND) & sought; |
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_OOB) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
259 happened |= (POLLPRI | POLLRDBAND) & 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 return happened; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
262 } |
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 #else /* !MinGW */ |
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 /* 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
|
267 static int |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
268 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
|
269 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
270 int happened = 0; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
271 if (FD_ISSET (fd, rfds)) |
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 r; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
274 int socket_errno; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
275 |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
276 # if defined __MACH__ && defined __APPLE__ |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
277 /* 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
|
278 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
|
279 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
|
280 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
|
281 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
|
282 socket_errno = (r < 0) ? errno : 0; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
283 if (r == 0 || socket_errno == ENOTSOCK) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
284 ioctl (fd, FIONREAD, &r); |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
285 # else |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
286 char data[64]; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
287 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
|
288 socket_errno = (r < 0) ? errno : 0; |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
289 # endif |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
290 if (r == 0) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
291 happened |= POLLHUP; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
292 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
293 /* 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
|
294 that's fine. */ |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
295 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
|
296 happened |= (POLLIN | POLLRDNORM) & sought; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
297 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
298 /* 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
|
299 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
|
300 || socket_errno == ECONNABORTED || socket_errno == ENETRESET) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
301 happened |= POLLHUP; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
302 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
303 else |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
304 happened |= POLLERR; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
305 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
306 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
307 if (FD_ISSET (fd, wfds)) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
308 happened |= (POLLOUT | POLLWRNORM | POLLWRBAND) & sought; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
309 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
310 if (FD_ISSET (fd, efds)) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
311 happened |= (POLLPRI | POLLRDBAND) & sought; |
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 return happened; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
314 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
315 #endif /* !MinGW */ |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
316 |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
317 int |
13741 | 318 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
|
319 { |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
320 #ifndef WIN32_NATIVE |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
321 fd_set rfds, wfds, efds; |
10310
b48c3c82f4d9
Further micro-optimization.
Bruno Haible <bruno@clisp.org>
parents:
10308
diff
changeset
|
322 struct timeval tv; |
10308
c3ecbe083a26
microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents:
9831
diff
changeset
|
323 struct timeval *ptv; |
7776
35d644ee8b48
2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
7382
diff
changeset
|
324 int maxfd, rc; |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
325 nfds_t i; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
326 |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
327 # ifdef _SC_OPEN_MAX |
10308
c3ecbe083a26
microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents:
9831
diff
changeset
|
328 static int sc_open_max = -1; |
c3ecbe083a26
microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents:
9831
diff
changeset
|
329 |
c3ecbe083a26
microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents:
9831
diff
changeset
|
330 if (nfd < 0 |
c3ecbe083a26
microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents:
9831
diff
changeset
|
331 || (nfd > sc_open_max |
c3ecbe083a26
microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents:
9831
diff
changeset
|
332 && (sc_open_max != -1 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
333 || 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
|
334 { |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
335 errno = EINVAL; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
336 return -1; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
337 } |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
338 # else /* !_SC_OPEN_MAX */ |
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
339 # ifdef OPEN_MAX |
10308
c3ecbe083a26
microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents:
9831
diff
changeset
|
340 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
|
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 # endif /* OPEN_MAX -- else, no check is needed */ |
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
346 # endif /* !_SC_OPEN_MAX */ |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
347 |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
348 /* 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
|
349 simplest case. */ |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
350 if (!pfd) |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
351 { |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
352 errno = EFAULT; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
353 return -1; |
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 |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
356 /* convert timeout number into a timeval structure */ |
10308
c3ecbe083a26
microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents:
9831
diff
changeset
|
357 if (timeout == 0) |
10310
b48c3c82f4d9
Further micro-optimization.
Bruno Haible <bruno@clisp.org>
parents:
10308
diff
changeset
|
358 { |
b48c3c82f4d9
Further micro-optimization.
Bruno Haible <bruno@clisp.org>
parents:
10308
diff
changeset
|
359 ptv = &tv; |
b48c3c82f4d9
Further micro-optimization.
Bruno Haible <bruno@clisp.org>
parents:
10308
diff
changeset
|
360 ptv->tv_sec = 0; |
b48c3c82f4d9
Further micro-optimization.
Bruno Haible <bruno@clisp.org>
parents:
10308
diff
changeset
|
361 ptv->tv_usec = 0; |
b48c3c82f4d9
Further micro-optimization.
Bruno Haible <bruno@clisp.org>
parents:
10308
diff
changeset
|
362 } |
10308
c3ecbe083a26
microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents:
9831
diff
changeset
|
363 else if (timeout > 0) |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
364 { |
10308
c3ecbe083a26
microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents:
9831
diff
changeset
|
365 ptv = &tv; |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
366 ptv->tv_sec = timeout / 1000; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
367 ptv->tv_usec = (timeout % 1000) * 1000; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
368 } |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
369 else if (timeout == INFTIM) |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
370 /* wait forever */ |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
371 ptv = NULL; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
372 else |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
373 { |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
374 errno = EINVAL; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
375 return -1; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
376 } |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
377 |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
378 /* 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
|
379 maxfd = -1; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
380 FD_ZERO (&rfds); |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
381 FD_ZERO (&wfds); |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
382 FD_ZERO (&efds); |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
383 for (i = 0; i < nfd; i++) |
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 if (pfd[i].fd < 0) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
386 continue; |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
387 |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
388 if (pfd[i].events & (POLLIN | POLLRDNORM)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
389 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
|
390 |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
391 /* 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
|
392 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
|
393 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
|
394 if (pfd[i].events & (POLLOUT | POLLWRNORM | POLLWRBAND)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
395 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
|
396 if (pfd[i].events & (POLLPRI | POLLRDBAND)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
397 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
|
398 if (pfd[i].fd >= maxfd |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
399 && (pfd[i].events & (POLLIN | POLLOUT | POLLPRI |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
400 | POLLRDNORM | POLLRDBAND |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
401 | POLLWRNORM | POLLWRBAND))) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
402 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
403 maxfd = pfd[i].fd; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
404 if (maxfd > FD_SETSIZE) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
405 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
406 errno = EOVERFLOW; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
407 return -1; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
408 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
409 } |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
410 } |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
411 |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
412 /* examine fd sets */ |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
413 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
|
414 if (rc < 0) |
35d644ee8b48
2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
7382
diff
changeset
|
415 return rc; |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
416 |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
417 /* establish results */ |
7776
35d644ee8b48
2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
7382
diff
changeset
|
418 rc = 0; |
35d644ee8b48
2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
7382
diff
changeset
|
419 for (i = 0; i < nfd; i++) |
35d644ee8b48
2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
7382
diff
changeset
|
420 if (pfd[i].fd < 0) |
35d644ee8b48
2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
7382
diff
changeset
|
421 pfd[i].revents = 0; |
35d644ee8b48
2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
7382
diff
changeset
|
422 else |
35d644ee8b48
2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
7382
diff
changeset
|
423 { |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
424 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
|
425 &rfds, &wfds, &efds); |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
426 if (happened) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
427 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
428 pfd[i].revents = happened; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
429 rc++; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
430 } |
7776
35d644ee8b48
2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
7382
diff
changeset
|
431 } |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
432 |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
433 return rc; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
434 #else |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
435 static struct timeval tv0; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
436 static HANDLE hEvent; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
437 WSANETWORKEVENTS ev; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
438 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
|
439 DWORD ret, wait_timeout, nhandles; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
440 fd_set rfds, wfds, xfds; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
441 BOOL poll_again; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
442 MSG msg; |
10996
724ce887ae63
poll: don't return uninitialized
Daniel P. Berrange <berrange@redhat.com>
parents:
10562
diff
changeset
|
443 int rc = 0; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
444 nfds_t i; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
445 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
446 if (nfd < 0 || timeout < -1) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
447 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
448 errno = EINVAL; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
449 return -1; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
450 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
451 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
452 if (!hEvent) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
453 hEvent = CreateEvent (NULL, FALSE, FALSE, NULL); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
454 |
15353
f3753e923660
poll: do not return 0 on timeout=-1
Erik Faye-Lund <kusmabite@gmail.com>
parents:
14079
diff
changeset
|
455 restart: |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
456 handle_array[0] = hEvent; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
457 nhandles = 1; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
458 FD_ZERO (&rfds); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
459 FD_ZERO (&wfds); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
460 FD_ZERO (&xfds); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
461 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
462 /* 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
|
463 for (i = 0; i < nfd; i++) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
464 { |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
465 int sought = pfd[i].events; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
466 pfd[i].revents = 0; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
467 if (pfd[i].fd < 0) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
468 continue; |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
469 if (!(sought & (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM | POLLWRBAND |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
470 | POLLPRI | POLLRDBAND))) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
471 continue; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
472 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
473 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
|
474 assert (h != NULL); |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
475 if (IsSocketHandle (h)) |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
476 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
477 int requested = FD_CLOSE; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
478 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
479 /* 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
|
480 if (sought & (POLLIN | POLLRDNORM)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
481 { |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
482 requested |= FD_READ | FD_ACCEPT; |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
483 FD_SET ((SOCKET) h, &rfds); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
484 } |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
485 if (sought & (POLLOUT | POLLWRNORM | POLLWRBAND)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
486 { |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
487 requested |= FD_WRITE | FD_CONNECT; |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
488 FD_SET ((SOCKET) h, &wfds); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
489 } |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
490 if (sought & (POLLPRI | POLLRDBAND)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
491 { |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
492 requested |= FD_OOB; |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
493 FD_SET ((SOCKET) h, &xfds); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
494 } |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
495 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
496 if (requested) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
497 WSAEventSelect ((SOCKET) h, hEvent, requested); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
498 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
499 else |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
500 { |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
501 /* 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
|
502 screen buffer handles are waitable, and they'll block until |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
503 a character is available. win32_compute_revents eliminates |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
504 bits for the "wrong" direction. */ |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
505 pfd[i].revents = win32_compute_revents (h, &sought); |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
506 if (sought) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
507 handle_array[nhandles++] = h; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
508 if (pfd[i].revents) |
13574
ac05886b34b6
poll: return immediately on POLLHUP.
Erik Faye-Lund <kusmabite@gmail.com>
parents:
13565
diff
changeset
|
509 timeout = 0; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
510 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
511 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
512 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
513 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
|
514 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
515 /* Do MsgWaitForMultipleObjects anyway to dispatch messages, but |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
516 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
|
517 poll_again = FALSE; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
518 wait_timeout = 0; |
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 else |
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 poll_again = TRUE; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
523 if (timeout == INFTIM) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
524 wait_timeout = INFINITE; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
525 else |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
526 wait_timeout = timeout; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
527 } |
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 for (;;) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
530 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
531 ret = MsgWaitForMultipleObjects (nhandles, handle_array, FALSE, |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
532 wait_timeout, QS_ALLINPUT); |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
533 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
534 if (ret == WAIT_OBJECT_0 + nhandles) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
535 { |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
536 /* new input of some other kind */ |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
537 BOOL bRet; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
538 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
|
539 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
540 TranslateMessage (&msg); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
541 DispatchMessage (&msg); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
542 } |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
543 } |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
544 else |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
545 break; |
10467
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 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
548 if (poll_again) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
549 select (0, &rfds, &wfds, &xfds, &tv0); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
550 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
551 /* 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
|
552 handle_array[nhandles] = NULL; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
553 nhandles = 1; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
554 for (i = 0; i < nfd; i++) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
555 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
556 int happened; |
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 if (pfd[i].fd < 0) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
559 continue; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
560 if (!(pfd[i].events & (POLLIN | POLLRDNORM | |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
561 POLLOUT | POLLWRNORM | POLLWRBAND))) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
562 continue; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
563 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
564 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
|
565 if (h != handle_array[nhandles]) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
566 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
567 /* It's a socket. */ |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
568 WSAEnumNetworkEvents ((SOCKET) h, NULL, &ev); |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
569 WSAEventSelect ((SOCKET) h, 0, 0); |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
570 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
571 /* 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
|
572 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
|
573 if (FD_ISSET ((SOCKET) h, &rfds) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
574 && !(ev.lNetworkEvents & (FD_READ | FD_ACCEPT))) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
575 ev.lNetworkEvents |= FD_READ | FD_ACCEPT; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
576 if (FD_ISSET ((SOCKET) h, &wfds)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
577 ev.lNetworkEvents |= FD_WRITE | FD_CONNECT; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
578 if (FD_ISSET ((SOCKET) h, &xfds)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
579 ev.lNetworkEvents |= FD_OOB; |
10562
ebbb50a36a9f
remove trailing spaces
Jim Meyering <meyering@redhat.com>
parents:
10467
diff
changeset
|
580 |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
581 happened = win32_compute_revents_socket ((SOCKET) h, pfd[i].events, |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
582 ev.lNetworkEvents); |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
583 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
584 else |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
585 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
586 /* Not a socket. */ |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
587 int sought = pfd[i].events; |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
588 happened = win32_compute_revents (h, &sought); |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
589 nhandles++; |
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 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
592 if ((pfd[i].revents |= happened) != 0) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
593 rc++; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
594 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
595 |
15353
f3753e923660
poll: do not return 0 on timeout=-1
Erik Faye-Lund <kusmabite@gmail.com>
parents:
14079
diff
changeset
|
596 if (!rc && timeout == INFTIM) |
f3753e923660
poll: do not return 0 on timeout=-1
Erik Faye-Lund <kusmabite@gmail.com>
parents:
14079
diff
changeset
|
597 { |
f3753e923660
poll: do not return 0 on timeout=-1
Erik Faye-Lund <kusmabite@gmail.com>
parents:
14079
diff
changeset
|
598 SwitchToThread(); |
f3753e923660
poll: do not return 0 on timeout=-1
Erik Faye-Lund <kusmabite@gmail.com>
parents:
14079
diff
changeset
|
599 goto restart; |
f3753e923660
poll: do not return 0 on timeout=-1
Erik Faye-Lund <kusmabite@gmail.com>
parents:
14079
diff
changeset
|
600 } |
f3753e923660
poll: do not return 0 on timeout=-1
Erik Faye-Lund <kusmabite@gmail.com>
parents:
14079
diff
changeset
|
601 |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
602 return rc; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
603 #endif |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
604 } |