unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Eli Zaretskii <eliz@gnu.org>
To: raimon@konghq.com
Cc: 31671@debbugs.gnu.org
Subject: bug#31671: 26.1; edebug-defun doesn't step if functions run in a separate thread
Date: Tue, 05 Jun 2018 17:51:03 +0300	[thread overview]
Message-ID: <834lihb82w.fsf@gnu.org> (raw)
In-Reply-To: <83r2lmbidf.fsf@gnu.org> (message from Eli Zaretskii on Mon, 04 Jun 2018 19:56:28 +0300)

> Date: Mon, 04 Jun 2018 19:56:28 +0300
> From: Eli Zaretskii <eliz@gnu.org>
> Cc: 31671@debbugs.gnu.org
> 
> The problem here is that Edebug enters recursive-editing (on the
> non-main thread which runs the function 'foo'), then waits for the
> user to press a key.  While it waits, it releases the global lock, and
> the main thread starts running.  So when you press a key, you are on a
> different thread, and 'throw' doesn't have a matching 'catch' (because
> that 'catch' is stored with the handlers of the thread which runs
> 'foo'.
> 
> Not yet sure how to deal with this.  Thoughts and ideas are welcome.

Any objections to the following band-aid?  (Of course, user-error does
nothing visible on non-main threads, and you have too invoke
thread-last-error to see the error message, but at least we don't
leave around a thread in limbo waiting forever for input that will
never come...)

--- src/thread.c~0	2018-01-28 06:56:25.000000000 +0200
+++ src/thread.c	2018-06-05 17:25:43.147566500 +0300
@@ -980,6 +980,23 @@ DEFUN ("thread-last-error", Fthread_last
   return last_thread_error;
 }
 
+DEFUN ("main-thread-p", Fmain_thread_p, Smain_thread_p, 0, 1, 0,
+       doc: /* Return non-nil if THREAD is the main thread.
+If THREAD is nil or omitted, it defaults to the current thread.  */)
+  (Lisp_Object thread)
+{
+  struct thread_state *tstate;
+
+  if (NILP (thread))
+    tstate = current_thread;
+  else
+    {
+      CHECK_THREAD (thread);
+      tstate = XTHREAD (thread);
+    }
+  return main_thread_p (tstate) ? Qt : Qnil;
+}
+
 \f
 
 bool
@@ -1073,6 +1090,7 @@ syms_of_threads (void)
       defsubr (&Scondition_mutex);
       defsubr (&Scondition_name);
       defsubr (&Sthread_last_error);
+      defsubr (&Smain_thread_p);
 
       staticpro (&last_thread_error);
       last_thread_error = Qnil;
--- lisp/emacs-lisp/edebug.el~0	2018-03-14 06:39:59.000000000 +0200
+++ lisp/emacs-lisp/edebug.el	2018-06-05 17:19:52.017355000 +0300
@@ -2425,6 +2425,8 @@
   (if inhibit-redisplay
       ;; Don't really try to enter edebug within an eval from redisplay.
       value
+    (or (main-thread-p)
+        (user-error "Debugging on non-main thread is not yet supported"))
     ;; Check breakpoints and pending input.
     ;; If edebug display should be updated, call edebug--display.
     ;; Return value.
--- lisp/emacs-lisp/debug.el~0	2018-01-03 13:08:56.000000000 +0200
+++ lisp/emacs-lisp/debug.el	2018-06-05 17:31:39.765134800 +0300
@@ -147,6 +147,8 @@
   (if inhibit-redisplay
       ;; Don't really try to enter debugger within an eval from redisplay.
       debugger-value
+    (or (main-thread-p)
+        (user-error "Debugging on non-main thread is not yet supported"))
     (unless noninteractive
       (message "Entering debugger..."))
     (let (debugger-value
--- doc/lispref/threads.texi~0	2018-01-03 13:08:46.000000000 +0200
+++ doc/lispref/threads.texi	2018-06-05 17:29:47.030735200 +0300
@@ -122,6 +122,12 @@
 Return the current thread.
 @end defun
 
+@defun main-thread-p &optional thread
+This function returns non-@code{nil} if @var{thread} is the main
+thread.  If @var{thread} is @code{nil} or omitted, it defaults to the
+current thread.
+@end defun
+
 @defun all-threads
 Return a list of all the live thread objects.  A new list is returned
 by each invocation.
--- etc/NEWS~	2018-03-14 06:39:58.000000000 +0200
+++ etc/NEWS	2018-06-05 17:37:13.301233500 +0300
@@ -361,6 +361,11 @@
 * Lisp Changes in Emacs 27.1
 
 +++
+** New primitive 'main-thread-p'.
+This can be used in code which needs to work differently when it runs
+in threads other than the main thread.
+
++++
 ** New function assoc-delete-all.
 
 ** 'print-quoted' now defaults to t, so if you want to see





  reply	other threads:[~2018-06-05 14:51 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-31 17:37 bug#31671: 26.1; edebug-defun doesn't step if functions run in a separate thread Raimon Grau
2018-06-04 16:56 ` Eli Zaretskii
2018-06-05 14:51   ` Eli Zaretskii [this message]
     [not found] <<87muwfsp4i.fsf@konghq.com>
     [not found] ` <<83r2lmbidf.fsf@gnu.org>
2018-06-04 17:23   ` Drew Adams
2018-06-05 14:47     ` Eli Zaretskii

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

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=834lihb82w.fsf@gnu.org \
    --to=eliz@gnu.org \
    --cc=31671@debbugs.gnu.org \
    --cc=raimon@konghq.com \
    /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 public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).