1 /***********************************************************************/
5 /* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
7 /* Copyright 1996 Institut National de Recherche en Informatique et */
8 /* en Automatique. All rights reserved. This file is distributed */
9 /* under the terms of the GNU Library General Public License, with */
10 /* the special exception on linking described in file ../../LICENSE. */
12 /***********************************************************************/
14 /* $Id: select.c 6824 2005-03-24 17:20:54Z doligez $ */
21 #include "unixsupport.h"
25 #include <sys/types.h>
27 #ifdef HAS_SYS_SELECT_H
28 #include <sys/select.h>
33 typedef fd_set file_descr_set;
35 static void fdlist_to_fdset(value fdlist, fd_set *fdset, int *maxfd)
39 for (l = fdlist; l != Val_int(0); l = Field(l, 1)) {
40 int fd = Int_val(Field(l, 0));
42 if (fd > *maxfd) *maxfd = fd;
46 static value fdset_to_fdlist(value fdlist, fd_set *fdset)
49 value res = Val_int(0);
52 for (l = fdlist; l != Val_int(0); l = Field(l, 1)) {
53 int fd = Int_val(Field(l, 0));
54 if (FD_ISSET(fd, fdset)) {
55 value newres = alloc_small(2, 0);
56 Field(newres, 0) = Val_int(fd);
57 Field(newres, 1) = res;
65 CAMLprim value unix_select(value readfds, value writefds, value exceptfds,
68 fd_set read, write, except;
76 Begin_roots3 (readfds, writefds, exceptfds);
78 fdlist_to_fdset(readfds, &read, &maxfd);
79 fdlist_to_fdset(writefds, &write, &maxfd);
80 fdlist_to_fdset(exceptfds, &except, &maxfd);
81 tm = Double_val(timeout);
83 tvp = (struct timeval *) NULL;
86 tv.tv_usec = (int) (1e6 * (tm - tv.tv_sec));
89 enter_blocking_section();
90 retcode = select(maxfd + 1, &read, &write, &except, tvp);
91 leave_blocking_section();
92 if (retcode == -1) uerror("select", Nothing);
93 readfds = fdset_to_fdlist(readfds, &read);
94 writefds = fdset_to_fdlist(writefds, &write);
95 exceptfds = fdset_to_fdlist(exceptfds, &except);
96 res = alloc_small(3, 0);
97 Field(res, 0) = readfds;
98 Field(res, 1) = writefds;
99 Field(res, 2) = exceptfds;
106 CAMLprim value unix_select(value readfds, value writefds, value exceptfds,
108 { invalid_argument("select not implemented"); }