From a3ca66db2b3bd7c0848e60eac0e5aa77050630dc Mon Sep 17 00:00:00 2001 From: "Tristan B. Kildaire" Date: Wed, 8 Sep 2021 14:06:38 +0200 Subject: [PATCH] Added the ability to set a max length for queues --- source/tristanable/queue.d | 43 +++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/source/tristanable/queue.d b/source/tristanable/queue.d index b7081e8..caa2c97 100644 --- a/source/tristanable/queue.d +++ b/source/tristanable/queue.d @@ -17,6 +17,11 @@ import bmessage : bSendMessage = sendMessage; import core.thread : Thread; import std.container.dlist; +public enum QueuePolicy : ubyte +{ + LENGTH_CAP = 1 +} + public final class Queue { /* This queue's tag */ @@ -32,7 +37,7 @@ public final class Queue * Construct a new queue with the given * tag */ - this(ulong tag) + this(ulong tag, QueuePolicy flags = cast(QueuePolicy)0) { this.tag = tag; @@ -40,14 +45,50 @@ public final class Queue queueLock = new Mutex(); } + public void setLengthCap(ulong lengthCap) + { + this.lengthCap = lengthCap; + } + + public ulong getLengthCap(ulong lengthCap) + { + return lengthCap; + } + + /** + * Queue policy settings + */ + private ulong lengthCap = 1; + private QueuePolicy flags; + + public void enqueue(QueueItem item) { /* Lock the queue */ queueLock.lock(); + /** + * Check to see if the queue has a length cap + * + * If so then determine whether to drop or + * keep dependent on current capacity + */ + if(flags & QueuePolicy.LENGTH_CAP) + { + + import std.range; + + if(walkLength(queue[]) == lengthCap) + { + goto unlock; + } + } + /* Add it to the queue */ queue ~= item; + unlock: + /* Unlock the queue */ queueLock.unlock(); }