From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Lynn Winebarger Newsgroups: gmane.emacs.devel Subject: Re: Blocking calls and threads Date: Fri, 21 Apr 2023 09:43:52 -0400 Message-ID: References: <838ren6mpp.fsf@gnu.org> <83cz3y65ev.fsf@gnu.org> <837cu662oo.fsf@gnu.org> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="000000000000b69bad05f9d8d920" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="11689"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel To: Eli Zaretskii , Stefan Monnier Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Fri Apr 21 15:44:57 2023 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 1ppr4K-0002oD-SL for ged-emacs-devel@m.gmane-mx.org; Fri, 21 Apr 2023 15:44:57 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ppr3f-0005uA-5k; Fri, 21 Apr 2023 09:44:15 -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 1ppr3c-0005qJ-IY for emacs-devel@gnu.org; Fri, 21 Apr 2023 09:44:12 -0400 Original-Received: from mail-pj1-x1031.google.com ([2607:f8b0:4864:20::1031]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ppr3Z-0007WQ-Hr; Fri, 21 Apr 2023 09:44:12 -0400 Original-Received: by mail-pj1-x1031.google.com with SMTP id 98e67ed59e1d1-247399d518dso1407520a91.0; Fri, 21 Apr 2023 06:44:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1682084644; x=1684676644; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=izf8gsPszfmobSnBphHVjUiRuLQIXgmqevlXzFv4HGY=; b=ZdlCAqY84CgmGmLWLgpaZIHiwb4ue2F05kUV171/UzZXU+0Ewz17Xpbdvj9lCElOt9 fo0+oolLppe9Fy9SgXjEHnaBAuFbXehvoD2hOFcmHJUiBB3r02jhfcoTVL+oSZ+7uBSG hSa4y5IJoda0gi3Y7eKQC5nnHForX8ww2FXUviM6hFRx9NT6RUWRBI2Fe8W+VGqj6rFc 9wQJ9OZ56k8HlhIawwdJPtPeUqKjowdwotwWpwOAKRs5IrG87Nd0g/5JgbRDcqi3JJTD i+EtAAGNiotcDiqhH1nQ1KpPB9D0d/0nmf282Ocu7rsFteyYreO0tierUYXOlBSUZFrg KJwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682084644; x=1684676644; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=izf8gsPszfmobSnBphHVjUiRuLQIXgmqevlXzFv4HGY=; b=Hb8ak8sCvXipg5GKfh0xX7orPmGPo2dLTFdyY6QPPtB4jhiEqMkG6Ck0laBLniP4eq K3/NhKiouiI1FkGa8SX9TXg+U4f34EPZHeFm2E6gw+gSbt/JC2Ut30F9gpMSTJL1XnMa gTaCpYhciHMXuUvcZZXY/oCVrOI85dIkXusAy+XNxpG16+Ef0sTEJIKmyGRONbFZxAhw T5LgcAUXywwnkCfZvhP9gabPDRreDHLWmLl8Ib7JZiFP93Fvazk1DAcaESWNChL+zZSZ qNs3ZXzxJDqWplX4KMG2QM9eZ47BsYdZ2p8Zqyb5htyg+J3xtDI+8cfMMNAq7MZZZPjW RV1Q== X-Gm-Message-State: AAQBX9eUfmuCtntPACe6cqrcZDLgH4/7+1ILclW8SNrqHB/BTJ1Xy+CL pg88+oLcZ0HQ7b1JuucCDQtiO/CefRHN6co2+oSOUlsD X-Google-Smtp-Source: AKy350aawEaIWVs8iLsiI4zxO3rqmKUwFbU4SHMhkM+Tsj6z6EV/rNTEocTupdMfNCTTJSrdQhzMXDI8V5x5L23ECNQ= X-Received: by 2002:a17:90a:2904:b0:246:75c8:f071 with SMTP id g4-20020a17090a290400b0024675c8f071mr5129338pjd.3.1682084644427; Fri, 21 Apr 2023 06:44:04 -0700 (PDT) In-Reply-To: Received-SPF: pass client-ip=2607:f8b0:4864:20::1031; envelope-from=owinebar@gmail.com; helo=mail-pj1-x1031.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action 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:305544 Archived-At: --000000000000b69bad05f9d8d920 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, Apr 21, 2023, 12:39 AM Lynn Winebarger wrote: > I'm not sure what the etiquette is here - I keep referring to Stefan's > effort on futur.el, so I've added him explicitly. > > On Thu, Apr 20, 2023 at 10:36=E2=80=AFAM Eli Zaretskii wro= te: > > > > > From: Lynn Winebarger > > > Date: Thu, 20 Apr 2023 10:19:11 -0400 > > > Cc: emacs-devel@gnu.org > > > > > > > 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. > > > > No, I'm also talking about the Lisp machine thread. The thread which > > calls insert-file-contents and runs the C code of insert-file-contents > > and of the subroutines called by insert-file-contents. > > A lisp thread is the context (lisp machine state) observable > (intentionally) by lisp programs. That is conceptually distinct from > the context tracked by the OS thread. To paraphrase a great mind, the > identification of the two "is an implementation detail". Meaning, it > is not normative with respect to the intended semantics of lisp > threads. > > > > 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. > > > > What you say here is not relevant to the issue at hand. > > I don't know what you think is the issue at hand. My question was > about when (or if) the lisp thread could yield during a blocking I/O > operation. You appear to have interpreted that question differently > than I intended, so I attempted to be more explicit about what I meant > by "the lisp thread" and "yielding". > > > > 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. > > > > Yes, and that's exactly where we will lose: most of the heavy > > processing cannot be done in a separate temporary buffer, because it > > calls functions from the Lisp machine, and those are written assuming > > nothing else is running in the Lisp machine concurrently. For > > example, take the code which decodes the file's contents we have just > > read. I encourage you to take a good look at that code (most of it is > > in coding.c) to appreciate the magnitude of the problem. > > I believe you. I did mention that the global lock could be (evidently > *must be*) reacquired before the actual call to "read". There's also > Tromey's comment on > > https://www.reddit.com/r/emacs/comments/utzxir/a_vision_of_a_multithreade= d_emacs/ > . > > OTOH, I asked the question in order to understand what it means to > give programmers control over asynchronous execution in controlled > ways. The most basic kind of control I can think of is whether > functions called in that code are expected to behave synchronously or > asynchronously. And what is a more basic operation, that could be > done synchronously or asynchronously, than reading text from a file. > If something like insert-file-contents can't be performed > asynchronously (not in parallel, just with other code running while > waiting for IO), the scope of what Stefan's effort is going to be very > limited. Limited to the point of not being very interesting. > > It's also possible that something could be built on the existing data > structures that doesn't rely on more fine-grained locking. I just > happened to be reading > https://www.gnu.org/software/emacs/manual/html_node/emacs/VCS-Merging.htm= l > today, after seeing this message, and it seemed to me highly relevant > to the problem of concurrent work on a text buffer. Particularly the > comment "Experience has shown that merging is superior to locking". > Maybe the thing to do to enable asynchronous/concurrent/parallel work > would be to add a new type of buffer, built on the existing one, that > behaves something like a git repo/working copy of the text buffer, > that eventually merges edits, say by windows displaying the buffer > each pulling updates from all the "repos" with checked-out copies. > Then the model for support of asynchronous programming could > encapsulate specifying how to merge and/or handle merge failure. > > Maybe that would be too expensive, but at least at first, these > distributed buffers would only be used by programs using explicit > asynchronous programming. Maybe that approach would even be helpful > in dealing with extremely large files or long lines. We could call it > "merge-oriented programming". :-) > I forgot to mention the concurrent version of the buffer would need a functional representation to avoid copying during the merge. Something along the lines of Okasaki's purely functional strings, except including all the other components of buffers - overlays, local variables, and whatever else would be implicated. I don't know if this would require a complete reimplementation of buffers, or if the current implementation could be tweaked to serve as an underlying component of a zippered buffer. > > So the code which can run in parallel with another Lisp thread will be > > able to do only very simple jobs, and will also add overhead due to > > the need of copying stuff from temporary buffers to Lisp objects. > > I'm not talking about running in parallel - there is still just one > lisp machine in this hypothetical. > > > Of course, we could redesign and reimplement this stuff, but that's a > > lot of non-trivial work. My assumption was that you are considering > > relatively lightweight changes on top of the existing code, not a > > complete redesign of how these primitives work. > > I wasn't considering anything. I asked a very limited question for > the purpose of giving some hard thought to the language problem Stefan > requested assistance on. I didn't offer these elaborations because I > have any plans, only to respond to your question "How could it?". > That is a purely hypothetical question. > > Lynn > --000000000000b69bad05f9d8d920 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Fri, Apr 21, 2023, 12:39 AM Lynn Winebarger <owinebar@gm= ail.com> wrote:
I'm not = sure what the etiquette is here - I keep referring to Stefan's
effort on futur.el, so I've added him explicitly.

On Thu, Apr 20, 2023 at 10:36=E2=80=AFAM Eli Zaretskii <eliz@gnu.or= g> wrote:
>
> > From: Lynn Winebarger <owinebar@gmail.com> > > Date: Thu, 20 Apr 2023 10:19:11 -0400
> > Cc: emacs-devel@gnu.org
> >
> > > If you yield before issuing the system call, the system call= will wait
> > > until you re-acquire the lock.=C2=A0 So how will this help?<= br> > >
> > You're talking about yielding the system thread, I'm talk= ing about
> > yielding the Lisp machine thread.
>
> No, I'm also talking about the Lisp machine thread.=C2=A0 The thre= ad which
> calls insert-file-contents and runs the C code of insert-file-contents=
> and of the subroutines called by insert-file-contents.

A lisp thread is the context (lisp machine state) observable
(intentionally) by lisp programs.=C2=A0 That is conceptually distinct from<= br> the context tracked by the OS thread.=C2=A0 To paraphrase a great mind, the=
identification of the two "is an implementation detail".=C2=A0 Me= aning, it
is not normative with respect to the intended semantics of lisp
threads.

> > 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.=C2=A0 Releasing t= he lock
> > is effectively yielding the Lisp machine thread.=C2=A0 The system= thread
> > will yield implicitly if the read blocks.
>
> What you say here is not relevant to the issue at hand.

I don't know what you think is the issue at hand.=C2=A0 My question was=
about when (or if) the lisp thread could yield during a blocking I/O
operation.=C2=A0 You appear to have interpreted that question differently than I intended, so I attempted to be more explicit about what I meant
by "the lisp thread" and "yielding".

> > the read operation should either use some temporary buffer and co= py
> > into the lisp buffer, or wait for data to be ready, then reacquir= e
> > the GIL before invoking the read syscall with a pointer into the<= br> > > Lisp buffer object.
>
> Yes, and that's exactly where we will lose: most of the heavy
> processing cannot be done in a separate temporary buffer, because it > calls functions from the Lisp machine, and those are written assuming<= br> > nothing else is running in the Lisp machine concurrently.=C2=A0 For > example, take the code which decodes the file's contents we have j= ust
> read.=C2=A0 I encourage you to take a good look at that code (most of = it is
> in coding.c) to appreciate the magnitude of the problem.

I believe you. I did mention that the global lock could be (evidently
*must be*) reacquired before the actual call to "read".=C2=A0 The= re's also
Tromey's comment on
https://www.reddit.com/r/emacs/comments/utzxir/a_vision_of_a_multithread= ed_emacs/.

OTOH, I asked the question in order to understand what it means to
give programmers control over asynchronous execution in controlled
ways.=C2=A0 The most basic kind of control I can think of is whether
functions called in that code are expected to behave synchronously or
asynchronously.=C2=A0 And what is a more basic operation, that could be
done synchronously or asynchronously, than reading text from a file.
If something like insert-file-contents can't be performed
asynchronously (not in parallel, just with other code running while
waiting for IO), the scope of what Stefan's effort is going to be very<= br> limited.=C2=A0 Limited to the point of not being very interesting.

It's also possible that something could be built on the existing data structures that doesn't rely on more fine-grained locking.=C2=A0 I just=
happened to be reading
http= s://www.gnu.org/software/emacs/manual/html_node/emacs/VCS-Merging.html<= br> today, after seeing this message, and it seemed to me highly relevant
to the problem of concurrent work on a text buffer.=C2=A0 Particularly the<= br> comment "Experience has shown that merging is superior to locking"= ;.
Maybe the thing to do to enable asynchronous/concurrent/parallel work
would be to add a new type of buffer, built on the existing one, that
behaves something like a git repo/working copy of the text buffer,
that eventually merges edits, say by windows displaying the buffer
each pulling updates from all the "repos" with checked-out copies= .
Then the model for support of asynchronous programming could
encapsulate specifying how to merge and/or handle merge failure.

Maybe that would be too expensive, but at least at first, these
distributed buffers would only be used by programs using explicit
asynchronous programming.=C2=A0 Maybe that approach would even be helpful in dealing with extremely large files or long lines.=C2=A0 We could call it=
"merge-oriented programming". :-)

I forgot to mention the concurre= nt version of the buffer would need a functional representation to avoid co= pying during the merge.=C2=A0 Something along the lines of=C2=A0Okasaki'= ;s purely functional strings, except including all the other components of = buffers - overlays, local variables, and whatever else would be implicated.= =C2=A0 I don't know if this would require a complete reimplementation o= f buffers, or if the current implementation could be tweaked to serve as an= underlying component of a zippered buffer.




> So the code which can run in parallel with another Lisp thread will be=
> able to do only very simple jobs, and will also add overhead due to > the need of copying stuff from temporary buffers to Lisp objects.

I'm not talking about running in parallel - there is still just one
lisp machine in this hypothetical.

> Of course, we could redesign and reimplement this stuff, but that'= s a
> lot of non-trivial work.=C2=A0 My assumption was that you are consider= ing
> relatively lightweight changes on top of the existing code, not a
> complete redesign of how these primitives work.

I wasn't considering anything.=C2=A0 I asked a very limited question fo= r
the purpose of giving some hard thought to the language problem Stefan
requested assistance on.=C2=A0 I didn't offer these elaborations becaus= e I
have any plans, only to respond to your question "How could it?".=
That is a purely hypothetical question.

Lynn
--000000000000b69bad05f9d8d920--