caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Samuel Mimram <smimram@gmail.com>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Portable timeout function
Date: Sun, 27 Jan 2013 17:23:26 +0100	[thread overview]
Message-ID: <CA+7PP=Etrpj6Ppg=0jPupxu2JPyhjG+AgQs3FV6ELgh_i-mGMQ@mail.gmail.com> (raw)
In-Reply-To: <1358953405.30715.2@samsung>

[-- Attachment #1: Type: text/plain, Size: 3469 bytes --]

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 <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 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<https://sympa.inria.fr/sympa/arc/caml-list>
>> Beginner's list: http://groups.yahoo.com/group/**ocaml_beginners<http://groups.yahoo.com/group/ocaml_beginners>
>> Bug reports: http://caml.inria.fr/bin/caml-**bugs<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<http://www.camlcity.org/contact.html>
> Company homepage:       http://www.gerd-stolpmann.de
> ------------------------------**------------------------------

[-- Attachment #2: Type: text/html, Size: 5087 bytes --]

      reply	other threads:[~2013-01-27 16:23 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 ` AW: " Gerd Stolpmann
2013-01-27 16:23   ` Samuel Mimram [this message]

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='CA+7PP=Etrpj6Ppg=0jPupxu2JPyhjG+AgQs3FV6ELgh_i-mGMQ@mail.gmail.com' \
    --to=smimram@gmail.com \
    --cc=caml-list@inria.fr \
    /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).