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>
|
|
|
|
#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
|
|
|
|
|
2002-01-18 07:13:26 -08:00
|
|
|
#if defined(__OpenBSD__) || defined(__MACH__)
|
1997-12-09 01:10:53 -08:00
|
|
|
#include <string.h>
|
1996-01-08 02:19:02 -08:00
|
|
|
#endif
|
1995-05-08 08:18:32 -07:00
|
|
|
|
|
|
|
typedef fd_set file_descr_set;
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static void fdlist_to_fdset(value fdlist, file_descr_set *fdset)
|
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)) {
|
1995-05-08 08:18:32 -07:00
|
|
|
FD_SET(Int_val(Field(l, 0)), fdset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static value fdset_to_fdlist(file_descr_set *fdset)
|
1995-05-08 08:18:32 -07:00
|
|
|
{
|
|
|
|
int i;
|
1997-05-26 10:16:31 -07:00
|
|
|
value res = Val_int(0);
|
|
|
|
|
|
|
|
Begin_root(res);
|
|
|
|
for (i = FD_SETSIZE - 1; i >= 0; i--) {
|
|
|
|
if (FD_ISSET(i, fdset)) {
|
1999-11-29 11:04:56 -08:00
|
|
|
value newres = alloc_small(2, 0);
|
|
|
|
Field(newres, 0) = Val_int(i);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_select(value readfds, value writefds, value exceptfds, value timeout)
|
1995-05-08 08:18:32 -07:00
|
|
|
{
|
|
|
|
file_descr_set read, write, except;
|
|
|
|
double tm;
|
|
|
|
struct timeval tv;
|
|
|
|
struct timeval * tvp;
|
|
|
|
int retcode;
|
1995-08-08 06:37:34 -07:00
|
|
|
value res;
|
1997-05-26 10:16:31 -07:00
|
|
|
value read_list = Val_unit, write_list = Val_unit, except_list = Val_unit;
|
1995-05-08 08:18:32 -07:00
|
|
|
|
1997-05-26 10:16:31 -07:00
|
|
|
Begin_roots3 (read_list, write_list, except_list);
|
|
|
|
fdlist_to_fdset(readfds, &read);
|
|
|
|
fdlist_to_fdset(writefds, &write);
|
|
|
|
fdlist_to_fdset(exceptfds, &except);
|
|
|
|
tm = Double_val(timeout);
|
|
|
|
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) uerror("select", Nothing);
|
|
|
|
read_list = fdset_to_fdlist(&read);
|
|
|
|
write_list = fdset_to_fdlist(&write);
|
|
|
|
except_list = fdset_to_fdlist(&except);
|
1998-10-26 11:19:32 -08:00
|
|
|
res = alloc_small(3, 0);
|
1997-05-26 10:16:31 -07:00
|
|
|
Field(res, 0) = read_list;
|
|
|
|
Field(res, 1) = write_list;
|
|
|
|
Field(res, 2) = except_list;
|
|
|
|
End_roots();
|
1995-05-08 08:18:32 -07:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2001-08-28 07:47:48 -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
|