aboutsummaryrefslogtreecommitdiffstats
path: root/src/thread.c
diff options
context:
space:
mode:
authorEli Zaretskii2016-12-19 19:11:16 +0200
committerEli Zaretskii2016-12-19 19:11:16 +0200
commitfe3188b1cecc7ac5534616c8edf14a84b1b3bbb0 (patch)
tree39041b114c679f98255e9c95f2b4666d6f269ab2 /src/thread.c
parent657bcaf5ac30449915e070c3fa80a2eaaf1ee7e1 (diff)
downloademacs-fe3188b1cecc7ac5534616c8edf14a84b1b3bbb0.tar.gz
emacs-fe3188b1cecc7ac5534616c8edf14a84b1b3bbb0.zip
Fix crashes upon C-g on Posix TTY frames
* src/thread.h (struct thread_state): New member not_holding_lock. (maybe_reacquire_global_lock): Add prototype. * src/thread.c: Include syssignal.h. (maybe_reacquire_global_lock): New function. (really_call_select): Set the not_holding_lock member of the thread state before releasing the lock, and rest it after re-acquiring the lock when the select function returns. Block SIGINT while doing this to make sure we are not interrupted on TTY frames. * src/sysdep.c (block_interrupt_signal, restore_signal_mask): New functions. * src/syssignal.h (block_interrupt_signal, restore_signal_mask): Add prototypes. * src/keyboard.c (read_char) [THREADS_ENABLED]: Call maybe_reacquire_global_lock. (Bug#25178)
Diffstat (limited to 'src/thread.c')
-rw-r--r--src/thread.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/thread.c b/src/thread.c
index e8cb430119f..bf2cf1b06c8 100644
--- a/src/thread.c
+++ b/src/thread.c
@@ -24,6 +24,7 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
24#include "buffer.h" 24#include "buffer.h"
25#include "process.h" 25#include "process.h"
26#include "coding.h" 26#include "coding.h"
27#include "syssignal.h"
27 28
28static struct thread_state primary_thread; 29static struct thread_state primary_thread;
29 30
@@ -100,6 +101,23 @@ acquire_global_lock (struct thread_state *self)
100 post_acquire_global_lock (self); 101 post_acquire_global_lock (self);
101} 102}
102 103
104/* This is called from keyboard.c when it detects that SIGINT
105 interrupted thread_select before the current thread could acquire
106 the lock. We must acquire the lock to prevent a thread from
107 running without holding the global lock, and to avoid repeated
108 calls to sys_mutex_unlock, which invokes undefined behavior. */
109void
110maybe_reacquire_global_lock (void)
111{
112 if (current_thread->not_holding_lock)
113 {
114 struct thread_state *self = current_thread;
115
116 acquire_global_lock (self);
117 current_thread->not_holding_lock = 0;
118 }
119}
120
103 121
104 122
105static void 123static void
@@ -493,11 +511,20 @@ really_call_select (void *arg)
493{ 511{
494 struct select_args *sa = arg; 512 struct select_args *sa = arg;
495 struct thread_state *self = current_thread; 513 struct thread_state *self = current_thread;
514 sigset_t oldset;
496 515
516 block_interrupt_signal (&oldset);
517 self->not_holding_lock = 1;
497 release_global_lock (); 518 release_global_lock ();
519 restore_signal_mask (&oldset);
520
498 sa->result = (sa->func) (sa->max_fds, sa->rfds, sa->wfds, sa->efds, 521 sa->result = (sa->func) (sa->max_fds, sa->rfds, sa->wfds, sa->efds,
499 sa->timeout, sa->sigmask); 522 sa->timeout, sa->sigmask);
523
524 block_interrupt_signal (&oldset);
500 acquire_global_lock (self); 525 acquire_global_lock (self);
526 self->not_holding_lock = 0;
527 restore_signal_mask (&oldset);
501} 528}
502 529
503int 530int