X-Git-Url: https://code.delx.au/gnu-emacs/blobdiff_plain/fa42927fd0ed023ec95a5475ce342429fd8a348d..578098f346bf9e1f23ca86ed764fc00b035b427f:/src/keyboard.c diff --git a/src/keyboard.c b/src/keyboard.c index 098d3530ef..f3d7df5e98 100644 --- a/src/keyboard.c +++ b/src/keyboard.c @@ -19,6 +19,7 @@ along with GNU Emacs. If not, see . */ #include +#define BLOCKINPUT_INLINE EXTERN_INLINE #define KEYBOARD_INLINE EXTERN_INLINE #include @@ -72,19 +73,12 @@ along with GNU Emacs. If not, see . */ /* Variables for blockinput.h: */ -/* Non-zero if interrupt input is blocked right now. */ +/* Positive if interrupt input is blocked right now. */ volatile int interrupt_input_blocked; -/* Nonzero means an input interrupt has arrived - during the current critical section. */ -int interrupt_input_pending; - -/* This var should be (interrupt_input_pending || pending_atimers). - The QUIT macro checks this instead of interrupt_input_pending and - pending_atimers separately, to reduce code size. So, any code that - changes interrupt_input_pending or pending_atimers should update - this too. */ -int pending_signals; +/* Nonzero means an input interrupt or alarm signal has arrived. + The QUIT macro checks this. */ +volatile int pending_signals; #define KBD_BUFFER_SIZE 4096 @@ -225,7 +219,11 @@ static Lisp_Object last_point_position_window; last event came from a macro. We use this to determine when to generate switch-frame events. This may be cleared by functions like Fselect_frame, to make sure that a switch-frame event is - generated by the next character. */ + generated by the next character. + + FIXME: This is modified by a signal handler so it should be volatile. + It's exported to Lisp, though, so it can't simply be marked + 'volatile' here. */ Lisp_Object internal_last_event_frame; /* The timestamp of the last input event we received from the X server. @@ -389,7 +387,7 @@ int interrupts_deferred; /* If we support a window system, turn on the code to poll periodically to detect C-g. It isn't actually used when doing interrupt input. */ -#if defined (HAVE_WINDOW_SYSTEM) && !defined (USE_ASYNC_EVENTS) +#ifdef HAVE_WINDOW_SYSTEM #define POLL_FOR_INPUT #endif @@ -413,7 +411,6 @@ static EMACS_TIME timer_last_idleness_start_time; /* Function for init_keyboard to call with no args (if nonzero). */ static void (*keyboard_init_hook) (void); -static int read_avail_input (void); static void get_input_pending (int *, int); static int readable_events (int); static Lisp_Object read_char_x_menu_prompt (ptrdiff_t, Lisp_Object *, @@ -440,7 +437,7 @@ static Lisp_Object restore_kboard_configuration (Lisp_Object); #ifdef USABLE_SIGIO static void deliver_input_available_signal (int signo); #endif -static void handle_interrupt (void); +static void handle_interrupt (bool); static _Noreturn void quit_throw_to_read_char (int); static void process_special_events (void); static void timer_start_idle (void); @@ -823,7 +820,7 @@ This function is called by the editor initialization to begin editing. */) /* If we enter while input is blocked, don't lock up here. This may happen through the debugger during redisplay. */ - if (INPUT_BLOCKED_P) + if (input_blocked_p ()) return Qnil; command_loop_level++; @@ -1216,8 +1213,7 @@ This also exits all active minibuffers. */) /* Unblock input if we enter with input blocked. This may happen if redisplay traps e.g. during tool-bar update with input blocked. */ - while (INPUT_BLOCKED_P) - UNBLOCK_INPUT; + totally_unblock_input (); Fthrow (Qtop_level, Qnil); } @@ -1460,15 +1456,6 @@ command_loop_1 (void) } } -#if 0 - /* Select the frame that the last event came from. Usually, - switch-frame events will take care of this, but if some lisp - code swallows a switch-frame event, we'll fix things up here. - Is this a good idea? */ - if (FRAMEP (internal_last_event_frame) - && !EQ (internal_last_event_frame, selected_frame)) - Fselect_frame (internal_last_event_frame, Qnil); -#endif /* If it has changed current-menubar from previous value, really recompute the menubar from the value. */ if (! NILP (Vlucid_menu_bar_dirty_flag) @@ -2008,9 +1995,9 @@ static struct atimer *poll_timer; void poll_for_input_1 (void) { - if (interrupt_input_blocked == 0 + if (! input_blocked_p () && !waiting_for_input) - read_avail_input (); + gobble_input (); } /* Timer callback function for poll_timer. TIMER is equal to @@ -2020,10 +2007,7 @@ static void poll_for_input (struct atimer *timer) { if (poll_suppress_count == 0) - { - interrupt_input_pending = 1; - pending_signals = 1; - } + pending_signals = 1; } #endif /* POLL_FOR_INPUT */ @@ -3344,7 +3328,7 @@ record_char (Lisp_Object c) If you, dear reader, have a better idea, you've got the source. :-) */ if (dribble) { - BLOCK_INPUT; + block_input (); if (INTEGERP (c)) { if (XUINT (c) < 0x100) @@ -3370,7 +3354,7 @@ record_char (Lisp_Object c) } fflush (dribble); - UNBLOCK_INPUT; + unblock_input (); } } @@ -3514,10 +3498,8 @@ kbd_buffer_store_event (register struct input_event *event) Else, if EVENT is a quit event, store the quit event in HOLD_QUIT, and return (thus ignoring further events). - This is used in read_avail_input to postpone the processing - of the quit event until all subsequent input events have been - parsed (and discarded). - */ + This is used to postpone the processing of the quit event until all + subsequent input events have been parsed (and discarded). */ void kbd_buffer_store_event_hold (register struct input_event *event, @@ -3587,7 +3569,8 @@ kbd_buffer_store_event_hold (register struct input_event *event, } last_event_timestamp = event->timestamp; - handle_interrupt (); + + handle_interrupt (0); return; } @@ -3643,7 +3626,6 @@ kbd_buffer_store_event_hold (register struct input_event *event, if (immediate_quit && NILP (Vinhibit_quit)) { immediate_quit = 0; - pthread_sigmask (SIG_SETMASK, &empty_mask, 0); QUIT; } } @@ -3793,14 +3775,6 @@ kbd_buffer_get_event (KBOARD **kbp, /* Start reading input again, we have processed enough so we can accept new events again. */ unhold_keyboard_input (); -#ifdef USABLE_SIGIO - if (!noninteractive) - { - struct sigaction action; - emacs_sigaction_init (&action, deliver_input_available_signal); - sigaction (SIGIO, &action, 0); - } -#endif start_polling (); } #endif /* subprocesses */ @@ -3869,7 +3843,7 @@ kbd_buffer_get_event (KBOARD **kbp, wait_reading_process_output (0, 0, -1, 1, Qnil, NULL, 0); if (!interrupt_input && kbd_fetch_ptr == kbd_store_ptr) - read_avail_input (); + gobble_input (); } if (CONSP (Vunread_command_events)) @@ -4333,25 +4307,18 @@ decode_timer (Lisp_Object timer, EMACS_TIME *result) should be done. */ static EMACS_TIME -timer_check_2 (void) +timer_check_2 (Lisp_Object timers, Lisp_Object idle_timers) { EMACS_TIME nexttime; EMACS_TIME now; EMACS_TIME idleness_now; - Lisp_Object timers, idle_timers, chosen_timer; - struct gcpro gcpro1, gcpro2, gcpro3; + Lisp_Object chosen_timer; + struct gcpro gcpro1; nexttime = invalid_emacs_time (); - /* Always consider the ordinary timers. */ - timers = Vtimer_list; - /* Consider the idle timers only if Emacs is idle. */ - if (EMACS_TIME_VALID_P (timer_idleness_start_time)) - idle_timers = Vtimer_idle_list; - else - idle_timers = Qnil; chosen_timer = Qnil; - GCPRO3 (timers, idle_timers, chosen_timer); + GCPRO1 (chosen_timer); /* First run the code that was delayed. */ while (CONSP (pending_funcalls)) @@ -4500,13 +4467,35 @@ EMACS_TIME timer_check (void) { EMACS_TIME nexttime; + Lisp_Object timers, idle_timers; + struct gcpro gcpro1, gcpro2; + + Lisp_Object tem = Vinhibit_quit; + Vinhibit_quit = Qt; + + /* We use copies of the timers' lists to allow a timer to add itself + again, without locking up Emacs if the newly added timer is + already ripe when added. */ + + /* Always consider the ordinary timers. */ + timers = Fcopy_sequence (Vtimer_list); + /* Consider the idle timers only if Emacs is idle. */ + if (EMACS_TIME_VALID_P (timer_idleness_start_time)) + idle_timers = Fcopy_sequence (Vtimer_idle_list); + else + idle_timers = Qnil; + + Vinhibit_quit = tem; + + GCPRO2 (timers, idle_timers); do { - nexttime = timer_check_2 (); + nexttime = timer_check_2 (timers, idle_timers); } while (EMACS_SECS (nexttime) == 0 && EMACS_NSECS (nexttime) == 0); + UNGCPRO; return nexttime; } @@ -6737,41 +6726,6 @@ get_input_pending (int *addr, int flags) *addr = (!NILP (Vquit_flag) || readable_events (flags)); } -/* Interface to read_avail_input, blocking SIGIO or SIGALRM if necessary. */ - -void -gobble_input (void) -{ -#ifdef USABLE_SIGIO - if (interrupt_input) - { - sigset_t blocked, procmask; - sigemptyset (&blocked); - sigaddset (&blocked, SIGIO); - pthread_sigmask (SIG_BLOCK, &blocked, &procmask); - read_avail_input (); - pthread_sigmask (SIG_SETMASK, &procmask, 0); - } - else -#ifdef POLL_FOR_INPUT - /* XXX This condition was (read_socket_hook && !interrupt_input), - but read_socket_hook is not global anymore. Let's pretend that - it's always set. */ - if (!interrupt_input && poll_suppress_count == 0) - { - sigset_t blocked, procmask; - sigemptyset (&blocked); - sigaddset (&blocked, SIGALRM); - pthread_sigmask (SIG_BLOCK, &blocked, &procmask); - read_avail_input (); - pthread_sigmask (SIG_SETMASK, &procmask, 0); - } - else -#endif -#endif - read_avail_input (); -} - /* Put a BUFFER_SWITCH_EVENT in the buffer so that read_key_sequence will notice the new current buffer. */ @@ -6799,14 +6753,7 @@ record_asynch_buffer_change (void) /* Make sure no interrupt happens while storing the event. */ #ifdef USABLE_SIGIO if (interrupt_input) - { - sigset_t blocked, procmask; - sigemptyset (&blocked); - sigaddset (&blocked, SIGIO); - pthread_sigmask (SIG_BLOCK, &blocked, &procmask); - kbd_buffer_store_event (&event); - pthread_sigmask (SIG_SETMASK, &procmask, 0); - } + kbd_buffer_store_event (&event); else #endif { @@ -6819,13 +6766,11 @@ record_asynch_buffer_change (void) /* Read any terminal input already buffered up by the system into the kbd_buffer, but do not wait. - EXPECTED should be nonzero if the caller knows there is some input. - - Returns the number of keyboard chars read, or -1 meaning + Return the number of keyboard chars read, or -1 meaning this is a bad time to try to read input. */ -static int -read_avail_input (void) +int +gobble_input (void) { int nread = 0; int err = 0; @@ -6845,11 +6790,17 @@ read_avail_input (void) int nr; struct input_event hold_quit; + if (input_blocked_p ()) + { + pending_signals = 1; + break; + } + EVENT_INIT (hold_quit); hold_quit.kind = NO_EVENT; /* No need for FIONREAD or fcntl; just say don't wait. */ - while (0 < (nr = (*t->read_socket_hook) (t, &hold_quit))) + while (0 < (nr = (*t->read_socket_hook) (t, &hold_quit))) nread += nr; if (nr == -1) /* Not OK to read input now. */ @@ -6870,7 +6821,7 @@ read_avail_input (void) this process rather than to the whole process group? Perhaps on systems with FIONREAD Emacs is alone in its group. */ - kill (getpid (), SIGHUP); + terminate_due_to_signal (SIGHUP, 10); /* XXX Is calling delete_terminal safe here? It calls delete_frame. */ { @@ -7159,34 +7110,68 @@ tty_read_avail_input (struct terminal *terminal, static void handle_async_input (void) { - interrupt_input_pending = 0; - pending_signals = pending_atimers; - +#ifdef USABLE_SIGIO while (1) { - int nread = read_avail_input (); + int nread = gobble_input (); /* -1 means it's not ok to read the input now. UNBLOCK_INPUT will read it later; now, avoid infinite loop. 0 means there was no keyboard input available. */ if (nread <= 0) break; } +#endif } void process_pending_signals (void) { - if (interrupt_input_pending) - handle_async_input (); + pending_signals = 0; + handle_async_input (); do_pending_atimers (); } +/* Undo any number of BLOCK_INPUT calls down to level LEVEL, + and also (if the level is now 0) reinvoke any pending signal. */ + +void +unblock_input_to (int level) +{ + interrupt_input_blocked = level; + if (level == 0) + { + if (pending_signals) + process_pending_signals (); + } + else if (level < 0) + emacs_abort (); +} + +/* End critical section. + + If doing signal-driven input, and a signal came in when input was + blocked, reinvoke the signal handler now to deal with it. */ + +void +unblock_input (void) +{ + unblock_input_to (interrupt_input_blocked - 1); +} + +/* Undo any number of BLOCK_INPUT calls, + and also reinvoke any pending signal. */ + +void +totally_unblock_input (void) +{ + unblock_input_to (0); +} + #ifdef USABLE_SIGIO -static void +void handle_input_available_signal (int sig) { - interrupt_input_pending = 1; pending_signals = 1; if (input_available_clear_time) @@ -7196,25 +7181,10 @@ handle_input_available_signal (int sig) static void deliver_input_available_signal (int sig) { - handle_on_main_thread (sig, handle_input_available_signal); + deliver_process_signal (sig, handle_input_available_signal); } #endif /* USABLE_SIGIO */ -/* Send ourselves a SIGIO. - - This function exists so that the UNBLOCK_INPUT macro in - blockinput.h can have some way to take care of input we put off - dealing with, without assuming that every file which uses - UNBLOCK_INPUT also has #included the files necessary to get SIGIO. */ -void -reinvoke_input_signal (void) -{ -#ifdef USABLE_SIGIO - handle_async_input (); -#endif -} - - /* User signal events. */ @@ -7285,7 +7255,7 @@ handle_user_signal (int sig) p->npending++; #ifdef USABLE_SIGIO if (interrupt_input) - kill (getpid (), SIGIO); + handle_input_available_signal (sig); else #endif { @@ -7301,7 +7271,7 @@ handle_user_signal (int sig) static void deliver_user_signal (int sig) { - handle_on_main_thread (sig, handle_user_signal); + deliver_process_signal (sig, handle_user_signal); } static char * @@ -7326,8 +7296,6 @@ store_user_signal_events (void) for (p = user_signals; p; p = p->next) if (p->npending > 0) { - sigset_t blocked, procmask; - if (! buf_initialized) { memset (&buf, 0, sizeof buf); @@ -7336,10 +7304,6 @@ store_user_signal_events (void) buf_initialized = 1; } - sigemptyset (&blocked); - sigaddset (&blocked, p->sig); - pthread_sigmask (SIG_BLOCK, &blocked, &procmask); - do { buf.code = p->sig; @@ -7347,8 +7311,6 @@ store_user_signal_events (void) p->npending--; } while (p->npending > 0); - - pthread_sigmask (SIG_SETMASK, &procmask, 0); } } @@ -10553,9 +10515,9 @@ The file will be closed when Emacs exits. */) { if (dribble) { - BLOCK_INPUT; + block_input (); fclose (dribble); - UNBLOCK_INPUT; + unblock_input (); dribble = 0; } if (!NILP (file)) @@ -10744,21 +10706,21 @@ handle_interrupt_signal (int sig) from the controlling tty. */ internal_last_event_frame = terminal->display_info.tty->top_frame; - handle_interrupt (); + handle_interrupt (1); } } static void deliver_interrupt_signal (int sig) { - handle_on_main_thread (sig, handle_interrupt_signal); + deliver_process_signal (sig, handle_interrupt_signal); } /* If Emacs is stuck because `inhibit-quit' is true, then keep track of the number of times C-g has been requested. If C-g is pressed enough times, then quit anyway. See bug#6585. */ -static int force_quit_count; +static int volatile force_quit_count; /* This routine is called at interrupt level in response to C-g. @@ -10772,7 +10734,7 @@ static int force_quit_count; non-nil, it stops the job right away. */ static void -handle_interrupt (void) +handle_interrupt (bool in_signal_handler) { char c; @@ -10781,13 +10743,16 @@ handle_interrupt (void) /* XXX This code needs to be revised for multi-tty support. */ if (!NILP (Vquit_flag) && get_named_tty ("/dev/tty")) { - /* If SIGINT isn't blocked, don't let us be interrupted by - another SIGINT, it might be harmful due to non-reentrancy - in I/O functions. */ - sigset_t blocked; - sigemptyset (&blocked); - sigaddset (&blocked, SIGINT); - pthread_sigmask (SIG_BLOCK, &blocked, 0); + if (! in_signal_handler) + { + /* If SIGINT isn't blocked, don't let us be interrupted by + a SIGINT. It might be harmful due to non-reentrancy + in I/O functions. */ + sigset_t blocked; + sigemptyset (&blocked); + sigaddset (&blocked, SIGINT); + pthread_sigmask (SIG_BLOCK, &blocked, 0); + } fflush (stdout); reset_all_sys_modes (); @@ -10858,7 +10823,6 @@ handle_interrupt (void) #endif /* not MSDOS */ fflush (stdout); init_all_sys_modes (); - pthread_sigmask (SIG_SETMASK, &empty_mask, 0); } else { @@ -10876,15 +10840,14 @@ handle_interrupt (void) GCPRO4 (saved.object, saved.global_code, saved.current_syntax_table, saved.old_prop); Fsignal (Qquit, Qnil); - /* FIXME: AFAIK, `quit' can never return, so this code is dead! */ gl_state = saved; UNGCPRO; } else { /* Else request quit when it's safe. */ - if (NILP (Vquit_flag)) - force_quit_count = 0; - if (++force_quit_count == 3) + int count = NILP (Vquit_flag) ? 1 : force_quit_count + 1; + force_quit_count = count; + if (count == 3) { immediate_quit = 1; Vinhibit_quit = Qnil; @@ -10893,6 +10856,8 @@ handle_interrupt (void) } } + pthread_sigmask (SIG_SETMASK, &empty_mask, 0); + /* TODO: The longjmp in this call throws the NS event loop integration off, and it seems to do fine without this. Probably some attention needs to be paid to the setting of waiting_for_input in @@ -10902,7 +10867,7 @@ handle_interrupt (void) separate event loop thread like W32. */ #ifndef HAVE_NS if (waiting_for_input && !echoing) - quit_throw_to_read_char (1); + quit_throw_to_read_char (in_signal_handler); #endif } @@ -10916,22 +10881,12 @@ quit_throw_to_read_char (int from_signal) if (!from_signal && EQ (Vquit_flag, Qkill_emacs)) Fkill_emacs (Qnil); - pthread_sigmask (SIG_SETMASK, &empty_mask, 0); /* Prevent another signal from doing this before we finish. */ clear_waiting_for_input (); input_pending = 0; Vunread_command_events = Qnil; -#if 0 /* Currently, sit_for is called from read_char without turning - off polling. And that can call set_waiting_for_input. - It seems to be harmless. */ -#ifdef POLL_FOR_INPUT - /* May be > 1 if in recursive minibuffer. */ - if (poll_suppress_count == 0) - emacs_abort (); -#endif -#endif if (FRAMEP (internal_last_event_frame) && !EQ (internal_last_event_frame, selected_frame)) do_switch_frame (make_lispy_switch_frame (internal_last_event_frame), @@ -11322,7 +11277,6 @@ init_keyboard (void) #endif input_pending = 0; interrupt_input_blocked = 0; - interrupt_input_pending = 0; pending_signals = 0; /* This means that command_loop_1 won't try to select anything the first @@ -11343,7 +11297,7 @@ init_keyboard (void) /* Before multi-tty support, these handlers used to be installed only if the current session was a tty session. Now an Emacs session may have multiple display types, so we always handle - SIGINT. There is special code in interrupt_signal to exit + SIGINT. There is special code in handle_interrupt_signal to exit Emacs on SIGINT when there are no termcap frames on the controlling terminal. */ struct sigaction action;