caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Interfacing C-code with ocaml and Exception handling
@ 2007-04-17 11:35 Christian Sternagel
  2007-04-17 12:02 ` [Caml-list] " Gerd Stolpmann
  0 siblings, 1 reply; 4+ messages in thread
From: Christian Sternagel @ 2007-04-17 11:35 UTC (permalink / raw)
  To: caml-list

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


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

* Re: [Caml-list] Interfacing C-code with ocaml and Exception handling
  2007-04-17 11:35 Interfacing C-code with ocaml and Exception handling Christian Sternagel
@ 2007-04-17 12:02 ` Gerd Stolpmann
  2007-04-17 13:56   ` Christian Sternagel
  0 siblings, 1 reply; 4+ messages in thread
From: Gerd Stolpmann @ 2007-04-17 12:02 UTC (permalink / raw)
  To: Christian Sternagel; +Cc: caml-list

Am Dienstag, den 17.04.2007, 13:35 +0200 schrieb Christian Sternagel:
> 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) 

Are you sure? I would be a bit surprised if the handler was actually
called. Signal handlers written in O'Caml behave differently than those
written in C: The execution is deferred until the next safe point. When
you call a C stub, this is certainly not before the stub returns.

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

No, this usually works.

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

Arbitrary code? No way to make that happen. For specific cases: yes,
there are many ways to do it.

Gerd

> 
> thnx in advance
> 
> christian
> 
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
> 
-- 
------------------------------------------------------------
Gerd Stolpmann * Viktoriastr. 45 * 64293 Darmstadt * Germany 
gerd@gerd-stolpmann.de          http://www.gerd-stolpmann.de
Phone: +49-6151-153855                  Fax: +49-6151-997714
------------------------------------------------------------


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

* Re: [Caml-list] Interfacing C-code with ocaml and Exception handling
  2007-04-17 12:02 ` [Caml-list] " Gerd Stolpmann
@ 2007-04-17 13:56   ` Christian Sternagel
  2007-04-17 14:40     ` Gerd Stolpmann
  0 siblings, 1 reply; 4+ messages in thread
From: Christian Sternagel @ 2007-04-17 13:56 UTC (permalink / raw)
  To: caml-list

On Tue, Apr 17, 2007 at 02:02:58PM +0200, Gerd Stolpmann wrote:
> Am Dienstag, den 17.04.2007, 13:35 +0200 schrieb Christian Sternagel:
> > 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) 
> 
> Are you sure? I would be a bit surprised if the handler was actually
> called. Signal handlers written in O'Caml behave differently than those
You are right, the handler is not called, my fault.
> written in C: The execution is deferred until the next safe point. When
> you call a C stub, this is certainly not before the stub returns.
> 
> > 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.
> 
> No, this usually works.
> 
> > Is this theory correct and are there any suggestions how to work around this problem (implementing timed execution of arbitrary code).
> 
> Arbitrary code? No way to make that happen. For specific cases: yes,
> there are many ways to do it.
I don't really need that for arbitraty code. Our tool calls a stub-function that executes C-code of a SAT-Solver. And I want to make sure, that this function stops after a certain timeout. Are there any suggestions?

cheers

christian
> 
> Gerd
> 
> > 
> > thnx in advance
> > 
> > christian
> > 
> > _______________________________________________
> > Caml-list mailing list. Subscription management:
> > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> > Archives: http://caml.inria.fr
> > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> > Bug reports: http://caml.inria.fr/bin/caml-bugs
> > 
> -- 
> ------------------------------------------------------------
> Gerd Stolpmann * Viktoriastr. 45 * 64293 Darmstadt * Germany 
> gerd@gerd-stolpmann.de          http://www.gerd-stolpmann.de
> Phone: +49-6151-153855                  Fax: +49-6151-997714
> ------------------------------------------------------------
> 


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

* Re: [Caml-list] Interfacing C-code with ocaml and Exception handling
  2007-04-17 13:56   ` Christian Sternagel
@ 2007-04-17 14:40     ` Gerd Stolpmann
  0 siblings, 0 replies; 4+ messages in thread
From: Gerd Stolpmann @ 2007-04-17 14:40 UTC (permalink / raw)
  To: Christian Sternagel; +Cc: caml-list

> > > Is this theory correct and are there any suggestions how to work around this problem (implementing timed execution of arbitrary code).
> > 
> > Arbitrary code? No way to make that happen. For specific cases: yes,
> > there are many ways to do it.
> I don't really need that for arbitraty code. Our tool calls a stub-function that executes C-code of a SAT-Solver. And I want to make sure, that this function stops after a certain timeout. Are there any suggestions?

Maybe this works:

- Create a new C function set_timeout_handler that defines the signal
handler. The handler is another C function timeout_handler.

- In O'Caml, only do:
  * block SIGALRM (so O'Caml never sees this signal)
  * Start the itimer

- Change the C code of the SAT solver as follows. Before it starts,
unblock SIGALRM, so this signal can be delivered. Then define an exit
with sigsetjmp.

- In timeout_handler, just siglongjump out of the solver to the
previously defined exit. SIGALRM should be blocked again, before
returning to O'Caml.

Complicated, isn't it, but if a signal is the only way to stop the
solver, the handler must be defined in C.

Gerd


> cheers
> 
> christian
> > 
> > Gerd
> > 
> > > 
> > > thnx in advance
> > > 
> > > christian
> > > 
> > > _______________________________________________
> > > Caml-list mailing list. Subscription management:
> > > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> > > Archives: http://caml.inria.fr
> > > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> > > Bug reports: http://caml.inria.fr/bin/caml-bugs
> > > 
> > -- 
> > ------------------------------------------------------------
> > Gerd Stolpmann * Viktoriastr. 45 * 64293 Darmstadt * Germany 
> > gerd@gerd-stolpmann.de          http://www.gerd-stolpmann.de
> > Phone: +49-6151-153855                  Fax: +49-6151-997714
> > ------------------------------------------------------------
> > 
> 
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
> 
-- 
------------------------------------------------------------
Gerd Stolpmann * Viktoriastr. 45 * 64293 Darmstadt * Germany 
gerd@gerd-stolpmann.de          http://www.gerd-stolpmann.de
Phone: +49-6151-153855                  Fax: +49-6151-997714
------------------------------------------------------------


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

end of thread, other threads:[~2007-04-17 14:40 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-04-17 11:35 Interfacing C-code with ocaml and Exception handling Christian Sternagel
2007-04-17 12:02 ` [Caml-list] " Gerd Stolpmann
2007-04-17 13:56   ` Christian Sternagel
2007-04-17 14:40     ` Gerd Stolpmann

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