You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by si...@apache.org on 2011/10/01 10:12:30 UTC

svn commit: r1177941 - /lucene/dev/trunk/lucene/src/java/org/apache/lucene/index/DocumentsWriterPerThreadPool.java

Author: simonw
Date: Sat Oct  1 08:12:30 2011
New Revision: 1177941

URL: http://svn.apache.org/viewvc?rev=1177941&view=rev
Log:
LUCENE-3462: Release lock in DocumentsWriterPerThreadPool if new state has been deactivated during close. 

Modified:
    lucene/dev/trunk/lucene/src/java/org/apache/lucene/index/DocumentsWriterPerThreadPool.java

Modified: lucene/dev/trunk/lucene/src/java/org/apache/lucene/index/DocumentsWriterPerThreadPool.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/java/org/apache/lucene/index/DocumentsWriterPerThreadPool.java?rev=1177941&r1=1177940&r2=1177941&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/src/java/org/apache/lucene/index/DocumentsWriterPerThreadPool.java (original)
+++ lucene/dev/trunk/lucene/src/java/org/apache/lucene/index/DocumentsWriterPerThreadPool.java Sat Oct  1 08:12:30 2011
@@ -186,13 +186,41 @@ public abstract class DocumentsWriterPer
     if (numThreadStatesActive < perThreads.length) {
       final ThreadState threadState = perThreads[numThreadStatesActive];
       threadState.lock(); // lock so nobody else will get this ThreadState
-      numThreadStatesActive++; // increment will publish the ThreadState
-      threadState.perThread.initialize();
-      return threadState;
+      boolean unlock = true;
+      try {
+        if (threadState.isActive()) {
+          // unreleased thread states are deactivated during DW#close()
+          numThreadStatesActive++; // increment will publish the ThreadState
+          assert threadState.perThread != null;
+          threadState.perThread.initialize();
+          unlock = false;
+          return threadState;
+        }
+        // unlock since the threadstate is not active anymore - we are closed!
+        assert assertUnreleasedThreadStatesInactive();
+        return null;
+      } finally {
+        if (unlock) {
+          // in any case make sure we unlock if we fail 
+          threadState.unlock();
+        }
+      }
     }
     return null;
   }
   
+  private synchronized boolean assertUnreleasedThreadStatesInactive() {
+    for (int i = numThreadStatesActive; i < perThreads.length; i++) {
+      assert perThreads[i].tryLock() : "unreleased threadstate should not be locked";
+      try {
+        assert !perThreads[i].isActive() : "expected unreleased thread state to be inactive";
+      } finally {
+        perThreads[i].unlock();
+      }
+    }
+    return true;
+  }
+  
   /**
    * Deactivate all unreleased threadstates 
    */