all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Qiantan Hong <qhong@mit.edu>
To: Eli Zaretskii <eliz@gnu.org>
Cc: "emacs-devel@gnu.org" <emacs-devel@gnu.org>
Subject: Re: save-excursion and multi-thread?
Date: Mon, 27 Sep 2021 06:14:58 +0000	[thread overview]
Message-ID: <973C3545-7E2C-4AEB-9581-44EFE9E8A395@mit.edu> (raw)
In-Reply-To: <83bl4emulq.fsf@gnu.org>

> Why do you need to start a new thread? why not have the filter
> function send the message to the client, and arrange for waiting for
> the response to that message via the same filter-function method
> (perhaps by changing the filter-function temporarily to some other
> function)?
> 
> IOW, it sounds to me like you need to suspend the execution of the
> command until the client answers the question, and you already have
> the machinery to handle bidirectional communications between the
> client and the server, so just reuse it for asking a question and
> receiving the response, before you perform the command.  Am I missing
> something?
Exactly, however I want the machinery to work for ordinary Emacs “stock” 
commands with minimal effort.

Suppose we opt to rewrite any Emacs command that would be remote-called,
then of course it’s easy to rewrite them so that they immediately return
when they originally READ-FROM-MINIBUFFER while pushing the rest
of the original command as a continuation on some queue 
(this is in fact just a CPS transform). 
The filter function will then execute continuations on the queue upon
receiving response.

We still need to deal with restoring point/mark/etc when switching continuation
(i.e. we need to save state when the command “immediately return” and
restore state when we run the continuation on the queue), but it’s well in our
control and doable.

However I want to use original commands without modification if possible.

My current trick is to run the whole command in new thread, and
override READ-FROM-MINIBUFFER using advice.
It’s conceptually very similar to the above CPS transform solution
(thread-yield is basically (call/cc (lambda (k) (push k continuation-queue))))
However there’s complication that thread-yield might happen at points
out of our control. We thus cannot rely on manually save/restore state
and have to put our hope on some primitive
(just like in a system that switch thread at well defined point one
can implement thread-local dynamic binding in the library, but
has to rely on primitive provided by runtime if there’s preemptive switching
or real parallelism).
Unfortunately such primitive doesn’t seem to exist for point and mark.

Another trick I could think of is to not start a new thread, but
override READ-FROM-MINIBUFFER such that it starts a recursive edit.
Therefore the unfinished state of the command is still on the stack and
we resume it by EXIT-RECURSIVE-EDIT upon receiving response.
It sounds like more madness… and it won’t quite work if the order of receiving
response is different from the order of sending questions.

  reply	other threads:[~2021-09-27  6:14 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-26 17:41 save-excursion and multi-thread? Qiantan Hong
2021-09-26 18:18 ` Eli Zaretskii
2021-09-26 18:28   ` Qiantan Hong
2021-09-26 18:53     ` Eli Zaretskii
2021-09-26 19:09       ` Qiantan Hong
2021-09-26 19:14         ` Eli Zaretskii
2021-09-26 19:13       ` Qiantan Hong
2021-09-26 19:16         ` Eli Zaretskii
2021-09-26 19:21           ` Qiantan Hong
2021-09-26 19:29             ` Eli Zaretskii
2021-09-26 19:42               ` Qiantan Hong
2021-09-26 19:45                 ` Qiantan Hong
2021-09-27  4:36                   ` Eli Zaretskii
2021-09-28 23:33                   ` Richard Stallman
2021-09-29  1:41                     ` dick
2021-09-29  2:59                       ` Qiantan Hong
2021-10-01 22:32                       ` Richard Stallman
2021-09-29 12:45                     ` Eli Zaretskii
2021-10-01 22:35                       ` Richard Stallman
2021-09-27  4:36                 ` Eli Zaretskii
2021-09-27  5:00                   ` Qiantan Hong
2021-09-27  5:54                     ` Eli Zaretskii
2021-09-27  6:14                       ` Qiantan Hong [this message]
2021-09-27 10:13                         ` Eli Zaretskii
2021-09-26 18:21 ` Tassilo Horn
2021-09-26 19:02   ` Eli Zaretskii
2021-09-27 19:35 ` Stefan Monnier

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=973C3545-7E2C-4AEB-9581-44EFE9E8A395@mit.edu \
    --to=qhong@mit.edu \
    --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 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.