* C-threads & callbacks
@ 2005-01-20 18:23 Markus Mottl
2005-01-20 18:58 ` [Caml-list] " Daniel Bünzli
` (3 more replies)
0 siblings, 4 replies; 9+ messages in thread
From: Markus Mottl @ 2005-01-20 18:23 UTC (permalink / raw)
To: OCaml
Hi,
I'm currently interfacing a 3rd-party library that spawns threads
to execute callbacks. I would like those callbacks to be handled by
OCaml-code, but have run into some issues here.
As it seems, it is not possible to run OCaml-code linked with thread
support while letting C-threads perform callbacks. This has already
been a topic on the list a while ago.
I have two solutions in mind: the first one would involve pre-spawning
an OCaml-thread and pass data from C to OCaml through an OCaml-reference
exposed to C, protected by mutexes and condition variables. This seems
pretty ugly, because it's error-prone and also involves a considerable
amount of context-switching, which is bad for my application (very high
volume realtime data).
The second solution would require setting up an OCaml thread descriptor
for the currently executing C-thread. However, AFAIK this solution
cannot be implemented without fiddling with the implementation of the
OCaml-runtime, because some global variables for thread-handling are not
exposed to user code (i.e. "static"). It would be great if the thread
implementation offered functions allowing users to have their C-threads
"migrate" to OCaml.
Does anybody know of another elegant and efficient solution for this
problem that does not require hacking the OCaml-runtime?
Best regards,
Markus
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] C-threads & callbacks
2005-01-20 18:23 C-threads & callbacks Markus Mottl
@ 2005-01-20 18:58 ` Daniel Bünzli
2005-01-20 19:23 ` Markus Mottl
2005-01-20 19:37 ` Alex Baretta
` (2 subsequent siblings)
3 siblings, 1 reply; 9+ messages in thread
From: Daniel Bünzli @ 2005-01-20 18:58 UTC (permalink / raw)
To: Markus Mottl; +Cc: caml-list caml-list
> As it seems, it is not possible to run OCaml-code linked with thread
> support while letting C-threads perform callbacks. This has already
> been a topic on the list a while ago.
I don't understand what you are refering to. I don't think there's a
problem as long as you take care to acquire ocaml's global lock to
exectue the callbacks.
#include <caml/signals.h>
...
caml_leave_blocking_section();
callback(*caml_named_value("myfun"), Val_unit);
caml_enter_blocking_section();
...
This ensures that callbacks into caml are properly serialized.
Daniel
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] C-threads & callbacks
2005-01-20 18:58 ` [Caml-list] " Daniel Bünzli
@ 2005-01-20 19:23 ` Markus Mottl
0 siblings, 0 replies; 9+ messages in thread
From: Markus Mottl @ 2005-01-20 19:23 UTC (permalink / raw)
To: Daniel Bünzli; +Cc: caml-list caml-list
Daniel Bünzli schrieb am Donnerstag, den 20. Jänner 2005:
> I don't understand what you are refering to. I don't think there's a
> problem as long as you take care to acquire ocaml's global lock to
> exectue the callbacks.
>
> #include <caml/signals.h>
> ...
> caml_leave_blocking_section();
> callback(*caml_named_value("myfun"), Val_unit);
> caml_enter_blocking_section();
> ...
>
> This ensures that callbacks into caml are properly serialized.
This only works if the callback is performed by a thread that was
spawned by OCaml (i.e. OCaml -> C -> OCaml). In this case the runtime
knows what the "current thread" is. But if a C-thread executes
"caml_leave_blocking_section" (C -> OCaml), it can segfault there,
because there may be no "current thread". And if there is, it is not
the C-thread but another currently executing OCaml-thread, which will
mess things up later.
That's why there should be some way to register a C-thread with the
OCaml-runtime. Currently this does not seem to be possible without
hacking the OCaml-implementation.
Regards,
Markus
--
Markus Mottl markus.mottl@gmail.com http://www.ocaml.info
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] C-threads & callbacks
2005-01-20 18:23 C-threads & callbacks Markus Mottl
2005-01-20 18:58 ` [Caml-list] " Daniel Bünzli
@ 2005-01-20 19:37 ` Alex Baretta
2005-01-20 20:19 ` Markus Mottl
2005-01-21 1:16 ` SooHyoung Oh
2005-01-30 10:19 ` [Caml-list] " Xavier Leroy
3 siblings, 1 reply; 9+ messages in thread
From: Alex Baretta @ 2005-01-20 19:37 UTC (permalink / raw)
To: Markus Mottl; +Cc: OCaml
Markus Mottl wrote:
> Hi,
>
> I'm currently interfacing a 3rd-party library that spawns threads
> to execute callbacks. I would like those callbacks to be handled by
> OCaml-code, but have run into some issues here.
>
Just a thought: how about running two different processes which share
relevant data through an mmapped file? The C-threads could communicate
with the Ocaml world through this shared memory segment. I am not sure
how much mileage you'll get out of this, but at least you would not have
to rewrite the runtime system...
Alex
--
*********************************************************************
http://www.barettadeit.com/
Baretta DE&IT
A division of Baretta SRL
tel. +39 02 370 111 55
fax. +39 02 370 111 54
Our technology:
The Application System/Xcaml (AS/Xcaml)
<http://www.asxcaml.org/>
The FreerP Project
<http://www.freerp.org/>
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] C-threads & callbacks
2005-01-20 19:37 ` Alex Baretta
@ 2005-01-20 20:19 ` Markus Mottl
0 siblings, 0 replies; 9+ messages in thread
From: Markus Mottl @ 2005-01-20 20:19 UTC (permalink / raw)
To: Alex Baretta; +Cc: OCaml
On Thu, 20 Jan 2005, Alex Baretta wrote:
> Just a thought: how about running two different processes which share
> relevant data through an mmapped file? The C-threads could communicate
> with the Ocaml world through this shared memory segment. I am not sure
> how much mileage you'll get out of this, but at least you would not have
> to rewrite the runtime system...
We have also thought about this. It would surely be a working solution
and much faster than our current cruel hack of reading data from the
tail of a file. But a solution involving only one process would be
preferable in the long run.
Regards,
Markus
--
Markus Mottl markus.mottl@gmail.com http://www.ocaml.info
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] C-threads & callbacks
2005-01-20 18:23 C-threads & callbacks Markus Mottl
2005-01-20 18:58 ` [Caml-list] " Daniel Bünzli
2005-01-20 19:37 ` Alex Baretta
@ 2005-01-21 1:16 ` SooHyoung Oh
2005-01-21 6:42 ` Alex Baretta
2005-01-30 10:19 ` [Caml-list] " Xavier Leroy
3 siblings, 1 reply; 9+ messages in thread
From: SooHyoung Oh @ 2005-01-21 1:16 UTC (permalink / raw)
To: Markus Mottl; +Cc: OCaml
I'v resolved the same problem using TCP/IP communication between
Ocaml thread and C thread.
Markus Mottl 쓴 글:
>Hi,
>
>I'm currently interfacing a 3rd-party library that spawns threads
>to execute callbacks. I would like those callbacks to be handled by
>OCaml-code, but have run into some issues here.
>
>As it seems, it is not possible to run OCaml-code linked with thread
>support while letting C-threads perform callbacks. This has already
>been a topic on the list a while ago.
>
>I have two solutions in mind: the first one would involve pre-spawning
>an OCaml-thread and pass data from C to OCaml through an OCaml-reference
>exposed to C, protected by mutexes and condition variables. This seems
>pretty ugly, because it's error-prone and also involves a considerable
>amount of context-switching, which is bad for my application (very high
>volume realtime data).
>
>The second solution would require setting up an OCaml thread descriptor
>for the currently executing C-thread. However, AFAIK this solution
>cannot be implemented without fiddling with the implementation of the
>OCaml-runtime, because some global variables for thread-handling are not
>exposed to user code (i.e. "static"). It would be great if the thread
>implementation offered functions allowing users to have their C-threads
>"migrate" to OCaml.
>
>Does anybody know of another elegant and efficient solution for this
>problem that does not require hacking the OCaml-runtime?
>
>Best regards,
>Markus
>
>_______________________________________________
>Caml-list mailing list. Subscription management:
>http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
>Archives: http://caml.inria.fr
>Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>
>
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] C-threads & callbacks
2005-01-20 18:23 C-threads & callbacks Markus Mottl
` (2 preceding siblings ...)
2005-01-21 1:16 ` SooHyoung Oh
@ 2005-01-30 10:19 ` Xavier Leroy
3 siblings, 0 replies; 9+ messages in thread
From: Xavier Leroy @ 2005-01-30 10:19 UTC (permalink / raw)
To: OCaml
> I'm currently interfacing a 3rd-party library that spawns threads
> to execute callbacks. I would like those callbacks to be handled by
> OCaml-code, but have run into some issues here.
>
> As it seems, it is not possible to run OCaml-code linked with thread
> support while letting C-threads perform callbacks. This has already
> been a topic on the list a while ago.
Correct. This is still on my "to do" list. It sounds feasible but
requires some intervention in the runtime system and the threading
library, so it's not something that can be done on the side by
3rd-party code.
- Xavier Leroy
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2005-01-30 10:19 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-01-20 18:23 C-threads & callbacks Markus Mottl
2005-01-20 18:58 ` [Caml-list] " Daniel Bünzli
2005-01-20 19:23 ` Markus Mottl
2005-01-20 19:37 ` Alex Baretta
2005-01-20 20:19 ` Markus Mottl
2005-01-21 1:16 ` SooHyoung Oh
2005-01-21 6:42 ` Alex Baretta
[not found] ` <200501211042.31749.vincenzo_mlRE.MOVE@yahoo.it>
2005-01-24 20:00 ` Markus Mottl
2005-01-30 10:19 ` [Caml-list] " Xavier Leroy
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox