1996-04-12 08:57:28 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
2011-07-27 07:17:02 -07:00
|
|
|
/* OCaml */
|
1996-04-12 08:57:28 -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. */
|
1996-04-12 08:57:28 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
#include <mlvalues.h>
|
|
|
|
#include <alloc.h>
|
2005-03-24 09:20:54 -08:00
|
|
|
#include <fail.h>
|
1996-04-12 08:57:28 -07:00
|
|
|
#include <memory.h>
|
1996-09-04 07:15:31 -07:00
|
|
|
#include "unixsupport.h"
|
1996-04-12 08:57:28 -07:00
|
|
|
|
|
|
|
#ifdef HAS_SETITIMER
|
|
|
|
|
2003-11-21 08:00:52 -08:00
|
|
|
#include <math.h>
|
1996-04-12 08:57:28 -07:00
|
|
|
#include <sys/time.h>
|
|
|
|
|
2003-11-21 08:00:52 -08:00
|
|
|
static void unix_set_timeval(struct timeval * tv, double d)
|
|
|
|
{
|
|
|
|
double integr, frac;
|
|
|
|
frac = modf(d, &integr);
|
|
|
|
/* Round time up so that if d is small but not 0, we end up with
|
|
|
|
a non-0 timeval. */
|
|
|
|
tv->tv_sec = integr;
|
|
|
|
tv->tv_usec = ceil(1e6 * frac);
|
|
|
|
if (tv->tv_usec >= 1000000) { tv->tv_sec++; tv->tv_usec = 0; }
|
|
|
|
}
|
1996-04-12 08:57:28 -07:00
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static value unix_convert_itimer(struct itimerval *tp)
|
1996-07-25 06:18:23 -07:00
|
|
|
{
|
2003-11-21 08:00:52 -08:00
|
|
|
#define Get_timeval(tv) (double) tv.tv_sec + (double) tv.tv_usec / 1e6
|
1998-10-26 11:19:32 -08:00
|
|
|
value res = alloc_small(Double_wosize * 2, Double_array_tag);
|
1996-07-25 06:18:23 -07:00
|
|
|
Store_double_field(res, 0, Get_timeval(tp->it_interval));
|
|
|
|
Store_double_field(res, 1, Get_timeval(tp->it_value));
|
|
|
|
return res;
|
2003-11-21 08:00:52 -08:00
|
|
|
#undef Get_timeval
|
1996-07-25 06:18:23 -07:00
|
|
|
}
|
|
|
|
|
1996-04-12 08:57:28 -07:00
|
|
|
static int itimers[3] = { ITIMER_REAL, ITIMER_VIRTUAL, ITIMER_PROF };
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_setitimer(value which, value newval)
|
1996-07-25 06:18:23 -07:00
|
|
|
{
|
|
|
|
struct itimerval new, old;
|
2003-11-21 08:00:52 -08:00
|
|
|
unix_set_timeval(&new.it_interval, Double_field(newval, 0));
|
|
|
|
unix_set_timeval(&new.it_value, Double_field(newval, 1));
|
1996-07-25 06:18:23 -07:00
|
|
|
if (setitimer(itimers[Int_val(which)], &new, &old) == -1)
|
|
|
|
uerror("setitimer", Nothing);
|
1998-06-03 05:27:25 -07:00
|
|
|
return unix_convert_itimer(&old);
|
1996-07-25 06:18:23 -07:00
|
|
|
}
|
2010-01-22 04:48:24 -08:00
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_getitimer(value which)
|
1996-04-12 08:57:28 -07:00
|
|
|
{
|
|
|
|
struct itimerval val;
|
|
|
|
if (getitimer(itimers[Int_val(which)], &val) == -1)
|
|
|
|
uerror("getitimer", Nothing);
|
|
|
|
return unix_convert_itimer(&val);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_setitimer(value which, value newval)
|
1996-07-25 06:18:23 -07:00
|
|
|
{ invalid_argument("setitimer not implemented"); }
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_getitimer(value which)
|
1996-07-25 06:18:23 -07:00
|
|
|
{ invalid_argument("getitimer not implemented"); }
|
1996-04-12 08:57:28 -07:00
|
|
|
|
|
|
|
#endif
|