[Git][ghc/ghc][wip/dcoutts/signals-cleanup] 4 commits: Push installUserSignalHandler down into individual I/O managers
Duncan Coutts pushed to branch wip/dcoutts/signals-cleanup at Glasgow Haskell Compiler / GHC Commits: 84627130 by Duncan Coutts at 2026-10-22T21:42:28+01:00 Push installUserSignalHandler down into individual I/O managers This will allow us to specialise them. Initally they all use the same (existing) generic_handler. - - - - - a03ec47f by Duncan Coutts at 2026-10-22T21:42:28+01:00 Specialise MIO's installing of signals It already used a separate implementation, but using cpp via the signal "generic_handler". Now it is much more direct and local. MIO writes signals into a pipe and reads and dispatches them at the other end (in Haskell). - - - - - b1f2d9ed by Duncan Coutts at 2026-10-22T21:42:28+01:00 Introduce an I/O manager data structure shared between all caps Accessed via cap->iomgr_shared, but it points to the same struct for all capabilities. We initialise and free this with cap 0. Use this initially for the MIO I/O manager's timer manager control fd. This fd is used for signals. Soon we will use the same mechanism for the same purpose of signal handling, but for the other posix I/O managers. This is a fairly general mechanism, so other I/O managers may be able to make use of it, once they are adapted to work in the threaded RTS with multiple capabilities. - - - - - f809312b by Duncan Coutts at 2026-10-22T21:42:37+01:00 Extract and add more utility functions for the signal self-pipe trick Extract a utility function writeSignalInfoToPipe from the MIO I/O manager so we can use it in other I/O managers too. Also add related utility functions readSignalInfoFromPipe, newNonBlockingPipe and startSignalHandler. We will use these in the poll and selectbis I/O managers. - - - - - 13 changed files: - rts/IOManager.c - rts/IOManagerInternals.h - rts/configure.ac - rts/posix/IOManSignals.c - rts/posix/IOManSignals.h - rts/posix/MIO.c - rts/posix/MIO.h - rts/posix/Poll.c - rts/posix/Poll.h - rts/posix/Select.c - rts/posix/Select.h - rts/posix/SelectBis.c - rts/posix/SelectBis.h Changes: ===================================== rts/IOManager.c ===================================== @@ -356,6 +356,15 @@ CapIOManager *allocCapabilityIOManager(Capability *cap) CapIOManager *iomgr = stgMallocBytes(sizeof(CapIOManager), "allocCapabilityIOManager"); iomgr->cap = cap; /* link back */ + + /* Allocate and/or link the shared I/O manager data structure */ + if (cap->no == 0) { + iomgr->shared = stgMallocBytes(sizeof(CapIOManagerShared), + "allocCapabilityIOManager"); + } else { + iomgr->shared = MainCapability.iomgr->shared; + } + return iomgr; } @@ -372,6 +381,8 @@ void initCapabilityIOManager(CapIOManager *iomgr) debugTrace(DEBUG_iomanager, "initialising I/O manager %s for cap %d", showIOManager(), iomgr->cap->no); + ASSERT(iomgr->cap && iomgr->shared); /* These are initialised already */ + switch (iomgr_type) { #if defined(IOMGR_ENABLED_SELECT) case IO_MANAGER_SELECT: @@ -400,7 +411,7 @@ void initCapabilityIOManager(CapIOManager *iomgr) #if defined(IOMGR_ENABLED_MIO_POSIX) case IO_MANAGER_MIO_POSIX: - iomgr->control_fd = -1; + initCapabilityIOManagerMIO(iomgr); break; #endif default: @@ -432,6 +443,13 @@ void freeCapabilityIOManager(CapIOManager *iomgr) default: break; } + + /* We expect the per-impl free function (for cap 0) to free anything within + * the shared structure. + */ + if (iomgr->cap->no == 0) { + stgFree(iomgr->shared); + } } @@ -653,6 +671,41 @@ setIOManagerControlFd(uint32_t cap_no, int fd) { #if !defined(mingw32_HOST_OS) +/* This is a plausible candidate for a newer public API, but it isn't yet. + * It is currently the worker for stg_sig_install below. + */ +static int installUserSignalHandler(int sig, int spi, sigset_t *mask, + bool nocldstop) +{ +#if !defined(RTS_USER_SIGNALS) + return STG_SIG_DFL; +#endif + + debugTrace(DEBUG_iomanager, "Installing a signal handler for sig %d", sig); + + switch (iomgr_type) { +#if defined(IOMGR_ENABLED_SELECT) + case IO_MANAGER_SELECT: + return installUserSignalHandlerSelect(sig, spi, mask, nocldstop); +#endif +#if defined(IOMGR_ENABLED_SELECTBIS) + case IO_MANAGER_SELECTBIS: + return installUserSignalHandlerSelectBis(sig, spi, mask, nocldstop); +#endif +#if defined(IOMGR_ENABLED_POLL) + case IO_MANAGER_POLL: + return installUserSignalHandlerPoll(sig, spi, mask, nocldstop); +#endif +#if defined(IOMGR_ENABLED_MIO_POSIX) + case IO_MANAGER_MIO_POSIX: + return installUserSignalHandlerMIO(sig, spi, mask, nocldstop); +#endif + default: + barf("installUserSignalHandler not implemented"); + } +} + + /* This flag is a public API, used by the unix package. It is an _implicit_ * argument to stg_sig_install to control whether or not to include * SA_NOCLDSTOP when installing a SIGCHLD handler. @@ -665,20 +718,14 @@ HsInt nocldstop = 0; /* Declared in rts/IOInterface.h. This is a public API, used by the unix * package for installHandler. + * + * This is used as an FFI ccall import, but it would be better as a primop, for + * consistency and so we can always have a Capability to hand. */ int stg_sig_install(int sig, int spi, void *mask) { - debugTrace(DEBUG_iomanager, "Installing a signal handler for sig %d", sig); - /* Grab the current value of the nocldstop argument. Yes this is madness. */ - bool nocldstop_arg = nocldstop; - -#if defined(RTS_USER_SIGNALS) - return installUserSignalHandler(sig, spi, mask, nocldstop_arg); - -#else /* !RTS_USER_SIGNALS */ - return STG_SIG_DFL; -#endif + return installUserSignalHandler(sig, spi, mask, nocldstop); } #endif ===================================== rts/IOManagerInternals.h ===================================== @@ -30,6 +30,11 @@ #include "BeginPrivate.h" +/* Forward reference */ +struct _CapIOManagerShared; +typedef struct _CapIOManagerShared CapIOManagerShared; + + /* The per-capability data structures belonging to the I/O manager. * * It can be accessed as cap->iomgr. Or given just the iomgr, you can access @@ -42,9 +47,12 @@ */ struct _CapIOManager { - /* Back reference to the containing capability */ + /* Back reference to the containing capability */ Capability *cap; + /* Data shared between the I/O manager instances across all capabilities */ + CapIOManagerShared *shared; + #if defined(IOMGR_ENABLED_SELECT) /* Thread queue for threads blocked on I/O completion. */ StgTSO *blocked_queue_hd; @@ -96,6 +104,24 @@ struct _CapIOManager { }; + +/* Much like above, but for I/O manager data that is shared between the + * I/O manager instances for all the capabilities. + * + * This gets allocated and linked by the shared IOManager.c code, and should be + * initialised by the individual I/O manager's init function but only for cap 0. + */ +struct _CapIOManagerShared { + +#if defined(IOMGR_ENABLED_MIO_POSIX) + /* The timer manager control pipe (write end). This is the pipe into which + * we will send our signals. + */ + int timer_manager_control_wr_fd; +#endif +}; + + /* Fill in the outcome and result/error on the TSO's stack frame. * The TSO must be blocked using one of the stg_block_io_* blocking actions. * ===================================== rts/configure.ac ===================================== @@ -221,10 +221,6 @@ AC_LINK_IFELSE([AC_LANG_CALL([], [printf\$LDBLStub])], FP_CHECK_PTHREAD_FUNCS -dnl ** check for eventfd which is needed by the I/O manager -AC_CHECK_HEADERS([sys/eventfd.h]) -AC_CHECK_FUNCS([eventfd]) - AC_CHECK_FUNCS([getpid getuid raise]) dnl large address space support (see rts/include/rts/storage/MBlock.h) @@ -359,6 +355,10 @@ dnl ------------------------------------------------------------------- dnl Here we check for platform constraints. The result is the CPP flag dnl IOMGR_BUILD_<name> and a EnableIOManager<Name> var for use here later. +dnl Check for functions (optionally) used by several I/O managers +AC_CHECK_HEADERS([sys/eventfd.h]) +AC_CHECK_FUNCS([eventfd pipe2]) + GHC_IOMANAGER_ENABLE([select], [EnableIOManagerSelect], [IOMGR_BUILD_SELECT], [if test "$HostOS" = "mingw32"; then EnableIOManagerSelect=NO ===================================== rts/posix/IOManSignals.c ===================================== @@ -22,6 +22,7 @@ #include <unistd.h> #include <signal.h> +#include <fcntl.h> #include <string.h> /* ----------------------------------------------------------------------------- @@ -111,16 +112,12 @@ static inline bool signals_pending(void) { * started up at the next context switch. * -------------------------------------------------------------------------- */ -static void +#if !defined(THREADED_RTS) +void generic_handler(int sig USED_IF_THREADS, siginfo_t *info, void *p STG_UNUSED) { -#if defined(THREADED_RTS) - //TODO: This calls MIO directly. We should go via IOManager API. - // The IOManager API should be extended to cover signals. - timerManagerNotifySignal(sig, info); -#else /* not THREADED_RTS */ /* Can't call allocate from here. Probably can't call malloc either. However, we have to schedule a new thread somehow. @@ -162,8 +159,8 @@ generic_handler(int sig USED_IF_THREADS, interruptCapability(&MainCapability); -#endif /* THREADED_RTS */ } +#endif /* THREADED_RTS */ /* ----------------------------------------------------------------------------- * Blocking/Unblocking of the user signals @@ -190,7 +187,8 @@ unblockUserSignals(void) * -------------------------------------------------------------------------- */ int -installUserSignalHandler(int sig, int spi, void *mask, bool nocldstop) +installSignalHandler(int sig, int spi, sigset_t *mask, bool nocldstop, + SignalHandler signal_handler) { ACQUIRE_LOCK(&sig_mutex); @@ -226,7 +224,7 @@ installUserSignalHandler(int sig, int spi, void *mask, bool nocldstop) action.sa_flags |= SA_RESETHAND; /* fall through */ case STG_SIG_HAN: - action.sa_sigaction = generic_handler; + action.sa_sigaction = signal_handler; action.sa_flags |= SA_SIGINFO; break; @@ -235,7 +233,7 @@ installUserSignalHandler(int sig, int spi, void *mask, bool nocldstop) } if (mask != NULL) { - action.sa_mask = *(sigset_t *)mask; + action.sa_mask = *mask; } else { sigemptyset(&action.sa_mask); } @@ -289,12 +287,27 @@ startPendingSignalHandlers(Capability *cap) continue; // handler has been changed. } - info = stgMallocBytes(sizeof(siginfo_t), "startPendingSignalHandlers"); - // freed by runHandler - memcpy(info, next_pending_handler, sizeof(siginfo_t)); + info = next_pending_handler; + startSignalHandler(cap, sig, info); + } + + unblockUserSignals(); +} +#endif + +void startSignalHandler(Capability *cap, int sig, siginfo_t *info) +{ + /* Allocate a copy. This is freed by runHandler. */ + // TODO: this could be improved to use a GC'd pinned byte array, but this + // would need coordination with the unix library. + siginfo_t *info_copy = stgMallocBytes(sizeof(siginfo_t), "startSignalHandler"); + *info_copy = *info; + + /* The info structure can be zero on solaris. So at least set the signo. */ + info_copy->si_signo = sig; debugTrace(DEBUG_iomanager, - "Handling pending signal, no %d", info->si_signo); + "Handling pending signal, no %d", info_copy->si_signo); StgTSO *t = createIOThread(cap, @@ -302,14 +315,75 @@ startPendingSignalHandlers(Capability *cap) rts_apply(cap, rts_apply(cap, runHandlersPtr_closure, - rts_mkPtr(cap, info)), - rts_mkInt(cap, info->si_signo))); + rts_mkPtr(cap, info_copy)), + rts_mkInt(cap, info_copy->si_signo))); scheduleThread(cap, t); setThreadLabel(cap, t, "signal handler thread"); - } - - unblockUserSignals(); } + +void newNonBlockingPipe(int *pipe_fd_r, int *pipe_fd_w) +{ + int pipefd[2]; +#if defined(HAVE_PIPE2) + int res = pipe2(pipefd, O_CLOEXEC | O_NONBLOCK); +#else + int res = pipe(pipefd); +#endif + if (RTS_UNLIKELY(res < 0)) { + sysErrorBelch("newNonBlockingPipe"); + stg_exit(EXIT_FAILURE); + } +#if !defined(HAVE_PIPE2) + for (int i = 0; i < 2; i++) { + int res1 = fcntl(pipefd[i], F_SETFD, FD_CLOEXEC); + int res2 = fcntl(pipefd[i], F_SETFL, O_NONBLOCK); + if (RTS_UNLIKELY(res1 < 0 || res2 < 0)) { + sysErrorBelch("newFdWakeup fcntl()"); + stg_exit(EXIT_FAILURE); + } + } #endif + *pipe_fd_r = pipefd[0]; /* read end */ + *pipe_fd_w = pipefd[1]; /* write end */ +} + +/* Write the signal number and siginfo into the given fd (which is + * typically a pipe in non-blocking mode). + */ +void writeSignalInfoToPipe(int sig, siginfo_t *info, int fd) +{ + StgWord8 buf[sizeof(siginfo_t) + 1]; + + buf[0] = sig; + if (info) { + memcpy(buf+1, info, sizeof(siginfo_t)); + } else { + // info may be NULL on Solaris (see #3790) + memset(buf+1, 0, sizeof(siginfo_t)); + } + + int r = write(fd, buf, sizeof(siginfo_t)+1); + if (r == -1 && errno == EAGAIN) { + errorBelch("lost signal due to full pipe: %d\n", sig); + } +} + +bool readSignalInfoFromPipe(int *sig, siginfo_t *info, int fd) +{ + StgWord8 buf[sizeof(siginfo_t) + 1]; + + int r = read(fd, buf, sizeof(siginfo_t)+1); + + if (r == sizeof(siginfo_t)+1) { + *sig = (int) buf[0]; + memcpy(info, buf+1, sizeof(siginfo_t)); + return true; + } else { + // TODO: if r is >0 and < expected then we're kind of screwed + // it means we're all out of sync with the protocol. + // it _should_ never happen of course! + return false; + } +} #endif /* RTS_USER_SIGNALS */ ===================================== rts/posix/IOManSignals.h ===================================== @@ -8,6 +8,10 @@ #pragma once +#if defined(RTS_USER_SIGNALS) + +#include <signal.h> + #include "BeginPrivate.h" /* @@ -31,7 +35,24 @@ void finiUserSignals(void); void startPendingSignalHandlers(Capability *cap); #endif -int installUserSignalHandler(int sig, int spi, void *mask, bool nocldstop); +void startSignalHandler(Capability *cap, int sig, siginfo_t *info); + +typedef void (*SignalHandler)(int sig, siginfo_t *info, void *p); + +int installSignalHandler(int sig, int spi, sigset_t *mask, bool nocldstop, + SignalHandler handler); + +/* Utilities to help with the self-pipe trick for signal handling. */ +void newNonBlockingPipe(int *pipe_fd_r, int *pipe_fd_w); +void writeSignalInfoToPipe (int sig, siginfo_t *info, int fd); +bool readSignalInfoFromPipe(int *sig, siginfo_t *info, int fd); + +/* Temporary */ +void generic_handler(int sig USED_IF_THREADS, + siginfo_t *info, + void *p STG_UNUSED); + +#endif #include "EndPrivate.h" ===================================== rts/posix/MIO.c ===================================== @@ -13,16 +13,19 @@ #include "rts/PosixSource.h" #include "Rts.h" -#include "Schedule.h" -#include "RtsUtils.h" -#include "Prelude.h" -#include "ThreadLabels.h" +#include "IOManager.h" + +#if defined(IOMGR_ENABLED_MIO_POSIX) #include "MIO.h" -#include "IOManager.h" #include "IOManagerInternals.h" #include "IOManSignals.h" +#include "Schedule.h" +#include "RtsUtils.h" +#include "Prelude.h" +#include "ThreadLabels.h" + #if defined(HAVE_ERRNO_H) # include <errno.h> #endif @@ -30,64 +33,65 @@ #include <stdlib.h> #include <unistd.h> -// Here's the pipe into which we will send our signals -static int timer_manager_control_wr_fd = -1; -// TODO: Eliminate these globals. Put then into the CapIOManager, but the -// problem is these are shared across all caps, not per cap. - #define IO_MANAGER_DIE 0xfe +void initCapabilityIOManagerMIO(CapIOManager *iomgr) +{ + iomgr->control_fd = -1; + if (iomgr->cap == 0) { + iomgr->shared->timer_manager_control_wr_fd = -1; + } +} + void setTimerManagerControlFd(int fd) { - RELAXED_STORE(&timer_manager_control_wr_fd, fd); + int *timer_manager_control_wr_fd = + &MainCapability.iomgr->shared->timer_manager_control_wr_fd; + RELAXED_STORE(timer_manager_control_wr_fd, fd); } -#if defined(THREADED_RTS) -void timerManagerNotifySignal(int sig, siginfo_t *info) +static void signal_handler(int sig, siginfo_t *info, void *p STG_UNUSED) { - StgWord8 buf[sizeof(siginfo_t) + 1]; - int r; - - buf[0] = sig; - if (info == NULL) { - // info may be NULL on Solaris (see #3790) - memset(buf+1, 0, sizeof(siginfo_t)); - } else { - memcpy(buf+1, info, sizeof(siginfo_t)); - } - - int timer_control_fd = RELAXED_LOAD(&timer_manager_control_wr_fd); + /* In the signal handler we do not have Capability context, so access the + * control fd via MainCapability. + */ + int *timer_manager_control_wr_fd = + &MainCapability.iomgr->shared->timer_manager_control_wr_fd; + int timer_control_fd = RELAXED_LOAD(timer_manager_control_wr_fd); if (0 <= timer_control_fd) { - r = write(timer_control_fd, buf, sizeof(siginfo_t)+1); - if (r == -1 && errno == EAGAIN) { - errorBelch("lost signal due to full pipe: %d\n", sig); - } + writeSignalInfoToPipe(sig, info, timer_control_fd); } // If the IO manager hasn't told us what the FD of the write end // of its pipe is, there's not much we can do here, so just ignore // the signal.. } -#endif +int installUserSignalHandlerMIO(int sig, int spi, sigset_t *mask, + bool nocldstop) +{ + return installSignalHandler(sig, spi, mask, nocldstop, signal_handler); +} -#if defined(THREADED_RTS) void ioManagerDie (void) { finiUserSignals(); + int *timer_manager_control_wr_fd = + &MainCapability.iomgr->shared->timer_manager_control_wr_fd; + StgWord8 byte = (StgWord8)IO_MANAGER_DIE; uint32_t i; int r; { // Shut down timer manager - const int fd = RELAXED_LOAD(&timer_manager_control_wr_fd); + const int fd = RELAXED_LOAD(timer_manager_control_wr_fd); if (0 <= fd) { r = write(fd, &byte, 1); if (r == -1) { sysErrorBelch("ioManagerDie: write"); } - RELAXED_STORE(&timer_manager_control_wr_fd, -1); + RELAXED_STORE(timer_manager_control_wr_fd, -1); } } @@ -115,13 +119,21 @@ ioManagerStart (void) { initUserSignals(); + int *timer_manager_control_wr_fd = + &MainCapability.iomgr->shared->timer_manager_control_wr_fd; + // Make sure the IO manager thread is running Capability *cap; - if (SEQ_CST_LOAD(&timer_manager_control_wr_fd) < 0) { + if (SEQ_CST_LOAD(timer_manager_control_wr_fd) < 0) { cap = rts_lock(); ioManagerStartCap(&cap); rts_unlock(cap); } } -#endif +#else // !IOMGR_ENABLED_MIO_POSIX + +/* This is exported from the RTS unconditionally, but only used for MIO. */ +void setTimerManagerControlFd(int fd STG_UNUSED) { /* do nothing */ } + +#endif // IOMGR_ENABLED_MIO_POSIX ===================================== rts/posix/MIO.h ===================================== @@ -19,11 +19,14 @@ /* Communicating with the IO manager thread (see GHC.Conc). */ #if defined(THREADED_RTS) +void initCapabilityIOManagerMIO(CapIOManager *iomgr); + void ioManagerDie (void); void ioManagerStart (void); void ioManagerStartCap (/* inout */ Capability **cap); -void timerManagerNotifySignal(int sig, siginfo_t *info); +int installUserSignalHandlerMIO(int sig, int spi, sigset_t *mask, + bool nocldstop); #endif #include "EndPrivate.h" ===================================== rts/posix/Poll.c ===================================== @@ -585,6 +585,13 @@ void interruptIOManagerPoll(CapIOManager *iomgr USED_IF_PREEMPTION) } +int installUserSignalHandlerPoll(int sig, int spi, sigset_t *mask, + bool nocldstop) +{ + return installSignalHandler(sig, spi, mask, nocldstop, generic_handler); +} + + /* Helper function to double the size of the aiop_table and aiop_poll_table. */ static bool enlargeTables(CapIOManager *iomgr) ===================================== rts/posix/Poll.h ===================================== @@ -34,6 +34,9 @@ void pollCompletedTimeoutsOrIOPoll(CapIOManager *iomgr); bool awaitCompletedTimeoutsOrIOPoll(CapIOManager *iomgr); void interruptIOManagerPoll(CapIOManager *iomgr); +/* Signals */ +int installUserSignalHandlerPoll(int sig, int spi, sigset_t *mask, bool nocldstop); + #endif /* IOMGR_ENABLED_POLL */ #include "EndPrivate.h" ===================================== rts/posix/Select.c ===================================== @@ -524,4 +524,10 @@ awaitCompletedTimeoutsOrIOSelect(CapIOManager *iomgr, bool wait) return !interrupt; } +int installUserSignalHandlerSelect(int sig, int spi, sigset_t *mask, + bool nocldstop) +{ + return installSignalHandler(sig, spi, mask, nocldstop, generic_handler); +} + #endif /* IOMGR_ENABLED_SELECT */ ===================================== rts/posix/Select.h ===================================== @@ -21,6 +21,8 @@ void wakeupIOManagerSelect(CapIOManager *iomgr); bool awaitCompletedTimeoutsOrIOSelect(CapIOManager *iomgr, bool wait); void interruptIOManagerSelect(CapIOManager *iomgr); +int installUserSignalHandlerSelect(int sig, int spi, sigset_t *mask, + bool nocldstop); #include "EndPrivate.h" ===================================== rts/posix/SelectBis.c ===================================== @@ -595,6 +595,13 @@ void interruptIOManagerSelectBis(CapIOManager *iomgr USED_IF_PREEMPTION) } +int installUserSignalHandlerSelectBis(int sig, int spi, sigset_t *mask, + bool nocldstop) +{ + return installSignalHandler(sig, spi, mask, nocldstop, generic_handler); +} + + /* Helper function to double the size of the aiop_table. */ static bool enlargeTables(CapIOManager *iomgr) ===================================== rts/posix/SelectBis.h ===================================== @@ -55,6 +55,10 @@ void pollCompletedTimeoutsOrIOSelectBis(CapIOManager *iomgr); bool awaitCompletedTimeoutsOrIOSelectBis(CapIOManager *iomgr); void interruptIOManagerSelectBis(CapIOManager *iomgr); +/* Signals */ +int installUserSignalHandlerSelectBis(int sig, int spi, sigset_t *mask, + bool nocldstop); + #endif /* IOMGR_ENABLED_SELECTBIS */ #include "EndPrivate.h" View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/544e5e6b234a359025121f804c9dad0... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/544e5e6b234a359025121f804c9dad0... You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
participants (1)
-
Duncan Coutts (@dcoutts)