summaryrefslogtreecommitdiffstats
path: root/yolean
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2016-10-26 14:07:55 +0200
committerHenning Baldersheim <balder@yahoo-inc.com>2016-10-26 14:07:55 +0200
commit661b6a01084036380234435aa6d14b37816e1406 (patch)
tree90c7ceae9f0c4590e88851e9c06e75d3a597375d /yolean
parent04ca5c7f537f43efd3b9b048033addd2e60da316 (diff)
Provide our own buffer pool. Now does no pooling.
Diffstat (limited to 'yolean')
-rw-r--r--yolean/src/main/java/com/yahoo/yolean/concurrent/ConcurrentResourcePool.java35
-rw-r--r--yolean/src/main/java/com/yahoo/yolean/concurrent/ResourceFactory.java11
-rw-r--r--yolean/src/main/java/com/yahoo/yolean/concurrent/ResourcePool.java38
3 files changed, 84 insertions, 0 deletions
diff --git a/yolean/src/main/java/com/yahoo/yolean/concurrent/ConcurrentResourcePool.java b/yolean/src/main/java/com/yahoo/yolean/concurrent/ConcurrentResourcePool.java
new file mode 100644
index 00000000000..5e7788e6e20
--- /dev/null
+++ b/yolean/src/main/java/com/yahoo/yolean/concurrent/ConcurrentResourcePool.java
@@ -0,0 +1,35 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.yolean.concurrent;
+
+import com.yahoo.yolean.concurrent.ResourceFactory;
+
+import java.util.Iterator;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+
+/**
+ * @author baldersheim
+ */
+public class ConcurrentResourcePool<T> implements Iterable<T> {
+
+ private final Queue<T> pool = new ConcurrentLinkedQueue<>();
+ private final ResourceFactory<T> factory;
+
+ public ConcurrentResourcePool(ResourceFactory<T> factory) {
+ this.factory = factory;
+ }
+
+ public final T alloc() {
+ final T e = pool.poll();
+ return e != null ? e : factory.create();
+ }
+
+ public final void free(T e) {
+ pool.offer(e);
+ }
+
+ @Override
+ public Iterator<T> iterator() {
+ return pool.iterator();
+ }
+}
diff --git a/yolean/src/main/java/com/yahoo/yolean/concurrent/ResourceFactory.java b/yolean/src/main/java/com/yahoo/yolean/concurrent/ResourceFactory.java
new file mode 100644
index 00000000000..68b505c1b03
--- /dev/null
+++ b/yolean/src/main/java/com/yahoo/yolean/concurrent/ResourceFactory.java
@@ -0,0 +1,11 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.yolean.concurrent;
+
+/**
+ * @author baldersheim
+ * @since 5.2
+ */
+public abstract class ResourceFactory<T> {
+
+ public abstract T create();
+}
diff --git a/yolean/src/main/java/com/yahoo/yolean/concurrent/ResourcePool.java b/yolean/src/main/java/com/yahoo/yolean/concurrent/ResourcePool.java
new file mode 100644
index 00000000000..0bf4749fe63
--- /dev/null
+++ b/yolean/src/main/java/com/yahoo/yolean/concurrent/ResourcePool.java
@@ -0,0 +1,38 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.yolean.concurrent;
+
+import com.yahoo.yolean.concurrent.ResourceFactory;
+
+import java.util.ArrayDeque;
+import java.util.Deque;
+import java.util.Iterator;
+
+/**
+ * <p>This implements a simple stack based resource pool. If you are out of resources new are allocated from the
+ * factory.</p>
+ *
+ * @author baldersheim
+ * @since 5.2
+ */
+public final class ResourcePool<T> implements Iterable<T> {
+
+ private final Deque<T> pool = new ArrayDeque<>();
+ private final ResourceFactory<T> factory;
+
+ public ResourcePool(ResourceFactory<T> factory) {
+ this.factory = factory;
+ }
+
+ public final T alloc() {
+ return pool.isEmpty() ? factory.create() : pool.pop();
+ }
+
+ public final void free(T e) {
+ pool.push(e);
+ }
+
+ @Override
+ public Iterator<T> iterator() {
+ return pool.iterator();
+ }
+}