From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p21CEGwg030757 for ; Tue, 1 Mar 2011 13:14:20 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AqgCAKZxbE3RVdi2kGdsb2JhbACYNY4OCBUBAQEBCQkMBxEEIaA4iiCCHYU9L4hbAQEDBYVcBIUMhxOGVIF9Og X-IronPort-AV: E=Sophos;i="4.62,246,1297033200"; d="scan'208";a="92326730" Received: from mail-qy0-f182.google.com ([209.85.216.182]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 01 Mar 2011 13:14:20 +0100 Received: by qyk27 with SMTP id 27so4406264qyk.6 for ; Tue, 01 Mar 2011 04:14:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type:content-transfer-encoding; bh=iUqDetpyIhROoBlVUx/xdRE0z9gZGuKkmY9HpLGOUyQ=; b=i5FsSUMPkEIHJo7Ml/ax3ktexaRjAIVwcoPBBuqgpcrMg0/DD+esbfQp5CWKec3Vsl cGB8yX/j4cibEyYfBBKI++mPOAqgjOPVwoqS2N0MfKzH0H/IkkWwMN7KYDc4g8jqRihu csCrQvDnfyd3JquU76izsyrW0GzkzaCjOkMVY= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=qhxDrXNjKuMC7Rlp511PPsWJ5Jy+/MfWeohLfdLPC8CJJPTuXxpHjt/7TXWXOH62sH PmX0Skaf5yX/8vc3WQ5NSCJW7Vl+ohJU/2tpHM+8Jd7wJ1oqE/s0V07v7jvnhWxRHEC1 CmrVyk53bRTH3DuERtpQ5msr3rpU6Ugjzo6AI= MIME-Version: 1.0 Received: by 10.229.246.209 with SMTP id lz17mr5084954qcb.271.1298981659196; Tue, 01 Mar 2011 04:14:19 -0800 (PST) Received: by 10.229.96.136 with HTTP; Tue, 1 Mar 2011 04:14:19 -0800 (PST) In-Reply-To: References: Date: Tue, 1 Mar 2011 15:14:19 +0300 Message-ID: From: Dmitry Bely To: caml-list@inria.fr Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by walapai.inria.fr id p21CEGwg030757 Subject: Re: [Caml-list] Using C threads On Tue, Mar 1, 2011 at 12:56 AM, Niki Yoshiuchi wrote: > I'm trying to call some OCaml code from a thread created in C.  I know > I'm supposed to call caml_c_thread_{un}register, however this pretty > much guarantees a segfault.  If I leave them out, my code works fine > so long as I don't try and do much of anything.  Here's my code > stripped down to the bare minimum: > > C code: > > void *my_thread(void *ptr) > { >    caml_c_thread_register(); >    for(;;) >    { >        caml_acquire_runtime_system(); >        caml_callback(*caml_named_value("my_callback"), Val_unit); >        caml_release_runtime_system(); >    } >    caml_c_thread_unregister(); // I realize this will never be > called.  I've tried without the for loop as well, same result > } > > CAMLprim value caml_create_callback(value unit) > { >    pthread_t thread1; >    pthread_create(&thread1, NULL, my_thread, NULL); >    return Val_unit; > } > > OCaml code: > > external create_callback : unit -> unit = "caml_create_callback" > > let my_thread () = >    () > > let _ = >    Callback.register "my_callback" random_thread; >    create_callback (); >    while true do >        () >    done > > This code will segfault immediately.  If I remove the thread_register > functions it will run as expected.  If I try and do anything more than > just return unit, it will segfault unless I add in some sleep > statements.  I'm assuming that the sleep function acts as a sort of > poor-man's thread synchronizer.  I've tried endless permutations Maybe you are trying to call caml_c_thread_register() inside a thread created by Caml runtime? Otherwise I cannot imagine how caml_callback() could ever succeed without registering C-thread first. - Dmitry Bely