diff options
author | Matthew Flatt <mflatt@racket-lang.org> | 2024-03-02 07:18:41 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-02 07:18:41 -0700 |
commit | fc081fc447a786dd53286e5d7314b7217631cb68 (patch) | |
tree | 781c8aab7246dd52ef00226eb007a9cb03f66795 | |
parent | b8838c3280ef10e115236d2f7ac9ae857f83e268 (diff) |
The intent is to avoid crashes when a signal gets delimited to a
thread that might not even be a Scheme thread. Also, we don't try to
queue the event directly in the main thread's context, because then
we'd need more of a lock (while signal handling is otherwise an
implicit lock).
-rw-r--r-- | c/globals.h | 1 | ||||
-rw-r--r-- | c/schsig.c | 10 | ||||
-rw-r--r-- | c/thread.c | 1 | ||||
-rw-r--r-- | csug/system.stex | 2 |
4 files changed, 14 insertions, 0 deletions
diff --git a/c/globals.h b/c/globals.h index d2a08299..eb2965c5 100644 --- a/c/globals.h +++ b/c/globals.h @@ -49,6 +49,7 @@ EXTERN int S_num_preserve_ownership_threads; # ifdef IMPLICIT_ATOMIC_AS_EXPLICIT EXTERN s_thread_mutex_t S_implicit_mutex; # endif +EXTERN s_thread_t S_main_thread_id; #endif /* segment.c */ @@ -666,6 +666,16 @@ ptr S_dequeue_scheme_signals(ptr tc) { static void forward_signal_to_scheme(INT sig) { ptr tc = get_thread_context(); +#ifdef PTHREADS + /* deliver signals to the main thread, only; depending + on the threads that are running, `tc` might even be NULL */ + if (tc != TO_PTR(&S_G.thread_context)) { + pthread_kill(S_main_thread_id, sig); + RESET_SIGNAL + return; + } +#endif + if (enqueue_scheme_signal(tc, sig)) { SIGNALINTERRUPTPENDING(tc) = Strue; SOMETHINGPENDING(tc) = Strue; @@ -40,6 +40,7 @@ void S_thread_init(void) { s_thread_cond_init(&S_terminated_cond); S_alloc_mutex.owner = 0; S_alloc_mutex.count = 0; + S_main_thread_id = s_thread_self(); # ifdef IMPLICIT_ATOMIC_AS_EXPLICIT s_thread_mutex_init(&S_implicit_mutex); diff --git a/csug/system.stex b/csug/system.stex index d4f2bcbb..bb89f419 100644 --- a/csug/system.stex +++ b/csug/system.stex @@ -547,6 +547,8 @@ After a signal handler for a given signal has been registered, receipt of the specified signal results in a call to the handler. The handler is passed the signal number, allowing the same handler to be used for different signals while differentiating among them. +In a threaded version of the system, signals are always delivered to +the main thread. Signals handled in this fashion are treated like keyboard interrupts in that the handler is not called immediately when the signal is delivered |