From: Niki Yoshiuchi <aplusbi@gmail.com>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Using C threads
Date: Thu, 3 Mar 2011 17:16:06 -0500 [thread overview]
Message-ID: <AANLkTikTV8kKKxnhrFp+SMhKi13U8avgK-dV+d9SewzW@mail.gmail.com> (raw)
In-Reply-To: <AANLkTi=XJ=ekYQ6qJ5eE+ce_Wdk9MYph5pVWAvFEHy86@mail.gmail.com>
I figured it out so I'm posting it here for reference.
caml_c_thread_register() attempts to get the master lock, which
involves dereferencing a pointer. That pointer is initialized by the
Thread module. However if the OCaml code doesn't use the Thread
module that pointer is never initialized resulting in a segfault. You
can force the initialization with something as simple as:
ignore (Thread.self ());
Additionally, if your OCaml code doesn't use any threads (or call any
c code with a call to caml_enter_blocking_section()) then it appears
that the master lock is never released by the main thread, so the C
thread hangs in the caml_c_thread_register() call.
-Niki Yoshiuchi
On Tue, Mar 1, 2011 at 7:14 AM, Dmitry Bely <dmitry.bely@gmail.com> wrote:
> 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
>
>
> --
> Caml-list mailing list. Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>
next prev parent reply other threads:[~2011-03-03 22:16 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
2011-03-03 22:16 ` Niki Yoshiuchi [this message]
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=AANLkTikTV8kKKxnhrFp+SMhKi13U8avgK-dV+d9SewzW@mail.gmail.com \
--to=aplusbi@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