From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.1 required=5.0 tests=AWL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id 25769BC0A for ; Tue, 17 Apr 2007 14:03:13 +0200 (CEST) Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.187]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l3HC3CWq032713 for ; Tue, 17 Apr 2007 14:03:12 +0200 Received: from [84.59.118.255] (helo=gate.lan.gerd-stolpmann.de) by mrelayeu.kundenserver.de (node=mrelayeu7) with ESMTP (Nemesis), id 0ML2xA-1HdmOm3VeZ-0007bO; Tue, 17 Apr 2007 14:03:05 +0200 Received: from flakew.lan.gerd-stolpmann.de (fw.lan.gerd-stolpmann.de [192.168.1.1]) by gate.lan.gerd-stolpmann.de (Postfix) with ESMTP id 436F1C178; Tue, 17 Apr 2007 14:03:04 +0200 (CEST) Subject: Re: [Caml-list] Interfacing C-code with ocaml and Exception handling From: Gerd Stolpmann To: Christian Sternagel Cc: caml-list@yquem.inria.fr In-Reply-To: <20070417113557.GC3368@pc6197-c703.uibk.ac.at> References: <20070417113557.GC3368@pc6197-c703.uibk.ac.at> Content-Type: text/plain Date: Tue, 17 Apr 2007 14:02:58 +0200 Message-Id: <1176811379.2316.13.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Evolution 2.6.1 Content-Transfer-Encoding: 7bit X-Provags-ID: V01U2FsdGVkX186J/hrBA2lYpBZNjEF0+8f91WpsAfPcWGJ9zs XRtYanPCv/+5AfN2jmvF4AU00OWmZ5nRhBIca2Ae4DRqmRrZU5 hY73RQv4Exfpx9smLhhMbmFWeK1nTLX X-Miltered: at discorde with ID 4624B780.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; interfacing:01 ocaml:01 gerd:01 stolpmann:01 ocaml:01 concretely:01 implements:01 sig:01 sig:01 stub:01 o'caml:01 stub:01 gerd:01 beginner's:01 bug:01 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 ------------------------------------------------------------