Mercurial > gnulib
annotate lib/poll.c @ 40231:9b3c79fdfe0b
strtod: fix clash with strtold
Problem reported for RHEL 5 by Jesse Caldwell (Bug#34817).
* lib/strtod.c (compute_minus_zero, minus_zero):
Simplify by remving the macro / external variable,
and having just a function. User changed. This avoids
the need for an external variable that might clash.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Mon, 11 Mar 2019 16:40:29 -0700 |
parents | b06060465f09 |
children |
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 |
40057
b06060465f09
maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents:
39795
diff
changeset
|
4 Copyright 2001-2003, 2006-2019 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 |
19190 | 19 with this program; if not, see <https://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 |
19595
beb2ad957aca
Simplify code. Drop support for Borland C++ on Windows.
Bruno Haible <bruno@clisp.org>
parents:
19484
diff
changeset
|
37 #if defined _WIN32 && ! defined __CYGWIN__ |
16214
ec738d6aeef5
Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents:
16213
diff
changeset
|
38 # define WINDOWS_NATIVE |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
39 # include <winsock2.h> |
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
40 # include <windows.h> |
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
41 # include <io.h> |
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
42 # include <stdio.h> |
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
43 # include <conio.h> |
18883
19886582ca8d
Implement a way to opt out from MSVC support.
Bruno Haible <bruno@clisp.org>
parents:
18808
diff
changeset
|
44 # if GNULIB_MSVC_NOTHROW |
19886582ca8d
Implement a way to opt out from MSVC support.
Bruno Haible <bruno@clisp.org>
parents:
18808
diff
changeset
|
45 # include "msvc-nothrow.h" |
19886582ca8d
Implement a way to opt out from MSVC support.
Bruno Haible <bruno@clisp.org>
parents:
18808
diff
changeset
|
46 # else |
19886582ca8d
Implement a way to opt out from MSVC support.
Bruno Haible <bruno@clisp.org>
parents:
18808
diff
changeset
|
47 # include <io.h> |
19886582ca8d
Implement a way to opt out from MSVC support.
Bruno Haible <bruno@clisp.org>
parents:
18808
diff
changeset
|
48 # endif |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
49 #else |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
50 # include <sys/time.h> |
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
51 # include <unistd.h> |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
52 #endif |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
53 |
17936
5aeafb603ed9
poll: port to MSVC v18 on MS-Windows 8.1
Paul Eggert <eggert@cs.ucla.edu>
parents:
17924
diff
changeset
|
54 #include <sys/select.h> |
5aeafb603ed9
poll: port to MSVC v18 on MS-Windows 8.1
Paul Eggert <eggert@cs.ucla.edu>
parents:
17924
diff
changeset
|
55 #include <sys/socket.h> |
5aeafb603ed9
poll: port to MSVC v18 on MS-Windows 8.1
Paul Eggert <eggert@cs.ucla.edu>
parents:
17924
diff
changeset
|
56 |
7382
8eedfb060b79
2006-09-28 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
6259
diff
changeset
|
57 #ifdef HAVE_SYS_IOCTL_H |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
58 # include <sys/ioctl.h> |
7382
8eedfb060b79
2006-09-28 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
6259
diff
changeset
|
59 #endif |
8eedfb060b79
2006-09-28 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
6259
diff
changeset
|
60 #ifdef HAVE_SYS_FILIO_H |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
61 # include <sys/filio.h> |
7382
8eedfb060b79
2006-09-28 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
6259
diff
changeset
|
62 #endif |
8eedfb060b79
2006-09-28 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
6259
diff
changeset
|
63 |
7908
d41d48e822b4
* lib/poll.c: Include sys/time.h and time.h unconditionally,
Paul Eggert <eggert@cs.ucla.edu>
parents:
7776
diff
changeset
|
64 #include <time.h> |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
65 |
17840 | 66 #include "assure.h" |
67 | |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
68 #ifndef INFTIM |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
69 # define INFTIM (-1) |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
70 #endif |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
71 |
8500
6c3093c67379
Fix compilation error on BeOS.
Bruno Haible <bruno@clisp.org>
parents:
7921
diff
changeset
|
72 /* BeOS does not have MSG_PEEK. */ |
6c3093c67379
Fix compilation error on BeOS.
Bruno Haible <bruno@clisp.org>
parents:
7921
diff
changeset
|
73 #ifndef MSG_PEEK |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
74 # define MSG_PEEK 0 |
8500
6c3093c67379
Fix compilation error on BeOS.
Bruno Haible <bruno@clisp.org>
parents:
7921
diff
changeset
|
75 #endif |
6c3093c67379
Fix compilation error on BeOS.
Bruno Haible <bruno@clisp.org>
parents:
7921
diff
changeset
|
76 |
16214
ec738d6aeef5
Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents:
16213
diff
changeset
|
77 #ifdef WINDOWS_NATIVE |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
78 |
18886
1cb5545b775d
poll: Fix link error on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
18883
diff
changeset
|
79 /* Here we need the recv() function from Windows, that takes a SOCKET as |
1cb5545b775d
poll: Fix link error on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
18883
diff
changeset
|
80 first argument, not any possible gnulib override. */ |
1cb5545b775d
poll: Fix link error on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
18883
diff
changeset
|
81 # undef recv |
1cb5545b775d
poll: Fix link error on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
18883
diff
changeset
|
82 |
18889
5a017bc00597
poll: Fix confusion between SOCKETs and FDs on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
18886
diff
changeset
|
83 /* Here we need the select() function from Windows, because we pass bit masks |
5a017bc00597
poll: Fix confusion between SOCKETs and FDs on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
18886
diff
changeset
|
84 of SOCKETs, not bit masks of FDs. */ |
5a017bc00597
poll: Fix confusion between SOCKETs and FDs on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
18886
diff
changeset
|
85 # undef select |
5a017bc00597
poll: Fix confusion between SOCKETs and FDs on native Windows.
Bruno Haible <bruno@clisp.org>
parents:
18886
diff
changeset
|
86 |
39795
ecc02d9772ef
Avoid -Wcast-function-type warnings from casts after GetProcAddress.
Bruno Haible <bruno@clisp.org>
parents:
19595
diff
changeset
|
87 /* Avoid warnings from gcc -Wcast-function-type. */ |
ecc02d9772ef
Avoid -Wcast-function-type warnings from casts after GetProcAddress.
Bruno Haible <bruno@clisp.org>
parents:
19595
diff
changeset
|
88 # define GetProcAddress \ |
ecc02d9772ef
Avoid -Wcast-function-type warnings from casts after GetProcAddress.
Bruno Haible <bruno@clisp.org>
parents:
19595
diff
changeset
|
89 (void *) GetProcAddress |
ecc02d9772ef
Avoid -Wcast-function-type warnings from casts after GetProcAddress.
Bruno Haible <bruno@clisp.org>
parents:
19595
diff
changeset
|
90 |
17702
77be5ed9d74f
select,poll: fix console handle check on windows 8
Pádraig Brady <P@draigBrady.com>
parents:
17576
diff
changeset
|
91 static BOOL IsConsoleHandle (HANDLE h) |
77be5ed9d74f
select,poll: fix console handle check on windows 8
Pádraig Brady <P@draigBrady.com>
parents:
17576
diff
changeset
|
92 { |
77be5ed9d74f
select,poll: fix console handle check on windows 8
Pádraig Brady <P@draigBrady.com>
parents:
17576
diff
changeset
|
93 DWORD mode; |
77be5ed9d74f
select,poll: fix console handle check on windows 8
Pádraig Brady <P@draigBrady.com>
parents:
17576
diff
changeset
|
94 return GetConsoleMode (h, &mode) != 0; |
77be5ed9d74f
select,poll: fix console handle check on windows 8
Pádraig Brady <P@draigBrady.com>
parents:
17576
diff
changeset
|
95 } |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
96 |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
97 static BOOL |
13051 | 98 IsSocketHandle (HANDLE h) |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
99 { |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
100 WSANETWORKEVENTS ev; |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
101 |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
102 if (IsConsoleHandle (h)) |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
103 return FALSE; |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
104 |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
105 /* 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
|
106 WSAEnumNetworkEvents instead distinguishes the two correctly. */ |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
107 ev.lNetworkEvents = 0xDEADBEEF; |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
108 WSAEnumNetworkEvents ((SOCKET) h, NULL, &ev); |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
109 return ev.lNetworkEvents != 0xDEADBEEF; |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
110 } |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
111 |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
112 /* Declare data structures for ntdll functions. */ |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
113 typedef struct _FILE_PIPE_LOCAL_INFORMATION { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
114 ULONG NamedPipeType; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
115 ULONG NamedPipeConfiguration; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
116 ULONG MaximumInstances; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
117 ULONG CurrentInstances; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
118 ULONG InboundQuota; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
119 ULONG ReadDataAvailable; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
120 ULONG OutboundQuota; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
121 ULONG WriteQuotaAvailable; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
122 ULONG NamedPipeState; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
123 ULONG NamedPipeEnd; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
124 } 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
|
125 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
126 typedef struct _IO_STATUS_BLOCK |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
127 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
128 union { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
129 DWORD Status; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
130 PVOID Pointer; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
131 } u; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
132 ULONG_PTR Information; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
133 } IO_STATUS_BLOCK, *PIO_STATUS_BLOCK; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
134 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
135 typedef enum _FILE_INFORMATION_CLASS { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
136 FilePipeLocalInformation = 24 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
137 } FILE_INFORMATION_CLASS, *PFILE_INFORMATION_CLASS; |
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 typedef DWORD (WINAPI *PNtQueryInformationFile) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
140 (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
|
141 |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
142 # ifndef PIPE_BUF |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
143 # define PIPE_BUF 512 |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
144 # endif |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
145 |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
146 /* 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
|
147 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
|
148 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
149 static int |
16214
ec738d6aeef5
Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents:
16213
diff
changeset
|
150 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
|
151 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
152 int i, ret, happened; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
153 INPUT_RECORD *irbuffer; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
154 DWORD avail, nbuffer; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
155 BOOL bRet; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
156 IO_STATUS_BLOCK iosb; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
157 FILE_PIPE_LOCAL_INFORMATION fpli; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
158 static PNtQueryInformationFile NtQueryInformationFile; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
159 static BOOL once_only; |
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 switch (GetFileType (h)) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
162 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
163 case FILE_TYPE_PIPE: |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
164 if (!once_only) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
165 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
166 NtQueryInformationFile = (PNtQueryInformationFile) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
167 GetProcAddress (GetModuleHandle ("ntdll.dll"), |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
168 "NtQueryInformationFile"); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
169 once_only = TRUE; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
170 } |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
171 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
172 happened = 0; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
173 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
|
174 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
175 if (avail) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
176 happened |= *p_sought & (POLLIN | POLLRDNORM); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
177 } |
13565
69913822a0f7
poll, select: handle ERROR_BROKEN_PIPE.
Paolo Bonzini <pbonzini@redhat.com>
parents:
13051
diff
changeset
|
178 else if (GetLastError () == ERROR_BROKEN_PIPE) |
69913822a0f7
poll, select: handle ERROR_BROKEN_PIPE.
Paolo Bonzini <pbonzini@redhat.com>
parents:
13051
diff
changeset
|
179 happened |= POLLHUP; |
10467
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 else |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
182 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
183 /* 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
|
184 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
|
185 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
|
186 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
|
187 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
|
188 (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
|
189 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
|
190 atomic writes. */ |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
191 memset (&iosb, 0, sizeof (iosb)); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
192 memset (&fpli, 0, sizeof (fpli)); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
193 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
194 if (!NtQueryInformationFile |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
195 || NtQueryInformationFile (h, &iosb, &fpli, sizeof (fpli), |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
196 FilePipeLocalInformation) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
197 || fpli.WriteQuotaAvailable >= PIPE_BUF |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
198 || (fpli.OutboundQuota < PIPE_BUF && |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
199 fpli.WriteQuotaAvailable == fpli.OutboundQuota)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
200 happened |= *p_sought & (POLLOUT | POLLWRNORM | POLLWRBAND); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
201 } |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
202 return happened; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
203 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
204 case FILE_TYPE_CHAR: |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
205 ret = WaitForSingleObject (h, 0); |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
206 if (!IsConsoleHandle (h)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
207 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
|
208 |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
209 nbuffer = avail = 0; |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
210 bRet = GetNumberOfConsoleInputEvents (h, &nbuffer); |
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
211 if (bRet) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
212 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
213 /* Input buffer. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
214 *p_sought &= POLLIN | POLLRDNORM; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
215 if (nbuffer == 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
216 return POLLHUP; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
217 if (!*p_sought) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
218 return 0; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
219 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
220 irbuffer = (INPUT_RECORD *) alloca (nbuffer * sizeof (INPUT_RECORD)); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
221 bRet = PeekConsoleInput (h, irbuffer, nbuffer, &avail); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
222 if (!bRet || avail == 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
223 return POLLHUP; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
224 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
225 for (i = 0; i < avail; i++) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
226 if (irbuffer[i].EventType == KEY_EVENT) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
227 return *p_sought; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
228 return 0; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
229 } |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
230 else |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
231 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
232 /* Screen buffer. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
233 *p_sought &= POLLOUT | POLLWRNORM | POLLWRBAND; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
234 return *p_sought; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
235 } |
10467
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 default: |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
238 ret = WaitForSingleObject (h, 0); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
239 if (ret == WAIT_OBJECT_0) |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
240 return *p_sought & ~(POLLPRI | POLLRDBAND); |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
241 |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
242 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
|
243 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
244 } |
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 /* 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
|
247 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
248 static int |
16214
ec738d6aeef5
Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents:
16213
diff
changeset
|
249 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
|
250 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
251 int happened = 0; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
252 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
253 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
|
254 happened |= (POLLIN | POLLRDNORM) & sought; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
255 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
256 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
|
257 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
258 int r, error; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
259 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
260 char data[64]; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
261 WSASetLastError (0); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
262 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
|
263 error = WSAGetLastError (); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
264 WSASetLastError (0); |
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 if (r > 0 || error == WSAENOTCONN) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
267 happened |= (POLLIN | POLLRDNORM) & sought; |
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 /* 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
|
270 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
|
271 || error == WSAECONNABORTED || error == WSAENETRESET) |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
272 happened |= POLLHUP; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
273 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
274 else |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
275 happened |= POLLERR; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
276 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
277 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
278 if (lNetworkEvents & (FD_WRITE | FD_CONNECT)) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
279 happened |= (POLLOUT | POLLWRNORM | POLLWRBAND) & sought; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
280 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
281 if (lNetworkEvents & FD_OOB) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
282 happened |= (POLLPRI | POLLRDBAND) & sought; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
283 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
284 return happened; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
285 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
286 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
287 #else /* !MinGW */ |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
288 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
289 /* 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
|
290 static int |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
291 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
|
292 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
293 int happened = 0; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
294 if (FD_ISSET (fd, rfds)) |
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 int r; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
297 int socket_errno; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
298 |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
299 # if defined __MACH__ && defined __APPLE__ |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
300 /* 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
|
301 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
|
302 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
|
303 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
|
304 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
|
305 socket_errno = (r < 0) ? errno : 0; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
306 if (r == 0 || socket_errno == ENOTSOCK) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
307 ioctl (fd, FIONREAD, &r); |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
308 # else |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
309 char data[64]; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
310 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
|
311 socket_errno = (r < 0) ? errno : 0; |
10997
92c82a6f26db
poll: filter through cppi
Jim Meyering <meyering@redhat.com>
parents:
10996
diff
changeset
|
312 # endif |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
313 if (r == 0) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
314 happened |= POLLHUP; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
315 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
316 /* 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
|
317 that's fine. */ |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
318 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
|
319 happened |= (POLLIN | POLLRDNORM) & sought; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
320 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
321 /* 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
|
322 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
|
323 || socket_errno == ECONNABORTED || socket_errno == ENETRESET) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
324 happened |= POLLHUP; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
325 |
17083
1ba58228acda
poll: fix for systems that can't recv() on a non-socket
Joachim Schmitz <jojo@schmitz-digital.de>
parents:
16869
diff
changeset
|
326 /* 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
|
327 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
|
328 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
|
329 |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
330 else |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
331 happened |= POLLERR; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
332 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
333 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
334 if (FD_ISSET (fd, wfds)) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
335 happened |= (POLLOUT | POLLWRNORM | POLLWRBAND) & sought; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
336 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
337 if (FD_ISSET (fd, efds)) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
338 happened |= (POLLPRI | POLLRDBAND) & sought; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
339 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
340 return happened; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
341 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
342 #endif /* !MinGW */ |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
343 |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
344 int |
13741 | 345 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
|
346 { |
16214
ec738d6aeef5
Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents:
16213
diff
changeset
|
347 #ifndef WINDOWS_NATIVE |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
348 fd_set rfds, wfds, efds; |
10310
b48c3c82f4d9
Further micro-optimization.
Bruno Haible <bruno@clisp.org>
parents:
10308
diff
changeset
|
349 struct timeval tv; |
10308
c3ecbe083a26
microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents:
9831
diff
changeset
|
350 struct timeval *ptv; |
7776
35d644ee8b48
2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
7382
diff
changeset
|
351 int maxfd, rc; |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
352 nfds_t i; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
353 |
18808
c6cb639ee9d2
poll: improve fast check for out-of-range NFD
Paul Eggert <eggert@cs.ucla.edu>
parents:
18806
diff
changeset
|
354 if (nfd > INT_MAX) |
4242
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 errno = EINVAL; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
357 return -1; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
358 } |
18808
c6cb639ee9d2
poll: improve fast check for out-of-range NFD
Paul Eggert <eggert@cs.ucla.edu>
parents:
18806
diff
changeset
|
359 /* Don't check directly for NFD greater than OPEN_MAX. Any practical use |
c6cb639ee9d2
poll: improve fast check for out-of-range NFD
Paul Eggert <eggert@cs.ucla.edu>
parents:
18806
diff
changeset
|
360 of a too-large NFD is caught by one of the other checks below, and |
17924
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
361 checking directly for getdtablesize is too much of a portability |
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
362 and/or performance and/or correctness hassle. */ |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
363 |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
364 /* 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
|
365 simplest case. */ |
17084
723cfa7546c0
poll: fix poll(0, NULL, msec)
Joachim Schmitz <jojo@schmitz-digital.de>
parents:
17083
diff
changeset
|
366 if (!pfd && nfd) |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
367 { |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
368 errno = EFAULT; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
369 return -1; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
370 } |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
371 |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
372 /* convert timeout number into a timeval structure */ |
10308
c3ecbe083a26
microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents:
9831
diff
changeset
|
373 if (timeout == 0) |
10310
b48c3c82f4d9
Further micro-optimization.
Bruno Haible <bruno@clisp.org>
parents:
10308
diff
changeset
|
374 { |
b48c3c82f4d9
Further micro-optimization.
Bruno Haible <bruno@clisp.org>
parents:
10308
diff
changeset
|
375 ptv = &tv; |
b48c3c82f4d9
Further micro-optimization.
Bruno Haible <bruno@clisp.org>
parents:
10308
diff
changeset
|
376 ptv->tv_sec = 0; |
b48c3c82f4d9
Further micro-optimization.
Bruno Haible <bruno@clisp.org>
parents:
10308
diff
changeset
|
377 ptv->tv_usec = 0; |
b48c3c82f4d9
Further micro-optimization.
Bruno Haible <bruno@clisp.org>
parents:
10308
diff
changeset
|
378 } |
10308
c3ecbe083a26
microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents:
9831
diff
changeset
|
379 else if (timeout > 0) |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
380 { |
10308
c3ecbe083a26
microoptimization of lib/poll.c
Paolo Bonzini <bonzini@gnu.org>
parents:
9831
diff
changeset
|
381 ptv = &tv; |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
382 ptv->tv_sec = timeout / 1000; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
383 ptv->tv_usec = (timeout % 1000) * 1000; |
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 else if (timeout == INFTIM) |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
386 /* wait forever */ |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
387 ptv = NULL; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
388 else |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
389 { |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
390 errno = EINVAL; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
391 return -1; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
392 } |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
393 |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
394 /* 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
|
395 maxfd = -1; |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
396 FD_ZERO (&rfds); |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
397 FD_ZERO (&wfds); |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
398 FD_ZERO (&efds); |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
399 for (i = 0; i < nfd; i++) |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
400 { |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
401 if (pfd[i].fd < 0) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
402 continue; |
17924
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
403 if (maxfd < pfd[i].fd) |
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
404 { |
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
405 maxfd = pfd[i].fd; |
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
406 if (FD_SETSIZE <= maxfd) |
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
407 { |
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
408 errno = EINVAL; |
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
409 return -1; |
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
410 } |
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
411 } |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
412 if (pfd[i].events & (POLLIN | POLLRDNORM)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
413 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
|
414 /* 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
|
415 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
|
416 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
|
417 if (pfd[i].events & (POLLOUT | POLLWRNORM | POLLWRBAND)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
418 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
|
419 if (pfd[i].events & (POLLPRI | POLLRDBAND)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
420 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
|
421 } |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
422 |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
423 /* examine fd sets */ |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
424 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
|
425 if (rc < 0) |
35d644ee8b48
2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
7382
diff
changeset
|
426 return rc; |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
427 |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
428 /* establish results */ |
7776
35d644ee8b48
2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
7382
diff
changeset
|
429 rc = 0; |
35d644ee8b48
2007-01-03 Paolo Bonzini <bonzini@gnu.org>
Paolo Bonzini <bonzini@gnu.org>
parents:
7382
diff
changeset
|
430 for (i = 0; i < nfd; i++) |
17924
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
431 { |
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
432 pfd[i].revents = (pfd[i].fd < 0 |
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
433 ? 0 |
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
434 : compute_revents (pfd[i].fd, pfd[i].events, |
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
435 &rfds, &wfds, &efds)); |
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
436 rc += pfd[i].revents != 0; |
68876b89b3ad
poll: fixes for large fds
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
437 } |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
438 |
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
439 return rc; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
440 #else |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
441 static struct timeval tv0; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
442 static HANDLE hEvent; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
443 WSANETWORKEVENTS ev; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
444 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
|
445 DWORD ret, wait_timeout, nhandles; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
446 fd_set rfds, wfds, xfds; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
447 BOOL poll_again; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
448 MSG msg; |
10996
724ce887ae63
poll: don't return uninitialized
Daniel P. Berrange <berrange@redhat.com>
parents:
10562
diff
changeset
|
449 int rc = 0; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
450 nfds_t i; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
451 |
18808
c6cb639ee9d2
poll: improve fast check for out-of-range NFD
Paul Eggert <eggert@cs.ucla.edu>
parents:
18806
diff
changeset
|
452 if (nfd > INT_MAX || timeout < -1) |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
453 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
454 errno = EINVAL; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
455 return -1; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
456 } |
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 if (!hEvent) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
459 hEvent = CreateEvent (NULL, FALSE, FALSE, NULL); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
460 |
15353
f3753e923660
poll: do not return 0 on timeout=-1
Erik Faye-Lund <kusmabite@gmail.com>
parents:
14079
diff
changeset
|
461 restart: |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
462 handle_array[0] = hEvent; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
463 nhandles = 1; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
464 FD_ZERO (&rfds); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
465 FD_ZERO (&wfds); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
466 FD_ZERO (&xfds); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
467 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
468 /* 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
|
469 for (i = 0; i < nfd; i++) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
470 { |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
471 int sought = pfd[i].events; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
472 pfd[i].revents = 0; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
473 if (pfd[i].fd < 0) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
474 continue; |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
475 if (!(sought & (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM | POLLWRBAND |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
476 | POLLPRI | POLLRDBAND))) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
477 continue; |
10467
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 h = (HANDLE) _get_osfhandle (pfd[i].fd); |
17840 | 480 assure (h != NULL); |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
481 if (IsSocketHandle (h)) |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
482 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
483 int requested = FD_CLOSE; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
484 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
485 /* 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
|
486 if (sought & (POLLIN | POLLRDNORM)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
487 { |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
488 requested |= FD_READ | FD_ACCEPT; |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
489 FD_SET ((SOCKET) h, &rfds); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
490 } |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
491 if (sought & (POLLOUT | POLLWRNORM | POLLWRBAND)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
492 { |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
493 requested |= FD_WRITE | FD_CONNECT; |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
494 FD_SET ((SOCKET) h, &wfds); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
495 } |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
496 if (sought & (POLLPRI | POLLRDBAND)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
497 { |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
498 requested |= FD_OOB; |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
499 FD_SET ((SOCKET) h, &xfds); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
500 } |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
501 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
502 if (requested) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
503 WSAEventSelect ((SOCKET) h, hEvent, requested); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
504 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
505 else |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
506 { |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
507 /* 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
|
508 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
|
509 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
|
510 bits for the "wrong" direction. */ |
16214
ec738d6aeef5
Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents:
16213
diff
changeset
|
511 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
|
512 if (sought) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
513 handle_array[nhandles++] = h; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
514 if (pfd[i].revents) |
13574
ac05886b34b6
poll: return immediately on POLLHUP.
Erik Faye-Lund <kusmabite@gmail.com>
parents:
13565
diff
changeset
|
515 timeout = 0; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
516 } |
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 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
|
520 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
521 /* Do MsgWaitForMultipleObjects anyway to dispatch messages, but |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
522 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
|
523 poll_again = FALSE; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
524 wait_timeout = 0; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
525 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
526 else |
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 poll_again = TRUE; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
529 if (timeout == INFTIM) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
530 wait_timeout = INFINITE; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
531 else |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
532 wait_timeout = timeout; |
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 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
535 for (;;) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
536 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
537 ret = MsgWaitForMultipleObjects (nhandles, handle_array, FALSE, |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
538 wait_timeout, QS_ALLINPUT); |
10467
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 if (ret == WAIT_OBJECT_0 + nhandles) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
541 { |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
542 /* new input of some other kind */ |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
543 BOOL bRet; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
544 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
|
545 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
546 TranslateMessage (&msg); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
547 DispatchMessage (&msg); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
548 } |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
549 } |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
550 else |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
551 break; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
552 } |
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 if (poll_again) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
555 select (0, &rfds, &wfds, &xfds, &tv0); |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
556 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
557 /* 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
|
558 handle_array[nhandles] = NULL; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
559 nhandles = 1; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
560 for (i = 0; i < nfd; i++) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
561 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
562 int happened; |
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 if (pfd[i].fd < 0) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
565 continue; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
566 if (!(pfd[i].events & (POLLIN | POLLRDNORM | |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
567 POLLOUT | POLLWRNORM | POLLWRBAND))) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
568 continue; |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
569 |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
570 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
|
571 if (h != handle_array[nhandles]) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
572 { |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
573 /* It's a socket. */ |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
574 WSAEnumNetworkEvents ((SOCKET) h, NULL, &ev); |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
575 WSAEventSelect ((SOCKET) h, 0, 0); |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
576 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
577 /* 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
|
578 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
|
579 if (FD_ISSET ((SOCKET) h, &rfds) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
580 && !(ev.lNetworkEvents & (FD_READ | FD_ACCEPT))) |
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 if (FD_ISSET ((SOCKET) h, &wfds)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
583 ev.lNetworkEvents |= FD_WRITE | FD_CONNECT; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
584 if (FD_ISSET ((SOCKET) h, &xfds)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11852
diff
changeset
|
585 ev.lNetworkEvents |= FD_OOB; |
10562
ebbb50a36a9f
remove trailing spaces
Jim Meyering <meyering@redhat.com>
parents:
10467
diff
changeset
|
586 |
16214
ec738d6aeef5
Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents:
16213
diff
changeset
|
587 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
|
588 ev.lNetworkEvents); |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
589 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
590 else |
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 /* Not a socket. */ |
11852
c9032834d889
Fix polling for writeability of a screen buffer.
Paolo Bonzini <bonzini@gnu.org>
parents:
11020
diff
changeset
|
593 int sought = pfd[i].events; |
16214
ec738d6aeef5
Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents:
16213
diff
changeset
|
594 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
|
595 nhandles++; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
596 } |
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 if ((pfd[i].revents |= happened) != 0) |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
599 rc++; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
600 } |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
601 |
15353
f3753e923660
poll: do not return 0 on timeout=-1
Erik Faye-Lund <kusmabite@gmail.com>
parents:
14079
diff
changeset
|
602 if (!rc && timeout == INFTIM) |
f3753e923660
poll: do not return 0 on timeout=-1
Erik Faye-Lund <kusmabite@gmail.com>
parents:
14079
diff
changeset
|
603 { |
16869
38e6d30d7e4d
poll/select: prevent busy-waiting
Paolo Bonzini <bonzini@gnu.org>
parents:
16366
diff
changeset
|
604 SleepEx (1, TRUE); |
15353
f3753e923660
poll: do not return 0 on timeout=-1
Erik Faye-Lund <kusmabite@gmail.com>
parents:
14079
diff
changeset
|
605 goto restart; |
f3753e923660
poll: do not return 0 on timeout=-1
Erik Faye-Lund <kusmabite@gmail.com>
parents:
14079
diff
changeset
|
606 } |
f3753e923660
poll: do not return 0 on timeout=-1
Erik Faye-Lund <kusmabite@gmail.com>
parents:
14079
diff
changeset
|
607 |
10467
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
608 return rc; |
f96117307f4a
implement a native version of poll for Win32
Paolo Bonzini <bonzini@gnu.org>
parents:
10414
diff
changeset
|
609 #endif |
4242
eec6ba9ed532
New module poll, from Paolo Bonzini <bonzini@gnu.org>.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
610 } |