From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 75694BC48 for ; Mon, 7 Mar 2005 17:22:11 +0100 (CET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j27GMADO013077 for ; Mon, 7 Mar 2005 17:22:11 +0100 Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id RAA00405 for ; Mon, 7 Mar 2005 17:22:10 +0100 (MET) Received: from rproxy.gmail.com (rproxy.gmail.com [64.233.170.202]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j27GM9Gr031396 for ; Mon, 7 Mar 2005 17:22:10 +0100 Received: by rproxy.gmail.com with SMTP id a36so2063798rnf for ; Mon, 07 Mar 2005 08:22:09 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:date:from:to:cc:subject:message-id:mail-followup-to:references:mime-version:content-type:content-disposition:in-reply-to:user-agent; b=gcBapn5bDqZTpqksG0rr6tsmyVzqFM2SCAQOmO3x5EnpRr9NZ6o93THkYJcW329UEZg1GcE/ZhRPoYAynOGZf0keBxGesVbi2/gEuhtYBX1QyRiUh9TYqI0ZtwHR/+vwQEyv3XkLYCVpEkAKHrAE6kDUaqo1bHXVVp474gJh7W0= Received: by 10.38.83.20 with SMTP id g20mr143935rnb; Mon, 07 Mar 2005 08:22:08 -0800 (PST) Received: from localhost ([141.155.88.179]) by smtp.gmail.com with ESMTP id 57sm654655rnc.2005.03.07.08.22.07; Mon, 07 Mar 2005 08:22:08 -0800 (PST) Date: Mon, 7 Mar 2005 11:22:06 -0500 From: Markus Mottl To: EL CHAAR RABIH Cc: OCaml Subject: Re: [Caml-list] Re: C-threads & callbacks vs camljava in a multithrea ded context Message-ID: <20050307162206.GA18087@quant3.janestcapital.quant> Mail-Followup-To: EL CHAAR RABIH , OCaml References: <71A291D8E360AF4EA6FFFAF9CAD136CA03773583@FR-MAILBOX2.fr.sgam.socgen> <71A291D8E360AF4EA6FFFAF9CAD136CA03773585@FR-MAILBOX2.fr.sgam.socgen> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <71A291D8E360AF4EA6FFFAF9CAD136CA03773583@FR-MAILBOX2.fr.sgam.socgen> <71A291D8E360AF4EA6FFFAF9CAD136CA03773585@FR-MAILBOX2.fr.sgam.socgen> User-Agent: Mutt/1.4.1i X-Miltered: at concorde with ID 422C7FB3.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 422C7FB1.002 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; markus:01 mottl:01 markus:01 mottl:01 caml-list:01 camljava:01 threads:01 threads:01 caml-list:01 camljava:01 ocaml:01 ocaml:01 mtx:01 mtx:01 ocaml-values:01 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on yquem.inria.fr X-Spam-Status: No, score=0.5 required=5.0 tests=INFO_TLD,RCVD_BY_IP autolearn=disabled version=3.0.2 X-Spam-Level: Hi, sorry for the delay, but I was extremely busy answering a huge backlog of mails recently. On Wed, 23 Feb 2005, EL CHAAR RABIH wrote: > I sent today a mail concenrning external threads (java threads) and caml. > When looking inside the caml-list, i came up upon your mail, which attracted > my attention. > > My goal is to try and switch the jvm thread into a caml thread. > > Is it possible to have your opinion upon my post "Using camljava library in > a multithreaded context"? > Is it also possible to have a small example (C and ocaml) realising this > coordination issue ? Unfortunately, I have no experience with Java so I cannot tell you whether there is a way to let Java-threads migrate to OCaml. My C-solution, which seems to work very well in practice, looks roughly as follows: pthread_mutex_t cb_act_mtx; pthread_cond_t cb_act_cnd; value *cb_act; pthread_mutex_t cb_res_mtx; pthread_cond_t cb_res_cnd; The first mutex and condition variable protect "cb_act", the callback action to be performed. If there is a C-thread that wants to call OCaml, it calls the following function/method (it's actually C++): void do_cb(value *action) { pthread_mutex_lock(&cb_act_mtx); cb_act = action; pthread_cond_signal(&cb_act_cnd); pthread_mutex_unlock(&cb_act_mtx); pthread_mutex_lock(&cb_res_mtx); while (not cb_res_avail) pthread_cond_wait(&cb_res_cnd, &cb_res_mtx); pthread_mutex_unlock(&cb_res_mtx); cb_res_avail = false; } Depending on the kind of action that should be performed, an OCaml-thread that has previously gone into C-land will eventually convert C-data to OCaml-values before performing the callback. After returning it will signal the C-thread that a result is available (or simply that it has completed the job). The OCaml-thread has to perform all the conversions, because the C-thread must not call the GC. Our C-function, in which the OCaml-thread waits for callbacks + associated data looks as follows: virtual value handle_cbs() { while (true) { caml_enter_blocking_section(); pthread_mutex_lock(&cb_act_mtx); while (cb_act == NULL) pthread_cond_wait(&cb_act_cnd, &cb_act_mtx); pthread_mutex_unlock(&cb_act_mtx); caml_leave_blocking_section(); if (cb_act == &v_OnRecordUpdateCallback) caml_callback(*cb_act, vrc_make(rup, free_record_update, lev1_GC_setting)); else if (cb_act == &v_Terminate) {} else cb_sc = caml_callback(*cb_act, Val_int(cb_sc)); pthread_mutex_lock(&cb_res_mtx); cb_act = NULL; cb_res_avail = true; pthread_cond_signal(&cb_res_cnd); pthread_mutex_unlock(&cb_res_mtx); if (cb_act == &v_Terminate) return Val_unit; }; return Val_unit; // we should never get here } In OCaml you just need to specify the external function that the OCaml-thread should call: external handle_cbs : t -> unit = "activ_handle_cbs_stub" Then do something like the following at startup: let wrap_handle_cbs () = try handle_cbs cgc with exc -> (match exc with | Status sc -> eprintf "Activ-exception: Status %s\n" (Status_code.to_string sc) | FieldStatus fs -> eprintf "Activ-exception: FieldStatus %s\n" (Field_status.to_string fs) | _ -> eprintf "Exception escaping callback: %s\n" (Printexc.to_string exc) ); exit 2 in ignore (Thread.create wrap_handle_cbs ()); I hope that this info is enough so that you can make OCaml- and C-threads interact smoothly. Regards, Markus -- Markus Mottl markus.mottl@gmail.com http://www.ocaml.info