3 * Support for threaded ops which may block (read, write, connect, etc.).
5 * Copyright (c) 1996 T. J. Wilkinson & Associates, London, UK.
7 * See the file "license.terms" for information on usage and redistribution
8 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
10 * Written by Tim Wilkinson <tim@tjwassoc.demon.co.uk>, 1996.
15 #include "sysdep/defines.h"
17 #include <sys/types.h>
19 #include <sys/socket.h>
28 * We only need this stuff is we are using the internal thread system.
30 #if defined(USE_INTERNAL_THREADS)
34 #define TH_ACCEPT TH_READ
35 #define TH_CONNECT TH_WRITE
38 static fd_set readsPending;
39 static fd_set writesPending;
40 static thread* readQ[FD_SETSIZE];
41 static thread* writeQ[FD_SETSIZE];
42 static struct timeval tm = { 0, 0 };
44 void blockOnFile(int, int);
45 void waitOnEvents(void);
47 extern thread* currentThread;
49 /* These are undefined because we do not yet support async I/O */
56 * Create a threaded file descriptor.
59 threadedFileDescriptor(int fd)
61 #if !defined(BLOCKING_CALLS)
66 /* Make non-blocking */
67 #if defined(HAVE_FCNTL) && defined(O_NONBLOCK)
68 r = fcntl(fd, F_GETFL, 0);
69 r = fcntl(fd, F_SETFL, r|O_NONBLOCK);
70 #elif defined(HAVE_IOCTL) && defined(FIONBIO)
71 r = ioctl(fd, FIONBIO, &on);
80 /* Allow socket to signal this process when new data is available */
82 #if defined(HAVE_FCNTL) && defined(F_SETOWN)
83 r = fcntl(fd, F_SETOWN, pid);
84 #elif defined(HAVE_IOCTL) && defined(FIOSETOWN)
85 r = ioctl(fd, FIOSETOWN, &pid);
94 #if defined(HAVE_FCNTL) && defined(O_ASYNC)
95 r = fcntl(fd, F_GETFL, 0);
96 r = fcntl(fd, F_SETFL, r|O_ASYNC);
97 #elif defined(HAVE_IOCTL) && defined(FIOASYNC)
98 r = ioctl(fd, FIOASYNC, &on);
111 * Threaded create socket.
114 threadedSocket(int af, int type, int proto)
121 fd = socket(af, type, proto);
122 return (threadedFileDescriptor(fd));
126 * Threaded file open.
129 threadedOpen(char* path, int flags, int mode)
136 fd = open(path, flags, mode);
137 return (threadedFileDescriptor(fd));
141 * Threaded socket connect.
144 threadedConnect(int fd, struct sockaddr* addr, int len)
148 r = connect(fd, addr, len);
149 #if !defined(BLOCKING_CALLS)
151 && (errno == EINPROGRESS || errno == EALREADY
152 || errno == EWOULDBLOCK)) {
153 blockOnFile(fd, TH_CONNECT);
154 r = 0; /* Assume it's okay when we get released */
162 * Threaded socket accept.
165 threadedAccept(int fd, struct sockaddr* addr, int* len)
172 #if defined(BLOCKING_CALLS)
173 blockOnFile(fd, TH_ACCEPT);
175 r = accept(fd, addr, len);
177 || !(errno == EINPROGRESS || errno == EALREADY
178 || errno == EWOULDBLOCK))
182 #if !defined(BLOCKING_CALLS)
183 blockOnFile(fd, TH_ACCEPT);
186 return (threadedFileDescriptor(r));
190 * Read but only if we can.
193 threadedRead(int fd, char* buf, int len)
197 DBG( printf("threadedRead\n"); )
199 #if defined(BLOCKING_CALLS)
200 blockOnFile(fd, TH_READ);
204 r = read(fd, buf, len);
206 && (errno == EAGAIN || errno == EWOULDBLOCK
209 blockOnFile(fd, TH_READ);
217 * Write but only if we can.
220 threadedWrite(int fd, char* buf, int len)
228 DBG( printf("threadedWrite %dbytes\n",len); )
230 while (len > 0 && r > 0)
232 #if defined(BLOCKING_CALLS)
233 blockOnFile(fd, TH_WRITE);
235 r = write(fd, ptr, len);
237 && (errno == EAGAIN || errno == EWOULDBLOCK
240 #if !defined(BLOCKING_CALLS)
241 blockOnFile(fd, TH_WRITE);
255 * An attempt to access a file would block, so suspend the thread until
259 blockOnFile(int fd, int op)
261 DBG( printf("blockOnFile()\n"); )
271 FD_SET(fd, &readsPending);
272 suspendOnQThread(currentThread, &readQ[fd]);
273 FD_CLR(fd, &readsPending);
277 FD_SET(fd, &writesPending);
278 suspendOnQThread(currentThread, &writeQ[fd]);
279 FD_CLR(fd, &writesPending);
286 * Check if some file descriptor or other event to become ready.
287 * Block if required (but make sure we can still take timer interrupts).
290 checkEvents(bool block)
300 DBG( printf("checkEvents block:%d\n", block); )
303 FD_COPY(&readsPending, &rd);
304 FD_COPY(&writesPending, &wr);
306 memcpy(&rd, &readsPending, sizeof(rd));
307 memcpy(&wr, &writesPending, sizeof(wr));
311 * If select() is called with indefinite wait, we have to make sure
312 * we can get interrupted by timer events.
318 r = select(maxFd+1, &rd, &wr, 0, 0);
323 r = select(maxFd+1, &rd, &wr, 0, &tm);
326 /* We must be holding off interrupts before we start playing with
327 * the read and write queues. This should be already done but a
328 * quick check never hurt anyone.
330 assert(blockInts > 0);
332 DBG( printf("Select returns %d\n", r); )
334 for (i = 0; r > 0 && i <= maxFd; i++)
336 if (readQ[i] != 0 && FD_ISSET(i, &rd))
338 for (tid = readQ[i]; tid != 0; tid = ntid)
346 if (writeQ[i] != 0 && FD_ISSET(i, &wr))
348 for (tid = writeQ[i]; tid != 0; tid = ntid)