From d607980a5ae370730a38f2e4fbcabbe784cacbc2 Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Sat, 1 Oct 2011 08:12:30 +0000 Subject: [PATCH] LUCENE-3462: Release lock in DocumentsWriterPerThreadPool if new state has been deactivated during close. git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1177941 13f79535-47bb-0310-9956-ffa450edef68 --- .../index/DocumentsWriterPerThreadPool.java | 34 +++++++++++++++++-- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/lucene/src/java/org/apache/lucene/index/DocumentsWriterPerThreadPool.java b/lucene/src/java/org/apache/lucene/index/DocumentsWriterPerThreadPool.java index cf2a1653d08..eb6f20944d1 100644 --- a/lucene/src/java/org/apache/lucene/index/DocumentsWriterPerThreadPool.java +++ b/lucene/src/java/org/apache/lucene/index/DocumentsWriterPerThreadPool.java @@ -186,13 +186,41 @@ public abstract class DocumentsWriterPerThreadPool { 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 */