1
/* $Xorg: waitfor.c,v 1.4 2001/02/09 02:05:45 xorgcvs Exp $ */
6
Copyright 1987, 1998 The Open Group
8
Permission to use, copy, modify, distribute, and sell this software and its
9
documentation for any purpose is hereby granted without fee, provided that
10
the above copyright notice appear in all copies and that both that
11
copyright notice and this permission notice appear in supporting
14
The above copyright notice and this permission notice shall be included in
15
all copies or substantial portions of the Software.
17
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
Except as contained in this notice, the name of The Open Group shall not be
25
used in advertising or otherwise to promote the sale, use or other dealings
26
in this Software without prior written authorization from The Open Group.
27
* Copyright 1990, 1991 Network Computing Devices;
28
* Portions Copyright 1987 by Digital Equipment Corporation
30
* Permission to use, copy, modify, distribute, and sell this software and its
31
* documentation for any purpose is hereby granted without fee, provided that
32
* the above copyright notice appear in all copies and that both that
33
* copyright notice and this permission notice appear in supporting
34
* documentation, and that the names of Network Computing Devices,
35
* or Digital not be used in advertising or
36
* publicity pertaining to distribution of the software without specific,
37
* written prior permission. Network Computing Devices, or Digital
38
* make no representations about the
39
* suitability of this software for any purpose. It is provided "as is"
40
* without express or implied warranty.
42
* NETWORK COMPUTING DEVICES, AND DIGITAL DISCLAIM ALL WARRANTIES WITH
43
* REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
44
* AND FITNESS, IN NO EVENT SHALL NETWORK COMPUTING DEVICES, OR DIGITAL BE
45
* LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
46
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
47
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
48
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
50
* $NCDXorg: @(#)waitfor.c,v 4.5 1991/06/24 11:59:20 lemke Exp $
53
/* $XFree86: xc/programs/xfs/os/waitfor.c,v 3.16 2002/05/31 18:46:12 dawes Exp $ */
55
#include <X11/Xos.h> /* strings, time, etc */
60
#include <sys/param.h>
63
#include "clientstr.h"
65
#include "X11/Xpoll.h"
70
#define select(n,r,w,x,t) os2PseudoSelect(n,r,w,x,t)
73
extern WorkQueuePtr workQueue;
76
extern fd_set WellKnownConnections;
77
extern fd_set LastSelectMask;
78
extern fd_set WriteMask;
79
extern fd_set ClientsWithInput;
80
extern fd_set ClientsWriteBlocked;
81
extern fd_set AllSockets;
82
extern fd_set AllClients;
83
extern fd_set OutputPending;
85
extern Bool AnyClientsWriteBlocked;
86
extern Bool NewOutputPending;
88
extern int ConnectionTranslation[];
95
* server suspends until
97
* - new client connects
98
* - room to write data to clients
102
WaitForSomething(int *pClientsReady)
106
fd_set clientsReadable;
107
fd_set clientsWriteable;
110
long current_time = 0;
116
/* handle the work Q */
120
if (XFD_ANYSET(&ClientsWithInput)) {
121
XFD_COPYSET(&ClientsWithInput, &clientsReadable);
125
* deal with KeepAlive timeouts. if this seems to costly, SIGALRM
126
* could be used, but its more dangerous since some it could catch us
127
* at an inopportune moment (like inside un-reentrant malloc()).
129
current_time = GetTimeInMillis();
130
timeout = current_time - LastReapTime;
131
if (timeout > ReapClientTime) {
133
LastReapTime = current_time;
134
timeout = ReapClientTime;
136
timeout = ReapClientTime - timeout;
137
waittime.tv_sec = timeout / MILLI_PER_SECOND;
138
waittime.tv_usec = (timeout % MILLI_PER_SECOND) *
139
(1000000 / MILLI_PER_SECOND);
142
XFD_COPYSET(&AllSockets, &LastSelectMask);
144
BlockHandler(&wt, (pointer) &LastSelectMask);
145
if (NewOutputPending)
148
if (AnyClientsWriteBlocked) {
149
XFD_COPYSET(&ClientsWriteBlocked, &clientsWriteable);
150
i = Select(MAXSOCKS, &LastSelectMask, &clientsWriteable, NULL, wt);
152
i = Select(MAXSOCKS, &LastSelectMask, NULL, NULL, wt);
156
WakeupHandler(i, (unsigned long *) &LastSelectMask);
157
if (i <= 0) { /* error or timeout */
158
FD_ZERO(&clientsWriteable);
160
if (selecterr == EBADF) { /* somebody disconnected */
162
} else if (selecterr != EINTR) {
163
ErrorF("WaitForSomething: select(): errno %d\n", selecterr);
166
* must have been broken by a signal. go deal with any
171
} else { /* must have timed out */
173
LastReapTime = GetTimeInMillis();
176
if (AnyClientsWriteBlocked && XFD_ANYSET(&clientsWriteable)) {
177
NewOutputPending = TRUE;
178
XFD_ORSET(&OutputPending, &clientsWriteable, &OutputPending);
179
XFD_UNSET(&ClientsWriteBlocked, &clientsWriteable);
180
if (!XFD_ANYSET(&ClientsWriteBlocked))
181
AnyClientsWriteBlocked = FALSE;
183
XFD_ANDSET(&clientsReadable, &LastSelectMask, &AllClients);
184
if (LastSelectMask.fds_bits[0] & WellKnownConnections.fds_bits[0])
185
MakeNewConnections();
186
if (XFD_ANYSET(&clientsReadable))
193
if (XFD_ANYSET(&clientsReadable)) {
197
if (current_time) /* may not have been set */
198
current_time = GetTimeInMillis();
199
for (i = 0; i < howmany(XFD_SETSIZE, NFDBITS); i++) {
200
while (clientsReadable.fds_bits[i]) {
201
curclient = ffs(clientsReadable.fds_bits[i]) - 1;
202
conn = ConnectionTranslation[curclient + (i << 5)];
203
clientsReadable.fds_bits[i] &= ~(((fd_mask)1L) << curclient);
204
client = clients[conn];
207
pClientsReady[nready++] = conn;
208
client->last_request_time = current_time;
209
client->clientGone = CLIENT_ALIVE;
218
* This is not always a macro
225
for (i = 0; i < howmany(XFD_SETSIZE, NFDBITS); i++)