unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Ihor Radchenko <yantar92@posteo.net>
To: Eli Zaretskii <eliz@gnu.org>
Cc: monnier@iro.umontreal.ca, luangruo@yahoo.com, emacs-devel@gnu.org
Subject: Re: Concurrency via isolated process/thread
Date: Mon, 10 Jul 2023 11:30:10 +0000	[thread overview]
Message-ID: <87edlg6m2l.fsf@localhost> (raw)
In-Reply-To: <838rbp9h6c.fsf@gnu.org>

Eli Zaretskii <eliz@gnu.org> writes:

>> And how frequently are case-table and display-table changed? AFAIK, not
>> frequently at all.
>
> Why does it matter?  Would you disallow programs doing that just
> because you think it's infrequent?

Not disallow. But the programs setting case-table and display-table will
block. That will be only a fraction of programs.

> Processing of various protocols frequently requires to do stuff like
>
>    (with-case-table ascii-case-table
>      DO SOMETHING...)
>
> because otherwise some locales, such as the Turkish one, cause trouble
> with case conversion of the upper-case 'I'.  So changing the
> case-table of a buffer is not such an outlandish operation to do.

Yet, a lot of Elisp programs do not need to deal with case tables.

And if we talk about this particular use case, may we simply allow
let-binding case tables?

>> May you please explain a bit more about the situation you are referring
>> to? My above statement was about consing, not GC.
>
> Consing can trigger GC if we detect the memory-full situation, see
> alloc.c:memory_full.

I see.
AFAIU, we can then raise a flag that GC is necessary, so that other
threads will stop next time they reach maybe_gc, until GC is complete.

>> For GC, as I mentioned earlier, we can pause each thread once maybe_gc()
>> determines that GC is necessary, until all the threads are paused. Then,
>> GC is executed and the threads continue.
>
> If all the threads are paused, which thread will run GC?

I imagine that a dedicated thread will be used for GC. It will do
nothing until a signal, acquire a global lock, perform GC, release
the lock, and continue waiting.

>> If an async thread want to request redisplay, it should be possible. But
>> the redisplay itself must not be done by this same thread. Instead, the
>> thread will send a request that Emacs needs redisplay and optionally
>> block until that redisplay finishes (optionally, because something like
>> displaying notification may not require waiting). The redisplay requests
>> will be processed separately.
>
> Ouch! this again kills opportunities for gains from concurrent
> processing.  It means, for example, that we will be unable to run in a
> thread some processing that affects a window and reflect that
> processing on display when it's ready.

We may or may not be able to get async redisplay, depending on Emacs
display implementation:

1. A thread that requests redisplay will send a signal request to
   perform redisplay.
2. Another thread, responsible for redisplay, will retrieve the signal
   and process it. That thread may or may not do it asynchronously.

AFAIU, it is currently not possible to redisplay asynchronously.
But if we can somehow make redisplay (or parts of it) asynchronous, we
will just need to make adjustments to redisplay thread implementation.
Other async threads will not need to be changed.

I just do not know enough about redisplay to dive into possibility of
async in it. I am afraid that this discussion is already complex enough,
and discussing redisplay will add yet another layer of complexity on top.

>> We can make reading input using similar idea to the above, but it will
>> always block until the response.
>
> This will have to be designed and implemented first, since we
> currently have no provision for multiple prompt sources.

>> For non-blocking input, you said that it has been discussed.
>> I do vaguely recall such discussion in the past and I even recall some
>> ideas about it, but it would be better if you can link to that
>> discussion, so that the participants of this thread can review the
>> previously proposed ideas.
>
>   https://lists.gnu.org/archive/html/emacs-devel/2018-08/msg00456.html

Thanks!
After re-reading that discussion, it looks like the most discussed idea
was about maintaining a queue of input requests and prompting users at
appropriate times. This is similar to what I proposed - async threads
should query to process input and then the input order and time will be
decided elsewhere.

The rest of the discussion revolved around the fact that

