2 % (c) The AQUA Project, Glasgow University, 1995
4 %************************************************************************
6 \section[Select.lc]{Select Available File Descriptors}
8 %************************************************************************
14 /* #define STK_CHK_DEBUG */
17 #define NON_POSIX_SOURCE
18 /* Should there be a POSIX alternative based on poll()? */
21 # if defined(HAVE_SYS_TYPES_H)
22 # include <sys/types.h>
25 # ifdef HAVE_SYS_TIME_H
26 # include <sys/time.h>
40 min = delta == 0 ? 0x7fffffff : 0;
43 * Collect all of the fd's that we're interested in, and capture
44 * the minimum waiting time for the delayed threads.
46 * (I_)TSO_EVENT(tso) < 0 => thread waiting on fd (-(I_)TSO_EVENT(tso))
50 for(tso = WaitingThreadsHd; tso != Nil_closure; tso = TSO_LINK(tso)) {
51 us = (I_) TSO_EVENT(tso);
53 /* Looking at a delay event */
57 /* Looking at a wait event */
58 maxfd = ((-us)> maxfd) ? (-us) : maxfd;
63 /* Check for any interesting events */
65 tv.tv_sec = min / 1000000;
66 tv.tv_usec = min % 1000000;
68 while (select((maxfd==0 ? 0 : (maxfd+1)), &rfd, NULL, NULL, &tv) < 0) {
71 fprintf(stderr, "AwaitEvent: select failed\n");
80 for(tso = WaitingThreadsHd; tso != Nil_closure; tso = next) {
82 us = (I_) TSO_EVENT(tso);
84 /* Looking at a delay event */
88 TSO_EVENT(tso) = (W_) us;
90 /* Looking at a wait event */
91 ready = FD_ISSET((-us), &rfd);
96 if (ThreadQueueTl == Nil_closure)
99 TSO_LINK(ThreadQueueTl) = tso;
101 TSO_LINK(tso) = Nil_closure;
103 if (RunnableThreadsTl == Nil_closure)
104 RunnableThreadsHd = tso;
106 TSO_LINK(RunnableThreadsTl) = tso;
107 RunnableThreadsTl = tso;
108 TSO_LINK(tso) = Nil_closure;
112 WaitingThreadsHd = tso;
114 TSO_LINK(prev) = tso;
119 WaitingThreadsHd = WaitingThreadsTl = Nil_closure;
121 TSO_LINK(prev) = Nil_closure;
122 WaitingThreadsTl = prev;
126 #endif /* CONCURRENT */