From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by sympa.inria.fr (Postfix) with ESMTPS id E22777EEAF for ; Sun, 27 Jan 2013 17:23:49 +0100 (CET) Received-SPF: None (mail1-smtp-roc.national.inria.fr: no sender authenticity information available from domain of smimram@gmail.com) identity=pra; client-ip=209.85.212.43; receiver=mail1-smtp-roc.national.inria.fr; envelope-from="smimram@gmail.com"; x-sender="smimram@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail1-smtp-roc.national.inria.fr: domain of smimram@gmail.com designates 209.85.212.43 as permitted sender) identity=mailfrom; client-ip=209.85.212.43; receiver=mail1-smtp-roc.national.inria.fr; envelope-from="smimram@gmail.com"; x-sender="smimram@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail1-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-vb0-f43.google.com) identity=helo; client-ip=209.85.212.43; receiver=mail1-smtp-roc.national.inria.fr; envelope-from="smimram@gmail.com"; x-sender="postmaster@mail-vb0-f43.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ap0CAA1UBVHRVdQrk2dsb2JhbAArGoZGpTyJQAGJFQgWDgEBAQEJCQsJFAQjgh4BAQQBIwQZARsSDAMBCwYDAgsaHQICIgERAQUBChIZCQkJh2MBAwkGDCyRN48Zi2RPgnuDYwoZJwMKWYh2AQUMjQiCeYETA5YNgRyNSxYpglGBSYFl X-IronPort-AV: E=Sophos;i="4.84,547,1355094000"; d="scan'208";a="191583524" Received: from mail-vb0-f43.google.com ([209.85.212.43]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 27 Jan 2013 17:23:48 +0100 Received: by mail-vb0-f43.google.com with SMTP id fr13so1379336vbb.2 for ; Sun, 27 Jan 2013 08:23:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:content-type; bh=smWRc7XKt4ItA1/0NKxBDlZpzRJH93MNyKF6M8IlrPk=; b=jx++VAkjqkIuqVacHz3oNE4Ih1tiTS8LCqjn4/p3hC9Js95pugApheDP+jC3fcVARj Dr0HcQ+mZ3VBEO+FhSLuiS0JbH2/mUYtVx09icubElCWM+rZNUJvsgZlFstccFo7HwJs DrSmSy5Rop9R69P5WS8qKjn3sQPkI3iiiQh70zfm5lUFaellhatFbUb607KORNtLeH9Z FVrcxkzgLROCi8fFqvwjgW0qr7n7DwuJlHqpyZkipYDeTadgpnXGnG9ovpI471ggmkIh Qyka+89k+TfatCyhfEN1t5ZACtTdMqLsU+daBU2DBbvEz+gPViZde5YBF91hArZARlYo vH0Q== X-Received: by 10.220.219.204 with SMTP id hv12mr12044992vcb.71.1359303827843; Sun, 27 Jan 2013 08:23:47 -0800 (PST) MIME-Version: 1.0 Received: by 10.58.247.137 with HTTP; Sun, 27 Jan 2013 08:23:26 -0800 (PST) In-Reply-To: <1358953405.30715.2@samsung> References: <1358953405.30715.2@samsung> From: Samuel Mimram Date: Sun, 27 Jan 2013 17:23:26 +0100 Message-ID: To: caml-list@inria.fr Content-Type: multipart/alternative; boundary=14dae9cfcea4cce90704d44795d1 X-Validation-by: smimram@gmail.com Subject: Re: [Caml-list] Portable timeout function --14dae9cfcea4cce90704d44795d1 Content-Type: text/plain; charset=UTF-8 Thanks for all your answers! I really appreciated the Gc.create_alarm hack, which does the job alright for now, but is quite imprecise. I have submitted a feature request for addition to the standard library [1], we'll see... Cheers, Samuel. [1] http://caml.inria.fr/mantis/view.php?id=5908 On Wed, Jan 23, 2013 at 4:03 PM, Gerd Stolpmann wrote: > Am 22.01.2013 19:57:15 schrieb(en) Samuel Mimram: > > Hi, >> >> I would like to implement a "timeout" function of type: >> >> float -> ('a -> 'b) -> 'a -> 'b option >> >> which takes a maximum number n of seconds to run, a function f, an >> argument >> x, and returns Some (f x) if the computation ends before n seconds and >> None >> otherwise. Of course, there is a simple implementation using >> Unix.setitimer, but apparently it does not work under windows because of >> signals implementation (and I don't have access to a windows machine...). >> Since this is a pretty standard idom I expected to find it implemented in >> some library, but could not find one. Also, I'd rather not heavily change >> the code (i.e. monadic threads are not really an option here, and a small >> function would be appreciated). >> > > I think this is not possible without changes in the OCaml runtime - what > we would need here is an emulation of signals under Windows, so that a > timer thread could be started that finally sends the signal to the compute > thread. However, such an emulation would be limited to pure computations, > and would not be able to interrupt system calls (no support from Windows). > > As long as you know that your compute functions allocate memory, it will > do garbage collections, and you could set a GC hook: > > > exception Timeout > > let timer tmo f x = > let t0 = Unix.gettimeofday() in > let alarm = ref None in > Gc.major(); > try > let al = > Gc.create_alarm > (fun () -> > let t1 = Unix.gettimeofday() in > if t1 -. t0 > tmo then raise Timeout > ) in > alarm := Some al; > let r = f x in > Gc.delete_alarm al; > alarm := None; > Some r > with Timeout -> > ( match !alarm with > | Some al -> Gc.delete_alarm al > | None -> () > ); > None > > But this does not work if the function does not allocate enough memory > (and also note that there are several race conditions in "timer"). > > > Extra points if your solution also works with js_of_ocaml! :) >> > > I don't think that there is any support in js_of_ocaml for completely > asynchronous events (i.e. something like the regular check for signals the > standard runtime does). > > Gerd > > >> Thanks! >> >> Regards, >> >> Samuel. >> >> -- >> Caml-list mailing list. Subscription management and archives: >> https://sympa.inria.fr/sympa/**arc/caml-list >> Beginner's list: http://groups.yahoo.com/group/**ocaml_beginners >> Bug reports: http://caml.inria.fr/bin/caml-**bugs >> > > > > -- > ------------------------------**------------------------------ > Gerd Stolpmann, Darmstadt, Germany gerd@gerd-stolpmann.de > Creator of GODI and camlcity.org. > Contact details: http://www.camlcity.org/**contact.html > Company homepage: http://www.gerd-stolpmann.de > ------------------------------**------------------------------ --14dae9cfcea4cce90704d44795d1 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Thanks for all your answers! I really appreciated the Gc.c= reate_alarm hack, which does the job alright for now, but is quite imprecis= e. I have submitted a feature request for addition to the standard library = [1], we'll see...

