unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Is automatic yield support feasible for threads?
@ 2021-11-04 17:10 ndame via Users list for the GNU Emacs text editor
  2021-11-09 14:53 ` Philipp
  0 siblings, 1 reply; 5+ messages in thread
From: ndame via Users list for the GNU Emacs text editor @ 2021-11-04 17:10 UTC (permalink / raw)
  To: help-gnu-emacs@gnu.org

AFAIK Emacs has cooperative threading, so a badly behaving thread can prevent UI updates, therefore if some costly operation is moved into a separate thread then the code has to be modified in order to yield periodically if the code doesn't do I/O.

If that's the case then what if the interpreter itself could support automatic yielding when some flag is set, so it could yield automatically after every several instructions?

For example, make-thread could have an optional argument specifying after how many instructions a thread-yield should be called automatically and if this argument is set then a global flag is set, so the interpreter knowns it should call yield regularly.

This automatic yielding would have some cost, but in return any code could safely be moved into a separate thread without the danger of locking up the UI if this optional argument is given to make-thread. So it could makes using cooperative threads easier.

Could such an automatic yield support be feasible for threads?

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Is automatic yield support feasible for threads?
  2021-11-04 17:10 Is automatic yield support feasible for threads? ndame via Users list for the GNU Emacs text editor
@ 2021-11-09 14:53 ` Philipp
  2021-11-10  7:46   ` ndame
  0 siblings, 1 reply; 5+ messages in thread
From: Philipp @ 2021-11-09 14:53 UTC (permalink / raw)
  To: ndame; +Cc: help-gnu-emacs@gnu.org



> Am 04.11.2021 um 18:10 schrieb ndame via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org>:
> 
> AFAIK Emacs has cooperative threading, so a badly behaving thread can prevent UI updates, therefore if some costly operation is moved into a separate thread then the code has to be modified in order to yield periodically if the code doesn't do I/O.
> 
> If that's the case then what if the interpreter itself could support automatic yielding when some flag is set, so it could yield automatically after every several instructions?
> 
> For example, make-thread could have an optional argument specifying after how many instructions a thread-yield should be called automatically and if this argument is set then a global flag is set, so the interpreter knowns it should call yield regularly.
> 
> This automatic yielding would have some cost, but in return any code could safely be moved into a separate thread without the danger of locking up the UI if this optional argument is given to make-thread. So it could makes using cooperative threads easier.
> 
> Could such an automatic yield support be feasible for threads?

I don't think it's feasible.  Emacs Lisp code typically relies heavily on mutating global state (think buffer contents, the buffer list, `point', dynamic variables, ...), and normally doesn't put such state mutations into properly synchronized critical sections.  Yielding from such unprotected critical sections would result in very subtle bugs (`point' randomly moving around, corruption of internal data structures, buffers vanishing surprisingly, ...).


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Is automatic yield support feasible for threads?
  2021-11-09 14:53 ` Philipp
@ 2021-11-10  7:46   ` ndame
  2021-11-10 13:46     ` Eli Zaretskii
  2021-11-11 18:11     ` Philipp
  0 siblings, 2 replies; 5+ messages in thread
From: ndame @ 2021-11-10  7:46 UTC (permalink / raw)
  To: Philipp; +Cc: help-gnu-emacs@gnu.org

On Tuesday, November 9th, 2021 at 3:53 PM, Philipp <p.stephani2@gmail.com> wrote:
>
> I don't think it's feasible. Emacs Lisp code typically relies heavily on mutating global state (think buffer contents, the buffer list, `point', dynamic variables, ...), and normally doesn't put such state mutations into properly synchronized critical sections. Yielding from such unprotected critical sections would result in very subtle bugs (`point' randomly moving around, corruption of internal data structures, buffers vanishing surprisingly, ...).

I don't think threads should be used for UI manipulation, that should be left for the main thread. Threads are more useful for longer running calculations, computations and those should have no problem if they work on local data.

But you are right if someone uses a thread for UI changes then there can be problems. My idea is about an _optional_ automatic yielding for threads, so it still could be useful for those threads which are self contained. Those could choose to start with periodic automatic yielding, so their code do not have to be sprinkled with manual yield calls, impacting code readability.




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Is automatic yield support feasible for threads?
  2021-11-10  7:46   ` ndame
@ 2021-11-10 13:46     ` Eli Zaretskii
  2021-11-11 18:11     ` Philipp
  1 sibling, 0 replies; 5+ messages in thread
From: Eli Zaretskii @ 2021-11-10 13:46 UTC (permalink / raw)
  To: help-gnu-emacs

> Date: Wed, 10 Nov 2021 07:46:48 +0000
> From: ndame <laszlomail@protonmail.com>
> Cc: "help-gnu-emacs@gnu.org" <help-gnu-emacs@gnu.org>
> 
> But you are right if someone uses a thread for UI changes then there can be problems. My idea is about an _optional_ automatic yielding for threads, so it still could be useful for those threads which are self contained. Those could choose to start with periodic automatic yielding, so their code do not have to be sprinkled with manual yield calls, impacting code readability.

How can a thread be made to yield at some arbitrary point of its code
without risking problems?  E.g., a thread could be in the middle of
some operation that must be done atomically.



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Is automatic yield support feasible for threads?
  2021-11-10  7:46   ` ndame
  2021-11-10 13:46     ` Eli Zaretskii
@ 2021-11-11 18:11     ` Philipp
  1 sibling, 0 replies; 5+ messages in thread
From: Philipp @ 2021-11-11 18:11 UTC (permalink / raw)
  To: ndame; +Cc: help-gnu-emacs@gnu.org



> Am 10.11.2021 um 08:46 schrieb ndame <laszlomail@protonmail.com>:
> 
> On Tuesday, November 9th, 2021 at 3:53 PM, Philipp <p.stephani2@gmail.com> wrote:
>> 
>> I don't think it's feasible. Emacs Lisp code typically relies heavily on mutating global state (think buffer contents, the buffer list, `point', dynamic variables, ...), and normally doesn't put such state mutations into properly synchronized critical sections. Yielding from such unprotected critical sections would result in very subtle bugs (`point' randomly moving around, corruption of internal data structures, buffers vanishing surprisingly, ...).
> 
> I don't think threads should be used for UI manipulation, that should be left for the main thread. Threads are more useful for longer running calculations, computations and those should have no problem if they work on local data.

There are two problems with that approach:
1. Emacs doesn't clearly separate UI work from non-UI work.  For example, buffers are primarily a UI feature, but are also widely used for string manipulation unrelated to any UI action, and such uses are typically considered implementation details.
2. There are many coding patterns involving global state mutations that are unrelated to UI actions, e.g., caching the result of an expensive computation.

> 
> But you are right if someone uses a thread for UI changes then there can be problems. My idea is about an _optional_ automatic yielding for threads, so it still could be useful for those threads which are self contained.

Only trivial thread bodies are guaranteed to be self-contained.  You'd need to audit every single function that the thread body calls whether it performs any unsynchronized global state mutations, which is infeasible since such state mutations are generally considered implementation details, and functions typically don't document them.




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-11-11 18:11 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-11-04 17:10 Is automatic yield support feasible for threads? ndame via Users list for the GNU Emacs text editor
2021-11-09 14:53 ` Philipp
2021-11-10  7:46   ` ndame
2021-11-10 13:46     ` Eli Zaretskii
2021-11-11 18:11     ` Philipp

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