Duncan Coutts pushed to branch wip/dcoutts/signals-cleanup at Glasgow Haskell Compiler / GHC Commits: a3541807 by Duncan Coutts at 2026-10-23T14:55:58+01:00 Push {init,fini}{UserSignals,ConsoleEvents} down into I/O managers So they become part of individual I/O managers, which will allow us to start specialising them more sanely. Rename the Win32 ones to {init,fini}ConsoleEvents. - - - - - 23cadcd2 by Duncan Coutts at 2026-10-23T14:56:04+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. - - - - - 071558cb by Duncan Coutts at 2026-10-23T14:56:04+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). - - - - - 60135da7 by Duncan Coutts at 2026-10-23T14:56:04+01:00 Introduce an I/O manager data structure shared between all caps Accessed via iomgr->shared, but it points to the same struct for all capabilities. We initialise and free this with cap 0. We will use it initially for signal handling, since only one capability needs to dispatch signals. This is a fairly general mechanism, so I/O managers may be able to make use of it for other purposes, especially once in-RTS ones are adapted to work in the threaded RTS with multiple capabilities. - - - - - 486972e4 by Duncan Coutts at 2026-10-23T14:56:04+01: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. - - - - - 0e5c12c2 by Duncan Coutts at 2026-10-23T14:56:04+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. - - - - - 16 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 - rts/win32/AwaitEvent.c - rts/win32/ConsoleHandler.c - rts/win32/ConsoleHandler.h Changes: ===================================== rts/IOManager.c ===================================== @@ -369,6 +369,18 @@ CapIOManager *allocCapabilityIOManager(Capability *cap) */ void initCapabilityIOManager(CapIOManager *iomgr) { + /* Allocate or link the shared I/O manager data structure. + * + * We do this here, not in allocCapabilityIOManager, because when we + * forkProcess, we free and re-init the CapIOManager without reallocating. + */ + if (iomgr->cap->no == 0) { + iomgr->shared = stgMallocBytes(sizeof(CapIOManagerShared), + "allocCapabilityIOManager"); + } else { + iomgr->shared = capabilities[0]->iomgr->shared; + } + debugTrace(DEBUG_iomanager, "initialising I/O manager %s for cap %d", showIOManager(), iomgr->cap->no); @@ -400,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: @@ -432,6 +444,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); + } } @@ -457,6 +476,7 @@ void startIOManager(void) * TODO: rename ioManagerStart to be less generic, impl specific */ ioManagerStart(); + initConsoleEvents(); //TODO: fully integrate break; #endif #if defined(IOMGR_ENABLED_WINIO) @@ -473,23 +493,18 @@ void startIOManager(void) #else startupAsyncWinIO(); #endif + initConsoleEvents(); //TODO: fully integrate break; #endif #if defined(IOMGR_ENABLED_WIN32_LEGACY) case IO_MANAGER_WIN32_LEGACY: startupAsyncIO(); + initConsoleEvents(); //TODO: fully integrate break; #endif default: break; } - -/* For the moment this is called here, but eventually it will be up to - * each I/O manager to decide what to do. - */ -#if defined(RTS_USER_SIGNALS) - initUserSignals(); -#endif } /* Called from forkProcess in the child process on the surviving capability. @@ -581,26 +596,26 @@ void exitIOManager(bool wait_threads) { switch (iomgr_type) { +#if defined(IOMGR_ENABLED_MIO_WIN32) + case IO_MANAGER_MIO_WIN32: + finiConsoleEvents(); //TODO: fully integrate + break; +#endif #if defined(IOMGR_ENABLED_WINIO) case IO_MANAGER_WINIO: shutdownAsyncWinIO(wait_threads); + finiConsoleEvents(); //TODO: fully integrate break; #endif #if defined(IOMGR_ENABLED_WIN32_LEGACY) case IO_MANAGER_WIN32_LEGACY: shutdownAsyncIO(wait_threads); + finiConsoleEvents(); //TODO: fully integrate break; #endif default: break; } - -/* For the moment this is called here, but eventually it will be up to - * each I/O manager to decide what to do. - */ -#if defined(RTS_USER_SIGNALS) - finiUserSignals (); -#endif } void markCapabilityIOManager(evac_fn evac, void *user, CapIOManager *iomgr) @@ -657,6 +672,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. @@ -672,17 +722,8 @@ HsInt nocldstop = 0; */ 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,15 +13,19 @@ #include "rts/PosixSource.h" #include "Rts.h" +#include "IOManager.h" + +#if defined(IOMGR_ENABLED_MIO_POSIX) + +#include "MIO.h" +#include "IOManagerInternals.h" +#include "IOManSignals.h" + #include "Schedule.h" #include "RtsUtils.h" #include "Prelude.h" #include "ThreadLabels.h" -#include "MIO.h" -#include "IOManager.h" -#include "IOManagerInternals.h" - #if defined(HAVE_ERRNO_H) # include <errno.h> #endif @@ -29,62 +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); } } @@ -110,13 +117,23 @@ ioManagerStartCap (Capability **cap) void 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 ===================================== @@ -158,11 +158,23 @@ void initCapabilityIOManagerPoll(CapIOManager *iomgr) .revents = 0 }; iomgr->aiop_poll_table = iomgr->full_poll_table+1; /* hence empty */ + +#if defined(RTS_USER_SIGNALS) + /* Signal handlers are shared across all caps. Only init once. */ + if (iomgr->cap->no == 0) { + initUserSignals(); + } +#endif } void freeCapabilityIOManagerPoll(CapIOManager *iomgr) { +#if defined(RTS_USER_SIGNALS) + if (iomgr->cap->no == 0) { + finiUserSignals(); + } +#endif stgFree(iomgr->full_poll_table); #if defined(HAVE_PREEMPTION) closeFdWakeup(iomgr->interrupt_fd_r, iomgr->interrupt_fd_w); @@ -573,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 ===================================== @@ -72,10 +72,16 @@ void initCapabilityIOManagerSelect(CapIOManager *iomgr) barf("initCapabilityIOManagerSelect: fds out of select range"); } #endif +#if defined(RTS_USER_SIGNALS) + initUserSignals(); +#endif } void freeCapabilityIOManagerSelect(CapIOManager *iomgr USED_IF_PREEMPTION) { +#if defined(RTS_USER_SIGNALS) + finiUserSignals(); +#endif #if defined(HAVE_PREEMPTION) closeFdWakeup(iomgr->interrupt_fd_r, iomgr->interrupt_fd_w); #endif @@ -518,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 ===================================== @@ -153,11 +153,23 @@ void initCapabilityIOManagerSelectBis(CapIOManager *iomgr) iomgr->rfds = stgMallocBytes(sizeof (fd_set), "IOManagerSelectBis"); iomgr->wfds = stgMallocBytes(sizeof (fd_set), "IOManagerSelectBis"); + +#if defined(RTS_USER_SIGNALS) + /* Signal handlers are shared across all caps. Only init once. */ + if (iomgr->cap->no == 0) { + initUserSignals(); + } +#endif } void freeCapabilityIOManagerSelectBis(CapIOManager *iomgr) { +#if defined(RTS_USER_SIGNALS) + if (iomgr->cap->no == 0) { + finiUserSignals(); + } +#endif stgFree(iomgr->rfds); stgFree(iomgr->wfds); #if defined(HAVE_PREEMPTION) @@ -583,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" ===================================== rts/win32/AwaitEvent.c ===================================== @@ -46,7 +46,7 @@ awaitCompletedTimeoutsOrIOWin32(Capability *cap, bool wait) // If a signal was raised, we need to service it. This will typically // start a thread, which will cause us to drop out of the loop. - startPendingSignalHandlers(cap); + startPendingConsoleEventHandlers(cap); // The return value from awaitRequests() reports if it was interrupted by // abandonRequestWait(). Return to the scheduler if !wait, or ===================================== rts/win32/ConsoleHandler.c ===================================== @@ -33,12 +33,12 @@ DWORD stg_pending_buf[N_PENDING_EVENTS]; /* their associated event numbers. */ #endif /* - * Function: initUserSignals() + * Function: initConsoleEvents() * * Initialize the console handling substrate. */ void -initUserSignals(void) +initConsoleEvents(void) { console_handler = STG_SIG_DFL; #if !defined(THREADED_RTS) @@ -56,7 +56,7 @@ initUserSignals(void) /* Called in hs_exit to clean up resources. */ void -finiUserSignals(void) +finiConsoleEvents(void) { #if !defined(THREADED_RTS) if (hConsoleEvent != INVALID_HANDLE_VALUE) { @@ -129,15 +129,15 @@ void resetDefaultHandlers(void) /* Temporarily block the delivery of further console events. Needed to * avoid race conditions when emptying the stack by running the handlers. */ -static void blockUserSignals(void) +static void blockConsoleEvents(void) { deliver_event = false; } -/* The inverse of blockUserSignals(); re-enable the deliver of console events. +/* The inverse of blockConsoleEvents(); re-enable the deliver of console events. */ -static void unblockUserSignals(void) +static void unblockConsoleEvents(void) { deliver_event = true; } @@ -145,12 +145,12 @@ static void unblockUserSignals(void) #if !defined(THREADED_RTS) /* - * Function: startPendingSignalHandlers() + * Function: startPendingConsoleEventHandlers() * * If there are any queued up console events, run the handlers associated with * them. Console event delivery is blocked for the duration of this call. */ -void startPendingSignalHandlers(Capability *cap) +void startPendingConsoleEventHandlers(Capability *cap) { StgStablePtr handler; @@ -158,7 +158,7 @@ void startPendingSignalHandlers(Capability *cap) return; } - blockUserSignals(); + blockConsoleEvents(); ACQUIRE_LOCK(&sched_mutex); handler = deRefStablePtr((StgStablePtr)console_handler); @@ -176,7 +176,7 @@ void startPendingSignalHandlers(Capability *cap) } RELEASE_LOCK(&sched_mutex); - unblockUserSignals(); + unblockConsoleEvents(); } #endif /* !THREADED_RTS */ ===================================== rts/win32/ConsoleHandler.h ===================================== @@ -20,15 +20,15 @@ */ /* - * Function: initUserSignals() + * Function: initConsoleEvents() * * Initialize the console handling substrate. */ -void initUserSignals(void); +void initConsoleEvents(void); /* Finalise signal and console events handling. */ -void finiUserSignals(void); +void finiConsoleEvents(void); #if !defined(THREADED_RTS) /* @@ -37,12 +37,12 @@ void finiUserSignals(void); */ /* - * Function: startPendingSignalHandlers() + * Function: startPendingConsoleEventHandlers() * * If there are any queued up win32 console events, run the handlers * associated with them. */ -void startPendingSignalHandlers(Capability *cap); +void startPendingConsoleEventHandlers(Capability *cap); /* * Function: rts_waitConsoleHandlerCompletion() View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/f809312ba843b79d1747a8359e16d6f... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/f809312ba843b79d1747a8359e16d6f... 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