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

Commits:

16 changed files:

Changes:

  • rts/IOManager.c
    ... ... @@ -369,6 +369,18 @@ CapIOManager *allocCapabilityIOManager(Capability *cap)
    369 369
      */
    
    370 370
     void initCapabilityIOManager(CapIOManager *iomgr)
    
    371 371
     {
    
    372
    +    /* Allocate or link the shared I/O manager data structure.
    
    373
    +     *
    
    374
    +     * We do this here, not in allocCapabilityIOManager, because when we
    
    375
    +     * forkProcess, we free and re-init the CapIOManager without reallocating.
    
    376
    +     */
    
    377
    +    if (iomgr->cap->no == 0) {
    
    378
    +        iomgr->shared = stgMallocBytes(sizeof(CapIOManagerShared),
    
    379
    +                                       "allocCapabilityIOManager");
    
    380
    +    } else {
    
    381
    +        iomgr->shared = capabilities[0]->iomgr->shared;
    
    382
    +    }
    
    383
    +
    
    372 384
         debugTrace(DEBUG_iomanager, "initialising I/O manager %s for cap %d",
    
    373 385
                    showIOManager(), iomgr->cap->no);
    
    374 386
     
    
    ... ... @@ -400,7 +412,7 @@ void initCapabilityIOManager(CapIOManager *iomgr)
    400 412
     
    
    401 413
     #if defined(IOMGR_ENABLED_MIO_POSIX)
    
    402 414
             case IO_MANAGER_MIO_POSIX:
    
    403
    -            iomgr->control_fd = -1;
    
    415
    +            initCapabilityIOManagerMIO(iomgr);
    
    404 416
                 break;
    
    405 417
     #endif
    
    406 418
             default:
    
    ... ... @@ -432,6 +444,13 @@ void freeCapabilityIOManager(CapIOManager *iomgr)
    432 444
             default:
    
    433 445
                 break;
    
    434 446
         }
    
    447
    +
    
    448
    +    /* We expect the per-impl free function (for cap 0) to free anything within
    
    449
    +     * the shared structure.
    
    450
    +     */
    
    451
    +    if (iomgr->cap->no == 0) {
    
    452
    +        stgFree(iomgr->shared);
    
    453
    +    }
    
    435 454
     }
    
    436 455
     
    
    437 456
     
    
    ... ... @@ -457,6 +476,7 @@ void startIOManager(void)
    457 476
                  * TODO: rename ioManagerStart to be less generic, impl specific
    
    458 477
                  */
    
    459 478
                 ioManagerStart();
    
    479
    +            initConsoleEvents(); //TODO: fully integrate
    
    460 480
                 break;
    
    461 481
     #endif
    
    462 482
     #if defined(IOMGR_ENABLED_WINIO)
    
    ... ... @@ -473,23 +493,18 @@ void startIOManager(void)
    473 493
     #else
    
    474 494
                 startupAsyncWinIO();
    
    475 495
     #endif
    
    496
    +            initConsoleEvents(); //TODO: fully integrate
    
    476 497
                 break;
    
    477 498
     #endif
    
    478 499
     #if defined(IOMGR_ENABLED_WIN32_LEGACY)
    
    479 500
             case IO_MANAGER_WIN32_LEGACY:
    
    480 501
                 startupAsyncIO();
    
    502
    +            initConsoleEvents(); //TODO: fully integrate
    
    481 503
                 break;
    
    482 504
     #endif
    
    483 505
             default:
    
    484 506
                 break;
    
    485 507
         }
    
    486
    -
    
    487
    -/* For the moment this is called here, but eventually it will be up to
    
    488
    - * each I/O manager to decide what to do.
    
    489
    - */
    
    490
    -#if defined(RTS_USER_SIGNALS)
    
    491
    -    initUserSignals();
    
    492
    -#endif
    
    493 508
     }
    
    494 509
     
    
    495 510
     /* Called from forkProcess in the child process on the surviving capability.
    
    ... ... @@ -581,26 +596,26 @@ void
    581 596
     exitIOManager(bool wait_threads)
    
    582 597
     {
    
    583 598
         switch (iomgr_type) {
    
    599
    +#if defined(IOMGR_ENABLED_MIO_WIN32)
    
    600
    +        case IO_MANAGER_MIO_WIN32:
    
    601
    +            finiConsoleEvents(); //TODO: fully integrate
    
    602
    +            break;
    
    603
    +#endif
    
    584 604
     #if defined(IOMGR_ENABLED_WINIO)
    
    585 605
             case IO_MANAGER_WINIO:
    
    586 606
                 shutdownAsyncWinIO(wait_threads);
    
    607
    +            finiConsoleEvents(); //TODO: fully integrate
    
    587 608
                 break;
    
    588 609
     #endif
    
    589 610
     #if defined(IOMGR_ENABLED_WIN32_LEGACY)
    
    590 611
             case IO_MANAGER_WIN32_LEGACY:
    
    591 612
                 shutdownAsyncIO(wait_threads);
    
    613
    +            finiConsoleEvents(); //TODO: fully integrate
    
    592 614
                 break;
    
    593 615
     #endif
    
    594 616
             default:
    
    595 617
                 break;
    
    596 618
         }
    
    597
    -
    
    598
    -/* For the moment this is called here, but eventually it will be up to
    
    599
    - * each I/O manager to decide what to do.
    
    600
    - */
    
    601
    -#if defined(RTS_USER_SIGNALS)
    
    602
    -   finiUserSignals ();
    
    603
    -#endif
    
    604 619
     }
    
    605 620
     
    
    606 621
     void markCapabilityIOManager(evac_fn evac, void *user, CapIOManager *iomgr)
    
    ... ... @@ -657,6 +672,41 @@ setIOManagerControlFd(uint32_t cap_no, int fd) {
    657 672
     
    
    658 673
     
    
    659 674
     #if !defined(mingw32_HOST_OS)
    
    675
    +/* This is a plausible candidate for a newer public API, but it isn't yet.
    
    676
    + * It is currently the worker for stg_sig_install below.
    
    677
    + */
    
    678
    +static int installUserSignalHandler(int sig, int spi, sigset_t *mask,
    
    679
    +                                    bool nocldstop)
    
    680
    +{
    
    681
    +#if !defined(RTS_USER_SIGNALS)
    
    682
    +    return STG_SIG_DFL;
    
    683
    +#endif
    
    684
    +
    
    685
    +    debugTrace(DEBUG_iomanager, "Installing a signal handler for sig %d", sig);
    
    686
    +
    
    687
    +    switch (iomgr_type) {
    
    688
    +#if defined(IOMGR_ENABLED_SELECT)
    
    689
    +        case IO_MANAGER_SELECT:
    
    690
    +          return installUserSignalHandlerSelect(sig, spi, mask, nocldstop);
    
    691
    +#endif
    
    692
    +#if defined(IOMGR_ENABLED_SELECTBIS)
    
    693
    +        case IO_MANAGER_SELECTBIS:
    
    694
    +          return installUserSignalHandlerSelectBis(sig, spi, mask, nocldstop);
    
    695
    +#endif
    
    696
    +#if defined(IOMGR_ENABLED_POLL)
    
    697
    +        case IO_MANAGER_POLL:
    
    698
    +          return installUserSignalHandlerPoll(sig, spi, mask, nocldstop);
    
    699
    +#endif
    
    700
    +#if defined(IOMGR_ENABLED_MIO_POSIX)
    
    701
    +        case IO_MANAGER_MIO_POSIX:
    
    702
    +          return installUserSignalHandlerMIO(sig, spi, mask, nocldstop);
    
    703
    +#endif
    
    704
    +        default:
    
    705
    +            barf("installUserSignalHandler not implemented");
    
    706
    +    }
    
    707
    +}
    
    708
    +
    
    709
    +
    
    660 710
     /* This flag is a public API, used by the unix package. It is an _implicit_
    
    661 711
      * argument to stg_sig_install to control whether or not to include
    
    662 712
      * SA_NOCLDSTOP when installing a SIGCHLD handler.
    
    ... ... @@ -672,17 +722,8 @@ HsInt nocldstop = 0;
    672 722
      */
    
    673 723
     int stg_sig_install(int sig, int spi, void *mask)
    
    674 724
     {
    
    675
    -    debugTrace(DEBUG_iomanager, "Installing a signal handler for sig %d", sig);
    
    676
    -
    
    677 725
         /* Grab the current value of the nocldstop argument. Yes this is madness. */
    
    678
    -    bool nocldstop_arg = nocldstop;
    
    679
    -
    
    680
    -#if defined(RTS_USER_SIGNALS)
    
    681
    -    return installUserSignalHandler(sig, spi, mask, nocldstop_arg);
    
    682
    -
    
    683
    -#else /* !RTS_USER_SIGNALS */
    
    684
    -  return STG_SIG_DFL;
    
    685
    -#endif
    
    726
    +    return installUserSignalHandler(sig, spi, mask, nocldstop);
    
    686 727
     }
    
    687 728
     #endif
    
    688 729
     
    

  • rts/IOManagerInternals.h
    ... ... @@ -30,6 +30,11 @@
    30 30
     
    
    31 31
     #include "BeginPrivate.h"
    
    32 32
     
    
    33
    +/* Forward reference */
    
    34
    +struct _CapIOManagerShared;
    
    35
    +typedef struct _CapIOManagerShared CapIOManagerShared;
    
    36
    +
    
    37
    +
    
    33 38
     /* The per-capability data structures belonging to the I/O manager.
    
    34 39
      *
    
    35 40
      * It can be accessed as cap->iomgr. Or given just the iomgr, you can access
    
    ... ... @@ -42,9 +47,12 @@
    42 47
      */
    
    43 48
     struct _CapIOManager {
    
    44 49
     
    
    45
    -   /* Back reference to the containing capability */
    
    50
    +    /* Back reference to the containing capability */
    
    46 51
         Capability *cap;
    
    47 52
     
    
    53
    +    /* Data shared between the I/O manager instances across all capabilities */
    
    54
    +    CapIOManagerShared *shared;
    
    55
    +
    
    48 56
     #if defined(IOMGR_ENABLED_SELECT)
    
    49 57
         /* Thread queue for threads blocked on I/O completion. */
    
    50 58
         StgTSO *blocked_queue_hd;
    
    ... ... @@ -96,6 +104,24 @@ struct _CapIOManager {
    96 104
     
    
    97 105
     };
    
    98 106
     
    
    107
    +
    
    108
    +/* Much like above, but for I/O manager data that is shared between the
    
    109
    + * I/O manager instances for all the capabilities.
    
    110
    + *
    
    111
    + * This gets allocated and linked by the shared IOManager.c code, and should be
    
    112
    + * initialised by the individual I/O manager's init function but only for cap 0.
    
    113
    + */
    
    114
    +struct _CapIOManagerShared {
    
    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
    
    122
    +};
    
    123
    +
    
    124
    +
    
    99 125
     /* Fill in the outcome and result/error on the TSO's stack frame.
    
    100 126
      * The TSO must be blocked using one of the stg_block_io_* blocking actions.
    
    101 127
      *
    

  • 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
     /* -----------------------------------------------------------------------------
    
    ... ... @@ -111,16 +112,12 @@ static inline bool signals_pending(void) {
    111 112
      * started up at the next context switch.
    
    112 113
      * -------------------------------------------------------------------------- */
    
    113 114
     
    
    114
    -static void
    
    115
    +#if !defined(THREADED_RTS)
    
    116
    +void
    
    115 117
     generic_handler(int sig USED_IF_THREADS,
    
    116 118
                     siginfo_t *info,
    
    117 119
                     void *p STG_UNUSED)
    
    118 120
     {
    
    119
    -#if defined(THREADED_RTS)
    
    120
    -    //TODO: This calls MIO directly. We should go via IOManager API.
    
    121
    -    // The IOManager API should be extended to cover signals.
    
    122
    -    timerManagerNotifySignal(sig, info);
    
    123
    -#else /* not THREADED_RTS */
    
    124 121
     
    
    125 122
         /* Can't call allocate from here.  Probably can't call malloc
    
    126 123
            either.  However, we have to schedule a new thread somehow.
    
    ... ... @@ -162,8 +159,8 @@ generic_handler(int sig USED_IF_THREADS,
    162 159
     
    
    163 160
         interruptCapability(&MainCapability);
    
    164 161
     
    
    165
    -#endif /* THREADED_RTS */
    
    166 162
     }
    
    163
    +#endif /* THREADED_RTS */
    
    167 164
     
    
    168 165
     /* -----------------------------------------------------------------------------
    
    169 166
      * Blocking/Unblocking of the user signals
    
    ... ... @@ -190,7 +187,8 @@ unblockUserSignals(void)
    190 187
      * -------------------------------------------------------------------------- */
    
    191 188
     
    
    192 189
     int
    
    193
    -installUserSignalHandler(int sig, int spi, void *mask, bool nocldstop)
    
    190
    +installSignalHandler(int sig, int spi, sigset_t *mask, bool nocldstop,
    
    191
    +                     SignalHandler signal_handler)
    
    194 192
     {
    
    195 193
         ACQUIRE_LOCK(&sig_mutex);
    
    196 194
     
    
    ... ... @@ -226,7 +224,7 @@ installUserSignalHandler(int sig, int spi, void *mask, bool nocldstop)
    226 224
             action.sa_flags |= SA_RESETHAND;
    
    227 225
             /* fall through */
    
    228 226
         case STG_SIG_HAN:
    
    229
    -        action.sa_sigaction = generic_handler;
    
    227
    +        action.sa_sigaction = signal_handler;
    
    230 228
             action.sa_flags |= SA_SIGINFO;
    
    231 229
             break;
    
    232 230
     
    
    ... ... @@ -235,7 +233,7 @@ installUserSignalHandler(int sig, int spi, void *mask, bool nocldstop)
    235 233
         }
    
    236 234
     
    
    237 235
         if (mask != NULL) {
    
    238
    -        action.sa_mask = *(sigset_t *)mask;
    
    236
    +        action.sa_mask = *mask;
    
    239 237
         } else {
    
    240 238
             sigemptyset(&action.sa_mask);
    
    241 239
         }
    
    ... ... @@ -289,12 +287,27 @@ startPendingSignalHandlers(Capability *cap)
    289 287
             continue; // handler has been changed.
    
    290 288
         }
    
    291 289
     
    
    292
    -    info = stgMallocBytes(sizeof(siginfo_t), "startPendingSignalHandlers");
    
    293
    -           // freed by runHandler
    
    294
    -    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;
    
    295 308
     
    
    296 309
         debugTrace(DEBUG_iomanager,
    
    297
    -               "Handling pending signal, no %d", info->si_signo);
    
    310
    +               "Handling pending signal, no %d", info_copy->si_signo);
    
    298 311
     
    
    299 312
         StgTSO *t =
    
    300 313
             createIOThread(cap,
    
    ... ... @@ -302,14 +315,75 @@ startPendingSignalHandlers(Capability *cap)
    302 315
                            rts_apply(cap,
    
    303 316
                                      rts_apply(cap,
    
    304 317
                                                runHandlersPtr_closure,
    
    305
    -                                           rts_mkPtr(cap, info)),
    
    306
    -                                 rts_mkInt(cap, info->si_signo)));
    
    318
    +                                           rts_mkPtr(cap, info_copy)),
    
    319
    +                                 rts_mkInt(cap, info_copy->si_signo)));
    
    307 320
         scheduleThread(cap, t);
    
    308 321
         setThreadLabel(cap, t, "signal handler thread");
    
    309
    -  }
    
    310
    -
    
    311
    -  unblockUserSignals();
    
    312 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
    +    }
    
    313 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
    +}
    
    314 388
     
    
    315 389
     #endif /* RTS_USER_SIGNALS */

  • rts/posix/IOManSignals.h
    ... ... @@ -8,6 +8,10 @@
    8 8
     
    
    9 9
     #pragma once
    
    10 10
     
    
    11
    +#if defined(RTS_USER_SIGNALS)
    
    12
    +
    
    13
    +#include <signal.h>
    
    14
    +
    
    11 15
     #include "BeginPrivate.h"
    
    12 16
     
    
    13 17
     /*
    
    ... ... @@ -31,7 +35,24 @@ void finiUserSignals(void);
    31 35
     void startPendingSignalHandlers(Capability *cap);
    
    32 36
     #endif
    
    33 37
     
    
    34
    -int installUserSignalHandler(int sig, int spi, void *mask, bool nocldstop);
    
    38
    +void startSignalHandler(Capability *cap, int sig, siginfo_t *info);
    
    39
    +
    
    40
    +typedef void (*SignalHandler)(int sig, siginfo_t *info, void *p);
    
    41
    +
    
    42
    +int installSignalHandler(int sig, int spi, sigset_t *mask, bool nocldstop,
    
    43
    +                         SignalHandler handler);
    
    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
    +
    
    50
    +/* Temporary */
    
    51
    +void generic_handler(int sig USED_IF_THREADS,
    
    52
    +                     siginfo_t *info,
    
    53
    +                     void *p STG_UNUSED);
    
    54
    +
    
    55
    +#endif
    
    35 56
     
    
    36 57
     #include "EndPrivate.h"
    
    37 58
     

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

  • rts/posix/Poll.c
    ... ... @@ -158,11 +158,23 @@ void initCapabilityIOManagerPoll(CapIOManager *iomgr)
    158 158
                                       .revents = 0
    
    159 159
                                     };
    
    160 160
         iomgr->aiop_poll_table = iomgr->full_poll_table+1; /* hence empty */
    
    161
    +
    
    162
    +#if defined(RTS_USER_SIGNALS)
    
    163
    +    /* Signal handlers are shared across all caps. Only init once. */
    
    164
    +    if (iomgr->cap->no == 0) {
    
    165
    +        initUserSignals();
    
    166
    +    }
    
    167
    +#endif
    
    161 168
     }
    
    162 169
     
    
    163 170
     
    
    164 171
     void freeCapabilityIOManagerPoll(CapIOManager *iomgr)
    
    165 172
     {
    
    173
    +#if defined(RTS_USER_SIGNALS)
    
    174
    +    if (iomgr->cap->no == 0) {
    
    175
    +        finiUserSignals();
    
    176
    +    }
    
    177
    +#endif
    
    166 178
         stgFree(iomgr->full_poll_table);
    
    167 179
     #if defined(HAVE_PREEMPTION)
    
    168 180
         closeFdWakeup(iomgr->interrupt_fd_r, iomgr->interrupt_fd_w);
    
    ... ... @@ -573,6 +585,13 @@ void interruptIOManagerPoll(CapIOManager *iomgr USED_IF_PREEMPTION)
    573 585
     }
    
    574 586
     
    
    575 587
     
    
    588
    +int installUserSignalHandlerPoll(int sig, int spi, sigset_t *mask,
    
    589
    +                                 bool nocldstop)
    
    590
    +{
    
    591
    +    return installSignalHandler(sig, spi, mask, nocldstop, generic_handler);
    
    592
    +}
    
    593
    +
    
    594
    +
    
    576 595
     /* Helper function to double the size of the aiop_table and aiop_poll_table.
    
    577 596
      */
    
    578 597
     static bool enlargeTables(CapIOManager *iomgr)
    

  • rts/posix/Poll.h
    ... ... @@ -34,6 +34,9 @@ void pollCompletedTimeoutsOrIOPoll(CapIOManager *iomgr);
    34 34
     bool awaitCompletedTimeoutsOrIOPoll(CapIOManager *iomgr);
    
    35 35
     void interruptIOManagerPoll(CapIOManager *iomgr);
    
    36 36
     
    
    37
    +/* Signals */
    
    38
    +int  installUserSignalHandlerPoll(int sig, int spi, sigset_t *mask, bool nocldstop);
    
    39
    +
    
    37 40
     #endif /* IOMGR_ENABLED_POLL */
    
    38 41
     
    
    39 42
     #include "EndPrivate.h"
    

  • rts/posix/Select.c
    ... ... @@ -72,10 +72,16 @@ void initCapabilityIOManagerSelect(CapIOManager *iomgr)
    72 72
             barf("initCapabilityIOManagerSelect: fds out of select range");
    
    73 73
         }
    
    74 74
     #endif
    
    75
    +#if defined(RTS_USER_SIGNALS)
    
    76
    +    initUserSignals();
    
    77
    +#endif
    
    75 78
     }
    
    76 79
     
    
    77 80
     void freeCapabilityIOManagerSelect(CapIOManager *iomgr USED_IF_PREEMPTION)
    
    78 81
     {
    
    82
    +#if defined(RTS_USER_SIGNALS)
    
    83
    +    finiUserSignals();
    
    84
    +#endif
    
    79 85
     #if defined(HAVE_PREEMPTION)
    
    80 86
         closeFdWakeup(iomgr->interrupt_fd_r, iomgr->interrupt_fd_w);
    
    81 87
     #endif
    
    ... ... @@ -518,4 +524,10 @@ awaitCompletedTimeoutsOrIOSelect(CapIOManager *iomgr, bool wait)
    518 524
         return !interrupt;
    
    519 525
     }
    
    520 526
     
    
    527
    +int installUserSignalHandlerSelect(int sig, int spi, sigset_t *mask,
    
    528
    +                                   bool nocldstop)
    
    529
    +{
    
    530
    +    return installSignalHandler(sig, spi, mask, nocldstop, generic_handler);
    
    531
    +}
    
    532
    +
    
    521 533
     #endif /* IOMGR_ENABLED_SELECT */

  • rts/posix/Select.h
    ... ... @@ -21,6 +21,8 @@ void wakeupIOManagerSelect(CapIOManager *iomgr);
    21 21
     
    
    22 22
     bool awaitCompletedTimeoutsOrIOSelect(CapIOManager *iomgr, bool wait);
    
    23 23
     void interruptIOManagerSelect(CapIOManager *iomgr);
    
    24
    +int  installUserSignalHandlerSelect(int sig, int spi, sigset_t *mask,
    
    25
    +                                    bool nocldstop);
    
    24 26
     
    
    25 27
     #include "EndPrivate.h"
    
    26 28
     

  • rts/posix/SelectBis.c
    ... ... @@ -153,11 +153,23 @@ void initCapabilityIOManagerSelectBis(CapIOManager *iomgr)
    153 153
     
    
    154 154
         iomgr->rfds = stgMallocBytes(sizeof (fd_set), "IOManagerSelectBis");
    
    155 155
         iomgr->wfds = stgMallocBytes(sizeof (fd_set), "IOManagerSelectBis");
    
    156
    +
    
    157
    +#if defined(RTS_USER_SIGNALS)
    
    158
    +    /* Signal handlers are shared across all caps. Only init once. */
    
    159
    +    if (iomgr->cap->no == 0) {
    
    160
    +        initUserSignals();
    
    161
    +    }
    
    162
    +#endif
    
    156 163
     }
    
    157 164
     
    
    158 165
     
    
    159 166
     void freeCapabilityIOManagerSelectBis(CapIOManager *iomgr)
    
    160 167
     {
    
    168
    +#if defined(RTS_USER_SIGNALS)
    
    169
    +    if (iomgr->cap->no == 0) {
    
    170
    +        finiUserSignals();
    
    171
    +    }
    
    172
    +#endif
    
    161 173
         stgFree(iomgr->rfds);
    
    162 174
         stgFree(iomgr->wfds);
    
    163 175
     #if defined(HAVE_PREEMPTION)
    
    ... ... @@ -583,6 +595,13 @@ void interruptIOManagerSelectBis(CapIOManager *iomgr USED_IF_PREEMPTION)
    583 595
     }
    
    584 596
     
    
    585 597
     
    
    598
    +int installUserSignalHandlerSelectBis(int sig, int spi, sigset_t *mask,
    
    599
    +                                      bool nocldstop)
    
    600
    +{
    
    601
    +    return installSignalHandler(sig, spi, mask, nocldstop, generic_handler);
    
    602
    +}
    
    603
    +
    
    604
    +
    
    586 605
     /* Helper function to double the size of the aiop_table.
    
    587 606
      */
    
    588 607
     static bool enlargeTables(CapIOManager *iomgr)
    

  • rts/posix/SelectBis.h
    ... ... @@ -55,6 +55,10 @@ void pollCompletedTimeoutsOrIOSelectBis(CapIOManager *iomgr);
    55 55
     bool awaitCompletedTimeoutsOrIOSelectBis(CapIOManager *iomgr);
    
    56 56
     void interruptIOManagerSelectBis(CapIOManager *iomgr);
    
    57 57
     
    
    58
    +/* Signals */
    
    59
    +int  installUserSignalHandlerSelectBis(int sig, int spi, sigset_t *mask,
    
    60
    +                                       bool nocldstop);
    
    61
    +
    
    58 62
     #endif /* IOMGR_ENABLED_SELECTBIS */
    
    59 63
     
    
    60 64
     #include "EndPrivate.h"
    

  • rts/win32/AwaitEvent.c
    ... ... @@ -46,7 +46,7 @@ awaitCompletedTimeoutsOrIOWin32(Capability *cap, bool wait)
    46 46
     
    
    47 47
         // If a signal was raised, we need to service it. This will typically
    
    48 48
         // start a thread, which will cause us to drop out of the loop.
    
    49
    -    startPendingSignalHandlers(cap);
    
    49
    +    startPendingConsoleEventHandlers(cap);
    
    50 50
     
    
    51 51
         // The return value from awaitRequests() reports if it was interrupted by
    
    52 52
         // 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. */
    33 33
     #endif
    
    34 34
     
    
    35 35
     /*
    
    36
    - * Function: initUserSignals()
    
    36
    + * Function: initConsoleEvents()
    
    37 37
      *
    
    38 38
      * Initialize the console handling substrate.
    
    39 39
      */
    
    40 40
     void
    
    41
    -initUserSignals(void)
    
    41
    +initConsoleEvents(void)
    
    42 42
     {
    
    43 43
         console_handler = STG_SIG_DFL;
    
    44 44
     #if !defined(THREADED_RTS)
    
    ... ... @@ -56,7 +56,7 @@ initUserSignals(void)
    56 56
     
    
    57 57
     /* Called in hs_exit to clean up resources.  */
    
    58 58
     void
    
    59
    -finiUserSignals(void)
    
    59
    +finiConsoleEvents(void)
    
    60 60
     {
    
    61 61
     #if !defined(THREADED_RTS)
    
    62 62
         if (hConsoleEvent != INVALID_HANDLE_VALUE) {
    
    ... ... @@ -129,15 +129,15 @@ void resetDefaultHandlers(void)
    129 129
     /* Temporarily block the delivery of further console events. Needed to
    
    130 130
      * avoid race conditions when emptying the stack by running the handlers.
    
    131 131
      */
    
    132
    -static void blockUserSignals(void)
    
    132
    +static void blockConsoleEvents(void)
    
    133 133
     {
    
    134 134
         deliver_event = false;
    
    135 135
     }
    
    136 136
     
    
    137 137
     
    
    138
    -/* The inverse of blockUserSignals(); re-enable the deliver of console events.
    
    138
    +/* The inverse of blockConsoleEvents(); re-enable the deliver of console events.
    
    139 139
      */
    
    140
    -static void unblockUserSignals(void)
    
    140
    +static void unblockConsoleEvents(void)
    
    141 141
     {
    
    142 142
         deliver_event = true;
    
    143 143
     }
    
    ... ... @@ -145,12 +145,12 @@ static void unblockUserSignals(void)
    145 145
     
    
    146 146
     #if !defined(THREADED_RTS)
    
    147 147
     /*
    
    148
    - * Function: startPendingSignalHandlers()
    
    148
    + * Function: startPendingConsoleEventHandlers()
    
    149 149
      *
    
    150 150
      * If there are any queued up console events, run the handlers associated with
    
    151 151
      * them. Console event delivery is blocked for the duration of this call.
    
    152 152
      */
    
    153
    -void startPendingSignalHandlers(Capability *cap)
    
    153
    +void startPendingConsoleEventHandlers(Capability *cap)
    
    154 154
     {
    
    155 155
         StgStablePtr handler;
    
    156 156
     
    
    ... ... @@ -158,7 +158,7 @@ void startPendingSignalHandlers(Capability *cap)
    158 158
             return;
    
    159 159
         }
    
    160 160
     
    
    161
    -    blockUserSignals();
    
    161
    +    blockConsoleEvents();
    
    162 162
         ACQUIRE_LOCK(&sched_mutex);
    
    163 163
     
    
    164 164
         handler = deRefStablePtr((StgStablePtr)console_handler);
    
    ... ... @@ -176,7 +176,7 @@ void startPendingSignalHandlers(Capability *cap)
    176 176
         }
    
    177 177
     
    
    178 178
         RELEASE_LOCK(&sched_mutex);
    
    179
    -    unblockUserSignals();
    
    179
    +    unblockConsoleEvents();
    
    180 180
     }
    
    181 181
     #endif /* !THREADED_RTS */
    
    182 182
     
    

  • rts/win32/ConsoleHandler.h
    ... ... @@ -20,15 +20,15 @@
    20 20
      */
    
    21 21
     
    
    22 22
     /*
    
    23
    - * Function: initUserSignals()
    
    23
    + * Function: initConsoleEvents()
    
    24 24
      *
    
    25 25
      * Initialize the console handling substrate.
    
    26 26
      */
    
    27
    -void initUserSignals(void);
    
    27
    +void initConsoleEvents(void);
    
    28 28
     
    
    29 29
     /* Finalise signal and console events handling.
    
    30 30
      */
    
    31
    -void finiUserSignals(void);
    
    31
    +void finiConsoleEvents(void);
    
    32 32
     
    
    33 33
     #if !defined(THREADED_RTS)
    
    34 34
     /*
    
    ... ... @@ -37,12 +37,12 @@ void finiUserSignals(void);
    37 37
      */
    
    38 38
     
    
    39 39
     /*
    
    40
    - * Function: startPendingSignalHandlers()
    
    40
    + * Function: startPendingConsoleEventHandlers()
    
    41 41
      *
    
    42 42
      * If there are any queued up win32 console events, run the handlers
    
    43 43
      * associated with them.
    
    44 44
      */
    
    45
    -void startPendingSignalHandlers(Capability *cap);
    
    45
    +void startPendingConsoleEventHandlers(Capability *cap);
    
    46 46
     
    
    47 47
     /*
    
    48 48
      * Function: rts_waitConsoleHandlerCompletion()