From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Helmut Eller Newsgroups: gmane.emacs.bugs Subject: bug#64819: 30.0.50; condition-wait not interruptible Date: Tue, 25 Jul 2023 10:06:40 +0200 Message-ID: References: <83pm4hse6n.fsf@gnu.org> <838rb5saau.fsf@gnu.org> <83r0oxqnwc.fsf@gnu.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="8340"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: 64819@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue Jul 25 10:07:23 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@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 1qOD4l-0001zi-9p for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 25 Jul 2023 10:07:23 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qOD4S-0006oN-5Z; Tue, 25 Jul 2023 04:07:04 -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 ) id 1qOD4Q-0006oD-C9 for bug-gnu-emacs@gnu.org; Tue, 25 Jul 2023 04:07:02 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qOD4Q-00064O-3n for bug-gnu-emacs@gnu.org; Tue, 25 Jul 2023 04:07:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qOD4P-0002Mr-JS for bug-gnu-emacs@gnu.org; Tue, 25 Jul 2023 04:07:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Helmut Eller Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 25 Jul 2023 08:07:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 64819 X-GNU-PR-Package: emacs Original-Received: via spool by 64819-submit@debbugs.gnu.org id=B64819.16902724139086 (code B ref 64819); Tue, 25 Jul 2023 08:07:01 +0000 Original-Received: (at 64819) by debbugs.gnu.org; 25 Jul 2023 08:06:53 +0000 Original-Received: from localhost ([127.0.0.1]:44590 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qOD4H-0002MU-4K for submit@debbugs.gnu.org; Tue, 25 Jul 2023 04:06:53 -0400 Original-Received: from mail-wm1-x32f.google.com ([2a00:1450:4864:20::32f]:53464) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qOD4B-0002MD-TQ for 64819@debbugs.gnu.org; Tue, 25 Jul 2023 04:06:52 -0400 Original-Received: by mail-wm1-x32f.google.com with SMTP id 5b1f17b1804b1-3fbc12181b6so51133165e9.2 for <64819@debbugs.gnu.org>; Tue, 25 Jul 2023 01:06:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1690272402; x=1690877202; h=mime-version:user-agent:message-id:date:references:in-reply-to :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to; bh=2+7PaJJdax7RZcquvQuR73pVikEc3BmHffUAdOO2dRA=; b=GwBX2L4VE1957f7g3Vy9Q2x3F+3yPhzKLN+EShOArI8QK2DQYMuCWpGo6l8DkKVcXO ibYmNRteNtDhOpdrf8h//1PtTSZpJHTj8qCdmvkM1UqhgE69rmGeQCP7/ntwwAMZBBzG D504ImK2jMg6peWAQZz/tL5lnTNqOAGgvV9zBPb8SswG//8UCkKzcjzvekMjkJjmo3DF F01yR/es8ty8QzoysIHJQwIJ7GhRIJHpL578bAAPnXxINIThQ2dVaIF4J/+Ks4USf+nx /gJ9yhI8/uCBDA2QVjyhspg5ztf9oGhL+xrmcwW5JDnpN3KL4JsQxlRmV5iA7pDo0yvk tcVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690272402; x=1690877202; h=mime-version:user-agent:message-id:date:references:in-reply-to :subject:cc:to:from:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=2+7PaJJdax7RZcquvQuR73pVikEc3BmHffUAdOO2dRA=; b=FKnbYvSfEarqH1z40jn9etEy37nBowVY9Oe6FNMyB2ATREXZv7ChAlMxsmQiFjQvdg dytaypkZGKl7B01F7I0L8nfFXgLIL90YvjWkCsyQt5RQLaCoCP/q9i2WyO6nfz1WY8DW uTEDEdeTAaz/txYPHeQA9CzSaDcfWiFMFKfxzrwaTgwazK1FWN9Vt4pGlnJ5qThpQ63d SaLd6go0OSzdgbbCTyaB1w1tidIq1PC3xjtgra3M6fnyWzIZTXBoJsWar046DVb8Nk4R AyCkhvY3ugS3BUT2WDwWJKgsUJ7D/ZObgoh0Xznw0JEPml27kZkZPDPjqGlZFP8PyrYe nFHA== X-Gm-Message-State: ABy/qLYzzpI2DFEZTZv8wcsqnK627JBHOqv5QTk5TxRLHhl4DuswqUjH 6ilTfdS/OzP3wZUSjxTgWyOXfCTMB5g= X-Google-Smtp-Source: APBJJlH/FZlQPx1zWX9iR2y4g6COFMc/JIrc5V9hLwWFXaBuZXkLMs88BYIZvOwMAUFK71YI5wxtFA== X-Received: by 2002:a05:600c:2106:b0:3f9:ba2:5d19 with SMTP id u6-20020a05600c210600b003f90ba25d19mr8733894wml.33.1690272401893; Tue, 25 Jul 2023 01:06:41 -0700 (PDT) Original-Received: from caladan (dial-183072.pool.broadband44.net. [212.46.183.72]) by smtp.gmail.com with ESMTPSA id j16-20020adfea50000000b0031424f4ef1dsm15588381wrn.19.2023.07.25.01.06.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Jul 2023 01:06:41 -0700 (PDT) In-Reply-To: <83r0oxqnwc.fsf@gnu.org> (Eli Zaretskii's message of "Mon, 24 Jul 2023 19:23:31 +0300") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:266028 Archived-At: --=-=-= Content-Type: text/plain On Mon, Jul 24 2023, Eli Zaretskii wrote: [...] > So for this to work, the C-g handler will have to release some thread. Here is a patch that works good enough for me: --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-Make-condition-wait-interruptible-by-C-g.patch >From 4de3198c10c4efaeaffdf43ba5e5b0f1729a7f09 Mon Sep 17 00:00:00 2001 From: Helmut Eller Date: Tue, 25 Jul 2023 10:03:53 +0200 Subject: [PATCH] Make condition-wait interruptible by C-g Code like (let* ((mutex (make-mutex)) (cvar (make-condition-variable mutex))) (with-mutex mutex (condition-wait cvar))) will block in pthread_cond_wait. The problem is that pthread_cond_wait may or may not return when it gets interrupted by a signal (SIGIO). On Linux it doesn't return and so even if a signal handler sets pending_signals=true nobody processes those pending signals. The patch modifies the signal handler so that it will force a spurious wakeup when the current thread is blocked in condition-wait. * src/keyboard.c (handle_input_available_signal) (handle_interrupt): Call maybe_awake_current_thread. * src/thread.c (maybe_awake_current_thread): New. * src/thread.h (maybe_awake_current_thread): New prototype. --- src/keyboard.c | 8 +++++++- src/thread.c | 16 ++++++++++++++++ src/thread.h | 1 + 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/keyboard.c b/src/keyboard.c index 41cda2e65de..f45bafa96c0 100644 --- a/src/keyboard.c +++ b/src/keyboard.c @@ -7745,6 +7745,10 @@ handle_input_available_signal (int sig) if (input_available_clear_time) *input_available_clear_time = make_timespec (0, 0); + +#ifdef THREADS_ENABLED + maybe_awake_current_thread (); +#endif } static void @@ -11556,8 +11560,10 @@ handle_interrupt (bool in_signal_handler) /* If we were called from a signal handler, we must be in the main thread, see deliver_process_signal. So we must make sure the main thread holds the global lock. */ - if (in_signal_handler) + if (in_signal_handler) { maybe_reacquire_global_lock (); + maybe_awake_current_thread(); + } #endif if (waiting_for_input && !echoing) quit_throw_to_read_char (in_signal_handler); diff --git a/src/thread.c b/src/thread.c index b8ca56fd372..0bd949f5779 100644 --- a/src/thread.c +++ b/src/thread.c @@ -172,6 +172,22 @@ maybe_reacquire_global_lock (void) } } +/* This is called from keyboard.c when it sets pending_signals=true. + If the current thread is waiting, we create a spurious wakeup by + broadcasting on wait_condvar. This is necessary because + pthread_cond_wait may or may not return if it was interrupted by a + signal (SIGIO). Without the wakeup, nobody would process a + potential C-g. +*/ +void +maybe_awake_current_thread (void) +{ + if (current_thread->wait_condvar != NULL) + { + sys_cond_broadcast (current_thread->wait_condvar); + } +} + static void diff --git a/src/thread.h b/src/thread.h index 9b14cc44f35..60f601a6248 100644 --- a/src/thread.h +++ b/src/thread.h @@ -311,6 +311,7 @@ extern void finalize_one_thread (struct thread_state *state); extern void finalize_one_mutex (struct Lisp_Mutex *); extern void finalize_one_condvar (struct Lisp_CondVar *); extern void maybe_reacquire_global_lock (void); +extern void maybe_awake_current_thread (void); extern void init_threads (void); extern void syms_of_threads (void); -- 2.39.2 --=-=-=--