caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Portable timeout function
@ 2013-01-22 18:57 Samuel Mimram
  2013-01-22 19:21 ` Daniel Bünzli
  2013-01-23 15:03 ` AW: " Gerd Stolpmann
  0 siblings, 2 replies; 4+ messages in thread
From: Samuel Mimram @ 2013-01-22 18:57 UTC (permalink / raw)
  To: caml-list

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

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

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

Thanks!

Regards,

Samuel.

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Portable timeout function
  2013-01-22 18:57 [Caml-list] Portable timeout function Samuel Mimram
@ 2013-01-22 19:21 ` Daniel Bünzli
  2013-01-23 15:03 ` AW: " Gerd Stolpmann
  1 sibling, 0 replies; 4+ messages in thread
From: Daniel Bünzli @ 2013-01-22 19:21 UTC (permalink / raw)
  To: Samuel Mimram; +Cc: caml-list

This looks very similar to a question I asked a few years ago. You can read these threads [1,2]. 

I'm afraid but I think there's little hope that you'll find what you are looking for (but I'd love the proven wrong). 

Best,

Daniel

[1] https://groups.google.com/d/topic/fa.caml/lga2GNq3n7U/discussion
[2] https://groups.google.com/forum/?fromgroups=#!topic/fa.caml/rwndTEzAWvE



^ permalink raw reply	[flat|nested] 4+ messages in thread

* AW: [Caml-list] Portable timeout function
  2013-01-22 18:57 [Caml-list] Portable timeout function Samuel Mimram
  2013-01-22 19:21 ` Daniel Bünzli
@ 2013-01-23 15:03 ` Gerd Stolpmann
  2013-01-27 16:23   ` Samuel Mimram
  1 sibling, 1 reply; 4+ messages in thread
From: Gerd Stolpmann @ 2013-01-23 15:03 UTC (permalink / raw)
  To: Samuel Mimram; +Cc: caml-list

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Portable timeout function
  2013-01-23 15:03 ` AW: " Gerd Stolpmann
@ 2013-01-27 16:23   ` Samuel Mimram
  0 siblings, 0 replies; 4+ messages in thread
From: Samuel Mimram @ 2013-01-27 16:23 UTC (permalink / raw)
  To: caml-list

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2013-01-27 16:23 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-01-22 18:57 [Caml-list] Portable timeout function 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 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).