caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gerd Stolpmann <info@gerd-stolpmann.de>
To: Samuel Mimram <smimram@gmail.com>
Cc: caml-list@inria.fr
Subject: AW: [Caml-list] Portable timeout function
Date: Wed, 23 Jan 2013 16:03:25 +0100	[thread overview]
Message-ID: <1358953405.30715.2@samsung> (raw)
In-Reply-To: <CA+7PP=Fd3Am2ZF5KPy+Bd7wWqnJgqdd86WG4ajPvGd8oD1G-NA@mail.gmail.com> (from smimram@gmail.com on Tue Jan 22 19:57:15 2013)

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
------------------------------------------------------------

  parent reply	other threads:[~2013-01-23 15:03 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-22 18:57 Samuel Mimram
2013-01-22 19:21 ` Daniel Bünzli
2013-01-23 15:03 ` Gerd Stolpmann [this message]
2013-01-27 16:23   ` Samuel Mimram

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1358953405.30715.2@samsung \
    --to=info@gerd-stolpmann.de \
    --cc=caml-list@inria.fr \
    --cc=smimram@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).