From: Eli Zaretskii <eliz@gnu.org>
To: sbaugh@catern.com
Cc: sbaugh@janestreet.com, emacs-devel@gnu.org
Subject: Re: Releasing the thread global_lock from the module API
Date: Sat, 02 Mar 2024 19:02:04 +0200 [thread overview]
Message-ID: <868r30pnxv.fsf@gnu.org> (raw)
In-Reply-To: <87plwck2q5.fsf@catern.com> (sbaugh@catern.com)
> From: sbaugh@catern.com
> Date: Sat, 02 Mar 2024 16:39:26 +0000 (UTC)
> Cc: Spencer Baugh <sbaugh@janestreet.com>, emacs-devel@gnu.org
>
> Oh, yes, that is similar to what I'm proposing.
>
> Just to summarize, if call_into_native_module looks like:
>
> emacs_value call_into_native_module(emacs_env *env, emacs_value input) {
> native_value native_input = convert_to_native(env, input);
> native_value native_output;
>
> [...some code...]
>
> return convert_to_emacs(env, native_output);
> }
>
> Then the current state of the world ("hold the lock" model):
>
> native_output = some_native_function(native_input);
>
> If I understand correctly, you are proposing (the "new thread" model):
>
> native_thread_handle handle = native_thread_create(some_native_function, native_input);
> while (!native_thread_done(handle)) {
> emacs_thread_yield(env);
> }
> native_output = native_thread_result(handle);
>
> And I am proposing (the "release lock" model):
>
> release_global_lock(env);
> native_output = some_native_function(native_input);
> acquire_global_lock(env);
>
> All three of these are used in the same way from Lisp programs. But the
> "new thread" and "release lock" models have the advantage over the "hold
> the lock" model that if called from a Lisp thread, that Lisp thread will
> run some_native_function in parallel with Lisp execution on other Lisp
> threads, including the main Emacs thread.
>
> To check my understanding: does this all seem correct so far, and match
> your proposal?
Yes.
> So, the main difference between the "new thread" model and the "release
> lock" model is that creating a native thread takes a nontrivial amount
> of time; maybe around 0.1 milliseconds. If some_native_function would
> takes less time than that, the thread creation cost will slow down
> Emacs, especially because the native module creates the native thread
> while holding the Lisp global_lock.
Why are you worried by 0.1 msec slowdown (if it indeed takes that
long; I think it should be around 10 to 20 usec at most)? If this
kind of slowdown is important for you, you are using the wrong
software package (and probably the wrong OS as well).
> The "release lock" model fits this need.
But it exposes the sensitive internals and runs the risk of more than
one Lisp thread running at the same time, and thus is not acceptable.
I'm afraid we will have to live with the 0.1 msec overhead.
> Releasing the lock is essentially free
Nothing is ever free in the world of synchronization primitives. But
that's a btw, not the most important aspect of this.
next prev parent reply other threads:[~2024-03-02 17:02 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-01 14:53 Releasing the thread global_lock from the module API Spencer Baugh
2024-03-01 16:47 ` 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 [this message]
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=868r30pnxv.fsf@gnu.org \
--to=eliz@gnu.org \
--cc=emacs-devel@gnu.org \
--cc=sbaugh@catern.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).