unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Spencer Baugh <sbaugh@janestreet.com>
To: emacs-devel@gnu.org
Cc: mshinwell@janestreet.com
Subject: Native module callback when Lisp thread exits
Date: Fri, 17 May 2024 13:09:35 -0400	[thread overview]
Message-ID: <iero794tlf4.fsf@janestreet.com> (raw)


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.




             reply	other threads:[~2024-05-17 17:09 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-17 17:09 Spencer Baugh [this message]
2024-05-17 18:26 ` Native module callback when Lisp thread exits Eli Zaretskii
2024-05-18 13:56   ` Spencer Baugh
2024-05-18 15:49     ` Eli Zaretskii

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=iero794tlf4.fsf@janestreet.com \
    --to=sbaugh@janestreet.com \
    --cc=emacs-devel@gnu.org \
    --cc=mshinwell@janestreet.com \
    /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.
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).