From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: yyoncho Newsgroups: gmane.emacs.devel Subject: Re: Questions about throw-on-input Date: Sun, 10 May 2020 14:11:48 +0300 Message-ID: References: <1e2afa74-a0b3-1d81-7111-2340f381bb9d@web.de> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="0000000000009c41b005a5494c68" Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="5764"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel To: Alexander Miller Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sun May 10 13:12:48 2020 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jXjt1-0001Od-V0 for ged-emacs-devel@m.gmane-mx.org; Sun, 10 May 2020 13:12:48 +0200 Original-Received: from localhost ([::1]:32958 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jXjt0-0000Vq-WF for ged-emacs-devel@m.gmane-mx.org; Sun, 10 May 2020 07:12:47 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:54576) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jXjsM-0008MW-HU for emacs-devel@gnu.org; Sun, 10 May 2020 07:12:06 -0400 Original-Received: from mail-lj1-x229.google.com ([2a00:1450:4864:20::229]:40512) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jXjsJ-0002Ss-3L for emacs-devel@gnu.org; Sun, 10 May 2020 07:12:05 -0400 Original-Received: by mail-lj1-x229.google.com with SMTP id g1so449018ljk.7 for ; Sun, 10 May 2020 04:12:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=7QhdyAP2QIG1XMLoBZ0JsR8UT55R2GWb9yQZFbVs9M0=; b=pDiR1kibYcvL5s6NinEBZqezafWuVnR75hkEbkSkQusW+gbg+w+iLEWzcY1hy+VU7Z HxqcPklDleyWjqRmfHsv/3qtXPOqgPaCcIgTWADNPOIsa1Kn/oVgoPzokQqZjxa6gSLx k2Ez18AejOLlBSwPgcDgqodAt1RF7vCy8YeDvKdnDqUWz31rtzphm5rAT7xm9ZDAeKjZ beKI4MgjawLNm1GdtpEXYvu3d3kAQItSldRfgoVC2yU6dq2JwWbYIFo2WXZkc8oxAYW+ SlnV+GWn3P7d4yncHkNXpvRM+krqiJFt8MiLf9CRnKuZ+iPLJJz5BUw0VqLIo2dNCGlf 4IGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=7QhdyAP2QIG1XMLoBZ0JsR8UT55R2GWb9yQZFbVs9M0=; b=LX9Lutk9cpGMoiL1pNfVF210YXzYGKsTqh4/zOwHAPDxWpId+wChjhTAA1BKST8E0G EFF0QcQeF/pcwdSQtDakM6qiQ+/BM0xWV8/D/5r7OaCp0dNxxv6BtEjY/jRM/I7gTkoG xo7uweeoK9OrT1QIw+jKVKmcboIByCJQrL3sx21g5k4SOGnzfPB4/3BV0HBntgMYpGpB mz4f7SZp4bTwW+rp+C5mMggA8+BJEDTVLd05Yr9kslSVoRxRFSoeWIOhEXSfQ3YRUPOc lwZoZnlRCYQoGI32KqLlksPBbwHfRZO1xtWMxlUvxbqGR172u4SPgMOGFyrYBs/OGBoK bjbA== X-Gm-Message-State: AOAM5338QVT86X38S+3JBeMba1GMj/a03h3R6TRvrbnLB1UxG+65Ly5t 2jkqRfNQelFhZPTSjepAAKn7Fs/mBmdEZfuTbJk= X-Google-Smtp-Source: ABdhPJwU5lXpqJ3S2G9nAnEuKA64geA581vt1UbCCwZneQMl9NQGxhC6SkCAn7VhL+c1XCGoaro5fZ4PZVbUon0utJk= X-Received: by 2002:a2e:3813:: with SMTP id f19mr7025452lja.216.1589109120056; Sun, 10 May 2020 04:12:00 -0700 (PDT) In-Reply-To: <1e2afa74-a0b3-1d81-7111-2340f381bb9d@web.de> Received-SPF: pass client-ip=2a00:1450:4864:20::229; envelope-from=yyoncho@gmail.com; helo=mail-lj1-x229.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_PASS=-0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 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-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:249646 Archived-At: --0000000000009c41b005a5494c68 Content-Type: text/plain; charset="UTF-8" Thank you, Alexander. I was planning to span a new thread but the approach with the queue might be a better fit for us. Do you have the numbers for what will yield better performance - checking input-pending and then thread-yield or directly calling thread-yield? Ivan On Sat, May 9, 2020 at 4:09 PM Alexander Miller wrote: > [I had to copy the In-Reply-To header from the page source because the > reply-to button > wouldn't set it, so let's hope I'm doing this right.] > > Incidentally I have recently been playing around with a similar idea - > to use a worker thread > to split and run long tasks in the background. Here's what I came up > with so far: > > (defconst worker-mutex (make-mutex "*WORKER MUTEX*")) > (defconst worker-cond-var (make-condition-variable worker-mutex)) > (defvar worker-queue (list)) > > (setf > worker > (make-thread > (lambda () > (while t > (while worker-queue > (let* ((work-unit (pop worker-queue)) > (fn (car work-unit)) > (args (cdr work-unit))) > (apply fn args) > (thread-yield))) > (with-mutex worker-mutex > (condition-wait worker-cond-var)))) > "*WORKER*")) > > (setf worker-timer > (run-with-idle-timer > 1 t (lambda () > (when worker-queue > (with-mutex worker-mutex > (condition-notify worker-cond-var)))))) > > (push (list #'shell-command-to-string "notify-send 'Hello' 'World!'") > worker-queue) > (push (list #'message "Hello %s!" "World") worker-queue) > (push (list #'call-interactively #'treemacs) worker-queue) > > Get enough thread-yield or input-pending checks into the work the thread > is doing and > you might just be able to get around blocking the UI despite having > plenty to do. > > > > --0000000000009c41b005a5494c68 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Thank you, Alexander.

I was planning to= span=C2=A0a new thread but the approach with the queue might be a better= =C2=A0fit for us. Do you have the numbers for what will yield=C2=A0better p= erformance - checking input-pending and then thread-yield=C2=A0or directly = calling thread-yield?

Ivan

On Sat, May 9, 202= 0 at 4:09 PM Alexander Miller <alex= anderm@web.de> wrote:
[I had to copy the In-Reply-To header from the page source bec= ause the
reply-to button
wouldn't set it, so let's hope I'm doing this right.]

Incidentally I have recently been playing around with a similar idea -
to use a worker thread
to split and run long tasks in the background. Here's what I came up with so far:

(defconst worker-mutex (make-mutex "*WORKER MUTEX*"))
(defconst worker-cond-var (make-condition-variable worker-mutex))
(defvar worker-queue (list))

(setf
=C2=A0=C2=A0worker
=C2=A0=C2=A0(make-thread
=C2=A0=C2=A0 (lambda ()
=C2=A0=C2=A0=C2=A0=C2=A0 (while t
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (while worker-queue
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (let* ((work-unit (pop wor= ker-queue))
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0 (fn (car work-unit))
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0 (args (cdr work-unit)))
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (apply fn args= )
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (thread-yield)= ))
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (with-mutex worker-mutex
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (condition-wait worker-con= d-var))))
=C2=A0=C2=A0 "*WORKER*"))

(setf worker-timer
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (run-with-idle-timer
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 1 t (lambda ()
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0 (when worker-queue
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0 (with-mutex worker-mutex
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0 (condition-notify worker-cond-var))))))

(push (list #'shell-command-to-string "notify-send 'Hello'= 'World!'")
worker-queue)
(push (list #'message "Hello %s!" "World") worker-q= ueue)
(push (list #'call-interactively #'treemacs) worker-queue)

Get enough thread-yield or input-pending checks into the work the thread is doing and
you might just be able to get around blocking the UI despite having
plenty to do.



--0000000000009c41b005a5494c68--