[MNG-4633] Changed to use nice clean countdownlatch instead of synchronized booleans

Also updated phase locking to only lock when project being built changes phase

git-svn-id: https://svn.apache.org/repos/asf/maven/maven-3/trunk@939220 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Kristian Rosenvold 2010-04-29 07:22:39 +00:00
parent 0d7a05f8b2
commit ab495d4a39
4 changed files with 17 additions and 46 deletions

View File

@ -19,7 +19,7 @@ import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.MojoExecution; import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.descriptor.MojoDescriptor; import org.apache.maven.plugin.descriptor.MojoDescriptor;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.CountDownLatch;
/** /**
* Wraps individual MojoExecutions, containing information about completion status and scheduling. * Wraps individual MojoExecutions, containing information about completion status and scheduling.
@ -35,9 +35,7 @@ public class ExecutionPlanItem
private final Schedule schedule; private final Schedule schedule;
// Completeness just indicates that it has been run or failed // Completeness just indicates that it has been run or failed
private final AtomicBoolean complete = new AtomicBoolean( false ); private final CountDownLatch done = new CountDownLatch( 1 );
private final Object monitor = new Object();
public ExecutionPlanItem( MojoExecution mojoExecution, Schedule schedule ) public ExecutionPlanItem( MojoExecution mojoExecution, Schedule schedule )
{ {
@ -57,47 +55,23 @@ public class ExecutionPlanItem
public void setComplete() public void setComplete()
{ {
boolean transitionSuccessful = ensureComplete(); done.countDown();
if ( !transitionSuccessful )
{
throw new IllegalStateException( "Expected to be able to setComplete node, but was complete already" );
}
} }
public boolean ensureComplete() public boolean isDone()
{ {
boolean f = complete.compareAndSet( false, true ); return done.getCount() < 1;
notifyListeners();
return f;
}
private void notifyListeners()
{
synchronized ( monitor )
{
monitor.notifyAll();
}
} }
public void forceComplete() public void forceComplete()
{ {
final boolean b = complete.getAndSet( true ); setComplete();
if ( !b )
{
notifyListeners();
} // Release anyone waiting for us
} }
public void waitUntilDone() public void waitUntilDone()
throws InterruptedException throws InterruptedException
{ {
synchronized ( monitor ) done.await();
{
while ( !complete.get() )
{
monitor.wait( 100 );
}
}
} }
public Schedule getSchedule() public Schedule getSchedule()

View File

@ -205,7 +205,7 @@ public class LifecycleWeaveBuilder
builtLogItem.setComplete(); builtLogItem.setComplete();
ExecutionPlanItem nextPlanItem = planItems.hasNext() ? planItems.next() : null; ExecutionPlanItem nextPlanItem = planItems.hasNext() ? planItems.next() : null;
if ( nextPlanItem != null ) if ( nextPlanItem != null && phaseRecorder.isDifferentPhase( nextPlanItem.getMojoExecution() ) )
{ {
final Schedule scheduleOfNext = nextPlanItem.getSchedule(); final Schedule scheduleOfNext = nextPlanItem.getSchedule();
@ -215,12 +215,9 @@ public class LifecycleWeaveBuilder
projectBuild ); projectBuild );
} }
if ( phaseRecorder.isDifferentPhase( nextPlanItem.getMojoExecution() ) ) for ( ArtifactLink dependencyLink : dependencyLinks )
{ {
for ( ArtifactLink dependencyLink : dependencyLinks ) dependencyLink.resolveFromUpstream();
{
dependencyLink.resolveFromUpstream();
}
} }
} }
current = nextPlanItem; current = nextPlanItem;
@ -259,13 +256,13 @@ public class LifecycleWeaveBuilder
{ {
final MavenExecutionPlan upstreamPlan = executionPlans.get( upstreamProject ); final MavenExecutionPlan upstreamPlan = executionPlans.get( upstreamProject );
final String nextPhase = nextPlanItem.getLifecyclePhase(); final String nextPhase = nextPlanItem.getLifecyclePhase();
final ExecutionPlanItem inSchedule = upstreamPlan.findLastInPhase( nextPhase ); final ExecutionPlanItem upstream = upstreamPlan.findLastInPhase( nextPhase );
if ( inSchedule != null ) if ( upstream != null )
{ {
long startWait = System.currentTimeMillis(); long startWait = System.currentTimeMillis();
inSchedule.waitUntilDone(); upstream.waitUntilDone();
builtLogItem.addWait( upstreamProject, inSchedule, startWait ); builtLogItem.addWait( upstreamProject, upstream, startWait );
} }
else if ( !upstreamPlan.containsPhase( nextPhase ) ) else if ( !upstreamPlan.containsPhase( nextPhase ) )
{ {

View File

@ -46,8 +46,8 @@ public class MavenExecutionPlanTest
MavenExecutionPlan plan = LifecycleExecutionPlanCalculatorStub.getProjectAExceutionPlan(); MavenExecutionPlan plan = LifecycleExecutionPlanCalculatorStub.getProjectAExceutionPlan();
plan.forceAllComplete(); plan.forceAllComplete();
final Iterator<ExecutionPlanItem> planItemIterator = plan.iterator(); final Iterator<ExecutionPlanItem> planItemIterator = plan.iterator();
assertFalse( planItemIterator.next().ensureComplete() ); assertTrue( planItemIterator.next().isDone() );
assertFalse( planItemIterator.next().ensureComplete() ); assertTrue( planItemIterator.next().isDone() );
} }
public void testFindLastInPhase() public void testFindLastInPhase()

View File

@ -31,7 +31,7 @@ public class ExecutionPlanItemTest
{ {
ExecutionPlanItem item = createExecutionPlanItem( "testMojo", null ); ExecutionPlanItem item = createExecutionPlanItem( "testMojo", null );
item.setComplete(); // This itself is a valid test item.setComplete(); // This itself is a valid test
assertFalse( item.ensureComplete() ); assertTrue( item.isDone() );
} }
public void testWaitUntilDone() public void testWaitUntilDone()