>From 8fda4d52c5d708a057d9bb3fa5e95f2aa77ab7ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20F=2E=20Wittenberger?= Date: Thu, 3 Dec 2015 10:52:06 +0100 Subject: [PATCH] Allow signal handlers to be dispatched to multiple threads To avoid clobbering the saved_stack_limit when multiple signals are dispatched in parallel: 1. Remember C_stack_limit 2. Test pending_interrupts_count 3. Increment pending_interrupts_count 4. Write remembered value to saved_stack_limit Signed-off-by: Evan Hanson --- runtime.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/runtime.c b/runtime.c index d65c3a5..112314d 100644 --- a/runtime.c +++ b/runtime.c @@ -4431,20 +4431,24 @@ C_regparm void C_fcall C_paranoid_check_for_interrupt(void) C_regparm void C_fcall C_raise_interrupt(int reason) { + /* + * Save the value of C_stack_limit from before the interrupt is queued + * to ensure that multiple signals only ever cause saved_stack_limit + * to be assigned a value from when pending_interrupts_count was zero. + */ + C_word *stack_limit = C_stack_limit; + if(C_interrupts_enabled) { if(pending_interrupts_count == 0 && !handling_interrupts) { - /* Force the next stack check to fail by faking a "full" stack. - That causes save_and_reclaim() to be called, which will - invoke handle_interrupt() (which restores the stack limit). */ - saved_stack_limit = C_stack_limit; - -#if C_STACK_GROWS_DOWNWARD - C_stack_limit = C_stack_pointer + 1000; -#else - C_stack_limit = C_stack_pointer - 1000; -#endif - interrupt_time = C_cpu_milliseconds(); pending_interrupts[ pending_interrupts_count++ ] = reason; + /* + * Force the next stack check to fail by faking a "full" stack. + * This causes save_and_reclaim() to be called, which invokes + * handle_interrupt(), which restores the stack limit. + */ + saved_stack_limit = stack_limit; + C_stack_limit = stack_bottom; + interrupt_time = C_cpu_milliseconds(); } else if(pending_interrupts_count < MAX_PENDING_INTERRUPTS) { int i; /* -- 2.6.4