diff --git a/Dorkbox-Util/src/dorkbox/util/objectPool/FastObjectPool.java b/Dorkbox-Util/src/dorkbox/util/objectPool/FastObjectPool.java deleted file mode 100644 index 877e709..0000000 --- a/Dorkbox-Util/src/dorkbox/util/objectPool/FastObjectPool.java +++ /dev/null @@ -1,149 +0,0 @@ -/* - * from: http://ashkrit.blogspot.de/2013/05/lock-less-java-object-pool.html - * https://github.com/ashkrit/blog/tree/master/FastObjectPool - * copyright ashkrit 2013 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Modified by dorkbox, llc - */ -package dorkbox.util.objectPool; - -import java.lang.reflect.Field; -import java.security.AccessController; -import java.security.PrivilegedExceptionAction; -import java.util.concurrent.locks.ReentrantLock; - - -class FastObjectPool implements ObjectPool { - - private final sun.misc.Unsafe unsafe; - - private static final boolean FREE = true; - private static final boolean USED = false; - - private final ObjectPoolHolder[] objects; - - private volatile int takePointer; - private volatile int releasePointer; - - private final int mask; - private final long BASE; - private final long INDEXSCALE; - private final long ASHIFT; - - public ReentrantLock lock = new ReentrantLock(); - private ThreadLocal> localValue = new ThreadLocal<>(); - - FastObjectPool(PoolableObject poolableObject, int size) { - try { - final PrivilegedExceptionAction action = new PrivilegedExceptionAction() { - @Override - public sun.misc.Unsafe run() throws Exception { - Class unsafeClass = sun.misc.Unsafe.class; - Field theUnsafe = unsafeClass.getDeclaredField("theUnsafe"); - theUnsafe.setAccessible(true); - Object unsafeObject = theUnsafe.get(null); - if (unsafeClass.isInstance(unsafeObject)) { - return unsafeClass.cast(unsafeObject); - } - - throw new NoSuchFieldError("the Unsafe"); - } - }; - - this.unsafe = AccessController.doPrivileged(action); - } - catch (Exception e) { - throw new RuntimeException("Unable to load unsafe", e); - } - - - int newSize = 1; - while (newSize < size) { - newSize = newSize << 1; - } - - size = newSize; - - @SuppressWarnings({"unchecked", "rawtypes"}) - ObjectPoolHolder[] stuff = new ObjectPoolHolder[size]; - this.objects = stuff; - - for (int x=0;x(poolableObject.create()); - } - - this.mask = size-1; - this.releasePointer = size; - this.BASE = this.unsafe.arrayBaseOffset(ObjectPoolHolder[].class); - this.INDEXSCALE = this.unsafe.arrayIndexScale(ObjectPoolHolder[].class); - this.ASHIFT = 31 - Integer.numberOfLeadingZeros((int) this.INDEXSCALE); - } - - @Override - public ObjectPoolHolder take() { - int localTakePointer; - - // if we have an object available in the cache, use it instead. - ObjectPoolHolder localObject = this.localValue.get(); - if (localObject != null) { - if (localObject.state.compareAndSet(FREE, USED)) { - return localObject; - } - } - - sun.misc.Unsafe unsafe = this.unsafe; - - while (this.releasePointer != (localTakePointer=this.takePointer)) { - int index = localTakePointer & this.mask; - - ObjectPoolHolder holder = this.objects[index]; - //if(holder!=null && THE_UNSAFE.compareAndSwapObject(objects, (index*INDEXSCALE)+BASE, holder, null)) - if (holder != null && unsafe.compareAndSwapObject(this.objects, (index< object) { - try { - this.lock.lockInterruptibly(); - - int localValue = this.releasePointer; - //long index = ((localValue & mask) * INDEXSCALE ) + BASE; - long index = ((localValue & this.mask)< { - /** - * Takes an object from the pool - */ - public ObjectPoolHolder take(); - - /** - * Return object to the pool - */ - public void release(ObjectPoolHolder object); -} diff --git a/Dorkbox-Util/src/dorkbox/util/objectPool/ObjectPoolFactory.java b/Dorkbox-Util/src/dorkbox/util/objectPool/ObjectPoolFactory.java deleted file mode 100644 index 647c1ca..0000000 --- a/Dorkbox-Util/src/dorkbox/util/objectPool/ObjectPoolFactory.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2014 dorkbox, llc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package dorkbox.util.objectPool; - -import dorkbox.util.Sys; - -public class ObjectPoolFactory { - - private ObjectPoolFactory() { - } - - /** - * Creates a pool of the specified size - */ - public static ObjectPool create(PoolableObject poolableObject, int size) { - if (Sys.isAndroid) { - // unfortunately, unsafe is not available in android - SlowObjectPool slowObjectPool = new SlowObjectPool(poolableObject, size); - return slowObjectPool; - } else { - // here we use FAST (via UNSAFE) one! - FastObjectPool fastObjectPool = new FastObjectPool(poolableObject, size); - return fastObjectPool; - } - } -} \ No newline at end of file diff --git a/Dorkbox-Util/src/dorkbox/util/objectPool/ObjectPoolHolder.java b/Dorkbox-Util/src/dorkbox/util/objectPool/ObjectPoolHolder.java deleted file mode 100644 index 80e6fef..0000000 --- a/Dorkbox-Util/src/dorkbox/util/objectPool/ObjectPoolHolder.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2014 dorkbox, llc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package dorkbox.util.objectPool; - -import java.util.concurrent.atomic.AtomicBoolean; - -public class ObjectPoolHolder { - private T value; - - AtomicBoolean state = new AtomicBoolean(true); - - public ObjectPoolHolder(T value) { - this.value = value; - } - - public T getValue() { - return this.value; - } -} diff --git a/Dorkbox-Util/src/dorkbox/util/objectPool/PoolableObject.java b/Dorkbox-Util/src/dorkbox/util/objectPool/PoolableObject.java deleted file mode 100644 index 1ba76d6..0000000 --- a/Dorkbox-Util/src/dorkbox/util/objectPool/PoolableObject.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright 2014 dorkbox, llc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package dorkbox.util.objectPool; - -public interface PoolableObject { - /** - * called when a new instance is created - */ - public T create(); -} diff --git a/Dorkbox-Util/src/dorkbox/util/objectPool/SlowObjectPool.java b/Dorkbox-Util/src/dorkbox/util/objectPool/SlowObjectPool.java deleted file mode 100644 index 329d372..0000000 --- a/Dorkbox-Util/src/dorkbox/util/objectPool/SlowObjectPool.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * from: http://ashkrit.blogspot.de/2013/05/lock-less-java-object-pool.html - * https://github.com/ashkrit/blog/tree/master/FastObjectPool - * copyright ashkrit 2013 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Modified by dorkbox, llc - */ -package dorkbox.util.objectPool; - -import java.util.concurrent.LinkedBlockingDeque; - - -class SlowObjectPool implements ObjectPool { - - private static final boolean FREE = true; - private static final boolean USED = false; - - - private final LinkedBlockingDeque> queue; - - private ThreadLocal> localValue = new ThreadLocal<>(); - - SlowObjectPool(PoolableObject poolableObject, int size) { - - this.queue = new LinkedBlockingDeque>(size); - - for (int x=0;x(poolableObject.create())); - } - } - - @Override - public ObjectPoolHolder take() { - // if we have an object available in the cache, use it instead. - ObjectPoolHolder localObject = this.localValue.get(); - if (localObject != null) { - if (localObject.state.compareAndSet(FREE, USED)) { - return localObject; - } - } - - ObjectPoolHolder holder = this.queue.poll(); - - if (holder == null) { - return null; - } - - // the use of a threadlocal reference here helps eliminates contention. This also checks OTHER threads, - // as they might have one sitting on the cache - if (holder.state.compareAndSet(FREE, USED)) { - this.localValue.set(holder); - return holder; - } else { - // put it back into the queue - this.queue.offer(holder); - return null; - } - } - - @Override - public void release(ObjectPoolHolder object) { - if (object.state.compareAndSet(USED, FREE)) { - this.queue.offer(object); - } - else { - throw new IllegalArgumentException("Invalid reference passed"); - } - } -} \ No newline at end of file