eventcore.drivers.posix.driver 51/132(38%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
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
669
670
689
699
709
719
729
739
749
759
769
770
780
790
800
810
820
830
840
850
860
870
880
890
900
910
920
9324
948
958
968
978
988
990
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
1120
1130
1140
1150
1160
1170
1180
1190
1200
1210
1220
1230
1249
1250
1269
1279
1289
1299
1300
1310
1329
1330
1340
1350
1360
1379
1389
1390
1400
1410
1420
1438
1448
1458
1468
1470
1480
1490
1500
1510
1520
1530
1540
1550
1560
1570
1580
1590
1600
1610
1620
1630
1640
1650
1660
1670
1680
1690
1700
1710
1720
1730
1740
1750
1760
1770
1780
1790
1800
1810
1820
1830
1840
1850
1860
1870
1880
1890
1900
1910
1920
1930
1940
1950
1960
1970
1980
1990
2000
2010
2020
2030
2040
2050
2060
2070
2080
2090
2100
2110
2120
2130
2140
2150
2160
2170
2180
2190
2200
2210
2220
2230
2240
2250
2260
2270
2280
2290
2300
2310
2320
2330
2340
2350
2360
2370
2380
2390
2400
2410
2420
2430
2440
2450
2460
2478
2488
2490
25024
25116
2520
2530
2540
2550
2560
2570
2580
2590
2600
2610
2620
2630
2640
2650
2660
2670
2680
2690
2700
2710
2720
2730
2740
2750
2760
2770
2780
2790
2800
2810
2820
2830
2840
2850
2860
2870
2880
2890
2900
2910
2920
2930
2940
2950
2960
2970
2980
2990
30018
3010
30218
3030
3040
30518
30618
30718
30818
3090
3100
3110
3120
31318
3140
3150
3160
3170
3180
31918
32018
32118
32218
32318
32418
3250
3260
3270
3280
3290
3300
3310
33232
33316
33416
33516
3360
33716
3380
3390
3400
34116
3420
3430
3440
3450
3460
3470
3480
3490
3500
3510
3520
3530
3540
3550
3560
3570
3580
3590
3600
3610
3620
3630
3640
3650
3660
3670
3680
3690
3700
3710
3720
3730
3740
3750
3760
3770
3780
3790
3800
3810
3820
3830
3840
3850
3860
3870
3880
3890
3900
3910
3920
3930
3940
3950
3960
3970
3980
3990
4000
4010
4020
4030
4040
4050
4060
4070
4080
4090
4100
4110
4120
4130
4140
4150
4160
4170
4180
4190
4200
4210
4220
4230
4240
4250
4260
4270
4280
4290
4300
4310
4320
4330
4340
4350
4360
4370
4380
4390
4400
4410
4420
4430
4440
4450
4460
4470
4480
4490
4500
/** Base class for BSD socket based driver implementations. See_also: `eventcore.drivers.select`, `eventcore.drivers.epoll`, `eventcore.drivers.kqueue` */ module eventcore.drivers.posix.driver; @safe: /*@nogc:*/ nothrow: public import eventcore.driver; import eventcore.drivers.posix.dns; import eventcore.drivers.posix.events; import eventcore.drivers.posix.signals; import eventcore.drivers.posix.sockets; import eventcore.drivers.posix.watchers; import eventcore.drivers.timer; import eventcore.drivers.threadedfile; import eventcore.internal.consumablequeue : ConsumableQueue; import eventcore.internal.utils; import std.algorithm.comparison : among, min, max; version (Posix) { package alias sock_t = int; } version (Windows) { package alias sock_t = size_t; } private long currStdTime() { import std.datetime : Clock; scope (failure) assert(false); return Clock.currStdTime; } final class PosixEventDriver(Loop : PosixEventLoop) : EventDriver { @safe: /*@nogc:*/ nothrow: private { alias CoreDriver = PosixEventDriverCore!(Loop, TimerDriver, EventsDriver); alias EventsDriver = PosixEventDriverEvents!(Loop, SocketsDriver); version (linux) alias SignalsDriver = SignalFDEventDriverSignals!Loop; else alias SignalsDriver = DummyEventDriverSignals!Loop; alias TimerDriver = LoopTimeoutTimerDriver; alias SocketsDriver = PosixEventDriverSockets!Loop; version (Windows) alias DNSDriver = EventDriverDNS_GHBN!(EventsDriver, SignalsDriver); //version (linux) alias DNSDriver = EventDriverDNS_GAIA!(EventsDriver, SignalsDriver); else alias DNSDriver = EventDriverDNS_GAI!(EventsDriver, SignalsDriver); alias FileDriver = ThreadedFileEventDriver!EventsDriver; version (linux) alias WatcherDriver = InotifyEventDriverWatchers!EventsDriver; //else version (OSX) alias WatcherDriver = FSEventsEventDriverWatchers!EventsDriver; else alias WatcherDriver = PollEventDriverWatchers!EventsDriver; Loop m_loop; CoreDriver m_core; EventsDriver m_events; SignalsDriver m_signals; LoopTimeoutTimerDriver m_timers; SocketsDriver m_sockets; DNSDriver m_dns; FileDriver m_files; WatcherDriver m_watchers; } this() { m_loop = new Loop; m_sockets = new SocketsDriver(m_loop); m_events = new EventsDriver(m_loop, m_sockets); m_signals = new SignalsDriver(m_loop); m_timers = new TimerDriver; m_core = new CoreDriver(m_loop, m_timers, m_events); m_dns = new DNSDriver(m_events, m_signals); m_files = new FileDriver(m_events); m_watchers = new WatcherDriver(m_events); } // force overriding these in the (final) sub classes to avoid virtual calls final override @property inout(CoreDriver) core() inout { return m_core; } final override @property shared(inout(CoreDriver)) core() shared inout { return m_core; } final override @property inout(EventsDriver) events() inout { return m_events; } final override @property shared(inout(EventsDriver)) events() shared inout { return m_events; } final override @property inout(SignalsDriver) signals() inout { return m_signals; } final override @property inout(TimerDriver) timers() inout { return m_timers; } final override @property inout(SocketsDriver) sockets() inout { return m_sockets; } final override @property inout(DNSDriver) dns() inout { return m_dns; } final override @property inout(FileDriver) files() inout { return m_files; } final override @property inout(WatcherDriver) watchers() inout { return m_watchers; } final override void dispose() { if (!m_loop) return; m_files.dispose(); m_dns.dispose(); m_core.dispose(); m_loop.dispose(); m_loop = null; } } final class PosixEventDriverCore(Loop : PosixEventLoop, Timers : EventDriverTimers, Events : EventDriverEvents) : EventDriverCore { @safe nothrow: import core.atomic : atomicLoad, atomicStore; import core.sync.mutex : Mutex; import core.time : Duration; import std.stdint : intptr_t; import std.typecons : Tuple, tuple; protected alias ExtraEventsCallback = bool delegate(long); private { Loop m_loop; Timers m_timers; Events m_events; bool m_exit = false; EventID m_wakeupEvent; shared Mutex m_threadCallbackMutex; ConsumableQueue!(Tuple!(ThreadCallback, intptr_t)) m_threadCallbacks; } protected this(Loop loop, Timers timers, Events events) { m_loop = loop; m_timers = timers; m_events = events; m_wakeupEvent = events.createInternal(); static if (__VERSION__ >= 2074) m_threadCallbackMutex = new shared Mutex; else { () @trusted { m_threadCallbackMutex = cast(shared)new Mutex; } (); } m_threadCallbacks = new ConsumableQueue!(Tuple!(ThreadCallback, intptr_t)); m_threadCallbacks.reserve(1000); } protected final void dispose() { executeThreadCallbacks(); m_events.releaseRef(m_wakeupEvent); atomicStore(m_threadCallbackMutex, null); m_wakeupEvent = EventID.invalid; // FIXME: this needs to be synchronized! } @property size_t waiterCount() const { return m_loop.m_waiterCount + m_timers.pendingCount; } final override ExitReason processEvents(Duration timeout) { import core.time : hnsecs, seconds; executeThreadCallbacks(); if (m_exit) { m_exit = false; return ExitReason.exited; } if (!waiterCount) { return ExitReason.outOfWaiters; } bool got_events; if (timeout <= 0.seconds) { got_events = m_loop.doProcessEvents(0.seconds); m_timers.process(currStdTime); } else { long now = currStdTime; do { auto nextto = max(min(m_timers.getNextTimeout(now), timeout), 0.seconds); got_events = m_loop.doProcessEvents(nextto); long prev_step = now; now = currStdTime; got_events |= m_timers.process(now); if (timeout != Duration.max) timeout -= (now - prev_step).hnsecs; } while (timeout > 0.seconds && !m_exit && !got_events); } executeThreadCallbacks(); if (m_exit) { m_exit = false; return ExitReason.exited; } if (!waiterCount) { return ExitReason.outOfWaiters; } if (got_events) return ExitReason.idle; return ExitReason.timeout; } final override void exit() { m_exit = true; // FIXME: this needs to be synchronized! () @trusted { (cast(shared)m_events).trigger(m_wakeupEvent, true); } (); } final override void clearExitFlag() { m_exit = false; } final override void runInOwnerThread(ThreadCallback del, intptr_t param) shared { auto m = atomicLoad(m_threadCallbackMutex); auto evt = atomicLoad(m_wakeupEvent); // NOTE: This case must be handled gracefully to avoid hazardous // race-conditions upon unexpected thread termination. The mutex // and the map will stay valid even after the driver has been // disposed, so no further synchronization is required. if (!m) return; try { synchronized (m) () @trusted { return (cast()this).m_threadCallbacks; } () .put(tuple(del, param)); } catch (Exception e) assert(false, e.msg); m_events.trigger(m_wakeupEvent, false); } final protected override void* rawUserData(StreamSocketFD descriptor, size_t size, DataInitializer initialize, DataInitializer destroy) @system { return rawUserDataImpl(descriptor, size, initialize, destroy); } final protected override void* rawUserData(DatagramSocketFD descriptor, size_t size, DataInitializer initialize, DataInitializer destroy) @system { return rawUserDataImpl(descriptor, size, initialize, destroy); } protected final void* rawUserDataImpl(FD descriptor, size_t size, DataInitializer initialize, DataInitializer destroy) @system { return m_loop.rawUserDataImpl(descriptor, size, initialize, destroy); } private void executeThreadCallbacks() { import std.stdint : intptr_t; while (true) { Tuple!(ThreadCallback, intptr_t) del; try { synchronized (m_threadCallbackMutex) { if (m_threadCallbacks.empty) break; del = m_threadCallbacks.consumeOne; } } catch (Exception e) assert(false, e.msg); del[0](del[1]); } } } package class PosixEventLoop { @safe: nothrow: import core.time : Duration; package { AlgebraicChoppedVector!(FDSlot, StreamSocketSlot, StreamListenSocketSlot, DgramSocketSlot, DNSSlot, WatcherSlot, EventSlot, SignalSlot) m_fds; size_t m_waiterCount = 0; } protected @property int maxFD() const { return cast(int)m_fds.length; } protected abstract void dispose(); protected abstract bool doProcessEvents(Duration dur); /// Registers the FD for general notification reception. protected abstract void registerFD(FD fd, EventMask mask, bool edge_triggered = true); /// Unregisters the FD for general notification reception. protected abstract void unregisterFD(FD fd, EventMask mask); /// Updates the event mask to use for listening for notifications. protected abstract void updateFD(FD fd, EventMask old_mask, EventMask new_mask, bool edge_triggered = true); final protected void notify(EventType evt)(FD fd) { //assert(m_fds[fd].callback[evt] !is null, "Notifying FD which is not listening for event."); if (m_fds[fd.value].common.callback[evt]) m_fds[fd.value].common.callback[evt](fd); } final protected void enumerateFDs(EventType evt)(scope FDEnumerateCallback del) { // TODO: optimize! foreach (i; 0 .. cast(int)m_fds.length) if (m_fds[i].common.callback[evt]) del(cast(FD)i); } package void setNotifyCallback(EventType evt)(FD fd, FDSlotCallback callback) { assert(m_fds[fd.value].common.refCount > 0, "Setting notification callback on unreferenced file descriptor slot."); assert((callback !is null) != (m_fds[fd.value].common.callback[evt] !is null), "Overwriting notification callback."); // ensure that the FD doesn't get closed before the callback gets called. with (m_fds[fd.value]) { if (callback !is null) { if (!(common.flags & FDFlags.internal)) m_waiterCount++; common.refCount++; } else { common.refCount--; if (!(common.flags & FDFlags.internal)) m_waiterCount--; } common.callback[evt] = callback; } } package void initFD(T)(FD fd, FDFlags flags, auto ref T slot_init) { with (m_fds[fd.value]) { assert(common.refCount == 0, "Initializing referenced file descriptor slot."); assert(specific.kind == typeof(specific).Kind.none, "Initializing slot that has not been cleared."); common.refCount = 1; common.flags = flags; specific = slot_init; } } package void clearFD(T)(FD fd) { import taggedalgebraic : hasType; auto slot = () @trusted { return &m_fds[fd.value]; } (); assert(slot.common.refCount == 0, "Clearing referenced file descriptor slot."); assert(slot.specific.hasType!T, "Clearing file descriptor slot with unmatched type."); if (slot.common.userDataDestructor) () @trusted { slot.common.userDataDestructor(slot.common.userData.ptr); } (); if (!(slot.common.flags & FDFlags.internal)) foreach (cb; slot.common.callback) if (cb !is null) m_waiterCount--; *slot = m_fds.FullField.init; } package final void* rawUserDataImpl(size_t descriptor, size_t size, DataInitializer initialize, DataInitializer destroy) @system { FDSlot* fds = &m_fds[descriptor].common; assert(fds.userDataDestructor is null || fds.userDataDestructor is destroy, "Requesting user data with differing type (destructor)."); assert(size <= FDSlot.userData.length, "Requested user data is too large."); if (size > FDSlot.userData.length) assert(false); if (!fds.userDataDestructor) { initialize(fds.userData.ptr); fds.userDataDestructor = destroy; } return fds.userData.ptr; } } alias FDEnumerateCallback = void delegate(FD); alias FDSlotCallback = void delegate(FD); private struct FDSlot { FDSlotCallback[EventType.max+1] callback; uint refCount; FDFlags flags; DataInitializer userDataDestructor; ubyte[16*size_t.sizeof] userData; @property EventMask eventMask() const nothrow { EventMask ret = cast(EventMask)0; if (callback[EventType.read] !is null) ret |= EventMask.read; if (callback[EventType.write] !is null) ret |= EventMask.write; if (callback[EventType.status] !is null) ret |= EventMask.status; return ret; } } enum FDFlags { none = 0, internal = 1<<0, } enum EventType { read, write, status } enum EventMask { read = 1<<0, write = 1<<1, status = 1<<2 } void log(ARGS...)(string fmt, ARGS args) @trusted { import std.stdio : writef, writefln; import core.thread : Thread; try { writef("[%s]: ", Thread.getThis().name); writefln(fmt, args); } catch (Exception) {} } /*version (Windows) { import std.c.windows.windows; import std.c.windows.winsock; alias EWOULDBLOCK = WSAEWOULDBLOCK; extern(System) DWORD FormatMessageW(DWORD dwFlags, const(void)* lpSource, DWORD dwMessageId, DWORD dwLanguageId, LPWSTR lpBuffer, DWORD nSize, void* Arguments); class WSAErrorException : Exception { int error; this(string message, string file = __FILE__, size_t line = __LINE__) { error = WSAGetLastError(); this(message, error, file, line); } this(string message, int error, string file = __FILE__, size_t line = __LINE__) { import std.string : format; ushort* errmsg; FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, null, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), cast(LPWSTR)&errmsg, 0, null); size_t len = 0; while (errmsg[len]) len++; auto errmsgd = (cast(wchar[])errmsg[0 .. len]).idup; LocalFree(errmsg); super(format("%s: %s (%s)", message, errmsgd, error), file, line); } } alias SystemSocketException = WSAErrorException; } else { import std.exception : ErrnoException; alias SystemSocketException = ErrnoException; } T socketEnforce(T)(T value, lazy string msg = null, string file = __FILE__, size_t line = __LINE__) { import std.exception : enforceEx; return enforceEx!SystemSocketException(value, msg, file, line); }*/