unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Spencer Baugh <sbaugh@janestreet.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: emacs-devel@gnu.org
Subject: Re: Releasing the thread global_lock from the module API
Date: Fri, 01 Mar 2024 16:56:55 -0500	[thread overview]
Message-ID: <ierr0gtsjiw.fsf@janestreet.com> (raw)
In-Reply-To: <864jdpr5zy.fsf@gnu.org> (Eli Zaretskii's message of "Fri, 01 Mar 2024 23:34:25 +0200")

Eli Zaretskii <eliz@gnu.org> writes:

>> From: Spencer Baugh <sbaugh@janestreet.com>
>> Date: Fri, 01 Mar 2024 16:21:03 -0500
>> 
>> Eli Zaretskii <eliz@gnu.org> writes:
>> 
>> > Where would you take the data for opening the socket? doesn't that
>> > come from some Lisp program or from a Lisp variable?  And how would
>> > you know what kind of request to send? doesn't that come from Lisp as
>> > well?
>> 
>> Yes: I get those things as arguments from Lisp and then convert them
>> into the native datastructures of my library, which can be used without
>> further interacting with the Lisp machine.
>> 
>> Then I would release the lock and call into my library, which does some
>> useful work which takes a while.
>
> How is this different from starting your own native thread, then
> releasing the lock?

They are completely different things.

Let me say again what this sub-thread is about:

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

So, if I am running the following program in a Lisp thread:

(while (do-emacs-things)
  (let ((input (do-emacs-things)))
    (let ((result (call-into-native-module input)))
      (do-emacs-things result))))

and call-into-native-module unlocks the global lock around the calls
into my library, then during the part of call-into-native-module which
calls into my library, the main Emacs thread will not be blocked and can
run in parallel with call-into-native-module.

>> > And what would you do with the stuff you get in response? don't
>> > you want to put this in some Lisp variable or provide as input for
>> > some Lisp program?  Etc. etc.
>> 
>> Yes: After I finish the call into my library, I would take the lock
>> again and call further Lisp functions to put the results back into the
>> Lisp machine.
>
> How is this different from doing the same when your native thread
> finishes?
>
>> >> Since it's useful for me, I'd like to write a patch which allow modules
>> >> to do this; would it be considered?
>> >
>> > Once again: what cannot you do from a native thread started by the
>> > module?  IOW, why would you need access to the global lock machinery
>> > in the first place, if all you want to do is something that is
>> > unrelated to Emacs and its global state?
>> 
>> See above: The call into my library, which takes a while, and is useful,
>> does not touch the Lisp machine.  But other code around that call does
>> touch the Lisp machine, and so needs to run with the lock.  The ability
>> to release and re-acquire the lock means my module doesn't hold the lock
>> when it doesn't need to.
>
> You didn't answer my question about doing this from a native thread.

I hope my answer above clarifies it.

> As for re-acquiring the lock: you cannot do that with how the Lisp
> threads are currently implemented.  You can only wait for the lock to
> be released, and try re-acquiring it; whether you succeed is anyone's
> guess, if there are other threads competing for the lock.

Yes, that is the normal behavior of locks.  I am well aware of it.  By
"re-acquire", I mean (in your words) "wait for the lock to be released,
and try re-acquiring it".

It is okay if my module has to wait for the main Emacs Lisp thread to
stop running before it gets the global lock again.



  reply	other threads:[~2024-03-01 21:56 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 [this message]
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=ierr0gtsjiw.fsf@janestreet.com \
    --to=sbaugh@janestreet.com \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    /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).