Duncan Coutts pushed to branch wip/dcoutts/signals-cleanup at Glasgow Haskell Compiler / GHC
Commits:
-
84627130
by Duncan Coutts at 2026-10-22T21:42:28+01:00
-
a03ec47f
by Duncan Coutts at 2026-10-22T21:42:28+01:00
-
b1f2d9ed
by Duncan Coutts at 2026-10-22T21:42:28+01:00
-
f809312b
by Duncan Coutts at 2026-10-22T21:42:37+01:00
13 changed files:
- rts/IOManager.c
- rts/IOManagerInternals.h
- rts/configure.ac
- rts/posix/IOManSignals.c
- rts/posix/IOManSignals.h
- rts/posix/MIO.c
- rts/posix/MIO.h
- rts/posix/Poll.c
- rts/posix/Poll.h
- rts/posix/Select.c
- rts/posix/Select.h
- rts/posix/SelectBis.c
- rts/posix/SelectBis.h
Changes:
| ... | ... | @@ -356,6 +356,15 @@ CapIOManager *allocCapabilityIOManager(Capability *cap) |
| 356 | 356 | CapIOManager *iomgr = stgMallocBytes(sizeof(CapIOManager),
|
| 357 | 357 | "allocCapabilityIOManager");
|
| 358 | 358 | iomgr->cap = cap; /* link back */
|
| 359 | + |
|
| 360 | + /* Allocate and/or link the shared I/O manager data structure */
|
|
| 361 | + if (cap->no == 0) {
|
|
| 362 | + iomgr->shared = stgMallocBytes(sizeof(CapIOManagerShared),
|
|
| 363 | + "allocCapabilityIOManager");
|
|
| 364 | + } else {
|
|
| 365 | + iomgr->shared = MainCapability.iomgr->shared;
|
|
| 366 | + }
|
|
| 367 | + |
|
| 359 | 368 | return iomgr;
|
| 360 | 369 | }
|
| 361 | 370 | |
| ... | ... | @@ -372,6 +381,8 @@ void initCapabilityIOManager(CapIOManager *iomgr) |
| 372 | 381 | debugTrace(DEBUG_iomanager, "initialising I/O manager %s for cap %d",
|
| 373 | 382 | showIOManager(), iomgr->cap->no);
|
| 374 | 383 | |
| 384 | + ASSERT(iomgr->cap && iomgr->shared); /* These are initialised already */
|
|
| 385 | + |
|
| 375 | 386 | switch (iomgr_type) {
|
| 376 | 387 | #if defined(IOMGR_ENABLED_SELECT)
|
| 377 | 388 | case IO_MANAGER_SELECT:
|
| ... | ... | @@ -400,7 +411,7 @@ void initCapabilityIOManager(CapIOManager *iomgr) |
| 400 | 411 | |
| 401 | 412 | #if defined(IOMGR_ENABLED_MIO_POSIX)
|
| 402 | 413 | case IO_MANAGER_MIO_POSIX:
|
| 403 | - iomgr->control_fd = -1;
|
|
| 414 | + initCapabilityIOManagerMIO(iomgr);
|
|
| 404 | 415 | break;
|
| 405 | 416 | #endif
|
| 406 | 417 | default:
|
| ... | ... | @@ -432,6 +443,13 @@ void freeCapabilityIOManager(CapIOManager *iomgr) |
| 432 | 443 | default:
|
| 433 | 444 | break;
|
| 434 | 445 | }
|
| 446 | + |
|
| 447 | + /* We expect the per-impl free function (for cap 0) to free anything within
|
|
| 448 | + * the shared structure.
|
|
| 449 | + */
|
|
| 450 | + if (iomgr->cap->no == 0) {
|
|
| 451 | + stgFree(iomgr->shared);
|
|
| 452 | + }
|
|
| 435 | 453 | }
|
| 436 | 454 | |
| 437 | 455 | |
| ... | ... | @@ -653,6 +671,41 @@ setIOManagerControlFd(uint32_t cap_no, int fd) { |
| 653 | 671 | |
| 654 | 672 | |
| 655 | 673 | #if !defined(mingw32_HOST_OS)
|
| 674 | +/* This is a plausible candidate for a newer public API, but it isn't yet.
|
|
| 675 | + * It is currently the worker for stg_sig_install below.
|
|
| 676 | + */
|
|
| 677 | +static int installUserSignalHandler(int sig, int spi, sigset_t *mask,
|
|
| 678 | + bool nocldstop)
|
|
| 679 | +{
|
|
| 680 | +#if !defined(RTS_USER_SIGNALS)
|
|
| 681 | + return STG_SIG_DFL;
|
|
| 682 | +#endif
|
|
| 683 | + |
|
| 684 | + debugTrace(DEBUG_iomanager, "Installing a signal handler for sig %d", sig);
|
|
| 685 | + |
|
| 686 | + switch (iomgr_type) {
|
|
| 687 | +#if defined(IOMGR_ENABLED_SELECT)
|
|
| 688 | + case IO_MANAGER_SELECT:
|
|
| 689 | + return installUserSignalHandlerSelect(sig, spi, mask, nocldstop);
|
|
| 690 | +#endif
|
|
| 691 | +#if defined(IOMGR_ENABLED_SELECTBIS)
|
|
| 692 | + case IO_MANAGER_SELECTBIS:
|
|
| 693 | + return installUserSignalHandlerSelectBis(sig, spi, mask, nocldstop);
|
|
| 694 | +#endif
|
|
| 695 | +#if defined(IOMGR_ENABLED_POLL)
|
|
| 696 | + case IO_MANAGER_POLL:
|
|
| 697 | + return installUserSignalHandlerPoll(sig, spi, mask, nocldstop);
|
|
| 698 | +#endif
|
|
| 699 | +#if defined(IOMGR_ENABLED_MIO_POSIX)
|
|
| 700 | + case IO_MANAGER_MIO_POSIX:
|
|
| 701 | + return installUserSignalHandlerMIO(sig, spi, mask, nocldstop);
|
|
| 702 | +#endif
|
|
| 703 | + default:
|
|
| 704 | + barf("installUserSignalHandler not implemented");
|
|
| 705 | + }
|
|
| 706 | +}
|
|
| 707 | + |
|
| 708 | + |
|
| 656 | 709 | /* This flag is a public API, used by the unix package. It is an _implicit_
|
| 657 | 710 | * argument to stg_sig_install to control whether or not to include
|
| 658 | 711 | * SA_NOCLDSTOP when installing a SIGCHLD handler.
|
| ... | ... | @@ -665,20 +718,14 @@ HsInt nocldstop = 0; |
| 665 | 718 | |
| 666 | 719 | /* Declared in rts/IOInterface.h. This is a public API, used by the unix
|
| 667 | 720 | * package for installHandler.
|
| 721 | + *
|
|
| 722 | + * This is used as an FFI ccall import, but it would be better as a primop, for
|
|
| 723 | + * consistency and so we can always have a Capability to hand.
|
|
| 668 | 724 | */
|
| 669 | 725 | int stg_sig_install(int sig, int spi, void *mask)
|
| 670 | 726 | {
|
| 671 | - debugTrace(DEBUG_iomanager, "Installing a signal handler for sig %d", sig);
|
|
| 672 | - |
|
| 673 | 727 | /* Grab the current value of the nocldstop argument. Yes this is madness. */
|
| 674 | - bool nocldstop_arg = nocldstop;
|
|
| 675 | - |
|
| 676 | -#if defined(RTS_USER_SIGNALS)
|
|
| 677 | - return installUserSignalHandler(sig, spi, mask, nocldstop_arg);
|
|
| 678 | - |
|
| 679 | -#else /* !RTS_USER_SIGNALS */
|
|
| 680 | - return STG_SIG_DFL;
|
|
| 681 | -#endif
|
|
| 728 | + return installUserSignalHandler(sig, spi, mask, nocldstop);
|
|
| 682 | 729 | }
|
| 683 | 730 | #endif
|
| 684 | 731 |
| ... | ... | @@ -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 | *
|
| ... | ... | @@ -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
|
| ... | ... | @@ -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 */ |
| ... | ... | @@ -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 |
| ... | ... | @@ -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,64 +33,65 @@ |
| 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 == 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 | + &MainCapability.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 | -void timerManagerNotifySignal(int sig, siginfo_t *info)
|
|
| 52 | +static void signal_handler(int sig, siginfo_t *info, void *p STG_UNUSED)
|
|
| 46 | 53 | {
|
| 47 | - StgWord8 buf[sizeof(siginfo_t) + 1];
|
|
| 48 | - int r;
|
|
| 49 | - |
|
| 50 | - buf[0] = sig;
|
|
| 51 | - if (info == NULL) {
|
|
| 52 | - // info may be NULL on Solaris (see #3790)
|
|
| 53 | - memset(buf+1, 0, sizeof(siginfo_t));
|
|
| 54 | - } else {
|
|
| 55 | - memcpy(buf+1, info, sizeof(siginfo_t));
|
|
| 56 | - }
|
|
| 57 | - |
|
| 58 | - 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);
|
|
| 59 | 60 | if (0 <= timer_control_fd)
|
| 60 | 61 | {
|
| 61 | - r = write(timer_control_fd, buf, sizeof(siginfo_t)+1);
|
|
| 62 | - if (r == -1 && errno == EAGAIN) {
|
|
| 63 | - errorBelch("lost signal due to full pipe: %d\n", sig);
|
|
| 64 | - }
|
|
| 62 | + writeSignalInfoToPipe(sig, info, timer_control_fd);
|
|
| 65 | 63 | }
|
| 66 | 64 | |
| 67 | 65 | // If the IO manager hasn't told us what the FD of the write end
|
| 68 | 66 | // of its pipe is, there's not much we can do here, so just ignore
|
| 69 | 67 | // the signal..
|
| 70 | 68 | }
|
| 71 | -#endif
|
|
| 72 | 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 | +}
|
|
| 73 | 75 | |
| 74 | -#if defined(THREADED_RTS)
|
|
| 75 | 76 | void
|
| 76 | 77 | ioManagerDie (void)
|
| 77 | 78 | {
|
| 78 | 79 | finiUserSignals();
|
| 79 | 80 | |
| 81 | + int *timer_manager_control_wr_fd =
|
|
| 82 | + &MainCapability.iomgr->shared->timer_manager_control_wr_fd;
|
|
| 83 | + |
|
| 80 | 84 | StgWord8 byte = (StgWord8)IO_MANAGER_DIE;
|
| 81 | 85 | uint32_t i;
|
| 82 | 86 | int r;
|
| 83 | 87 | |
| 84 | 88 | {
|
| 85 | 89 | // Shut down timer manager
|
| 86 | - const int fd = RELAXED_LOAD(&timer_manager_control_wr_fd);
|
|
| 90 | + const int fd = RELAXED_LOAD(timer_manager_control_wr_fd);
|
|
| 87 | 91 | if (0 <= fd) {
|
| 88 | 92 | r = write(fd, &byte, 1);
|
| 89 | 93 | if (r == -1) { sysErrorBelch("ioManagerDie: write"); }
|
| 90 | - RELAXED_STORE(&timer_manager_control_wr_fd, -1);
|
|
| 94 | + RELAXED_STORE(timer_manager_control_wr_fd, -1);
|
|
| 91 | 95 | }
|
| 92 | 96 | }
|
| 93 | 97 | |
| ... | ... | @@ -115,13 +119,21 @@ ioManagerStart (void) |
| 115 | 119 | {
|
| 116 | 120 | initUserSignals();
|
| 117 | 121 | |
| 122 | + int *timer_manager_control_wr_fd =
|
|
| 123 | + &MainCapability.iomgr->shared->timer_manager_control_wr_fd;
|
|
| 124 | + |
|
| 118 | 125 | // Make sure the IO manager thread is running
|
| 119 | 126 | Capability *cap;
|
| 120 | - if (SEQ_CST_LOAD(&timer_manager_control_wr_fd) < 0) {
|
|
| 127 | + if (SEQ_CST_LOAD(timer_manager_control_wr_fd) < 0) {
|
|
| 121 | 128 | cap = rts_lock();
|
| 122 | 129 | ioManagerStartCap(&cap);
|
| 123 | 130 | rts_unlock(cap);
|
| 124 | 131 | }
|
| 125 | 132 | }
|
| 126 | -#endif
|
|
| 127 | 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 |
| ... | ... | @@ -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" |
| ... | ... | @@ -585,6 +585,13 @@ void interruptIOManagerPoll(CapIOManager *iomgr USED_IF_PREEMPTION) |
| 585 | 585 | }
|
| 586 | 586 | |
| 587 | 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 | + |
|
| 588 | 595 | /* Helper function to double the size of the aiop_table and aiop_poll_table.
|
| 589 | 596 | */
|
| 590 | 597 | static bool enlargeTables(CapIOManager *iomgr)
|
| ... | ... | @@ -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"
|
| ... | ... | @@ -524,4 +524,10 @@ awaitCompletedTimeoutsOrIOSelect(CapIOManager *iomgr, bool wait) |
| 524 | 524 | return !interrupt;
|
| 525 | 525 | }
|
| 526 | 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 | + |
|
| 527 | 533 | #endif /* IOMGR_ENABLED_SELECT */ |
| ... | ... | @@ -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 |
| ... | ... | @@ -595,6 +595,13 @@ void interruptIOManagerSelectBis(CapIOManager *iomgr USED_IF_PREEMPTION) |
| 595 | 595 | }
|
| 596 | 596 | |
| 597 | 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 | + |
|
| 598 | 605 | /* Helper function to double the size of the aiop_table.
|
| 599 | 606 | */
|
| 600 | 607 | static bool enlargeTables(CapIOManager *iomgr)
|
| ... | ... | @@ -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"
|