Subject: Re: bug#28630: 27.0.50; C-g while a non-main thread is sitting crashes Emacs

From: YAMAMOTO Mitsuharu <[email-masked]>
Date: Wed, 04 Oct 2017 16:39:27 +0900

>>>>> On Mon, 02 Oct 2017 19:08:59 +0300, Eli Zaretskii <[email-masked]> said:

> I think the second thread calls setjmp and clobbers the values set
> by the main thread, when the main thread called setjmp.  We need to
> eliminate this possibility of threads stepping on each other's toes.

Make the global variable `getcjmp' thread-local?

			     YAMAMOTO Mitsuharu
			[email-masked]

diff --git a/src/keyboard.c b/src/keyboard.c
index e8701b8870..42d51a2ee8 100644
--- a/src/keyboard.c
+++ b/src/keyboard.c
@@ -145,10 +145,6 @@ static Lisp_Object recover_top_level_message;
 /* Message normally displayed by Vtop_level.  */
 static Lisp_Object regular_top_level_message;
 
-/* For longjmp to where kbd input is being done.  */
-
-static sys_jmp_buf getcjmp;
-
 /* True while displaying for echoing.   Delays C-g throwing.  */
 
 static bool echoing;
@@ -3325,13 +3321,13 @@ record_char (Lisp_Object c)
 static void
 save_getcjmp (sys_jmp_buf temp)
 {
-  memcpy (temp, getcjmp, sizeof getcjmp);
+  memcpy (temp, current_thread->getcjmp, sizeof current_thread->getcjmp);
 }
 
 static void
 restore_getcjmp (sys_jmp_buf temp)
 {
-  memcpy (getcjmp, temp, sizeof getcjmp);
+  memcpy (current_thread->getcjmp, temp, sizeof current_thread->getcjmp);
 }
 
 /* Low level keyboard/mouse input.
@@ -10534,7 +10530,7 @@ quit_throw_to_read_char (bool from_signal)
     do_switch_frame (make_lispy_switch_frame (internal_last_event_frame),
 		     0, 0, Qnil);
 
-  sys_longjmp (getcjmp, 1);
+  sys_longjmp (current_thread->getcjmp, 1);
 }
 
 DEFUN ("set-input-interrupt-mode", Fset_input_interrupt_mode,
diff --git a/src/lisp.h b/src/lisp.h
index 680c25d4c4..ed2423596e 100644
--- a/src/lisp.h
+++ b/src/lisp.h
@@ -1865,8 +1865,6 @@ verify (offsetof (struct Lisp_Sub_Char_Table, contents)
 	== (offsetof (struct Lisp_Vector, contents)
 	    + SUB_CHAR_TABLE_OFFSET * sizeof (Lisp_Object)));
 
-#include "thread.h"
-
 /***********************************************************************
 			       Symbols
  ***********************************************************************/
@@ -3096,6 +3094,8 @@ union specbinding
     } bt;
   };
 
+#include "thread.h"
+
 /* These 3 are defined as macros in thread.h.  */
 /* extern union specbinding *specpdl; */
 /* extern union specbinding *specpdl_ptr; */
diff --git a/src/thread.h b/src/thread.h
index 7fce8674f0..125ca23b55 100644
--- a/src/thread.h
+++ b/src/thread.h
@@ -177,6 +177,9 @@ struct thread_state
      It must do so ASAP.  */
   int not_holding_lock;
 
+  /* For longjmp to where kbd input is being done.  */
+  sys_jmp_buf getcjmp;
+
   /* Threads are kept on a linked list.  */
   struct thread_state *next_thread;
 };