From: Helmut Eller <eller.helmut@gmail.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 64819@debbugs.gnu.org
Subject: bug#64819: 30.0.50; condition-wait not interruptible
Date: Tue, 25 Jul 2023 10:06:40 +0200 [thread overview]
Message-ID: <m2pm4g8lf3.fsf@gmail.com> (raw)
In-Reply-To: <83r0oxqnwc.fsf@gnu.org> (Eli Zaretskii's message of "Mon, 24 Jul 2023 19:23:31 +0300")
[-- Attachment #1: Type: text/plain, Size: 172 bytes --]
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:
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Make-condition-wait-interruptible-by-C-g.patch --]
[-- Type: text/x-diff, Size: 3180 bytes --]
From 4de3198c10c4efaeaffdf43ba5e5b0f1729a7f09 Mon Sep 17 00:00:00 2001
From: Helmut Eller <eller.helmut@gmail.com>
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);
+ }
+}
+
\f
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
next prev parent reply other threads:[~2023-07-25 8:06 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-24 6:32 bug#64819: 30.0.50; condition-wait not interruptible Helmut Eller
2023-07-24 12:10 ` Eli Zaretskii
2023-07-24 12:58 ` Helmut Eller
2023-07-24 13:34 ` Eli Zaretskii
2023-07-24 14:57 ` Helmut Eller
2023-07-24 16:23 ` Eli Zaretskii
2023-07-25 8:06 ` Helmut Eller [this message]
2023-07-25 12:18 ` Eli Zaretskii
2023-07-25 12:59 ` Helmut Eller
2023-09-02 21:58 ` Stefan Kangas
2023-09-03 19:53 ` Helmut Eller
2023-09-06 9:35 ` Stefan Kangas
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=m2pm4g8lf3.fsf@gmail.com \
--to=eller.helmut@gmail.com \
--cc=64819@debbugs.gnu.org \
--cc=eliz@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.