1. Input may involve non-trivial Elisp that requires thread context - so
   the input processing must know the context of the original thread.
   Ergo, thread itself may need to be involved to process input, not
   some other thread.

   For async threads, it means that input may need to pause the whole
   async thread until scheduler (or whatever code decides when and where
   to read/display input or query) decides that it is appropriate time
   to query the user.

   We may also need to allow some API to make other thread read input
   independently while the current thread continues on. But that's not a
   requirement - I imagine that it can be done with `make-thread' from
   inside the async thread.

2. There should be some kind of indication for the user which thread is
   requesting input:

   - Emacs may display that intput is pending in modeline indicator (aka
     "unread" notifications); or Emacs may display upcoming query in the
     minibuffer until the user explicitly switches there and inputs the
     answer.
     
   - When input query is actually displayed, there should be an
     indication which thread the query belongs to - either a thread name
     or an appropriately designed prompt.

     Possibly, prompt history for the relevant thread may be displayed.

3. Threads may want to group their prompts into chunks that should be
   read without interrupt, so that we do not mix two threads like
   "Login for host1: "->"Password for host1: " and "Login for host2:
   "->"Password for host2: ".

>> > What about changes to frame-parameters?  Those don't necessarily
>> > affect display.
>> 
>> But doesn't it depend on graphic toolkit?
>
> Not necessarily: frame parameters are also used for "frame-local"
> variables, and those have nothing to do with GUI toolkits.

I see.
Then, when an async threads modifies frame parameters, it should be
allowed to do so. However, during redisplay, redisplay code should block
frame parameters - it should not be allowed to change during redisplay.

>> >>    This means that any code that is using things like
>> >>    `save-window-excursion', `display-buffer', and other display-related
>> >>    staff cannot run asynchronously.
>> >
>> > What about with-selected-window? also forbidden?
>> 
>> Yes.
>
> Too bad.

Well. In theory, this is a problem similar to set-buffer - we need to
deal with the fact that Emacs assumes that there is always a single
"current" window and frame.

I assume that this problem is not more difficult than with set-buffer.
If we can solve the problem with global state related to buffer, it
should be doable to solve async window/frame setting. (I am being
optimistic here)

>> >>    Async threads will make an assumption that
>> >>    (set-buffer "1") (goto-char 100) (set-buffer "2") (set-buffer "1")
>> >>    (= (point) 100) invalid.
>> >
> ... 
>> Hmm. I realized that it is already invalid. At least, if `thread-yield'
>> is triggered somewhere between `set-buffer' calls and other thread
>> happens to move point in buffer "1".
>
> But the programmer is in control!  If no such API is called, point
> stays put.  And if such APIs are called, the program can save and
> restore point around the calls.  By contrast, you want to be able to
> pause and resume threads at will, which means a thread can be
> suspended at any time.  So in this case, the programmer will be unable
> to do anything against such calamities.

Right. So, we may need to store per-thread history of PT, BEGV, and ZV
for each buffer that was current during thread execution.

>> >> > What if the main thread modifies buffer text, while one of the other
>> >> > threads wants to read from it?
>> >> 
>> >> Reading and writing should be blocked while buffer is being modified.
>> >
>> > This will basically mean many/most threads will be blocked most of the
>> > time.  Lisp programs in Emacs read and write buffers a lot, and the
>> > notion of forcing a thread to work only on its own single set of
>> > buffers is quite a restriction, IMO.
>> 
>> But not the same buffers!
>
> I don't see why not.

Of course, one may want to run two async threads that modify the same
buffer simultaneously. Not all the Elisp code will need this, but some
may.

And it is indeed a restriction. But I do not see that it should be the
restriction that stops us from implementing async support, even if we
cannot solve it.

> But that's the case with most Emacs Lisp programs: we rarely try too
> hard to make functions pure even if they can be pure.  What's more
> important, most useful programs cannot be pure at all, because Emacs
> is about text processing, which means modifying text, and good Emacs
> Lisp programs process text in buffers, instead of returning strings,
> which makes them not pure.

I am not sure if it is a good aim to design async threads in such a way
that _any_ (rather than explicitly designed) Elisp can work
asynchronously. It would be cool if we could achieve this, but I do not
feel like most of Elisp actually require async.

Yes, most of Elisp is about text processing. But when we really need to
utilize asynchronous code, it is usually not about reading/writing text
- it is about CPU-heavy analysis of text. This analysis is what truly
needs async threads. Writing back, if it is necessary, may be separated
from the analysis code or even done in separate buffer followed by
`buffer-swap-text' or `replace-buffer-contents'.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>



  reply	other threads:[~2023-07-10 11:30 UTC|newest]

