English version
Accueil     Ŕ propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis ŕ jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml ŕ l'adresse ocaml.org.

Browse thread
C-threads & callbacks
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-01-20 (18:23)
From: Markus Mottl <markus.mottl@g...>
Subject: C-threads & callbacks

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,