eventcore.drivers.posix.events 22/55(40%) 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
371
380
391
401
410
420
430
440
451
460
470
480
490
500
510
520
530
540
552
562
572
580
592
600
612
622
632
642
652
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
1220
1230
1240
1250
1260
1270
1280
1290
1300
1310
1320
1330
1340
1350
1360
1370
1380
1390
1400
1410
1420
1430
1440
1450
1460
1470
1480
1490
1500
1510
1520
1530
1540
1550
1560
1570
1581
1591
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
2052
2062
2070
2080
2090
2100
2110
2120
2130
2140
2150
2160
2170
2180
2190
2200
2210
2220
2230
2240
2250
2260
2270
2280
2292
2300
2310
2320
2330
2340
2350
2360
2370
2380
2392
2404
2410
2420
2430
2440
2456
2460
2470
2480
2490
2501
2510
2520
2530
2540
2550
2560
2570
2580
2590
2600
2610
2620
2630
module eventcore.drivers.posix.events; @safe: import eventcore.driver; import eventcore.drivers.posix.driver; import eventcore.internal.consumablequeue : ConsumableQueue; import eventcore.internal.utils : nogc_assert; version (linux) { nothrow @nogc extern (C) int eventfd(uint initval, int flags); enum EFD_NONBLOCK = 0x800; enum EFD_CLOEXEC = 0x80000; } version (Posix) { import core.sys.posix.unistd : close, read, write; } else { import core.sys.windows.winsock2 : closesocket, AF_INET, SOCKET, SOCK_DGRAM, bind, connect, getsockname, send, socket; } final class PosixEventDriverEvents(Loop : PosixEventLoop, Sockets : EventDriverSockets) : EventDriverEvents { @safe: /*@nogc:*/ nothrow: private { Loop m_loop; Sockets m_sockets; ubyte[ulong.sizeof] m_buf; version (linux) {} else { // TODO: avoid the overhead of a mutex backed map here import core.sync.mutex : Mutex; Mutex m_eventsMutex; EventID[DatagramSocketFD] m_events; } } this(Loop loop, Sockets sockets) { m_loop = loop; m_sockets = sockets; version (linux) {} else m_eventsMutex = new Mutex; } package @property Loop loop() { return m_loop; } final override EventID create() { return createInternal(false); } package(eventcore) EventID createInternal(bool is_internal = true) { version (linux) { auto eid = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); if (eid == -1) return EventID.invalid; auto id = cast(EventID)eid; // FIXME: avoid dynamic memory allocation for the queue m_loop.initFD(id, FDFlags.internal, EventSlot(new ConsumableQueue!EventCallback, false, is_internal)); m_loop.registerFD(id, EventMask.read); m_loop.setNotifyCallback!(EventType.read)(id, &onEvent); releaseRef(id); // setNotifyCallback increments the reference count, but we need a value of 1 upon return assert(getRC(id) == 1); return id; } else { sock_t[2] fd; version (Posix) { import core.sys.posix.fcntl : fcntl, F_SETFL; import eventcore.drivers.posix.sockets : O_CLOEXEC; // create a pair of sockets to communicate between threads import core.sys.posix.sys.socket : SOCK_DGRAM, AF_UNIX, socketpair; if (() @trusted { return socketpair(AF_UNIX, SOCK_DGRAM, 0, fd); } () != 0) return EventID.invalid; assert(fd[0] != fd[1]); // use the first socket as the async receiver auto s = m_sockets.adoptDatagramSocketInternal(fd[0], true, true); () @trusted { fcntl(fd[1], F_SETFL, O_CLOEXEC); } (); } else { // fake missing socketpair support on Windows import std.socket : InternetAddress; auto addr = new InternetAddress(0x7F000001, 0); auto s = m_sockets.createDatagramSocketInternal(addr, null, true); if (s == DatagramSocketFD.invalid) return EventID.invalid; fd[0] = cast(sock_t)s; if (!() @trusted { fd[1] = socket(AF_INET, SOCK_DGRAM, 0); int nl = addr.nameLen; import eventcore.internal.utils : print; if (bind(fd[1], addr.name, addr.nameLen) != 0) return false; assert(nl == addr.nameLen); if (getsockname(fd[0], addr.name, &nl) != 0) return false; if (connect(fd[1], addr.name, addr.nameLen) != 0) return false; return true; } ()) { m_sockets.releaseRef(s); return EventID.invalid; } } m_sockets.receive(s, m_buf, IOMode.once, &onSocketData); // use the second socket as the event ID and as the sending end for // other threads auto id = cast(EventID)fd[1]; try { synchronized (m_eventsMutex) m_events[s] = id; } catch (Exception e) assert(false, e.msg); // FIXME: avoid dynamic memory allocation for the queue m_loop.initFD(id, FDFlags.internal, EventSlot(new ConsumableQueue!EventCallback, false, is_internal, s)); assert(getRC(id) == 1); return id; } } final override void trigger(EventID event, bool notify_all) { auto slot = getSlot(event); if (notify_all) { //log("emitting only for this thread (%s waiters)", m_fds[event].waiters.length); foreach (w; slot.waiters.consume) { //log("emitting waiter %s %s", cast(void*)w.funcptr, w.ptr); if (!isInternal(event)) m_loop.m_waiterCount--; w(event); } } else { if (!slot.waiters.empty) { if (!isInternal(event)) m_loop.m_waiterCount--; slot.waiters.consumeOne()(event); } } } final override void trigger(EventID event, bool notify_all) shared @trusted { import core.atomic : atomicStore; auto thisus = cast(PosixEventDriverEvents)this; assert(event < thisus.m_loop.m_fds.length, "Invalid event ID passed to shared triggerEvent."); long one = 1; //log("emitting for all threads"); if (notify_all) atomicStore(thisus.getSlot(event).triggerAll, true); version (Posix) .write(cast(int)event, &one, one.sizeof); else assert(send(cast(int)event, cast(const(ubyte*))&one, one.sizeof, 0) == one.sizeof); } final override void wait(EventID event, EventCallback on_event) { if (!isInternal(event)) m_loop.m_waiterCount++; getSlot(event).waiters.put(on_event); } final override void cancelWait(EventID event, EventCallback on_event) { import std.algorithm.searching : countUntil; import std.algorithm.mutation : remove; if (!isInternal(event)) m_loop.m_waiterCount--; getSlot(event).waiters.removePending(on_event); } private void onEvent(FD fd) @trusted { EventID event = cast(EventID)fd; version (linux) { ulong cnt; () @trusted { .read(cast(int)event, &cnt, cnt.sizeof); } (); } import core.atomic : cas; auto all = cas(&getSlot(event).triggerAll, true, false); trigger(event, all); } version (linux) {} else { private void onSocketData(DatagramSocketFD s, IOStatus, size_t, scope RefAddress) { m_sockets.receive(s, m_buf, IOMode.once, &onSocketData); EventID evt; try { synchronized (m_eventsMutex) evt = m_events[s]; onEvent(evt); } catch (Exception e) assert(false, e.msg); } } final override void addRef(EventID descriptor) { assert(getRC(descriptor) > 0, "Adding reference to unreferenced event FD."); getRC(descriptor)++; } final override bool releaseRef(EventID descriptor) { nogc_assert(getRC(descriptor) > 0, "Releasing reference to unreferenced event FD."); if (--getRC(descriptor) == 0) { if (!isInternal(descriptor)) m_loop.m_waiterCount -= getSlot(descriptor).waiters.length; () @trusted nothrow { try .destroy(getSlot(descriptor).waiters); catch (Exception e) nogc_assert(false, e.msg); } (); version (linux) { m_loop.unregisterFD(descriptor, EventMask.read); } else { auto rs = getSlot(descriptor).recvSocket; m_sockets.cancelReceive(rs); m_sockets.releaseRef(rs); try { synchronized (m_eventsMutex) m_events.remove(rs); } catch (Exception e) nogc_assert(false, e.msg); } m_loop.clearFD!EventSlot(descriptor); version (Posix) close(cast(int)descriptor); else () @trusted { closesocket(cast(SOCKET)descriptor); } (); return false; } return true; } final protected override void* rawUserData(EventID descriptor, size_t size, DataInitializer initialize, DataInitializer destroy) @system { return m_loop.rawUserDataImpl(descriptor, size, initialize, destroy); } private EventSlot* getSlot(EventID id) { nogc_assert(id < m_loop.m_fds.length, "Invalid event ID."); return () @trusted { return &m_loop.m_fds[id].event(); } (); } private ref uint getRC(EventID id) { return m_loop.m_fds[id].common.refCount; } private bool isInternal(EventID id) { return getSlot(id).isInternal; } } package struct EventSlot { alias Handle = EventID; ConsumableQueue!EventCallback waiters; shared bool triggerAll; bool isInternal; version (linux) {} else { DatagramSocketFD recvSocket; } }