From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: Stephen Brackin <stephen.brackin@verizon.net>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Ask-if-continue wrapper?
Date: Wed, 28 Dec 2005 19:34:05 +0100 [thread overview]
Message-ID: <43B2DA9D.9090907@inria.fr> (raw)
In-Reply-To: <0IS700AHX9M9TKT2@vms042.mailsrvcs.net>
> I'd like an OCaml function, which I'll call continueq, with the property
> that for any function f with argument(s) fargs,
>
> continueq f fargs tsecs defaultval
>
> starts evaluating f on fargs and lets this evaluation proceed for up to
> tsecs seconds. If the computation of (f fargs) completes in this time,
> then it returns the result of that computation. Otherwise, it asks the
> user how many seconds to let the computation of (f fargs) proceed. If
> the user inputs a value less than or equal to 0, then it returns
> defaultval. If the user inputs a value tsecs' greater than 0, then it
> evaluates
>
> continueq f' fargs' tsecs' defaultval
>
> where (f' fargs') denotes the computation state of (f fargs) at the time
> it was interrupted.
The latter ("the computation state of ...") is not something you can
manipulate programmatically in OCaml. However, there is no need to:
Unix timer signals are sufficient to do what you want. (If you're on
Windows, there's nothing I can do for you, in this particular instance
and in general.) See below for the code.
A word of caution: if your function f performs I/O operations, be
prepared for them to fail with a EINTR Unix error. That's the Unix
(SVR4 / POSIX) way of being unhelpful...
- Xavier Leroy
----------------------------------------------------------------------
let set_timer tsecs =
ignore (Unix.setitimer Unix.ITIMER_REAL
{ Unix.it_interval = 0.0; Unix.it_value = tsecs })
exception Timeout
let handle_sigalrm signo =
print_string "Continue for how long? "; flush stdout;
let f = read_float() in
if f <= 0.0
then raise Timeout
else set_timer f
let continueq f arg tsecs defaultval =
let oldsig = Sys.signal Sys.sigalrm (Sys.Signal_handle handle_sigalrm) in
try
set_timer tsecs;
let res = f arg in
set_timer 0.0;
Sys.set_signal Sys.sigalrm oldsig;
res
with Timeout ->
Sys.set_signal Sys.sigalrm oldsig;
defaultval
prev parent reply other threads:[~2005-12-28 18:34 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-12-28 8:44 Stephen Brackin
2005-12-28 9:36 ` [Caml-list] " Jonathan Roewen
2005-12-28 9:53 ` Florian Weimer
2005-12-28 18:34 ` Xavier Leroy [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=43B2DA9D.9090907@inria.fr \
--to=xavier.leroy@inria.fr \
--cc=caml-list@yquem.inria.fr \
--cc=stephen.brackin@verizon.net \
/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