From: Ihor Radchenko <yantar92@posteo.net>
To: Eli Zaretskii <eliz@gnu.org>
Cc: Stefan Monnier <monnier@iro.umontreal.ca>,
luangruo@yahoo.com, emacs-devel@gnu.org
Subject: Re: Concurrency via isolated process/thread
Date: Sun, 09 Jul 2023 15:49:41 +0000 [thread overview]
Message-ID: <87v8et6q5m.fsf@localhost> (raw)
In-Reply-To: <83cz119lxu.fsf@gnu.org>
Eli Zaretskii <eliz@gnu.org> writes:
>> I now understand that the gap can be moved by the code that is not
>> actually writing text in buffer. However, I do not see how this is a
>> problem we need to care about more than about generic problem with
>> simultaneous write.
>
> Imagine a situation where we need to process XML or HTML, and since
> that's quite expensive, we want to do that in a thread. What you are
> saying is that this will either be impossible/impractical to do from a
> thread, or will require to lock the entire buffer from access, because
> the above processing moves the gap. If that is not a problem, I don't
> know what is, because there could be a lot of such scenarios, and they
> all will be either forbidden or very hard to implement.
In this particular example, the need to move the gap is there because
htmlReadMemory requires memory segment as input. Obviously, it requires
a block in the current implementation.
Can it be done async-safe? We would need to memcpy the parsed buffer
block. Or up to 3 memcpy if we do not want to move the gap.
>> If a variable or object value is being written, we need to block it.
>> If a buffer object is being written (like when moving the gap or writing
>> text), we need to block it. And this blocking will generally pose a
>> problem only when multiple threads try to access the same object, which
>> is generally unlikely.
>
> My impression is that this is very likely, because of the many global
> objects in Emacs.
There are many objects, but each individual thread will use a subset of
these objects. What are the odds that intersection of these subsets are
frequent? Not high, except certain frequently used objects. And we need
to focus on identifying and figuring out what to do with these
likely-to-clash objects.
> ... Moreover, if you intend to allow several threads
> using the same buffer (and I'm not yet sure whether you want that or
> not),
It would be nice if multiple threads can work with the same buffer in
read-only mode, maybe with a single main thread editing the buffer (and
pausing the async read-only threads while doing so).
Writing simultaneously is a much bigger ask.
> ... then the buffer-local variables of that buffer present the same
> problem as global variables. Take the case-table or display-table,
> for example: those are buffer-local in many cases, but their changes
> will affect all the threads that work on the buffer.
And how frequently are case-table and display-table changed? AFAIK, not
frequently at all.
>> We need to ensure that simultaneous consing will never happen. AFAIU,
>> it should be ok if something that does not involve consing is running
>> at the same time with cons (correct me if I am wrong here).
>
> What do you do if some thread hits the memory-full condition? The
> current handling includes GC.
May you please explain a bit more about the situation you are referring
to? My above statement was about consing, not GC.
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.
>> 2. Redisplay cannot be asynchronous in a sense that it does not make
>> sense that multiple threads, possibly working with different buffers
>> and different points in those buffers, request redisplay
>> simultaneously. Of course, it is impossible to display several places
>> in a buffer at once.
>
> But what about different threads redisplaying different windows? is
> that allowed? If not, here goes one more benefit of concurrent
> threads.
I think I need to elaborate what I mean by "redisplay cannot be
asynchronous".
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.
Is Emacs display code even capable of redisplaying two different windows
at the same time?
> Also, that issue with prompting the user also needs some solution,
> otherwise the class of jobs that non-main threads can do will be even
> smaller.
We can make reading input using similar idea to the above, but it will
always block until the response.
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.
>> Only a single `main-thread' should be allowed to modify frames,
>> window configurations, and generally trigger redisplay. And thread
>> that attempts to do such modifications must wait to become
>> `main-thread' first.
>
> What about changes to frame-parameters? Those don't necessarily
> affect display.
But doesn't it depend on graphic toolkit? I got an impression (from Po
Lu's replies) that graphic toolkits generally do not handle async
requests well.
>> 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. A given frame must always have a single window active, which is not
compatible with async threads.
In addition, `with-selected-window' triggers redisplay. In particular,
it triggers redisplaying mode-lines.
It is a problem similar to async redisplay.
>> Async threads will make an assumption that
>> (set-buffer "1") (goto-char 100) (set-buffer "2") (set-buffer "1")
>> (= (point) 100) invalid.
>
> If this is invalid, I don't see how one can write useful Lisp
> programs, except of we request Lisp to explicitly define critical
> sections.
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 I realize that something like
(while (re-search-forward "foo") nil t)
(with-current-buffer "bar" (insert (match-string 0))))
may be broken if point is moved when switching between "bar" and "foo".
Maybe, the last PV, ZV, and BEGV should not be stored in the buffer
object upon switching away and instead recorded in a thread-local
((buffer PV ZV BEGV) ...) alist. Then, thread will set PV, ZV, and BEGV
from its local alist rather than by reading buffer->... values.
>> > 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!
>> >> >> For example, `org-element-interpret-data' converts Org mode AST to
>> >> >> string. Just now, I tried it using AST of one of my large Org buffers.
>> >> >> It took 150seconds to complete, while blocking Emacs.
>> >> >
>> >> > It isn't side-effect-free, though.
>> >>
>> >> It is, just not declared so.
>> >
>> > No, it isn't. For starters, it changes obarray.
>>
>> Do you mean `intern'? `intern-soft' would be equivalent there.
>
> "Equivalent" in what way? AFAIU, the function does want to create a
> symbol when it doesn't already exist.
No.
(intern (format "org-element-%s-interpreter" type)) is just to retrieve
existing function symbol used for a given AST element type.
(interpret
(let ((fun (intern-soft
(format "org-element-%s-interpreter" type))))
(if (and fun (fboundp fun)) fun (lambda (_ contents) contents))))
would also work.
To be clear, I do know how this function is designed to work.
It may not be de-facto pure, but that's just because nobody tried to
ensure it - the usefulness of pure declarations is questionable in Emacs
now.
>> There will indeed be a lot of work to make the range of Lisp functions
>> available for async code large enough. But it does not have to be done
>> all at once.
>
> No, it doesn't. But until we have enough of those functions
> available, one will be unable to write applications without
> implementing and debugging a lot of those new functions as part of the
> job. It will make simple programming jobs much larger and more
> complicated, especially since it will require the programmers to
> understand very well the limitations and requirements of concurrent
> code programming, something Lisp programmers don't know very well, and
> rightfully so.
I disagree.
If Emacs supports async threads, it does not mean that every single
peace of Elisp should be async-compatible.
But if a programmer is explicitly writing async code, it is natural to
expect limitations.
--
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>
next prev parent reply other threads:[~2023-07-09 15:49 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 [this message]
2023-07-09 16:35 ` Eli Zaretskii
2023-07-10 11:30 ` Ihor Radchenko
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87v8et6q5m.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 external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.