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=AWL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id EFE0ABBAF for ; Sat, 11 Jul 2009 13:39:31 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgICAO8WWEpRZ90xkWdsb2JhbACZJgEBAQEJCwoHEwOzIYI5gVAF X-IronPort-AV: E=Sophos;i="4.42,383,1243807200"; d="scan'208";a="29659500" Received: from mtaout03-winn.ispmail.ntl.com ([81.103.221.49]) by mail2-smtp-roc.national.inria.fr with ESMTP; 11 Jul 2009 13:39:31 +0200 Received: from aamtaout04-winn.ispmail.ntl.com ([81.103.221.35]) by mtaout03-winn.ispmail.ntl.com (InterMail vM.7.08.04.00 201-2186-134-20080326) with ESMTP id <20090711113931.VDYQ5579.mtaout03-winn.ispmail.ntl.com@aamtaout04-winn.ispmail.ntl.com> for ; Sat, 11 Jul 2009 12:39:31 +0100 Received: from romulus.metastack.com ([81.102.132.77]) by aamtaout04-winn.ispmail.ntl.com (InterMail vG.2.02.00.01 201-2161-120-102-20060912) with ESMTP id <20090711113930.PRL22934.aamtaout04-winn.ispmail.ntl.com@romulus.metastack.com> for ; Sat, 11 Jul 2009 12:39:30 +0100 Received: from Tenor ([212.183.134.128]) (authenticated bits=0) by romulus.metastack.com (8.14.2/8.14.2) with ESMTP id n6BBdP76030597 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NO) for ; Sat, 11 Jul 2009 12:39:27 +0100 From: "David Allsopp" To: References: <000d01ca014f$e119a980$a34cfc80$@com> <20090711105403.8bb1e112.ygrekheretix@gmail.com> In-Reply-To: <20090711105403.8bb1e112.ygrekheretix@gmail.com> Subject: RE: [Caml-list] Callbacks from C to OCaml Date: Sat, 11 Jul 2009 12:39:23 +0100 Message-ID: <005b01ca021c$3e7f9950$bb7ecbf0$@metastack.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook 12.0 Thread-Index: AcoB/MdE0wJHYaoHSD25FfFM85rAvwAHYh0A Content-Language: en-gb Organization: MetaStack Solutions Ltd. X-Scanned-By: MIMEDefang 2.65 on 81.102.132.77 X-Cloudmark-Analysis: v=1.0 c=1 a=9KcXRCvU3UIA:10 a=SGJpDYsEAAAA:8 a=ZOzjf2MOAAAA:8 a=bqJmYc2s8LyXVap8QvAA:9 a=tq_5HCee-SSpJXKh8qsA:7 a=wRYNixXgbMvLShcNNX0NXCI3lDgA:4 a=dTth3pfvbawA:10 a=NDhzgQLg9ypzJmPl:21 a=dxhSEjdBMvImbizN:21 X-Spam: no; 0.00; callbacks:01 ocaml:01 ocaml:01 ocaml's:01 threading:01 runtime:01 off-list:01 model:01 callbacks:01 runtime:01 2009:98 scm:98 scm:98 invoke:01 threads:01 ygrek wrote: > On Fri, 10 Jul 2009 12:16:17 +0100 > "David Allsopp" wrote: > > > Now, at any time, the SCM may invoke the ServiceCtrlHandler function > > registered with it. This also needs to callback to an OCaml function > > to work out what to do (the closure was registered in the initial call). > > > > What happens at this point with regard to OCaml's global lock and > > being in the correct threading context? I must confess that I don't > > fully understand how the callback works at all in the single-threaded > > context - presumably when SCM calls the function, it simply executes > > code in its own thread context (so it can presumably introduce a form > > of multi-threading to a program which isn't expecting it?). > > Yes, SCM creates new thread for the callback. That's handy to know - I was going to do some further experimentation in C to check that so that hassle is saved, thanks! > And this thread is not registered with OCaml runtime, and so you can't > do any allocation on it. I used windows services in the same setup and > just set a boolean flag when SCM signalled service to stop, while > periodically checking this flag in ocaml thread.. Following this, and an off-list exchange with Philippe Wang, I'd arrived at the same conclusion. I'd tried to model the whole procedure essentially on how'd you'd write it in C with callbacks and that was clearly the wrong decision! > See also http://caml.inria.fr/mantis/view.php?id=4702 Presumably if this patch is eventually incorporated, that would allow the C version of ServiceCtrlHandler to register its thread with the OCaml runtime and therefore safely callback into OCaml code? I shall watch the issue - although having to create a couple of message-passing helper threads to accomplish this task is hardly going to slow anything down. > Hope it helps. Thank you! It's good to know for definite that I've been barking up the wrong tree - especially as it tantalising almost worked. David