Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Christian Sternagel <christian.sternagel@uibk.ac.at>
To: caml-list@yquem.inria.fr
Subject: Interfacing C-code with ocaml and Exception handling
Date: Tue, 17 Apr 2007 13:35:57 +0200	[thread overview]
Message-ID: <20070417113557.GC3368@pc6197-c703.uibk.ac.at> (raw)

I'm not sure whether this question was asked before (at least I didn't find any answer). Can it be, that raising an (OCaml)Exception during execution of
C-code (interfaced with OCaml) is simply ignored?

More concretely an example:

We have a module Timer that implements execution of functions given a certain timeout. Therefor we have the function

 Timer.run : float -> (unit -> 'a) -> 'a

where [Timer.run t f] starts a timer raising the signal SIG_ALRM after
[t] seconds and executes [f ()]. If [f] finishes in time the result of
[f ()] is returned. In the background a handler for SIG_ALRM is installed, that just raises the exception [Timer.Timeout]. The implementation of
[Timer.run] looks like this:

 let run t f =
  try
   start t;
   let result = f () in
   stop ();
   result
  with
   | e -> stop (); raise e
 ;;
 
and the handler assigned to SIG_ALRM like this

 let handler _ = raise Timeout;;

where [start] and [stop] take care of a [Unix.itimer]. The intended behaviour is that if [f ()] finishes within time the result is returned and the exception [Timeout] is raised otherwise. This works fine, as long as the used [f] is implemented in OCaml. But when [f] is just an OCaml stub for a C-function, then the handler for SIG_ALRM is called (and hence the exception Timeout is raised) but no exception [Timeout] arrives anywhere and hence the code of [f] runs as long as it needs ignoring any timeout.

My theory is that when SIG_ALRM is raised during execution of C-code, than
the [raise Timeout] statement raises an exception but as there is no context set up for exception handling this exception just gets lost.

Is this theory correct and are there any suggestions how to work around this problem (implementing timed execution of arbitrary code).

thnx in advance

christian


             reply	other threads:[~2007-04-17 11:36 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-04-17 11:35 Christian Sternagel [this message]
2007-04-17 12:02 ` [Caml-list] " Gerd Stolpmann
2007-04-17 13:56   ` Christian Sternagel
2007-04-17 14:40     ` Gerd Stolpmann

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=20070417113557.GC3368@pc6197-c703.uibk.ac.at \
    --to=christian.sternagel@uibk.ac.at \
    --cc=caml-list@yquem.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