caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Dmitry Bely <dmitry.bely@gmail.com>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Using C threads
Date: Tue, 1 Mar 2011 15:14:19 +0300	[thread overview]
Message-ID: <AANLkTi=XJ=ekYQ6qJ5eE+ce_Wdk9MYph5pVWAvFEHy86@mail.gmail.com> (raw)
In-Reply-To: <AANLkTim-aovgVd4uKvjDbq8J-8VeTh=S5CLYz3QcEMxL@mail.gmail.com>

On Tue, Mar 1, 2011 at 12:56 AM, Niki Yoshiuchi <aplusbi@gmail.com> 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


  reply	other threads:[~2011-03-01 12:14 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-02-28 21:56 Niki Yoshiuchi
2011-03-01 12:14 ` Dmitry Bely [this message]
2011-03-03 22:16   ` Niki Yoshiuchi
2011-03-03 23:03     ` Dmitry Bely
2011-03-03 23:23       ` Niki Yoshiuchi
2011-03-03 23:38         ` Dmitry Bely
2011-03-03 23:46           ` Niki Yoshiuchi
2011-03-04  0:17             ` Dmitry Bely
2011-03-04  9:21             ` xclerc

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='AANLkTi=XJ=ekYQ6qJ5eE+ce_Wdk9MYph5pVWAvFEHy86@mail.gmail.com' \
    --to=dmitry.bely@gmail.com \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).