From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Spencer Baugh Newsgroups: gmane.emacs.devel Subject: Native module callback when Lisp thread exits Date: Fri, 17 May 2024 13:09:35 -0400 Message-ID: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="38760"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: mshinwell@janestreet.com To: emacs-devel@gnu.org Cancel-Lock: sha1:BEOUtht5GSCzt7A7BmnBmjj0xFw= Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Fri May 17 19:40:02 2024 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1s81Yn-0009qB-Tp for ged-emacs-devel@m.gmane-mx.org; Fri, 17 May 2024 19:40:01 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1s81YE-0001EO-5S; Fri, 17 May 2024 13:39:26 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s815c-0008GV-TO for emacs-devel@gnu.org; Fri, 17 May 2024 13:09:52 -0400 Original-Received: from ciao.gmane.io ([116.202.254.214]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s815a-0003sp-Mi for emacs-devel@gnu.org; Fri, 17 May 2024 13:09:52 -0400 Original-Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1s815W-000AYk-P8 for emacs-devel@gnu.org; Fri, 17 May 2024 19:09:46 +0200 X-Injected-Via-Gmane: http://gmane.org/ Received-SPF: pass client-ip=116.202.254.214; envelope-from=ged-emacs-devel@m.gmane-mx.org; helo=ciao.gmane.io X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.249, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Fri, 17 May 2024 13:39:24 -0400 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:319330 Archived-At: 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.