From 201e4f6077680a28b636088a434d3da076e4dfb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Mon, 17 Oct 2016 21:53:30 +0200 Subject: [PATCH] Make all releaseRef() methods return a Boolean. --- source/eventcore/driver.d | 28 +++++++++++++++++---- source/eventcore/drivers/libasync.d | 33 +++++++++++-------------- source/eventcore/drivers/posix.d | 14 ++++++++--- source/eventcore/drivers/threadedfile.d | 4 ++- source/eventcore/drivers/timer.d | 7 ++++-- 5 files changed, 55 insertions(+), 31 deletions(-) diff --git a/source/eventcore/driver.d b/source/eventcore/driver.d index c8dd1f3..3f5014c 100644 --- a/source/eventcore/driver.d +++ b/source/eventcore/driver.d @@ -103,8 +103,11 @@ interface EventDriverSockets { Once the reference count reaches zero, all associated resources will be freed and the resource descriptor gets invalidated. + + Returns: + Returns `false` $(I iff) the last reference was removed by this call. */ - void releaseRef(SocketFD descriptor); + bool releaseRef(SocketFD descriptor); } interface EventDriverDNS { @@ -134,8 +137,11 @@ interface EventDriverFiles { Once the reference count reaches zero, all associated resources will be freed and the resource descriptor gets invalidated. + + Returns: + Returns `false` $(I iff) the last reference was removed by this call. */ - void releaseRef(FileFD descriptor); + bool releaseRef(FileFD descriptor); } interface EventDriverEvents { @@ -154,8 +160,11 @@ interface EventDriverEvents { Once the reference count reaches zero, all associated resources will be freed and the resource descriptor gets invalidated. + + Returns: + Returns `false` $(I iff) the last reference was removed by this call. */ - void releaseRef(EventID descriptor); + bool releaseRef(EventID descriptor); } interface EventDriverSignals { @@ -170,8 +179,11 @@ interface EventDriverSignals { Once the reference count reaches zero, all associated resources will be freed and the resource descriptor gets invalidated. + + Returns: + Returns `false` $(I iff) the last reference was removed by this call. */ - void releaseRef(SignalListenID descriptor); + bool releaseRef(SignalListenID descriptor); } interface EventDriverTimers { @@ -192,8 +204,11 @@ interface EventDriverTimers { Once the reference count reaches zero, all associated resources will be freed and the resource descriptor gets invalidated. + + Returns: + Returns `false` $(I iff) the last reference was removed by this call. */ - void releaseRef(TimerID descriptor); + bool releaseRef(TimerID descriptor); } interface EventDriverWatchers { @@ -208,6 +223,9 @@ interface EventDriverWatchers { Once the reference count reaches zero, all associated resources will be freed and the resource descriptor gets invalidated. + + Returns: + Returns `false` $(I iff) the last reference was removed by this call. */ bool releaseRef(WatcherID descriptor); } diff --git a/source/eventcore/drivers/libasync.d b/source/eventcore/drivers/libasync.d index 828d7ae..f161638 100644 --- a/source/eventcore/drivers/libasync.d +++ b/source/eventcore/drivers/libasync.d @@ -163,7 +163,7 @@ final class LibasyncEventDriverSockets : EventDriverSockets { assert(false, "TODO!"); } - override void releaseRef(SocketFD descriptor) + override bool releaseRef(SocketFD descriptor) { assert(false, "TODO!"); } @@ -231,7 +231,7 @@ final class LibasyncEventDriverFiles : EventDriverFiles { assert(false, "TODO!"); } - override void releaseRef(FileFD descriptor) + override bool releaseRef(FileFD descriptor) { assert(false, "TODO!"); } @@ -269,7 +269,7 @@ final class LibasyncEventDriverEvents : EventDriverEvents { assert(false, "TODO!"); } - override void releaseRef(EventID descriptor) + override bool releaseRef(EventID descriptor) { assert(false, "TODO!"); } @@ -277,12 +277,17 @@ final class LibasyncEventDriverEvents : EventDriverEvents { final class LibasyncEventDriverSignals : EventDriverSignals { @safe: /*@nogc:*/ nothrow: - override void wait(int sig, SignalCallback on_signal) + override SignalListenID listen(int sig, SignalCallback on_signal) { assert(false, "TODO!"); } - override void cancelWait(int sig) + override void addRef(SignalListenID descriptor) + { + assert(false, "TODO!"); + } + + override bool releaseRef(SignalListenID descriptor) { assert(false, "TODO!"); } @@ -320,7 +325,7 @@ final class LibasyncEventDriverTimers : EventDriverTimers { assert(false, "TODO!"); } - override void cancelWait(TimerID timer, TimerCallback callback) + override void cancelWait(TimerID timer) { assert(false, "TODO!"); } @@ -330,7 +335,7 @@ final class LibasyncEventDriverTimers : EventDriverTimers { assert(false, "TODO!"); } - override void releaseRef(TimerID descriptor) + override bool releaseRef(TimerID descriptor) { assert(false, "TODO!"); } @@ -338,17 +343,7 @@ final class LibasyncEventDriverTimers : EventDriverTimers { final class LibasyncEventDriverWatchers : EventDriverWatchers { @safe: /*@nogc:*/ nothrow: - override WatcherID watchDirectory(string path, bool recursive) - { - assert(false, "TODO!"); - } - - override void wait(WatcherID watcher, FileChangesCallback callback) - { - assert(false, "TODO!"); - } - - override void cancelWait(WatcherID watcher) + override WatcherID watchDirectory(string path, bool recursive, FileChangesCallback callback) { assert(false, "TODO!"); } @@ -358,7 +353,7 @@ final class LibasyncEventDriverWatchers : EventDriverWatchers { assert(false, "TODO!"); } - override void releaseRef(WatcherID descriptor) + override bool releaseRef(WatcherID descriptor) { assert(false, "TODO!"); } diff --git a/source/eventcore/drivers/posix.d b/source/eventcore/drivers/posix.d index 07cf509..8480586 100644 --- a/source/eventcore/drivers/posix.d +++ b/source/eventcore/drivers/posix.d @@ -735,7 +735,7 @@ final class PosixEventDriverSockets(Loop : PosixEventLoop) : EventDriverSockets m_loop.m_fds[fd].refCount++; } - final override void releaseRef(SocketFD fd) + final override bool releaseRef(SocketFD fd) { auto pfd = () @trusted { return &m_loop.m_fds[fd]; } (); assert(pfd.refCount > 0, "Releasing reference to unreferenced socket FD."); @@ -743,7 +743,9 @@ final class PosixEventDriverSockets(Loop : PosixEventLoop) : EventDriverSockets m_loop.unregisterFD(fd); m_loop.clearFD(fd); closeSocket(fd); + return false; } + return true; } private SocketFD createSocket(AddressFamily family, int type) @@ -1069,14 +1071,16 @@ final class PosixEventDriverEvents(Loop : PosixEventLoop) : EventDriverEvents { m_loop.m_fds[descriptor].refCount++; } - final override void releaseRef(EventID descriptor) + final override bool releaseRef(EventID descriptor) { assert(m_loop.m_fds[descriptor].refCount > 0, "Releasing reference to unreferenced event FD."); if (--m_loop.m_fds[descriptor].refCount == 0) { m_loop.unregisterFD(descriptor); m_loop.clearFD(descriptor); close(descriptor); + return false; } + return true; } } @@ -1119,7 +1123,7 @@ final class PosixEventDriverSignals(Loop : PosixEventLoop) : EventDriverSignals m_loop.m_fds[descriptor].refCount++; } - override void releaseRef(SignalListenID descriptor) + override bool releaseRef(SignalListenID descriptor) { FD fd = cast(FD)descriptor; assert(m_loop.m_fds[fd].refCount > 0, "Releasing reference to unreferenced event FD."); @@ -1127,7 +1131,9 @@ final class PosixEventDriverSignals(Loop : PosixEventLoop) : EventDriverSignals m_loop.unregisterFD(fd); m_loop.clearFD(fd); close(fd); + return false; } + return true; } private void onSignal(FD fd) @@ -1292,7 +1298,7 @@ final class PosixEventDriverWatchers(Loop : PosixEventLoop) : EventDriverWatcher assert(false, "TODO!"); } - final override void releaseRef(WatcherID descriptor) + final override bool releaseRef(WatcherID descriptor) { assert(false, "TODO!"); } diff --git a/source/eventcore/drivers/threadedfile.d b/source/eventcore/drivers/threadedfile.d index 0f70fb3..1d0a1dd 100644 --- a/source/eventcore/drivers/threadedfile.d +++ b/source/eventcore/drivers/threadedfile.d @@ -226,7 +226,7 @@ log("start task"); m_files[descriptor].refCount++; } - final override void releaseRef(FileFD descriptor) + final override bool releaseRef(FileFD descriptor) { auto f = () @trusted { return &m_files[descriptor]; } (); if (!--f.refCount) { @@ -234,7 +234,9 @@ log("start task"); *f = FileInfo.init; assert(!m_activeReads.contains(descriptor)); assert(!m_activeWrites.contains(descriptor)); + return false; } + return true; } /// private diff --git a/source/eventcore/drivers/timer.d b/source/eventcore/drivers/timer.d index eccdfd6..5248d43 100644 --- a/source/eventcore/drivers/timer.d +++ b/source/eventcore/drivers/timer.d @@ -154,18 +154,21 @@ final class LoopTimeoutTimerDriver : EventDriverTimers { m_timers[descriptor].refCount++; } - final override void releaseRef(TimerID descriptor) + final override bool releaseRef(TimerID descriptor) { assert(descriptor != TimerID.init, "Invalid timer ID."); assert(descriptor in m_timers, "Unknown timer ID."); - if (descriptor !in m_timers) return; + if (descriptor !in m_timers) return true; auto tm = m_timers[descriptor]; if (!--tm.refCount) { if (tm.pending) stop(tm.id); m_timers.remove(descriptor); () @trusted { scope (failure) assert(false); ms_allocator.dispose(tm); } (); + return false; } + + return true; } }