From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: John Shahid Newsgroups: gmane.emacs.devel Subject: Re: User interaction from multiple threads Date: Wed, 29 Aug 2018 13:36:55 -0400 Message-ID: <87efehqdlv.fsf@gmail.com> References: <838t59j821.fsf@gnu.org> <87lg92q7ih.fsf@runbox.com> <87bm9xqg46.fsf@runbox.com> <838t51dl10.fsf@gnu.org> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1535564850 3329 195.159.176.226 (29 Aug 2018 17:47:30 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 29 Aug 2018 17:47:30 +0000 (UTC) User-Agent: mu4e 1.1.0; emacs 27.0.50 Cc: psainty@orcon.net.nz, Gemini Lasswell , emacs-devel-bounces+psainty=orcon.net.nz@gnu.org, emacs-devel@gnu.org To: Eli Zaretskii Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Wed Aug 29 19:47:25 2018 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1fv4Yv-0000hV-A6 for ged-emacs-devel@m.gmane.org; Wed, 29 Aug 2018 19:47:25 +0200 Original-Received: from localhost ([::1]:44293 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fv4b1-0004Vd-Q8 for ged-emacs-devel@m.gmane.org; Wed, 29 Aug 2018 13:49:35 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:47241) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fv4ar-00040j-5D for emacs-devel@gnu.org; Wed, 29 Aug 2018 13:49:29 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fv4Os-0001FL-VG for emacs-devel@gnu.org; Wed, 29 Aug 2018 13:37:07 -0400 Original-Received: from mail-qk0-x244.google.com ([2607:f8b0:400d:c09::244]:41866) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fv4Os-0001EU-K7; Wed, 29 Aug 2018 13:37:02 -0400 Original-Received: by mail-qk0-x244.google.com with SMTP id h138-v6so3908905qke.8; Wed, 29 Aug 2018 10:37:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=references:user-agent:from:to:cc:subject:message-id:in-reply-to :date:mime-version; bh=8Q+PFK1p9O+elXjZM1FkA4gVPolN6jZoBpTp0ZQZTnM=; b=n6EVKuvZLWth7uY2cUHuUWZlm+FVywQuPowmOnin2g7/FXVXHG3CU7PmgCfU/XcH0/ zkrhzCABz7g4cVj/MWPBFBp9JLTiHGlb2pqEg7OHWtRw3xUYvyUU37vRI6IwyT1BxzYS i8kPsOZRsYbbSGQ4ATYotciz989KNFS6SVo/5X79SkCfsuYtu92N+NymnCP9ugcw00sY /4AusupaoJKHjuWb9t78Pd19zDTKIylmtLmyy8U489MuFKuDrVX34JfYgp7CJLKd1CAC icGQ9WtzMjyNdBZBOBy2ACVdQJFt/EYXZqOzPEJKZtl1XxwqSgKDsLQNt5noL6x6Quqk oTWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:references:user-agent:from:to:cc:subject :message-id:in-reply-to:date:mime-version; bh=8Q+PFK1p9O+elXjZM1FkA4gVPolN6jZoBpTp0ZQZTnM=; b=ue+vxlktUgZlQ5yZNY98MNppJg9nGFn5C6EKJvZSRIyIvAaZSVX4gExr39liJ/iZjx UQo0DY8CCvLXYD4hfV7zCqJ7Z4HyPAbMsJ7yjzlWM8K9w1tgDZz7IHUAggbpDwbD97fE tN0jJyo0nkYb1oY61UYWNOBe5CzljSQjSG6OsQ+u1yX5qtEO+QALB2toB/e6Ivv9VlVv iBLSgOmQLgS1J2H+d3C5LANYmplK6dmztvykCWexOOLRWEV9L/VSwzOqaxAaoeZZ2Pb6 yydDhOlcmhYCFp/6K2gyE9O6msz65JQqbHg5m1bO1HVkN5OJx0A4hCuWMmEUr0bimjiI YfRg== X-Gm-Message-State: APzg51BBiPYNWbU+hPsDHzwKxnzvmIofwyxlrLSEabVYTbmWVRR3nVM0 zuJeOjA5DM+gKn4f87zbcEue3jfM X-Google-Smtp-Source: ANB0Vdbp2pdiguaBJZUcW3+Zy4p0/X5KKOKOlMWnUONQ2pPGL37idR7JmRo+sH0YrdCblpNcvzuyIA== X-Received: by 2002:a37:5e03:: with SMTP id s3-v6mr7453432qkb.339.1535564220157; Wed, 29 Aug 2018 10:37:00 -0700 (PDT) Original-Received: from amun ([142.154.219.2]) by smtp.gmail.com with ESMTPSA id j5-v6sm2573997qtb.34.2018.08.29.10.36.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 29 Aug 2018 10:36:58 -0700 (PDT) In-reply-to: <838t51dl10.fsf@gnu.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400d:c09::244 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 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.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:229059 Archived-At: I had some thoughts recently on this issue. I see three things being discussed in this thread: 1. How to implement it. 2. How does this implementation interact with current user input, for example if the user is in the middle of a key sequence. 3. How does the change affect the APIs and how to request multiple inputs. 4. How to indicate which thread will the input go to. Regarding the implementation, I'm proposing to use the current primitives and to implement reading key sequences over IPC/RPC. The child thread can request the main thread to display a prompt and return the value that the user entered. This is somewhat similar to the idea of having queues, but I believe has some nice side effects related to the `2'. In fact, this IPC/RPC doesn't have to be limited to prompts. It could be a general macro that takes any body and return its value, e.g.: (on-main-thread (read-file-name "source: ")) Since all interactions between the child thread and the main thread go over IPC, where the main thread act like a server, those prompts will only be displayed if the main thread was idle and waiting for processes input. I believe this answer the second question. In fact, this email thread made me think about asynchronous processes and how they interact with the main thread. A thread is not any different from an asynchronous process. An asynchronous process could (through the process filter) cause user prompt just like threads do. The exception being, process input plays nice with Emacs' input handling. We could make all thread interactions with the display go over IPC and handled the same way like processes. Regarding the API, I'm proposing a new lower level `on-main-thread` macro that can run arbitrary code on the main thread returning the value of the body. This can be used to implement multiple prompts, e.g.: (on-main-thread (let ((src (read-file-name "source: ")) (dest (read-file-name "dest: "))) `(,src ,dest))) I also propose that all high level prompt related functions be modified to do that automatically, e.g.: (defun read-file-name (prompt ...) (if (not main-thread) (on-main-thread (read-file-prompt prompt ...)) ;; current implementation of read-file-prompt )) This can be used for other display related functions, such as changing the current buffer or opening a new file, preferring communication with the main thread as the synchronization mechanism instead of using low level mutexes. As to how to indicate which thread the input will go to, I don't currently have good ideas, other than what have been proposed already in this email thread. I also think this is the same problem as an asynchronous process requesting input. I apologize for any ignorance regarding the internals of Emacs and how this proposal might interact with those internals. I am hoping your responses will enlighten me and teach me a few things. Thanks, -js