unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Eli Zaretskii <eliz@gnu.org>
To: Spencer Baugh <sbaugh@janestreet.com>
Cc: emacs-devel@gnu.org, mshinwell@janestreet.com
Subject: Re: Native module callback when Lisp thread exits
Date: Fri, 17 May 2024 21:26:59 +0300	[thread overview]
Message-ID: <86msoofg5o.fsf@gnu.org> (raw)
In-Reply-To: <iero794tlf4.fsf@janestreet.com> (message from Spencer Baugh on Fri, 17 May 2024 13:09:35 -0400)

> From: Spencer Baugh <sbaugh@janestreet.com>
> Date: Fri, 17 May 2024 13:09:35 -0400
> Cc: mshinwell@janestreet.com
> 
> 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.

I don't understand why you need something module-specific.  Shouldn't
a thread-exit-hook be enough?  We could easily add that if that helps.



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

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-17 17:09 Native module callback when Lisp thread exits Spencer Baugh
2024-05-17 18:26 ` Eli Zaretskii [this message]
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=86msoofg5o.fsf@gnu.org \
    --to=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=mshinwell@janestreet.com \
    --cc=sbaugh@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).