From: Lynn Winebarger <owinebar@gmail.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: emacs-devel@gnu.org
Subject: Re: Blocking calls and threads
Date: Thu, 20 Apr 2023 10:19:11 -0400 [thread overview]
Message-ID: <CAM=F=bAKAz_NAhxhoYbauVpj48Cpc2DKTD92pQhNh=+rYXOH4Q@mail.gmail.com> (raw)
In-Reply-To: <83cz3y65ev.fsf@gnu.org>
On Thu, Apr 20, 2023 at 9:37 AM Eli Zaretskii <eliz@gnu.org> wrote:
> > From: Lynn Winebarger <owinebar@gmail.com>
> > Date: Thu, 20 Apr 2023 09:06:16 -0400
> > Cc: emacs-devel@gnu.org
> >
> > > What do you mean by "blocking system calls", exactly?
> > >
> > > If you mean the likes of 'read' and 'write' (i.e. "blocking system
> > > calls" on the OS level),
> >
> > Almost - I mean the subrs that make those operations available to the
> > lisp machine, e.g. insert-file-contents.
>
> insert-file-contents does a lot of simple bookkeeping stuff, then
> calls 'read' in a loop to read the file and decode the stuff it reads.
> We could perhaps yield between reading and processing chunks (we don't
> in the current Emacs), but would it help? Modern systems are very
> fast in reading local files, so you'd make insert-file-contents and
> its callers much slower for no good reason. Likewise in write-region.
I don't know whether it's a good idea. I'm not sure how anyone can
work out how to provide explicit but generic asynchronous programming
constructs, as Stefan is, when some operations are implemented in ways
that don't allow it.
>
> If you yield before issuing the system call, the system call will wait
> until you re-acquire the lock. So how will this help?
You're talking about yielding the system thread, I'm talking about
yielding the Lisp machine thread. Even though Lisp machine threads
are implemented by mapping them to the underlying system thread, the
Lisp machine execution state is kept coherent by the global
(interpreter) lock. Releasing the lock is effectively yielding the
Lisp machine thread. The system thread will yield implicitly if the
read blocks.
>
> To be effective, this needs to yield _after_ issuing the system call,
> so that the system call proceeds in parallel with Emacs doing
> something else. But since the system call is not implemented by
> Emacs, I don't see how this could be done? In a new non-Lisp thread
> that we would start to issue the system call from it? is that what you
> have in mind? But then we'd have the "usual" problem with Emacs: the
> huge global state that we have. That non-Lisp thread cannot use any
> machinery that changes the global state, nor call any Lisp or Lisp
> primitives, so it will only be able to do very simple processing, thus
> making the whole business much less beneficial, from the user's POV.
So making "insert-file-contents" yield would introduce race conditions
with respect to the buffer being read into that the lisp programmer
choosing to use threads would have to be responsible for (which would
be what Stefan is trying to simplify, I think). I don't mean
lisp-machine violating race conditions - the read operation should
either use some temporary buffer and copy into the lisp buffer, or
wait for data to be ready, then reacquire the GIL before invoking the
read syscall with a pointer into the Lisp buffer object.
Lynn
next prev parent reply other threads:[~2023-04-20 14:19 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-04-20 5:31 Blocking calls and threads Lynn Winebarger
2023-04-20 7:00 ` Po Lu
2023-04-20 7:23 ` Eli Zaretskii
2023-04-20 13:06 ` Lynn Winebarger
2023-04-20 13:28 ` Po Lu
2023-04-20 14:26 ` Lynn Winebarger
2023-04-20 13:37 ` Eli Zaretskii
2023-04-20 14:19 ` Lynn Winebarger [this message]
2023-04-20 14:36 ` Eli Zaretskii
2023-04-21 4:39 ` Lynn Winebarger
2023-04-21 13:43 ` Lynn Winebarger
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='CAM=F=bAKAz_NAhxhoYbauVpj48Cpc2DKTD92pQhNh=+rYXOH4Q@mail.gmail.com' \
--to=owinebar@gmail.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).