Thread overview: 192+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-04 16:58 Concurrency via isolated process/thread Ihor Radchenko
2023-07-04 17:12 ` Eli Zaretskii
2023-07-04 17:29   ` Ihor Radchenko
2023-07-04 17:35     ` Eli Zaretskii
2023-07-04 17:52       ` Ihor Radchenko
2023-07-04 18:24         ` Eli Zaretskii
2023-07-05 11:23           ` Ihor Radchenko
2023-07-05 11:49             ` Eli Zaretskii
2023-07-05 12:40               ` Ihor Radchenko
2023-07-05 13:02                 ` Lynn Winebarger
2023-07-05 13:10                   ` Ihor Radchenko
2023-07-06 18:35                     ` Lynn Winebarger
2023-07-07 11:48                       ` Ihor Radchenko
2023-07-05 13:33                 ` Eli Zaretskii
2023-07-05 13:35                   ` Ihor Radchenko
2023-07-05  0:34         ` Po Lu
2023-07-05 11:26           ` Ihor Radchenko
2023-07-05 12:11             ` Po Lu
2023-07-05 12:44               ` Ihor Radchenko
2023-07-05 13:21                 ` Po Lu
2023-07-05 13:26                   ` Ihor Radchenko
2023-07-05 13:51                     ` Eli Zaretskii
2023-07-05 14:00                       ` Ihor Radchenko
2023-07-06  0:32                         ` Po Lu
2023-07-06 10:46                           ` Ihor Radchenko
2023-07-06 12:24                             ` Po Lu
2023-07-06 12:31                               ` Ihor Radchenko
2023-07-06 12:41                                 ` Po Lu
2023-07-06 12:51                                   ` Ihor Radchenko
2023-07-06 12:58                                     ` Po Lu
2023-07-06 13:13                                       ` Ihor Radchenko
2023-07-06 14:13                                         ` Eli Zaretskii
2023-07-06 14:47                                           ` Ihor Radchenko
2023-07-06 15:10                                             ` Eli Zaretskii
2023-07-06 16:17                                               ` Ihor Radchenko
2023-07-06 18:19                                                 ` Eli Zaretskii
2023-07-07 12:04                                                   ` Ihor Radchenko
2023-07-07 13:16                                                     ` Eli Zaretskii
2023-07-07 14:29                                                       ` Ihor Radchenko
2023-07-07 14:47                                                         ` Eli Zaretskii
2023-07-07 15:21                                                           ` Ihor Radchenko
2023-07-07 18:04                                                             ` Eli Zaretskii
2023-07-07 18:24                                                               ` Ihor Radchenko
2023-07-07 19:36                                                                 ` Eli Zaretskii
2023-07-07 20:05                                                                   ` Ihor Radchenko
2023-07-08  7:05                                                                     ` Eli Zaretskii
2023-07-08 10:53                                                                       ` Ihor Radchenko
2023-07-08 14:26                                                                         ` Eli Zaretskii
2023-07-09  9:36                                                                           ` Ihor Radchenko
2023-07-09  9:56                                                                             ` Po Lu
2023-07-09 10:04                                                                               ` Ihor Radchenko
2023-07-09 11:59                                                                             ` Eli Zaretskii
2023-07-09 13:58                                                                               ` Ihor Radchenko
2023-07-09 14:52                                                                                 ` Eli Zaretskii
2023-07-09 15:49                                                                                   ` Ihor Radchenko
2023-07-09 16:35                                                                                     ` Eli Zaretskii
2023-07-10 11:30                                                                                       ` Ihor Radchenko [this message]
2023-07-10 12:13                                                                                         ` Po Lu
2023-07-10 12:28                                                                                           ` Ihor Radchenko
2023-07-10 12:48                                                                                             ` Po Lu
2023-07-10 12:53                                                                                               ` Ihor Radchenko
2023-07-10 13:18                                                                                                 ` Po Lu
2023-07-10 13:09                                                                                         ` Eli Zaretskii
2023-07-10 13:58                                                                                           ` Ihor Radchenko
2023-07-10 14:37                                                                                             ` Eli Zaretskii
2023-07-10 14:55                                                                                               ` Ihor Radchenko
2023-07-10 16:03                                                                                                 ` Eli Zaretskii
2023-07-16 14:58                                                                                 ` Ihor Radchenko
2023-07-17  7:55                                                                                   ` Ihor Radchenko
2023-07-17  8:36                                                                                     ` Po Lu
2023-07-17  8:52                                                                                       ` Ihor Radchenko
2023-07-17  9:39                                                                                         ` Po Lu
2023-07-17  9:54                                                                                           ` Ihor Radchenko
2023-07-17 10:08                                                                                             ` Po Lu
2023-07-24  8:42                                                                                 ` Ihor Radchenko
2023-07-24  9:52                                                                                   ` Po Lu
2023-07-24 10:09                                                                                     ` Ihor Radchenko
2023-07-24 12:15                                                                                       ` Po Lu
2023-07-24 12:25                                                                                         ` Ihor Radchenko
2023-07-24 13:31                                                                                           ` Po Lu
2023-07-24 13:53                                                                                             ` Ihor Radchenko
2023-07-25  0:12                                                                                               ` Po Lu
2023-07-25  4:28                                                                                                 ` Ihor Radchenko
2023-07-24 12:50                                                                                       ` Eli Zaretskii
2023-07-24 13:15                                                                                         ` Ihor Radchenko
2023-07-24 13:41                                                                                           ` Eli Zaretskii
2023-07-24 14:13                                                                                             ` Ihor Radchenko
2023-07-24 12:44                                                                                   ` Eli Zaretskii
2023-07-24 13:02                                                                                     ` Ihor Radchenko
2023-07-24 13:54                                                                                       ` Eli Zaretskii
2023-07-24 14:24                                                                                         ` Ihor Radchenko
2023-07-24 16:00                                                                                           ` Eli Zaretskii
2023-07-24 16:38                                                                                             ` Ihor Radchenko
2023-07-25  0:20                                                                                               ` Po Lu
2023-07-25  4:36                                                                                                 ` Ihor Radchenko
2023-07-25  7:27                                                                                                   ` Po Lu
2023-07-25  7:59                                                                                                     ` Ihor Radchenko
2023-07-25  8:27                                                                                                       ` Po Lu
2023-07-25  8:45                                                                                                         ` Ihor Radchenko
2023-07-25  8:53                                                                                                           ` Po Lu
2023-07-25  9:03                                                                                                             ` Ihor Radchenko
2023-07-25  9:17                                                                                                               ` Po Lu
2023-07-25  9:27                                                                                                                 ` Ihor Radchenko
2023-07-25  9:37                                                                                                                   ` Po Lu
2023-07-25 12:40                                                                                                                   ` Eli Zaretskii
2023-07-25 11:29                                                                                               ` Eli Zaretskii
2023-07-25 11:52                                                                                                 ` Ihor Radchenko
2023-07-25 14:27                                                                                                   ` Eli Zaretskii
2023-07-09 17:13                                                                             ` Gregory Heytings
2023-07-10 11:37                                                                               ` Ihor Radchenko
2023-07-13 13:54                                                                                 ` Gregory Heytings
2023-07-13 14:23                                                                                   ` Ihor Radchenko
2023-07-07  0:21                                         ` Po Lu
2023-07-06 14:08                             ` Eli Zaretskii
2023-07-06 15:01                               ` Ihor Radchenko
2023-07-06 15:16                                 ` Eli Zaretskii
2023-07-06 16:32                                   ` Ihor Radchenko
2023-07-06 17:50                                     ` Eli Zaretskii
2023-07-07 12:30                                       ` Ihor Radchenko
2023-07-07 13:34                                         ` Eli Zaretskii
2023-07-07 15:17                                           ` Ihor Radchenko
2023-07-07 19:31                                             ` Eli Zaretskii
2023-07-07 20:01                                               ` Ihor Radchenko
2023-07-08  6:50                                                 ` Eli Zaretskii
2023-07-08 11:55                                                   ` Ihor Radchenko
2023-07-08 14:43                                                     ` Eli Zaretskii
2023-07-09  9:57                                                       ` Ihor Radchenko
2023-07-09 12:08                                                         ` Eli Zaretskii
2023-07-09 14:16                                                           ` Ihor Radchenko
2023-07-09 15:00                                                             ` Eli Zaretskii
2023-07-09 12:22                                                         ` Po Lu
2023-07-09 13:12                                                           ` Eli Zaretskii
2023-07-10  0:18                                                             ` Po Lu
2023-07-08  0:51                                               ` Po Lu
2023-07-08  4:18                                                 ` tomas
2023-07-08  5:51                                                   ` Po Lu
2023-07-08  6:01                                                     ` tomas
2023-07-08 10:02                                                       ` Ihor Radchenko
2023-07-08 19:39                                                         ` tomas
2023-07-08  6:25                                                 ` Eli Zaretskii
2023-07-08  6:38                                                   ` Ihor Radchenko
2023-07-08  7:45                                                     ` Eli Zaretskii
2023-07-08  8:16                                                       ` Ihor Radchenko
2023-07-08 10:13                                                         ` Eli Zaretskii
2023-07-07 13:35                                         ` Po Lu
2023-07-07 15:31                                           ` Ihor Radchenko
2023-07-08  0:44                                             ` Po Lu
2023-07-08  4:29                                               ` tomas
2023-07-08  7:21                                               ` Eli Zaretskii
2023-07-08  7:48                                                 ` Po Lu
2023-07-08 10:02                                                   ` Eli Zaretskii
2023-07-08 11:54                                                     ` Po Lu
2023-07-08 14:12                                                       ` Eli Zaretskii
2023-07-09  0:37                                                         ` Po Lu
2023-07-09  7:01                                                           ` Eli Zaretskii
2023-07-09  7:14                                                             ` Po Lu
2023-07-09  7:35                                                               ` Eli Zaretskii
2023-07-09  7:57                                                                 ` Ihor Radchenko
2023-07-09  8:41                                                                   ` Eli Zaretskii
2023-07-10 14:53                                                                     ` Dmitry Gutov
2023-07-09  9:25                                                                 ` Po Lu
2023-07-09 11:14                                                                   ` Eli Zaretskii
2023-07-09 11:23                                                                     ` Ihor Radchenko
2023-07-09 12:10                                                                     ` Po Lu
2023-07-09 13:03                                                                       ` Eli Zaretskii
2023-07-08 12:01                                                     ` Ihor Radchenko
2023-07-08 14:45                                                       ` Eli Zaretskii
2023-07-07  0:41                                     ` Po Lu
2023-07-07 12:42                                       ` Ihor Radchenko
2023-07-07 13:31                                         ` Po Lu
2023-07-07  0:27                                 ` Po Lu
2023-07-07 12:45                                   ` Ihor Radchenko
2023-07-06  0:27                     ` Po Lu
2023-07-06 10:48                       ` Ihor Radchenko
2023-07-06 12:15                         ` Po Lu
2023-07-06 14:10                         ` Eli Zaretskii
2023-07-06 15:09                           ` Ihor Radchenko
2023-07-06 15:18                             ` Eli Zaretskii
2023-07-06 16:36                               ` Ihor Radchenko
2023-07-06 17:53                                 ` Eli Zaretskii
2023-07-07  0:22                             ` Po Lu
2023-07-05  0:33 ` Po Lu
2023-07-05  2:31   ` Eli Zaretskii
2023-07-17 20:43     ` Hugo Thunnissen
2023-07-18  4:51       ` tomas
2023-07-18  5:25       ` Ihor Radchenko
2023-07-18  5:39         ` Po Lu
2023-07-18  5:49           ` Ihor Radchenko
2023-07-18 12:14         ` Hugo Thunnissen
2023-07-18 12:39           ` Async IO and queing process sentinels (was: Concurrency via isolated process/thread) Ihor Radchenko
2023-07-18 12:49             ` Ihor Radchenko
2023-07-18 14:12             ` Async IO and queing process sentinels Michael Albinus

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=87edlg6m2l.fsf@localhost \
    --to=yantar92@posteo.net \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=luangruo@yahoo.com \
    --cc=monnier@iro.umontreal.ca \
    /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).