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 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 8B388BC0B for ; Thu, 25 Jan 2007 12:52:41 +0100 (CET) Received: from orion.metastack.com (no-dns-yet.demon.co.uk [80.177.38.218] (may be forged)) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id l0PBqfhV001706 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL) for ; Thu, 25 Jan 2007 12:52:44 +0100 Received: from treble (cpc2-cmbg6-0-0-cust535.cmbg.cable.ntl.com [81.107.34.24]) (authenticated bits=0) by orion.metastack.com (8.13.4/8.13.3) with ESMTP id l0PB9vZO017119 (version=TLSv1/SSLv3 cipher=RC4-MD5 bits=128 verify=NO) for ; Thu, 25 Jan 2007 11:09:58 GMT From: "David Allsopp" To: "OCaml List" Subject: Win32 Services & O'Caml Date: Thu, 25 Jan 2007 11:53:17 -0000 Organization: MetaStack Solutions Ltd. Message-ID: <003501c74077$6785db20$6a7ba8c0@treble> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook 11 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3028 Thread-Index: AcdAd2cJKZqaQ1mYTGekZ1yfoL7Wag== X-Miltered: at concorde with ID 45B89A09.002 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; o'caml:01 o'caml:01 invokes:01 invokes:01 callbacks:01 model:01 threading:01 threads:01 xavier's:01 callbacks:01 makefile:01 forks:01 stdin:01 stdout:01 scm:98 Yesterday I completed a module that allows construction of Win32 Services in O'Caml. Non-threaded O'Caml apps seem to be working just fine but I've hit a brick wall with multi-threaded applications. Win32 Services are started by the SCM and so, with the code I've set-up, we get the following O'Caml sequence of execution: O'Caml code invokes a C-stub which calls Win32 StartServiceCtrlDispatcher, Win32 then invokes the C-function ServiceMain which executes an O'Caml callback for the O'Caml version of ServiceMain. ServiceMain (in C) also registers a C handler function which can also make callbacks to another function in O'Caml. This model is working perfectly with single-threaded apps. However, as soon as I enable threading, I get problems (principally associated with I/O). Experiments so far have shown that the only way I can get close to anything is to have the O'Caml ServiceMain spawn a thread that runs the service --- however, in order to get this working I have to keep cycling the pause/continue controls on the service for the thread to get a little bit of time. Once the O'Caml main function returns, the C ServiceMain waits on an Event --- even putting caml_enter_blocking_section around it doesn't seem to allow other O'Caml threads to execute. I'm aware of Xavier's comments in http://tinyurl.com/2suroy ... is that the problem in this situation? As far as I can see the main thread is still an O'Caml thread. Interestingly, the I/O functions that cause problems are all ones which themselves use caml_enter_blocking_section - I can, for example, use Unix.unlink in the O'Caml ServiceMain without any problems. Interestingly, with all these problems on the "main" thread, the handler function and its associated callbacks are always working fine (so I get a service that does nothing because the main thread seems to have crashed but that I can still control the service and bring it down successfully [albeit with sporadic GPFs]) The code is at http://tinyurl.com/2wvd2x ... the Makefile contains the command to build it. Microsoft's SC utility is the easiest way of installing the service by running sc create Testing binPath= {full path to exe} You can then either control the service using Services.msc or via the sc start start/stop/pause/continue commands. Any comments much appreciated --- my workaround for now is to use a single-threaded service that forks a multi-threaded process and controls that via stdin/stdout but I'd be interested to know whether there's something blindingly wrong in the C file that would prevent the need for forking a separate process or whether it just can't be done in O'Caml (at the moment!). TIA, David