unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Native module callback when Lisp thread exits
@ 2024-05-17 17:09 Spencer Baugh
  2024-05-17 18:26 ` Eli Zaretskii
  0 siblings, 1 reply; 4+ messages in thread
From: Spencer Baugh @ 2024-05-17 17:09 UTC (permalink / raw)
  To: emacs-devel; +Cc: mshinwell


Hi,

I have a native module written in OCaml which provides some useful
functions.  Sometimes, I would like to call these native module
functions from Lisp threads other than the main Lisp thread.

For a native module written in C, this works fine.  However, the OCaml
runtime requires that if it's called from other threads, the thread must
first call caml_c_thread_register; and when the thread exits, it should
call caml_c_thread_unregister. See
https://ocaml.org/manual/5.1/intfc.html#ss:c-thread-register

My native module can call caml_c_thread_register when the native module
is first called in a non-main thread; then calling my native module
functions works fine in Lisp threads.

However, there's no way for my native module to call
caml_c_thread_unregister when the thread exits.  That causes memory
leaks and means that OCaml GC performance degrades over time, since the
GC scans all known threads.

Could we add a way for a native module to call some functions when a
Lisp thread exits?

Currently, native modules expose a emacs_module_init function which is
called when the module is loaded.  Maybe a native module could
optionally also expose an emacs_module_thread_init function which is
called when a Lisp thread is created (or maybe when the module is first
used on a Lisp thread).  And likewise, an emacs_module_thread_uninit
function which is called when a Lisp thread exits.




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-05-18 15:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-17 17:09 Native module callback when Lisp thread exits Spencer Baugh
2024-05-17 18:26 ` Eli Zaretskii
2024-05-18 13:56   ` Spencer Baugh
2024-05-18 15:49     ` Eli Zaretskii

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).