1997-09-04 06:45:56 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
|
|
|
/* Objective Caml */
|
|
|
|
/* */
|
|
|
|
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
|
|
|
/* Copyright 1996 Institut National de Recherche en Informatique et */
|
1999-11-17 10:59:06 -08:00
|
|
|
/* en Automatique. All rights reserved. This file is distributed */
|
2001-12-07 05:41:02 -08:00
|
|
|
/* under the terms of the GNU Library General Public License, with */
|
|
|
|
/* the special exception on linking described in file ../../LICENSE. */
|
1997-09-04 06:45:56 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
#include <mlvalues.h>
|
|
|
|
#include <alloc.h>
|
|
|
|
#include <memory.h>
|
|
|
|
#include <signals.h>
|
|
|
|
#include "unixsupport.h"
|
|
|
|
|
|
|
|
static void fdlist_to_fdset(value fdlist, fd_set *fdset)
|
|
|
|
{
|
|
|
|
value l;
|
|
|
|
FD_ZERO(fdset);
|
|
|
|
for (l = fdlist; l != Val_int(0); l = Field(l, 1)) {
|
2002-04-30 08:00:48 -07:00
|
|
|
FD_SET(Socket_val(Field(l, 0)), fdset);
|
1997-09-04 06:45:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static value fdset_to_fdlist(value fdlist, fd_set *fdset)
|
|
|
|
{
|
|
|
|
value res = Val_int(0);
|
|
|
|
Begin_roots2(fdlist, res)
|
|
|
|
for (/*nothing*/; fdlist != Val_int(0); fdlist = Field(fdlist, 1)) {
|
|
|
|
value s = Field(fdlist, 0);
|
2002-04-30 08:00:48 -07:00
|
|
|
if (FD_ISSET(Socket_val(s), fdset)) {
|
1999-11-29 11:04:56 -08:00
|
|
|
value newres = alloc_small(2, 0);
|
|
|
|
Field(newres, 0) = s;
|
|
|
|
Field(newres, 1) = res;
|
|
|
|
res = newres;
|
1997-09-04 06:45:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
End_roots();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_select(value readfds, value writefds, value exceptfds, value timeout)
|
1997-09-04 06:45:56 -07:00
|
|
|
{
|
|
|
|
fd_set read, write, except;
|
|
|
|
double tm;
|
|
|
|
struct timeval tv;
|
|
|
|
struct timeval * tvp;
|
|
|
|
int retcode;
|
|
|
|
value res;
|
|
|
|
value read_list = Val_unit, write_list = Val_unit, except_list = Val_unit;
|
|
|
|
|
|
|
|
Begin_roots3 (readfds, writefds, exceptfds)
|
|
|
|
Begin_roots3 (read_list, write_list, except_list)
|
2003-01-07 08:16:44 -08:00
|
|
|
tm = Double_val(timeout);
|
2003-01-06 08:44:21 -08:00
|
|
|
if (readfds == Val_int(0)
|
|
|
|
&& writefds == Val_int(0)
|
|
|
|
&& exceptfds == Val_int(0)) {
|
|
|
|
if ( tm > 0.0 ) {
|
|
|
|
enter_blocking_section();
|
|
|
|
Sleep( (int)(tm * 1000));
|
|
|
|
leave_blocking_section();
|
|
|
|
}
|
|
|
|
read_list = write_list = except_list = Val_int(0);
|
|
|
|
} else {
|
|
|
|
fdlist_to_fdset(readfds, &read);
|
|
|
|
fdlist_to_fdset(writefds, &write);
|
|
|
|
fdlist_to_fdset(exceptfds, &except);
|
|
|
|
if (tm < 0.0)
|
|
|
|
tvp = (struct timeval *) NULL;
|
|
|
|
else {
|
|
|
|
tv.tv_sec = (int) tm;
|
|
|
|
tv.tv_usec = (int) (1e6 * (tm - (int) tm));
|
|
|
|
tvp = &tv;
|
|
|
|
}
|
|
|
|
enter_blocking_section();
|
|
|
|
retcode = select(FD_SETSIZE, &read, &write, &except, tvp);
|
|
|
|
leave_blocking_section();
|
|
|
|
if (retcode == -1) {
|
|
|
|
win32_maperr(WSAGetLastError());
|
|
|
|
uerror("select", Nothing);
|
|
|
|
}
|
|
|
|
read_list = fdset_to_fdlist(readfds, &read);
|
|
|
|
write_list = fdset_to_fdlist(writefds, &write);
|
|
|
|
except_list = fdset_to_fdlist(exceptfds, &except);
|
2001-12-03 02:33:39 -08:00
|
|
|
}
|
1998-10-26 11:19:32 -08:00
|
|
|
res = alloc_small(3, 0);
|
1997-09-04 06:45:56 -07:00
|
|
|
Field(res, 0) = read_list;
|
|
|
|
Field(res, 1) = write_list;
|
|
|
|
Field(res, 2) = except_list;
|
|
|
|
End_roots();
|
|
|
|
End_roots();
|
|
|
|
return res;
|
|
|
|
}
|