You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2014/08/01 21:59:55 UTC

git commit: TAP-2334: Optimizations to PerThreadManager

Repository: tapestry-5
Updated Branches:
  refs/heads/master b0f280657 -> 6e210b275


TAP-2334: Optimizations to PerThreadManager


Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo
Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/6e210b27
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/6e210b27
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/6e210b27

Branch: refs/heads/master
Commit: 6e210b27558eaf9e3be8678954ee196b3d26202b
Parents: b0f2806
Author: Howard M. Lewis Ship <hl...@apache.org>
Authored: Fri Aug 1 12:59:59 2014 -0700
Committer: Howard M. Lewis Ship <hl...@apache.org>
Committed: Fri Aug 1 12:59:59 2014 -0700

----------------------------------------------------------------------
 .../services/PerThreadServiceCreator.java       | 50 -----------------
 .../services/PerThreadServiceLifecycle.java     |  4 +-
 .../internal/services/PerthreadManagerImpl.java | 58 +++++++++++++++-----
 .../ioc/services/PerthreadManager.java          | 13 ++++-
 4 files changed, 55 insertions(+), 70 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/6e210b27/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/PerThreadServiceCreator.java
----------------------------------------------------------------------
diff --git a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/PerThreadServiceCreator.java b/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/PerThreadServiceCreator.java
deleted file mode 100644
index 44b0d0f..0000000
--- a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/PerThreadServiceCreator.java
+++ /dev/null
@@ -1,50 +0,0 @@
-// Copyright 2006, 2008, 2010 The Apache Software Foundation
-//
-// 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 org.apache.tapestry5.ioc.internal.services;
-
-import org.apache.tapestry5.ioc.ObjectCreator;
-import org.apache.tapestry5.ioc.services.PerThreadValue;
-import org.apache.tapestry5.ioc.services.PerthreadManager;
-
-/**
- * Provides per-thread implementations of services.
- */
-public class PerThreadServiceCreator implements ObjectCreator
-{
-    private final PerThreadValue<Object> perThreadValue;
-
-    private final ObjectCreator delegate;
-
-    public PerThreadServiceCreator(PerthreadManager perthreadManager, ObjectCreator delegate)
-    {
-        perThreadValue = perthreadManager.createValue();
-
-        this.delegate = delegate;
-    }
-
-    /**
-     * For each thread, the first call will use the delegate {@link org.apache.tapestry5.ioc.ObjectCreator} to create
-     * an instance, and later calls will reuse the same per-thread instance. The instance is stored in the
-     * {@link org.apache.tapestry5.ioc.services.PerthreadManager} and will be released at the end of the request.
-     */
-    @Override
-    public Object createObject()
-    {
-        if (perThreadValue.exists())
-            return perThreadValue.get();
-
-        return perThreadValue.set(delegate.createObject());
-    }
-}

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/6e210b27/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/PerThreadServiceLifecycle.java
----------------------------------------------------------------------
diff --git a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/PerThreadServiceLifecycle.java b/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/PerThreadServiceLifecycle.java
index 118cb44..f9017c2 100644
--- a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/PerThreadServiceLifecycle.java
+++ b/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/PerThreadServiceLifecycle.java
@@ -1,5 +1,3 @@
-// Copyright 2006, 2007, 2008, 2009, 2010, 2011 The Apache Software Foundation
-//
 // 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
