all messages for Emacs-related lists mirrored at yhetil.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

* Re: Native module callback when Lisp thread exits
  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
  0 siblings, 1 reply; 4+ messages in thread
From: Eli Zaretskii @ 2024-05-17 18:26 UTC (permalink / raw)
  To: Spencer Baugh; +Cc: emacs-devel, mshinwell

> 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.



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

* Re: Native module callback when Lisp thread exits
  2024-05-17 18:26 ` Eli Zaretskii
@ 2024-05-18 13:56   ` Spencer Baugh
  2024-05-18 15:49     ` Eli Zaretskii
  0 siblings, 1 reply; 4+ messages in thread
From: Spencer Baugh @ 2024-05-18 13:56 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel, mshinwell

Eli Zaretskii <eliz@gnu.org> writes:
> 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.

Yes, that would be enough.

Although I'm now realizing that this could be done in my native module
by passing a destructor to pthread_key_create, so maybe I don't need
anything in Emacs after all.



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

* Re: Native module callback when Lisp thread exits
  2024-05-18 13:56   ` Spencer Baugh
@ 2024-05-18 15:49     ` Eli Zaretskii
  0 siblings, 0 replies; 4+ messages in thread
From: Eli Zaretskii @ 2024-05-18 15:49 UTC (permalink / raw)
  To: Spencer Baugh; +Cc: emacs-devel, mshinwell

> From: Spencer Baugh <sbaugh@janestreet.com>
> Cc: emacs-devel@gnu.org,  mshinwell@janestreet.com
> Date: Sat, 18 May 2024 09:56:26 -0400
> 
> Eli Zaretskii <eliz@gnu.org> writes:
> > 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.
> 
> Yes, that would be enough.
> 
> Although I'm now realizing that this could be done in my native module
> by passing a destructor to pthread_key_create, so maybe I don't need
> anything in Emacs after all.

If you don't care about portability to systems that don't use pthreads
for implementing the Lisp threads, then I guess you can do that as
well.



^ 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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.