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.0 required=5.0 tests=none autolearn=disabled version=3.1.3 Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id 219C3BC0A for ; Tue, 17 Apr 2007 13:36:00 +0200 (CEST) Received: from smtp.uibk.ac.at (lmr1.uibk.ac.at [138.232.1.142]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l3HBZxix026575 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Tue, 17 Apr 2007 13:35:59 +0200 Received: from smc.uibk.ac.at (smc.uibk.ac.at [138.232.1.226] c703350@smc.uibk.ac.at) by smtp.uibk.ac.at (8.13.8/8.13.1/F1) with ESMTP id l3HBZwxx000630 for ; Tue, 17 Apr 2007 13:35:58 +0200 Received: from smc.uibk.ac.at (localhost [127.0.0.1] c703350@smc.uibk.ac.at) by smc.uibk.ac.at (8.13.8+Sun/uibk) with ESMTP id l3HBZwrT013981 for ; Tue, 17 Apr 2007 13:35:58 +0200 (MEST) Received: (from c703350@localhost) by smc.uibk.ac.at (8.13.8+Sun/8.13.8/Submit) id l3HBZwP8013979 for caml-list@yquem.inria.fr; Tue, 17 Apr 2007 13:35:58 +0200 (MEST) Date: Tue, 17 Apr 2007 13:35:57 +0200 From: Christian Sternagel To: caml-list@yquem.inria.fr Subject: Interfacing C-code with ocaml and Exception handling Message-ID: <20070417113557.GC3368@pc6197-c703.uibk.ac.at> Mail-Followup-To: caml-list@yquem.inria.fr Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.4.2.2i X-Scanned-By: MIMEDefang 2.58 at uibk.ac.at on 138.232.1.140 X-Miltered: at discorde with ID 4624B11F.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; interfacing:01 ocaml:01 ocaml:01 concretely:01 implements:01 sig:01 sig:01 stub:01 unix:01 arbitrary:01 exception:01 exception:01 behaviour:01 executes:01 functions:01 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