1 module eventy.queues; 2 3 import eventy.event : Event; 4 import core.sync.mutex : Mutex; 5 import std.container.dlist; 6 import std.range; 7 8 /** 9 * Queue 10 * 11 * Represents a queue with a given ID that can 12 * have Event-s enqueued to it 13 */ 14 public final class Queue 15 { 16 public ulong id; 17 /* TODO: Add queue of Event's here */ 18 19 private DList!(Event) queue; 20 private Mutex queueLock; 21 22 23 this(ulong id) 24 { 25 this.id = id; 26 queueLock = new Mutex(); 27 } 28 29 public DList!(Event).Range getKak() 30 { 31 return queue[]; 32 } 33 34 public void add(Event e) 35 { 36 /* Lock the queue */ 37 queueLock.lock(); 38 39 queue.insert(e); 40 41 /* Unlock the queue */ 42 queueLock.unlock(); 43 } 44 45 public bool hasEvents() 46 { 47 bool has; 48 49 /* Lock the queue */ 50 queueLock.lock(); 51 52 has = !(queue[]).empty(); 53 54 /* Unlock the queue */ 55 queueLock.unlock(); 56 57 return has; 58 } 59 60 public Event popEvent() 61 { 62 Event poppedEvent; 63 64 /* Lock the queue */ 65 queueLock.lock(); 66 67 poppedEvent = (queue[]).front(); 68 queue.removeFront(); 69 70 /* Unlock the queue */ 71 queueLock.unlock(); 72 73 return poppedEvent; 74 } 75 }