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 mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id 46CF37EEAF for ; Wed, 23 Jan 2013 16:03:29 +0100 (CET) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of info@gerd-stolpmann.de) identity=pra; client-ip=212.227.17.10; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="info@gerd-stolpmann.de"; x-sender="info@gerd-stolpmann.de"; x-conformance=sidf_compatible Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of info@gerd-stolpmann.de) identity=mailfrom; client-ip=212.227.17.10; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="info@gerd-stolpmann.de"; x-sender="info@gerd-stolpmann.de"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of postmaster@moutng.kundenserver.de designates 212.227.17.10 as permitted sender) identity=helo; client-ip=212.227.17.10; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="info@gerd-stolpmann.de"; x-sender="postmaster@moutng.kundenserver.de"; x-conformance=sidf_compatible; x-record-type="v=spf1" X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AqUAAEL5/1DU4xEKjmdsb2JhbABEq1KSWRYOAQEBAQkLCQkSBSSCHgEBBAEnEzQLBSQXXQkSBhMJCQmHbAMJCgizUwOJXxWMcIQuA41miUKSI4Fl X-IPAS-Result: AqUAAEL5/1DU4xEKjmdsb2JhbABEq1KSWRYOAQEBAQkLCQkSBSSCHgEBBAEnEzQLBSQXXQkSBhMJCQmHbAMJCgizUwOJXxWMcIQuA41miUKSI4Fl X-IronPort-AV: E=Sophos;i="4.84,523,1355094000"; d="scan'208";a="31881" Received: from moutng.kundenserver.de ([212.227.17.10]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 23 Jan 2013 16:03:28 +0100 Received: from office1.lan.sumadev.de (dslb-094-219-211-172.pools.arcor-ip.net [94.219.211.172]) by mrelayeu.kundenserver.de (node=mreu2) with ESMTP (Nemesis) id 0Ls5Ln-1Su3l33oY3-013UHB; Wed, 23 Jan 2013 16:03:27 +0100 Received: from samsung (ip-5-146-55-186.unitymediagroup.de [5.146.55.186]) by office1.lan.sumadev.de (Postfix) with ESMTPSA id 7A74DC00D0; Wed, 23 Jan 2013 16:03:26 +0100 (CET) Date: Wed, 23 Jan 2013 16:03:25 +0100 From: Gerd Stolpmann To: Samuel Mimram Cc: caml-list@inria.fr In-Reply-To: (from smimram@gmail.com on Tue Jan 22 19:57:15 2013) X-Mailer: Balsa 2.4.11 Message-Id: <1358953405.30715.2@samsung> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; DelSp=Yes; Format=Flowed Content-Disposition: inline Content-Transfer-Encoding: quoted-printable X-Provags-ID: V02:K0:HgZX0cp/qIFGr1kCW706Jkok4/ijZyh7u10tFJv3s4k JZ450l3rjEfrOgDF/7Kpu5HqQiwVLy3G8Hj8w6WB9FTFf1l3qz /hDNSPjzoHsu/1yKLLW1p87es12RMxSgOmYLq45MDtue68cAN9 Hi2vDASZC1Js/KUxWNUGLVq4CMC4DHSa6FnK2yZ0S1m7Dx9os8 F5MjCqpPU+lkzWT0Ki7fzsGAKJ+LS1P4GxsL7UJqDzC0Ks90PL mQ7KDwdJClxG3BnuSGk7h061fGnks3xiKzF5AVW4SEWzcU6suI xEkto63oKThl6vmYG90xpvrl5S1/Er+S0QRn66meihDkgPCgaR fnmWRav9w7kWkAKSW4GI= Subject: AW: [Caml-list] Portable timeout function Am 22.01.2013 19:57:15 schrieb(en) Samuel Mimram: > Hi, >=20 > I would like to implement a "timeout" function of type: >=20 > float -> ('a -> 'b) -> 'a -> 'b option >=20 > which takes a maximum number n of seconds to run, a function f, an=20=20 > argument > x, and returns Some (f x) if the computation ends before n seconds=20=20 > and None > otherwise. Of course, there is a simple implementation using > Unix.setitimer, but apparently it does not work under windows because=20= =20 > of > signals implementation (and I don't have access to a windows=20=20 > machine...). > Since this is a pretty standard idom I expected to find it=20=20 > implemented in > some library, but could not find one. Also, I'd rather not heavily=20=20 > change > the code (i.e. monadic threads are not really an option here, and a=20=20 > small > function would be appreciated). I think this is not possible without changes in the OCaml runtime -=20=20 what we would need here is an emulation of signals under Windows, so=20=20 that a timer thread could be started that finally sends the signal to=20=20 the compute thread. However, such an emulation would be limited to pure=20= =20 computations, and would not be able to interrupt system calls (no=20=20 support from Windows). As long as you know that your compute functions allocate memory, it=20=20 will do garbage collections, and you could set a GC hook: exception Timeout let timer tmo f x =3D let t0 =3D Unix.gettimeofday() in let alarm =3D ref None in Gc.major(); try let al =3D Gc.create_alarm (fun () -> let t1 =3D Unix.gettimeofday() in if t1 -. t0 > tmo then raise Timeout ) in alarm :=3D Some al; let r =3D f x in Gc.delete_alarm al; alarm :=3D 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=20= =20 (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=20=20 asynchronous events (i.e. something like the regular check for signals=20= =20 the standard runtime does). Gerd >=20 > Thanks! >=20 > Regards, >=20 > Samuel. >=20 > -- > 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 --=20 ------------------------------------------------------------ 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 ------------------------------------------------------------=