Cheers,



On Wed, Jan 23, 2013 at 4:03 PM, Gerd St= olpmann <info@gerd-stolpmann.de> wrote:
Am 22.01.2013 19:57:15 schrieb(en) Samuel Mimram:

Hi,

I would like to implement a "timeout" function of type:

float -> ('a -> 'b) -> 'a -> 'b option

which takes a maximum number n of seconds to run, a function f, an argument=
x, and returns Some (f x) if the computation ends before n seconds and None=
otherwise. Of course, there is a simple implementation using
Unix.setitimer, but apparently it does not work under windows because of
signals implementation (and I don't have access to a windows machine...= ).
Since this is a pretty standard idom I expected to find it implemented in some library, but could not find one. Also, I'd rather not heavily chan= ge
the code (i.e. monadic threads are not really an option here, and a small function would be appreciated).

I think this is not possible without changes in the OCaml runtime - what we= would need here is an emulation of signals under Windows, so that a timer = thread could be started that finally sends the signal to the compute thread= . However, such an emulation would be limited to pure computations, and wou= ld not be able to interrupt system calls (no support from Windows).

As long as you know that your compute functions allocate memory, it will do= garbage collections, and you could set a GC hook:


exception Timeout

let timer tmo f x =3D
=C2=A0 let t0 =3D Unix.gettimeofday() in
=C2=A0 let alarm =3D ref None in
=C2=A0 Gc.major();
=C2=A0 try
=C2=A0 =C2=A0 let al =3D
=C2=A0 =C2=A0 =C2=A0 Gc.create_alarm
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (fun () ->
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0let t1 =3D Unix.gettimeofday() in<= br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if t1 -. t0 > tmo then raise Ti= meout
=C2=A0 =C2=A0 =C2=A0 =C2=A0 ) in
=C2=A0 =C2=A0 alarm :=3D Some al;
=C2=A0 =C2=A0 let r =3D f x in
=C2=A0 =C2=A0 Gc.delete_alarm al;
=C2=A0 =C2=A0 alarm :=3D None;
=C2=A0 =C2=A0 Some r
=C2=A0 with Timeout ->
=C2=A0 =C2=A0 ( match !alarm with
=C2=A0 =C2=A0 =C2=A0 =C2=A0 | Some al -> Gc.delete_alarm al
=C2=A0 =C2=A0 =C2=A0 =C2=A0 | None -> ()
=C2=A0 =C2=A0 );
=C2=A0 =C2=A0 None

But this does not work if the function does not allocate enough memory (and= also note that there are several race conditions in "timer").

Extra points if your solution also works with js_of_ocaml! :)

I don't think that there is any support in js_of_ocaml for completely a= synchronous events (i.e. something like the regular check for signals the s= tandard runtime does).

Gerd


Thanks!

Regards,

Samuel.

--
Caml-list mailing list. =C2=A0Subscription management and archives:
ht= tps://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners<= /a>
Bug reports:
http://caml.inria.fr/bin/caml-bugs



--
------------------------------------------------------------
Gerd Stolpmann, Darmstadt, Germany =C2=A0 =C2=A0gerd@gerd-stolpmann.de
Creator of GODI and camlc= ity.org.
Contact details: =C2=A0 =C2=A0 =C2=A0 =C2=A0http://www.camlcity.org/contact.= html
Company homepage: =C2=A0 =C2=A0 =C2=A0 http://www.gerd-stolpmann.de
------------------------------------------------------------
<= /span>

--14dae9cfcea4cce90704d44795d1--