|
| 1 | +package datadog.trace.util.queue; |
| 2 | + |
| 3 | +import java.util.Collection; |
| 4 | +import java.util.Iterator; |
| 5 | +import java.util.concurrent.TimeUnit; |
| 6 | +import java.util.concurrent.locks.LockSupport; |
| 7 | +import java.util.function.Consumer; |
| 8 | +import java.util.function.Supplier; |
| 9 | +import javax.annotation.Nonnull; |
| 10 | + |
| 11 | +public abstract class BaseQueueVarHandle<E> extends BaseQueue<E> { |
| 12 | + public BaseQueueVarHandle(int capacity) { |
| 13 | + super(capacity); |
| 14 | + } |
| 15 | + |
| 16 | + /** |
| 17 | + * Timed offer with progressive backoff. |
| 18 | + * |
| 19 | + * <p>Tries to insert an element into the queue within the given timeout. Uses a spin → yield → |
| 20 | + * park backoff strategy to reduce CPU usage under contention. |
| 21 | + * |
| 22 | + * @param e the element to insert |
| 23 | + * @param timeout maximum time to wait |
| 24 | + * @param unit time unit of timeout |
| 25 | + * @return {@code true} if inserted, {@code false} if timeout expires |
| 26 | + * @throws InterruptedException if interrupted while waiting |
| 27 | + */ |
| 28 | + public boolean offer(E e, long timeout, @Nonnull TimeUnit unit) throws InterruptedException { |
| 29 | + if (e == null) { |
| 30 | + throw new NullPointerException(); |
| 31 | + } |
| 32 | + final long deadline = System.nanoTime() + unit.toNanos(timeout); |
| 33 | + int idle = 0; |
| 34 | + |
| 35 | + while (true) { |
| 36 | + if (offer(e)) return true; |
| 37 | + |
| 38 | + long remaining = deadline - System.nanoTime(); |
| 39 | + if (remaining <= 0) return false; |
| 40 | + |
| 41 | + // Progressive backoff |
| 42 | + if (idle < 100) { |
| 43 | + // spin |
| 44 | + } else if (idle < 1_000) { |
| 45 | + Thread.yield(); |
| 46 | + } else { |
| 47 | + LockSupport.parkNanos(1_000L); |
| 48 | + } |
| 49 | + |
| 50 | + if (Thread.interrupted()) { |
| 51 | + throw new InterruptedException(); |
| 52 | + } |
| 53 | + idle++; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Polls with a timeout using progressive backoff. |
| 59 | + * |
| 60 | + * @param timeout max wait time |
| 61 | + * @param unit time unit |
| 62 | + * @return the head element, or null if timed out |
| 63 | + * @throws InterruptedException if interrupted |
| 64 | + */ |
| 65 | + public E poll(long timeout, @Nonnull TimeUnit unit) throws InterruptedException { |
| 66 | + if (timeout <= 0) { |
| 67 | + return poll(); |
| 68 | + } |
| 69 | + |
| 70 | + final long deadline = System.nanoTime() + unit.toNanos(timeout); |
| 71 | + int idleCount = 0; |
| 72 | + |
| 73 | + while (true) { |
| 74 | + E e = poll(); |
| 75 | + if (e != null) return e; |
| 76 | + |
| 77 | + long remaining = deadline - System.nanoTime(); |
| 78 | + if (remaining <= 0) return null; |
| 79 | + |
| 80 | + if (idleCount < 100) { |
| 81 | + // spin |
| 82 | + } else if (idleCount < 1_000) { |
| 83 | + Thread.yield(); |
| 84 | + } else { |
| 85 | + LockSupport.parkNanos(1_000L); |
| 86 | + } |
| 87 | + |
| 88 | + if (Thread.interrupted()) { |
| 89 | + throw new InterruptedException(); |
| 90 | + } |
| 91 | + idleCount++; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Drains all available elements from the queue to a consumer. |
| 97 | + * |
| 98 | + * <p>This is efficient since it avoids repeated size() checks and returns immediately when empty. |
| 99 | + * |
| 100 | + * @param consumer a consumer to accept elements |
| 101 | + * @return number of elements drained |
| 102 | + */ |
| 103 | + public int drain(Consumer<E> consumer) { |
| 104 | + return drain(consumer, Integer.MAX_VALUE); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Drains up to {@code limit} elements from the queue to a consumer. |
| 109 | + * |
| 110 | + * <p>This method is useful for batch processing. |
| 111 | + * |
| 112 | + * <p>Each element is removed atomically using poll() and passed to the consumer. |
| 113 | + * |
| 114 | + * @param consumer a consumer to accept elements |
| 115 | + * @param limit maximum number of elements to drain |
| 116 | + * @return number of elements drained |
| 117 | + */ |
| 118 | + public int drain(Consumer<E> consumer, int limit) { |
| 119 | + int count = 0; |
| 120 | + E e; |
| 121 | + while (count < limit && (e = poll()) != null) { |
| 122 | + consumer.accept(e); |
| 123 | + count++; |
| 124 | + } |
| 125 | + return count; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Fills the queue with elements provided by the supplier until either: - the queue is full, or - |
| 130 | + * the supplier runs out of elements (returns null) |
| 131 | + * |
| 132 | + * @param supplier a supplier of elements |
| 133 | + * @param limit maximum number of elements to attempt to insert |
| 134 | + * @return number of elements successfully enqueued |
| 135 | + */ |
| 136 | + public int fill(@Nonnull Supplier<? extends E> supplier, int limit) { |
| 137 | + if (limit <= 0) { |
| 138 | + return 0; |
| 139 | + } |
| 140 | + |
| 141 | + int added = 0; |
| 142 | + while (added < limit) { |
| 143 | + E e = supplier.get(); |
| 144 | + if (e == null) { |
| 145 | + break; // stop if supplier exhausted |
| 146 | + } |
| 147 | + |
| 148 | + if (offer(e)) { |
| 149 | + added++; |
| 150 | + } else { |
| 151 | + break; // queue is full |
| 152 | + } |
| 153 | + } |
| 154 | + return added; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Iterator is not supported. |
| 159 | + * |
| 160 | + * @throws UnsupportedOperationException always |
| 161 | + */ |
| 162 | + @Override |
| 163 | + public Iterator<E> iterator() { |
| 164 | + throw new UnsupportedOperationException(); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Returns the remaining capacity. |
| 169 | + * |
| 170 | + * @return number of additional elements this queue can accept |
| 171 | + */ |
| 172 | + public int remainingCapacity() { |
| 173 | + return capacity - size(); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Returns the maximum queue capacity. |
| 178 | + * |
| 179 | + * @return number of total elements this queue can accept |
| 180 | + */ |
| 181 | + public int capacity() { |
| 182 | + return capacity; |
| 183 | + } |
| 184 | + |
| 185 | + @Override |
| 186 | + public void put(E e) throws InterruptedException { |
| 187 | + throw new UnsupportedOperationException("Not implementing blocking operations for producers"); |
| 188 | + } |
| 189 | + |
| 190 | + @Override |
| 191 | + public E take() throws InterruptedException { |
| 192 | + throw new UnsupportedOperationException("Not implementing blocking operations for consumers"); |
| 193 | + } |
| 194 | + |
| 195 | + @Override |
| 196 | + public int drainTo(Collection<? super E> c) { |
| 197 | + return drainTo(c, Integer.MAX_VALUE); |
| 198 | + } |
| 199 | + |
| 200 | + @Override |
| 201 | + public int drainTo(Collection<? super E> c, int maxElements) { |
| 202 | + return drain(c::add, maxElements); |
| 203 | + } |
| 204 | +} |
0 commit comments