package server.util; /* !!! WORK IN PROGRESS !!! trying to implement a wait-free queue */ import java.util.concurrent.atomic.AtomicReferenceArray; public class Queue { private volatile int head = 0, tail = 0; private final int capacity; private AtomicReferenceArray items; public Queue(int capacity) { this.capacity = capacity; items = new AtomicReferenceArray<>(capacity); } public boolean enq(T x) { if (tail - head == capacity) return false; items.set((tail + 1) % capacity, x); tail++; return true; } public T deq() { if (tail - head == 0) return null; T x = items.get((head + 1) % capacity); head++; return x; } }