From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: 813gan <813gan@protonmail.com> Newsgroups: gmane.emacs.devel Subject: Re: Threads vs url-http Date: Sun, 29 Sep 2024 23:53:44 +0000 Message-ID: References: <3GKP9PAUIwZh8IYYiDahnaeVDrY2O7V_LkjMJ_EcOfSz0ltAUheLQ47o9l8VIm0-8vGVQpKuFMjq2qLiHVN3BIeLKx_AbaacMBQbfvR9_cs=@protonmail.com> <8634lj58uc.fsf@gnu.org> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="18369"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel@gnu.org To: Eli Zaretskii Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Mon Sep 30 01:54:56 2024 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 1sv3ke-0004eL-IM for ged-emacs-devel@m.gmane-mx.org; Mon, 30 Sep 2024 01:54:56 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sv3jw-0007Hf-6D; Sun, 29 Sep 2024 19:54:14 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <813gan@protonmail.com>) id 1sv3jl-0007HP-I6 for emacs-devel@gnu.org; Sun, 29 Sep 2024 19:54:01 -0400 Original-Received: from mail-40133.protonmail.ch ([185.70.40.133]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <813gan@protonmail.com>) id 1sv3jb-0004IH-HA for emacs-devel@gnu.org; Sun, 29 Sep 2024 19:54:01 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1727654028; x=1727913228; bh=3eAQQOhQxxiIu1RJktgAJGMeuKbuGA2cYPEhceqdUVs=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=FJc2fcBBPymbc6kic+km1xxrkMkKGaYUjvmeBsFW5AnVbW/5xHB/eqHJIkLn1ogS0 mrkUxR1rT5de3TSnTkUCcZ6jhltqm3YkM3/RyouL+ztDzAAIju4Y5VKZbLuHgdygtA bCqmoqAMxNaoGVEbb8d2WodoEXy9XobLElOYl1/6j2o3yttYHC1GkktXKgFRECuZvU zakayZyo8WpOdE1JAItnsz+qTVR/NynQ85B9bsCZ6YdVrI4vqqJ3ATV1TkTGJtPWy6 S9+czv+SeskOjBzLETVOD63q/rErYVUltSWRZwztrIY9HHiywxgmkDr5mZm8Zp3bKm B2BeeFC6Cu64g== In-Reply-To: <8634lj58uc.fsf@gnu.org> Feedback-ID: 117569825:user:proton X-Pm-Message-ID: a7a2f64cf346fa8ac6181470136856a2b09d4cd5 Received-SPF: pass client-ip=185.70.40.133; envelope-from=813gan@protonmail.com; helo=mail-40133.protonmail.ch X-Spam_score_int: -13 X-Spam_score: -1.4 X-Spam_bar: - X-Spam_report: (-1.4 / 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, FROM_STARTS_WITH_NUMS=0.738, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 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-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:324203 Archived-At: On Sunday, September 29th, 2024 at 07:26, Eli Zaretskii wrot= e: >=20 >=20 > > Date: Sun, 29 Sep 2024 00:28:36 +0000 >=20 > > From: 813gan 813gan@protonmail.com > >=20 > > (require 'url-http) > > (require 'url-parse) > > (defun url-retrieve-thread (url) > > "Wrapper around url-http that download `URL' without blocking main thre= ad." > > (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 (cond= ition-notify cond-var)) )) ) > > (url-http parsed-url cb nil) > > (with-mutex mut > > (condition-wait cond-var) ) > > buf)) > >=20 > > Idea was to call it from thread. In most trivial case: > > (make-thread (apply 'url-retrieve-thread "http://example.com" nil)) >=20 >=20 > 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. >=20 > AFAIU your intent, you should wait on the condvar in the main thread, > not in the thread which calls url-http. >=20 > > It makes Emacs unresponsive (even C-g does not work). >=20 >=20 > 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))) =09 (buf nil) =09 (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