From: Spencer Baugh <sbaugh@janestreet.com>
To: emacs-devel@gnu.org
Cc: Philipp Stephani <phst@google.com>
Subject: Releasing the thread global_lock from the module API
Date: Fri, 01 Mar 2024 09:53:41 -0500 [thread overview]
Message-ID: <ierfrxat34a.fsf@janestreet.com> (raw)
Lisp threads all take global_lock while they are running Lisp code.
This is good and correct.
Lisp threads can also call module code. This is also good and correct.
In other languages with an FFI and a global interpreter lock taken by
threads, such as Python, it's possible for a native function called from
the interpreter to release the global lock, and then re-acquire it
before calling any interpreter functions (or automatically re-acquire it
upon returning to the interpreter).
This allows for a limited form of actual parallelism: if a native
function is doing work that doesn't involve the Python interpreter,
e.g. doing numerical computations in native code, it can release the
lock so that it can run in parallel with other threads. If the native
function needs to call a function which does involve the Python
interpreter, it can re-acquire the global lock around that call.
Could the same functionality be added to the Emacs module API? Then
module code could release the global lock when doing Emacs-independent
computation, and re-acquire the lock when calling into Emacs. This
would allow a limited form of parallelism: if a Lisp thread is calling
module code which releases the lock, then the module code could run in
parallel with the rest of Emacs on that thread.
This should be safe, because this is in effect already possibly through
thread-yield: that can be called from anywhere in a Lisp thread, and
releases the global lock, calls the operating system thread_yield
function, and then re-acquires the global lock. If instead of doing
thread_yield, it did some computation, e.g.
int x = 0
for (int i = 0; i<999999; i++)
x += i;
that would have the same effect as yielding, but the computation would
run in parallel with the main Emacs thread. This is what would happen
if module code released the lock, did some Emacs-independent work, and
then re-acquired the lock.
As an additional bonus, this would allow the module API to extend Lisp
threads: if a C library provides some blocking function which does some
complicated form of IO, a module providing bindings for that C library
can release global_lock before calling that function, and then
re-acquire the lock after the function returns. Then Lisp threads
calling this module function will not block the main Emacs thread.
next reply other threads:[~2024-03-01 14:53 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-01 14:53 Spencer Baugh [this message]
2024-03-01 16:47 ` Releasing the thread global_lock from the module API Eli Zaretskii
2024-03-01 17:34 ` Spencer Baugh
2024-03-01 18:44 ` Eli Zaretskii
2024-03-01 19:02 ` Spencer Baugh
2024-03-01 19:26 ` Eli Zaretskii
2024-03-01 19:51 ` Spencer Baugh
2024-03-01 20:42 ` Eli Zaretskii
2024-03-01 21:21 ` Spencer Baugh
2024-03-01 21:34 ` Eli Zaretskii
2024-03-01 21:56 ` Spencer Baugh
2024-03-02 6:43 ` Eli Zaretskii
2024-03-02 16:39 ` sbaugh
2024-03-02 17:02 ` Eli Zaretskii
2024-03-02 20:33 ` Spencer Baugh
2024-03-03 6:13 ` Eli Zaretskii
2024-03-03 13:19 ` sbaugh
2024-03-03 15:42 ` Dmitry Gutov
2024-03-03 15:51 ` Eli Zaretskii
2024-03-01 19:30 ` tomas
2024-03-01 23:53 ` Dmitry Gutov
2024-03-02 5:57 ` tomas
2024-03-02 15:35 ` Dmitry Gutov
2024-03-02 16:31 ` tomas
2024-03-02 21:41 ` sbaugh
2024-03-03 6:25 ` tomas
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=ierfrxat34a.fsf@janestreet.com \
--to=sbaugh@janestreet.com \
--cc=emacs-devel@gnu.org \
--cc=phst@google.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).