1995-08-09 08:06:35 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Objective Caml */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* 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. */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
1995-05-08 08:18:32 -07:00
|
|
|
#include <mlvalues.h>
|
|
|
|
#include <alloc.h>
|
2005-03-24 09:20:54 -08:00
|
|
|
#include <fail.h>
|
1995-05-08 08:18:32 -07:00
|
|
|
#include <memory.h>
|
1996-07-23 01:12:41 -07:00
|
|
|
#include <signals.h>
|
1996-09-04 07:15:31 -07:00
|
|
|
#include "unixsupport.h"
|
1995-05-08 08:18:32 -07:00
|
|
|
|
|
|
|
#ifdef HAS_SELECT
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
1996-01-08 02:19:02 -08:00
|
|
|
#ifdef HAS_SYS_SELECT_H
|
|
|
|
#include <sys/select.h>
|
1997-12-09 01:10:53 -08:00
|
|
|
#endif
|
|
|
|
#include <string.h>
|
2002-01-20 08:30:56 -08:00
|
|
|
#include <unistd.h>
|
1995-05-08 08:18:32 -07:00
|
|
|
|
|
|
|
typedef fd_set file_descr_set;
|
|
|
|
|
2002-05-06 05:05:37 -07:00
|
|
|
static void fdlist_to_fdset(value fdlist, fd_set *fdset, int *maxfd)
|
1995-05-08 08:18:32 -07:00
|
|
|
{
|
|
|
|
value l;
|
|
|
|
FD_ZERO(fdset);
|
1995-06-18 07:59:32 -07:00
|
|
|
for (l = fdlist; l != Val_int(0); l = Field(l, 1)) {
|
2002-05-06 05:05:37 -07:00
|
|
|
int fd = Int_val(Field(l, 0));
|
|
|
|
FD_SET(fd, fdset);
|
|
|
|
if (fd > *maxfd) *maxfd = fd;
|
1995-05-08 08:18:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-05-06 05:05:37 -07:00
|
|
|
static value fdset_to_fdlist(value fdlist, fd_set *fdset)
|
1995-05-08 08:18:32 -07:00
|
|
|
{
|
2002-05-06 05:05:37 -07:00
|
|
|
value l;
|
1997-05-26 10:16:31 -07:00
|
|
|
value res = Val_int(0);
|
|
|
|
|
2002-05-06 05:05:37 -07:00
|
|
|
Begin_roots2(l, res);
|
|
|
|
for (l = fdlist; l != Val_int(0); l = Field(l, 1)) {
|
|
|
|
int fd = Int_val(Field(l, 0));
|
|
|
|
if (FD_ISSET(fd, fdset)) {
|
1999-11-29 11:04:56 -08:00
|
|
|
value newres = alloc_small(2, 0);
|
2002-05-06 05:05:37 -07:00
|
|
|
Field(newres, 0) = Val_int(fd);
|
1999-11-29 11:04:56 -08:00
|
|
|
Field(newres, 1) = res;
|
|
|
|
res = newres;
|
1997-05-26 10:16:31 -07:00
|
|
|
}
|
1995-05-08 08:18:32 -07:00
|
|
|
}
|
1997-05-26 10:16:31 -07:00
|
|
|
End_roots();
|
1995-05-08 08:18:32 -07:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2002-05-06 05:05:37 -07:00
|
|
|
CAMLprim value unix_select(value readfds, value writefds, value exceptfds,
|
|
|
|
value timeout)
|
1995-05-08 08:18:32 -07:00
|
|
|
{
|
2002-05-06 05:05:37 -07:00
|
|
|
fd_set read, write, except;
|
|
|
|
int maxfd;
|
1995-05-08 08:18:32 -07:00
|
|
|
double tm;
|
|
|
|
struct timeval tv;
|
|
|
|
struct timeval * tvp;
|
|
|
|
int retcode;
|
1995-08-08 06:37:34 -07:00
|
|
|
value res;
|
1995-05-08 08:18:32 -07:00
|
|
|
|
2002-05-06 05:05:37 -07:00
|
|
|
Begin_roots3 (readfds, writefds, exceptfds);
|
2002-05-07 00:37:18 -07:00
|
|
|
maxfd = -1;
|
2002-05-06 05:05:37 -07:00
|
|
|
fdlist_to_fdset(readfds, &read, &maxfd);
|
|
|
|
fdlist_to_fdset(writefds, &write, &maxfd);
|
|
|
|
fdlist_to_fdset(exceptfds, &except, &maxfd);
|
1997-05-26 10:16:31 -07:00
|
|
|
tm = Double_val(timeout);
|
|
|
|
if (tm < 0.0)
|
|
|
|
tvp = (struct timeval *) NULL;
|
|
|
|
else {
|
|
|
|
tv.tv_sec = (int) tm;
|
2002-05-06 05:05:37 -07:00
|
|
|
tv.tv_usec = (int) (1e6 * (tm - tv.tv_sec));
|
1997-05-26 10:16:31 -07:00
|
|
|
tvp = &tv;
|
|
|
|
}
|
|
|
|
enter_blocking_section();
|
2002-05-07 00:37:18 -07:00
|
|
|
retcode = select(maxfd + 1, &read, &write, &except, tvp);
|
1997-05-26 10:16:31 -07:00
|
|
|
leave_blocking_section();
|
|
|
|
if (retcode == -1) uerror("select", Nothing);
|
2002-05-06 05:05:37 -07:00
|
|
|
readfds = fdset_to_fdlist(readfds, &read);
|
|
|
|
writefds = fdset_to_fdlist(writefds, &write);
|
|
|
|
exceptfds = fdset_to_fdlist(exceptfds, &except);
|
1998-10-26 11:19:32 -08:00
|
|
|
res = alloc_small(3, 0);
|
2002-05-06 05:05:37 -07:00
|
|
|
Field(res, 0) = readfds;
|
|
|
|
Field(res, 1) = writefds;
|
|
|
|
Field(res, 2) = exceptfds;
|
1997-05-26 10:16:31 -07:00
|
|
|
End_roots();
|
1995-05-08 08:18:32 -07:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2002-05-06 05:05:37 -07:00
|
|
|
CAMLprim value unix_select(value readfds, value writefds, value exceptfds,
|
|
|
|
value timeout)
|
1997-09-02 05:55:01 -07:00
|
|
|
{ invalid_argument("select not implemented"); }
|
1995-05-08 08:18:32 -07:00
|
|
|
|
|
|
|
#endif
|