eventcore.drivers.posix.signals 1/38(2%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
2018
210
220
230
240
250
260
270
280
290
300
310
320
330
340
350
360
370
380
390
400
410
420
430
440
450
460
470
480
490
500
510
520
530
540
550
560
570
580
590
600
610
620
630
640
650
660
670
680
690
700
710
720
730
740
750
760
770
780
790
800
810
820
830
840
850
860
870
880
890
900
910
920
930
940
950
960
970
980
990
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
1120
1130
1140
1150
1160
1170
1180
1190
1200
1210
module eventcore.drivers.posix.signals; @safe: import eventcore.driver; import eventcore.drivers.posix.driver; import eventcore.internal.utils : nogc_assert; import std.algorithm.comparison : among; final class SignalFDEventDriverSignals(Loop : PosixEventLoop) : EventDriverSignals { @safe: /*@nogc:*/ nothrow: import core.stdc.errno : errno, EAGAIN, EINPROGRESS; import core.sys.posix.signal; import core.sys.posix.unistd : close, read, write; import core.sys.linux.sys.signalfd; private Loop m_loop; this(Loop loop) { m_loop = loop; } override SignalListenID listen(int sig, SignalCallback on_signal) { return listenInternal(sig, on_signal, false); } package SignalListenID listenInternal(int sig, SignalCallback on_signal, bool is_internal = true) { auto fd = () @trusted { sigset_t sset; sigemptyset(&sset); sigaddset(&sset, sig); if (sigprocmask(SIG_BLOCK, &sset, null) != 0) return SignalListenID.invalid; return SignalListenID(signalfd(-1, &sset, SFD_NONBLOCK | SFD_CLOEXEC)); } (); m_loop.initFD(cast(FD)fd, is_internal ? FDFlags.internal : FDFlags.none, SignalSlot(on_signal)); m_loop.registerFD(cast(FD)fd, EventMask.read); m_loop.setNotifyCallback!(EventType.read)(cast(FD)fd, &onSignal); onSignal(cast(FD)fd); return fd; } override void addRef(SignalListenID descriptor) { assert(m_loop.m_fds[descriptor].common.refCount > 0, "Adding reference to unreferenced event FD."); m_loop.m_fds[descriptor].common.refCount++; } override bool releaseRef(SignalListenID descriptor) { FD fd = cast(FD)descriptor; nogc_assert(m_loop.m_fds[fd].common.refCount > 0, "Releasing reference to unreferenced event FD."); if (--m_loop.m_fds[fd].common.refCount == 1) { // NOTE: 1 because setNotifyCallback adds a second reference m_loop.setNotifyCallback!(EventType.read)(fd, null); m_loop.unregisterFD(fd, EventMask.read); m_loop.clearFD!SignalSlot(fd); close(cast(int)fd); return false; } return true; } private void onSignal(FD fd) { SignalListenID lid = cast(SignalListenID)fd; signalfd_siginfo nfo; do { auto ret = () @trusted { return read(cast(int)fd, &nfo, nfo.sizeof); } (); if (ret == -1 && errno.among!(EAGAIN, EINPROGRESS)) break; auto cb = m_loop.m_fds[fd].signal.callback; if (ret != nfo.sizeof) { cb(lid, SignalStatus.error, -1); return; } addRef(lid); cb(lid, SignalStatus.ok, nfo.ssi_signo); releaseRef(lid); } while (m_loop.m_fds[fd].common.refCount > 0); } } final class DummyEventDriverSignals(Loop : PosixEventLoop) : EventDriverSignals { @safe: /*@nogc:*/ nothrow: private Loop m_loop; this(Loop loop) { m_loop = loop; } override SignalListenID listen(int sig, SignalCallback on_signal) { return listenInternal(sig, on_signal, false); } package SignalListenID listenInternal(int sig, SignalCallback on_signal, bool is_internal = true) { assert(false); } override void addRef(SignalListenID descriptor) { assert(false); } override bool releaseRef(SignalListenID descriptor) { assert(false); } } package struct SignalSlot { alias Handle = SignalListenID; SignalCallback callback; }