From 333ae361a81bf506f60911a3d5a80b4ede3f12eb Mon Sep 17 00:00:00 2001 From: "Tristan B. Kildaire" Date: Tue, 31 Aug 2021 11:34:22 +0200 Subject: [PATCH] Added a few methods for adding to the queue, checking if it is empoty and popping from the queue --- source/eventy/queues.d | 53 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/source/eventy/queues.d b/source/eventy/queues.d index a27d2ea..2691f32 100644 --- a/source/eventy/queues.d +++ b/source/eventy/queues.d @@ -1,5 +1,10 @@ module eventy.queues; +import eventy.event : Event; +import core.sync.mutex : Mutex; +import std.container.dlist; +import std.range; + /** * Queue * @@ -8,6 +13,52 @@ module eventy.queues; */ public final class Queue { - private ulong id; + public ulong id; /* TODO: Add queue of Event's here */ + + private DList!(Event) queue; + private Mutex queueLock; + + + public void add(Event e) + { + /* Lock the queue */ + queueLock.lock(); + + queue.insert(e); + + /* Unlock the queue */ + queueLock.unlock(); + } + + public bool hasEvents() + { + bool has; + + /* Lock the queue */ + queueLock.lock(); + + has = (queue[]).empty(); + + /* Unlock the queue */ + queueLock.unlock(); + + return has; + } + + public Event popEvent() + { + Event poppedEvent; + + /* Lock the queue */ + queueLock.lock(); + + poppedEvent = (queue[]).front(); + (queue[]).popFront(); + + /* Unlock the queue */ + queueLock.unlock(); + + return poppedEvent; + } } \ No newline at end of file