Duncan Coutts pushed to branch wip/dcoutts/signals-cleanup at Glasgow Haskell Compiler / GHC Commits: f066a992 by Duncan Coutts at 2026-10-26T14:49:20+00:00 Use the shared I/O manager data structure for MIO for signals Move the MIO I/O manager's timer manager control fd from a global variable into the shared I/O manager struct. Soon we will use the same mechanism for the same purpose of signal handling for the other posix I/O managers. - - - - - 0267a165 by Duncan Coutts at 2026-10-26T14:49:20+00: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. - - - - - 7 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 Changes: ===================================== rts/IOManager.c ===================================== @@ -412,7 +412,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: ===================================== rts/IOManagerInternals.h ===================================== @@ -113,6 +113,12 @@ struct _CapIOManager { */ 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 }; ===================================== 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> /* ----------------------------------------------------------------------------- @@ -286,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, @@ -299,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 ===================================== @@ -35,11 +35,18 @@ void finiUserSignals(void); void startPendingSignalHandlers(Capability *cap); #endif +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, ===================================== 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,39 +33,33 @@ #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->no == 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 = + &getCapability(0)->iomgr->shared->timer_manager_control_wr_fd; + RELAXED_STORE(timer_manager_control_wr_fd, fd); } -#if defined(THREADED_RTS) -static void timerManagerNotifySignal(int sig, siginfo_t *info, - void *p STG_UNUSED) +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 capability 0. + */ + int *timer_manager_control_wr_fd = + &getCapability(0)->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 @@ -73,29 +70,28 @@ static void timerManagerNotifySignal(int sig, siginfo_t *info, int installUserSignalHandlerMIO(int sig, int spi, sigset_t *mask, bool nocldstop) { - return installSignalHandler(sig, spi, mask, nocldstop, - timerManagerNotifySignal); + return installSignalHandler(sig, spi, mask, nocldstop, signal_handler); } -#endif - -#if defined(THREADED_RTS) void ioManagerDie (void) { finiUserSignals(); + int *timer_manager_control_wr_fd = + &getCapability(0)->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); } } @@ -123,13 +119,21 @@ ioManagerStart (void) { initUserSignals(); + int *timer_manager_control_wr_fd = + &getCapability(0)->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,6 +19,8 @@ /* 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); View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/0e5c12c264aa39945a631586da17b78... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/0e5c12c264aa39945a631586da17b78... 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