@@ -58,7 +56,7 @@ public class PerThreadServiceLifecycle implements ServiceLifecycle
     @Override
     public Object createService(ServiceResources resources, ObjectCreator creator)
     {
-        ObjectCreator perThreadCreator = new PerThreadServiceCreator(perthreadManager, creator);
+        ObjectCreator perThreadCreator = perthreadManager.createValue(creator);
 
         Class serviceInterface = resources.getServiceInterface();
 

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/6e210b27/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/PerthreadManagerImpl.java
----------------------------------------------------------------------
diff --git a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/PerthreadManagerImpl.java b/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/PerthreadManagerImpl.java
index 1c60370..00be7a4 100644
--- a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/PerthreadManagerImpl.java
+++ b/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/PerthreadManagerImpl.java
@@ -1,5 +1,3 @@
-// Copyright 2006-2013 The Apache Software Foundation
-//
 // 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
@@ -14,7 +12,13 @@
 
 package org.apache.tapestry5.ioc.internal.services;
 
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+
 import org.apache.tapestry5.ioc.Invokable;
+import org.apache.tapestry5.ioc.ObjectCreator;
 import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
 import org.apache.tapestry5.ioc.services.PerThreadValue;
 import org.apache.tapestry5.ioc.services.PerthreadManager;
@@ -22,12 +26,6 @@ import org.apache.tapestry5.ioc.services.RegistryShutdownHub;
 import org.apache.tapestry5.ioc.services.ThreadCleanupListener;
 import org.slf4j.Logger;
 
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.concurrent.atomic.AtomicInteger;
-import java.util.concurrent.locks.Lock;
-
 @SuppressWarnings("all")
 public class PerthreadManagerImpl implements PerthreadManager
 {
@@ -155,6 +153,34 @@ public class PerthreadManagerImpl implements PerthreadManager
 
     private static Object NULL_VALUE = new Object();
 
+    <T> ObjectCreator<T> createValue(final Object key, final ObjectCreator<T> delegate)
+    {
+        return new ObjectCreator<T>()
+        {
+            public T createObject()
+            {
+                Map map = getPerthreadMap();
+                T storedValue = (T) map.get(key);
+
+                if (storedValue != null)
+                {
+                    return (storedValue == NULL_VALUE) ? null : storedValue;
+                }
+
+                T newValue = delegate.createObject();
+
+                map.put(key, newValue == null ? NULL_VALUE : newValue);
+
+                return newValue;
+            }
+        };
+    }
+
+    public <T> ObjectCreator<T> createValue(ObjectCreator<T> delegate)
+    {
+        return createValue(uuidGenerator.getAndIncrement(), delegate);
+    }
+
     <T> PerThreadValue<T> createValue(final Object key)
     {
         return new PerThreadValue<T>()
@@ -170,17 +196,19 @@ public class PerthreadManagerImpl implements PerthreadManager
             {
                 Map map = getPerthreadMap();
 
-                if (map.containsKey(key))
-                {
-                    Object storedValue = map.get(key);
+                Object storedValue = map.get(key);
 
-                    if (storedValue == NULL_VALUE)
-                        return null;
+                if (storedValue == null)
+                {
+                    return defaultValue;
+                }
 
-                    return (T) storedValue;
+                if (storedValue == NULL_VALUE)
+                {
+                    return null;
                 }
 
-                return defaultValue;
+                return (T) storedValue;
             }
 
             @Override

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/6e210b27/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/services/PerthreadManager.java
----------------------------------------------------------------------
diff --git a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/services/PerthreadManager.java b/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/services/PerthreadManager.java
index 0db6020..ad2512f 100644
--- a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/services/PerthreadManager.java
+++ b/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/services/PerthreadManager.java
@@ -1,5 +1,3 @@
-// Copyright 2006-2013 The Apache Software Foundation
-//
 // 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
@@ -15,6 +13,7 @@
 package org.apache.tapestry5.ioc.services;
 
 import org.apache.tapestry5.ioc.Invokable;
+import org.apache.tapestry5.ioc.ObjectCreator;
 
 /**
  * Manages per-thread data, and provides a way for listeners to know when such data should be cleaned up. Typically,
@@ -58,6 +57,16 @@ public interface PerthreadManager
     <T> PerThreadValue<T> createValue();
 
     /**
+     * Return {@link org.apache.tapestry5.ioc.ObjectCreator}, which for each thread,
+     * the first call will use the delegate {@link org.apache.tapestry5.ioc.ObjectCreator} to create
+     * an instance, and later calls will reuse the same per-thread instance. The instance is stored in the
+     * {@link org.apache.tapestry5.ioc.services.PerthreadManager} and will be released at the end of the request.
+     *
+     * @since 5.4
+     */
+    <T> ObjectCreator<T> createValue(ObjectCreator<T> delegate);
+
+    /**
      * Invokes {@link Runnable#run()}, providing a try...finally to {@linkplain #cleanup() cleanup} after.
      *
      * @since 5.2.0