From: 813gan <813gan@protonmail.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: emacs-devel@gnu.org
Subject: Re: Threads vs url-http
Date: Sun, 29 Sep 2024 23:53:44 +0000 [thread overview]
Message-ID: <UgdWoRkf0HbyGdpZ6Z1jEmQKtWD-ID9LWWdmw8ceFAZXmdURYu9iMzI1HUA3M5BnihGLlu55dD0cOZZK0J1K4-7buOT69iaXO_9WRG5_wuk=@protonmail.com> (raw)
In-Reply-To: <8634lj58uc.fsf@gnu.org>
On Sunday, September 29th, 2024 at 07:26, Eli Zaretskii <eliz@gnu.org> wrote:
>
>
> > Date: Sun, 29 Sep 2024 00:28:36 +0000
>
> > From: 813gan 813gan@protonmail.com
> >
> > (require 'url-http)
> > (require 'url-parse)
> > (defun url-retrieve-thread (url)
> > "Wrapper around url-http that download `URL' without blocking main thread."
> > (let* ((parsed-url (if (url-p url) url (url-generic-parse-url url)))
> > (mut (make-mutex))
> > (cond-var (make-condition-variable mut))
> > (buf nil)
> > (cb (lambda (&rest _) (setq buf (current-buffer)) (with-mutex mut (condition-notify cond-var)) )) )
> > (url-http parsed-url cb nil)
> > (with-mutex mut
> > (condition-wait cond-var) )
> > buf))
> >
> > Idea was to call it from thread. In most trivial case:
> > (make-thread (apply 'url-retrieve-thread "http://example.com" nil))
>
>
> You are waiting on a condvar in the same thread which should notify
> the condvar, don't you? The callback passed to url-http will be
> called in the context of the same thread in which url-http was called,
> but that thread is meanwhile blocked in the condition-wait you call
> after url-http.
>
> AFAIU your intent, you should wait on the condvar in the main thread,
> not in the thread which calls url-http.
>
> > It makes Emacs unresponsive (even C-g does not work).
>
>
> Yes, because it's deadlock: a thread is waiting in a condvar which no
> other thread will call.
Yeah. my understanding of parallelism seems to be worst than i excepted.
Other implementation (naive?) follows.
(defun wiki-read--url-retrieve (url)
"Wrapper around url-http that download `URL' without blocking thread."
(let* ((parsed-url (if (url-p url) url (url-generic-parse-url url)))
(buf nil)
(cb (lambda (&rest _) (setq buf (current-buffer)) )) )
(url-https parsed-url cb nil)
(while (not buf) (thread-yield))
buf))
(make-thread (apply 'wiki-read--url-retrieve "https://google.com" nil))
Why thread-yield don't unblock this?
Thanks
next prev parent reply other threads:[~2024-09-29 23:53 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-29 0:28 Threads vs url-http 813gan
2024-09-29 5:26 ` Eli Zaretskii
2024-09-29 23:53 ` 813gan [this message]
2024-09-30 7:38 ` Michael Albinus
2024-09-30 12:22 ` Eli Zaretskii
2024-10-06 15:06 ` 813gan
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='UgdWoRkf0HbyGdpZ6Z1jEmQKtWD-ID9LWWdmw8ceFAZXmdURYu9iMzI1HUA3M5BnihGLlu55dD0cOZZK0J1K4-7buOT69iaXO_9WRG5_wuk=@protonmail.com' \
--to=813gan@protonmail.com \
--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.