Duncan Coutts pushed to branch wip/dcoutts/signals-cleanup at Glasgow Haskell Compiler / GHC

Commits:

7 changed files:

Changes:

  • rts/IOManager.c
    ... ... @@ -412,7 +412,7 @@ void initCapabilityIOManager(CapIOManager *iomgr)
    412 412
     
    
    413 413
     #if defined(IOMGR_ENABLED_MIO_POSIX)
    
    414 414
             case IO_MANAGER_MIO_POSIX:
    
    415
    -            iomgr->control_fd = -1;
    
    415
    +            initCapabilityIOManagerMIO(iomgr);
    
    416 416
                 break;
    
    417 417
     #endif
    
    418 418
             default:
    

  • rts/IOManagerInternals.h
    ... ... @@ -113,6 +113,12 @@ struct _CapIOManager {
    113 113
      */
    
    114 114
     struct _CapIOManagerShared {
    
    115 115
     
    
    116
    +#if defined(IOMGR_ENABLED_MIO_POSIX)
    
    117
    +    /* The timer manager control pipe (write end). This is the pipe into which
    
    118
    +     * we will send our signals.
    
    119
    +     */
    
    120
    +    int timer_manager_control_wr_fd;
    
    121
    +#endif
    
    116 122
     };
    
    117 123
     
    
    118 124
     
    

  • rts/configure.ac
    ... ... @@ -221,10 +221,6 @@ AC_LINK_IFELSE([AC_LANG_CALL([], [printf\$LDBLStub])],
    221 221
     
    
    222 222
     FP_CHECK_PTHREAD_FUNCS
    
    223 223
     
    
    224
    -dnl ** check for eventfd which is needed by the I/O manager
    
    225
    -AC_CHECK_HEADERS([sys/eventfd.h])
    
    226
    -AC_CHECK_FUNCS([eventfd])
    
    227
    -
    
    228 224
     AC_CHECK_FUNCS([getpid getuid raise])
    
    229 225
     
    
    230 226
     dnl large address space support (see rts/include/rts/storage/MBlock.h)
    
    ... ... @@ -359,6 +355,10 @@ dnl -------------------------------------------------------------------
    359 355
     dnl Here we check for platform constraints. The result is the CPP flag
    
    360 356
     dnl IOMGR_BUILD_<name> and a EnableIOManager<Name> var for use here later.
    
    361 357
     
    
    358
    +dnl Check for functions (optionally) used by several I/O managers
    
    359
    +AC_CHECK_HEADERS([sys/eventfd.h])
    
    360
    +AC_CHECK_FUNCS([eventfd pipe2])
    
    361
    +
    
    362 362
     GHC_IOMANAGER_ENABLE([select], [EnableIOManagerSelect], [IOMGR_BUILD_SELECT],
    
    363 363
       [if test "$HostOS" = "mingw32"; then
    
    364 364
            EnableIOManagerSelect=NO
    

  • rts/posix/IOManSignals.c
    ... ... @@ -22,6 +22,7 @@
    22 22
     
    
    23 23
     #include <unistd.h>
    
    24 24
     #include <signal.h>
    
    25
    +#include <fcntl.h>
    
    25 26
     #include <string.h>
    
    26 27
     
    
    27 28
     /* -----------------------------------------------------------------------------
    
    ... ... @@ -286,12 +287,27 @@ startPendingSignalHandlers(Capability *cap)
    286 287
             continue; // handler has been changed.
    
    287 288
         }
    
    288 289
     
    
    289
    -    info = stgMallocBytes(sizeof(siginfo_t), "startPendingSignalHandlers");
    
    290
    -           // freed by runHandler
    
    291
    -    memcpy(info, next_pending_handler, sizeof(siginfo_t));
    
    290
    +    info = next_pending_handler;
    
    291
    +    startSignalHandler(cap, sig, info);
    
    292
    +  }
    
    293
    +
    
    294
    +  unblockUserSignals();
    
    295
    +}
    
    296
    +#endif
    
    297
    +
    
    298
    +void startSignalHandler(Capability *cap, int sig, siginfo_t *info)
    
    299
    +{
    
    300
    +    /* Allocate a copy. This is freed by runHandler. */
    
    301
    +    // TODO: this could be improved to use a GC'd pinned byte array, but this
    
    302
    +    // would need coordination with the unix library.
    
    303
    +    siginfo_t *info_copy = stgMallocBytes(sizeof(siginfo_t), "startSignalHandler");
    
    304
    +    *info_copy = *info;
    
    305
    +
    
    306
    +    /* The info structure can be zero on solaris. So at least set the signo. */
    
    307
    +    info_copy->si_signo = sig;
    
    292 308
     
    
    293 309
         debugTrace(DEBUG_iomanager,
    
    294
    -               "Handling pending signal, no %d", info->si_signo);
    
    310
    +               "Handling pending signal, no %d", info_copy->si_signo);
    
    295 311
     
    
    296 312
         StgTSO *t =
    
    297 313
             createIOThread(cap,
    
    ... ... @@ -299,14 +315,75 @@ startPendingSignalHandlers(Capability *cap)
    299 315
                            rts_apply(cap,
    
    300 316
                                      rts_apply(cap,
    
    301 317
                                                runHandlersPtr_closure,
    
    302
    -                                           rts_mkPtr(cap, info)),
    
    303
    -                                 rts_mkInt(cap, info->si_signo)));
    
    318
    +                                           rts_mkPtr(cap, info_copy)),
    
    319
    +                                 rts_mkInt(cap, info_copy->si_signo)));
    
    304 320
         scheduleThread(cap, t);
    
    305 321
         setThreadLabel(cap, t, "signal handler thread");
    
    306
    -  }
    
    307
    -
    
    308
    -  unblockUserSignals();
    
    309 322
     }
    
    323
    +
    
    324
    +void newNonBlockingPipe(int *pipe_fd_r, int *pipe_fd_w)
    
    325
    +{
    
    326
    +    int pipefd[2];
    
    327
    +#if defined(HAVE_PIPE2)
    
    328
    +    int res = pipe2(pipefd, O_CLOEXEC | O_NONBLOCK);
    
    329
    +#else
    
    330
    +    int res = pipe(pipefd);
    
    331
    +#endif
    
    332
    +    if (RTS_UNLIKELY(res < 0)) {
    
    333
    +        sysErrorBelch("newNonBlockingPipe");
    
    334
    +        stg_exit(EXIT_FAILURE);
    
    335
    +    }
    
    336
    +#if !defined(HAVE_PIPE2)
    
    337
    +    for (int i = 0; i < 2; i++) {
    
    338
    +        int res1 = fcntl(pipefd[i], F_SETFD, FD_CLOEXEC);
    
    339
    +        int res2 = fcntl(pipefd[i], F_SETFL, O_NONBLOCK);
    
    340
    +        if (RTS_UNLIKELY(res1 < 0 || res2 < 0)) {
    
    341
    +            sysErrorBelch("newFdWakeup fcntl()");
    
    342
    +            stg_exit(EXIT_FAILURE);
    
    343
    +        }
    
    344
    +    }
    
    310 345
     #endif
    
    346
    +    *pipe_fd_r = pipefd[0]; /* read end */
    
    347
    +    *pipe_fd_w = pipefd[1]; /* write end */
    
    348
    +}
    
    349
    +
    
    350
    +/* Write the signal number and siginfo into the given fd (which is
    
    351
    + * typically a pipe in non-blocking mode).
    
    352
    + */
    
    353
    +void writeSignalInfoToPipe(int sig, siginfo_t *info, int fd)
    
    354
    +{
    
    355
    +    StgWord8 buf[sizeof(siginfo_t) + 1];
    
    356
    +
    
    357
    +    buf[0] = sig;
    
    358
    +    if (info) {
    
    359
    +        memcpy(buf+1, info, sizeof(siginfo_t));
    
    360
    +    } else {
    
    361
    +        // info may be NULL on Solaris (see #3790)
    
    362
    +        memset(buf+1, 0, sizeof(siginfo_t));
    
    363
    +    }
    
    364
    +
    
    365
    +    int r = write(fd, buf, sizeof(siginfo_t)+1);
    
    366
    +    if (r == -1 && errno == EAGAIN) {
    
    367
    +        errorBelch("lost signal due to full pipe: %d\n", sig);
    
    368
    +    }
    
    369
    +}
    
    370
    +
    
    371
    +bool readSignalInfoFromPipe(int *sig, siginfo_t *info, int fd)
    
    372
    +{
    
    373
    +    StgWord8 buf[sizeof(siginfo_t) + 1];
    
    374
    +
    
    375
    +    int r = read(fd, buf, sizeof(siginfo_t)+1);
    
    376
    +
    
    377
    +    if (r == sizeof(siginfo_t)+1) {
    
    378
    +        *sig = (int) buf[0];
    
    379
    +        memcpy(info, buf+1, sizeof(siginfo_t));
    
    380
    +        return true;
    
    381
    +    } else {
    
    382
    +        // TODO: if r is >0 and < expected then we're kind of screwed
    
    383
    +        // it means we're all out of sync with the protocol.
    
    384
    +        // it _should_ never happen of course!
    
    385
    +        return false;
    
    386
    +    }
    
    387
    +}
    
    311 388
     
    
    312 389
     #endif /* RTS_USER_SIGNALS */

  • rts/posix/IOManSignals.h
    ... ... @@ -35,11 +35,18 @@ void finiUserSignals(void);
    35 35
     void startPendingSignalHandlers(Capability *cap);
    
    36 36
     #endif
    
    37 37
     
    
    38
    +void startSignalHandler(Capability *cap, int sig, siginfo_t *info);
    
    39
    +
    
    38 40
     typedef void (*SignalHandler)(int sig, siginfo_t *info, void *p);
    
    39 41
     
    
    40 42
     int installSignalHandler(int sig, int spi, sigset_t *mask, bool nocldstop,
    
    41 43
                              SignalHandler handler);
    
    42 44
     
    
    45
    +/* Utilities to help with the self-pipe trick for signal handling. */
    
    46
    +void newNonBlockingPipe(int *pipe_fd_r, int *pipe_fd_w);
    
    47
    +void writeSignalInfoToPipe (int  sig, siginfo_t *info, int fd);
    
    48
    +bool readSignalInfoFromPipe(int *sig, siginfo_t *info, int fd);
    
    49
    +
    
    43 50
     /* Temporary */
    
    44 51
     void generic_handler(int sig USED_IF_THREADS,
    
    45 52
                          siginfo_t *info,
    

  • rts/posix/MIO.c
    ... ... @@ -13,16 +13,19 @@
    13 13
     #include "rts/PosixSource.h"
    
    14 14
     #include "Rts.h"
    
    15 15
     
    
    16
    -#include "Schedule.h"
    
    17
    -#include "RtsUtils.h"
    
    18
    -#include "Prelude.h"
    
    19
    -#include "ThreadLabels.h"
    
    16
    +#include "IOManager.h"
    
    17
    +
    
    18
    +#if defined(IOMGR_ENABLED_MIO_POSIX)
    
    20 19
     
    
    21 20
     #include "MIO.h"
    
    22
    -#include "IOManager.h"
    
    23 21
     #include "IOManagerInternals.h"
    
    24 22
     #include "IOManSignals.h"
    
    25 23
     
    
    24
    +#include "Schedule.h"
    
    25
    +#include "RtsUtils.h"
    
    26
    +#include "Prelude.h"
    
    27
    +#include "ThreadLabels.h"
    
    28
    +
    
    26 29
     #if defined(HAVE_ERRNO_H)
    
    27 30
     # include <errno.h>
    
    28 31
     #endif
    
    ... ... @@ -30,39 +33,33 @@
    30 33
     #include <stdlib.h>
    
    31 34
     #include <unistd.h>
    
    32 35
     
    
    33
    -// Here's the pipe into which we will send our signals
    
    34
    -static int timer_manager_control_wr_fd = -1;
    
    35
    -// TODO: Eliminate these globals. Put then into the CapIOManager, but the
    
    36
    -// problem is these are shared across all caps, not per cap.
    
    37
    -
    
    38 36
     #define IO_MANAGER_DIE    0xfe
    
    39 37
     
    
    38
    +void initCapabilityIOManagerMIO(CapIOManager *iomgr)
    
    39
    +{
    
    40
    +    iomgr->control_fd = -1;
    
    41
    +    if (iomgr->cap->no == 0) {
    
    42
    +        iomgr->shared->timer_manager_control_wr_fd = -1;
    
    43
    +    }
    
    44
    +}
    
    45
    +
    
    40 46
     void setTimerManagerControlFd(int fd) {
    
    41
    -    RELAXED_STORE(&timer_manager_control_wr_fd, fd);
    
    47
    +    int *timer_manager_control_wr_fd =
    
    48
    +            &getCapability(0)->iomgr->shared->timer_manager_control_wr_fd;
    
    49
    +    RELAXED_STORE(timer_manager_control_wr_fd, fd);
    
    42 50
     }
    
    43 51
     
    
    44
    -#if defined(THREADED_RTS)
    
    45
    -static void timerManagerNotifySignal(int sig, siginfo_t *info,
    
    46
    -                                     void *p STG_UNUSED)
    
    52
    +static void signal_handler(int sig, siginfo_t *info, void *p STG_UNUSED)
    
    47 53
     {
    
    48
    -    StgWord8 buf[sizeof(siginfo_t) + 1];
    
    49
    -    int r;
    
    50
    -
    
    51
    -    buf[0] = sig;
    
    52
    -    if (info == NULL) {
    
    53
    -        // info may be NULL on Solaris (see #3790)
    
    54
    -        memset(buf+1, 0, sizeof(siginfo_t));
    
    55
    -    } else {
    
    56
    -        memcpy(buf+1, info, sizeof(siginfo_t));
    
    57
    -    }
    
    58
    -
    
    59
    -    int timer_control_fd = RELAXED_LOAD(&timer_manager_control_wr_fd);
    
    54
    +    /* In the signal handler we do not have Capability context, so access the
    
    55
    +     * control fd via capability 0.
    
    56
    +     */
    
    57
    +    int *timer_manager_control_wr_fd =
    
    58
    +            &getCapability(0)->iomgr->shared->timer_manager_control_wr_fd;
    
    59
    +    int timer_control_fd = RELAXED_LOAD(timer_manager_control_wr_fd);
    
    60 60
         if (0 <= timer_control_fd)
    
    61 61
         {
    
    62
    -        r = write(timer_control_fd, buf, sizeof(siginfo_t)+1);
    
    63
    -        if (r == -1 && errno == EAGAIN) {
    
    64
    -            errorBelch("lost signal due to full pipe: %d\n", sig);
    
    65
    -        }
    
    62
    +        writeSignalInfoToPipe(sig, info, timer_control_fd);
    
    66 63
         }
    
    67 64
     
    
    68 65
         // 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,
    73 70
     int installUserSignalHandlerMIO(int sig, int spi, sigset_t *mask,
    
    74 71
                                     bool nocldstop)
    
    75 72
     {
    
    76
    -    return installSignalHandler(sig, spi, mask, nocldstop,
    
    77
    -                                timerManagerNotifySignal);
    
    73
    +    return installSignalHandler(sig, spi, mask, nocldstop, signal_handler);
    
    78 74
     }
    
    79
    -#endif
    
    80
    -
    
    81 75
     
    
    82
    -#if defined(THREADED_RTS)
    
    83 76
     void
    
    84 77
     ioManagerDie (void)
    
    85 78
     {
    
    86 79
         finiUserSignals();
    
    87 80
     
    
    81
    +    int *timer_manager_control_wr_fd =
    
    82
    +            &getCapability(0)->iomgr->shared->timer_manager_control_wr_fd;
    
    83
    +
    
    88 84
         StgWord8 byte = (StgWord8)IO_MANAGER_DIE;
    
    89 85
         uint32_t i;
    
    90 86
         int r;
    
    91 87
     
    
    92 88
         {
    
    93 89
             // Shut down timer manager
    
    94
    -        const int fd = RELAXED_LOAD(&timer_manager_control_wr_fd);
    
    90
    +        const int fd = RELAXED_LOAD(timer_manager_control_wr_fd);
    
    95 91
             if (0 <= fd) {
    
    96 92
                 r = write(fd, &byte, 1);
    
    97 93
                 if (r == -1) { sysErrorBelch("ioManagerDie: write"); }
    
    98
    -            RELAXED_STORE(&timer_manager_control_wr_fd, -1);
    
    94
    +            RELAXED_STORE(timer_manager_control_wr_fd, -1);
    
    99 95
             }
    
    100 96
         }
    
    101 97
     
    
    ... ... @@ -123,13 +119,21 @@ ioManagerStart (void)
    123 119
     {
    
    124 120
         initUserSignals();
    
    125 121
     
    
    122
    +    int *timer_manager_control_wr_fd =
    
    123
    +            &getCapability(0)->iomgr->shared->timer_manager_control_wr_fd;
    
    124
    +
    
    126 125
         // Make sure the IO manager thread is running
    
    127 126
         Capability *cap;
    
    128
    -    if (SEQ_CST_LOAD(&timer_manager_control_wr_fd) < 0) {
    
    127
    +    if (SEQ_CST_LOAD(timer_manager_control_wr_fd) < 0) {
    
    129 128
             cap = rts_lock();
    
    130 129
             ioManagerStartCap(&cap);
    
    131 130
             rts_unlock(cap);
    
    132 131
         }
    
    133 132
     }
    
    134
    -#endif
    
    135 133
     
    
    134
    +#else // !IOMGR_ENABLED_MIO_POSIX
    
    135
    +
    
    136
    +/* This is exported from the RTS unconditionally, but only used for MIO. */
    
    137
    +void setTimerManagerControlFd(int fd STG_UNUSED) { /* do nothing */ }
    
    138
    +
    
    139
    +#endif // IOMGR_ENABLED_MIO_POSIX

  • rts/posix/MIO.h
    ... ... @@ -19,6 +19,8 @@
    19 19
     /* Communicating with the IO manager thread (see GHC.Conc).
    
    20 20
      */
    
    21 21
     #if defined(THREADED_RTS)
    
    22
    +void initCapabilityIOManagerMIO(CapIOManager *iomgr);
    
    23
    +
    
    22 24
     void ioManagerDie (void);
    
    23 25
     void ioManagerStart (void);
    
    24 26
     void ioManagerStartCap (/* inout */ Capability **cap);