Author: ssteiner
Date: Fri Dec 23 15:39:40 2022
New Revision: 1906188

URL: http://svn.apache.org/viewvc?rev=1906188&view=rev
Log:
FOP-3111: Make property cache thread safe

Modified:
    
xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/fo/properties/PropertyCache.java
    
xmlgraphics/fop/trunk/fop-core/src/test/java/org/apache/fop/fonts/FontSelectorTestCase.java

Modified: 
xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/fo/properties/PropertyCache.java
URL: 
http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/fo/properties/PropertyCache.java?rev=1906188&r1=1906187&r2=1906188&view=diff
==============================================================================
--- 
xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/fo/properties/PropertyCache.java
 (original)
+++ 
xmlgraphics/fop/trunk/fop-core/src/main/java/org/apache/fop/fo/properties/PropertyCache.java
 Fri Dec 23 15:39:40 2022
@@ -189,6 +189,14 @@ public final class PropertyCache<T> {
     }
 
     private boolean eq(Object p, Object q) {
-        return (p == q || p.equals(q));
+        if (p == q) {
+            return true;
+        }
+        cleanupLock.lock();
+        try {
+            return p.equals(q);
+        } finally {
+            cleanupLock.unlock();
+        }
     }
 }

Modified: 
xmlgraphics/fop/trunk/fop-core/src/test/java/org/apache/fop/fonts/FontSelectorTestCase.java
URL: 
http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/fop-core/src/test/java/org/apache/fop/fonts/FontSelectorTestCase.java?rev=1906188&r1=1906187&r2=1906188&view=diff
==============================================================================
--- 
xmlgraphics/fop/trunk/fop-core/src/test/java/org/apache/fop/fonts/FontSelectorTestCase.java
 (original)
+++ 
xmlgraphics/fop/trunk/fop-core/src/test/java/org/apache/fop/fonts/FontSelectorTestCase.java
 Fri Dec 23 15:39:40 2022
@@ -19,6 +19,11 @@
 
 package org.apache.fop.fonts;
 
+import java.io.PrintStream;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.invocation.InvocationOnMock;
@@ -30,12 +35,16 @@ import static org.mockito.ArgumentMatche
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
+import org.apache.commons.io.output.NullPrintStream;
+
 import org.apache.fop.datatypes.PercentBaseContext;
 import org.apache.fop.fo.Constants;
 import org.apache.fop.fo.FOEventHandler;
 import org.apache.fop.fo.FOText;
 import org.apache.fop.fo.PropertyList;
+import org.apache.fop.fo.StaticPropertyList;
 import org.apache.fop.fo.expr.PropertyException;
+import org.apache.fop.fo.pagination.Root;
 import org.apache.fop.fo.properties.CommonFont;
 import org.apache.fop.fo.properties.EnumProperty;
 import org.apache.fop.fo.properties.FixedLength;
@@ -43,7 +52,6 @@ import org.apache.fop.fo.properties.Font
 import org.apache.fop.fo.properties.NumberProperty;
 import org.apache.fop.fo.properties.Property;
 
-
 public class FontSelectorTestCase {
 
     private static final FontTriplet LATIN_FONT_TRIPLET = new 
FontTriplet("Verdana", "normal", 400);
@@ -136,4 +144,34 @@ public class FontSelectorTestCase {
         return CommonFont.getInstance(pList);
     }
 
+
+    @Test
+    public void testFontCacheWithThreads() throws Exception {
+        PrintStream old = System.err;
+        System.setErr(new NullPrintStream());
+        ExecutorService executor = Executors.newFixedThreadPool(10);
+        for (int i = 0; i < 100000; i++) {
+            executor.submit(new FontTask());
+        }
+        executor.shutdown();
+        executor.awaitTermination(1, TimeUnit.DAYS);
+        System.setErr(old);
+    }
+
+    static class FontTask implements Runnable {
+        public void run() {
+            try {
+                Root r = new Root(null);
+                StaticPropertyList pList = new StaticPropertyList(r, null);
+                Property x = new 
FontFamilyProperty.Maker(Constants.PR_FONT_FAMILY).make(pList, "a-963191172", 
null);
+                pList.putExplicit(Constants.PR_FONT_FAMILY, x);
+                CommonFont.getInstance(pList);
+                x = new 
FontFamilyProperty.Maker(Constants.PR_FONT_FAMILY).make(pList, "a385863058", 
null);
+                pList.putExplicit(Constants.PR_FONT_FAMILY, x);
+                CommonFont.getInstance(pList);
+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            }
+        }
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: fop-commits-unsubscr...@xmlgraphics.apache.org
For additional commands, e-mail: fop-commits-h...@xmlgraphics.apache.org

Reply via email to