Skip to content

Commit

Permalink
Better synchronization for mojo model update
Browse files Browse the repository at this point in the history
On previous solution code flow could squeeze between conditions of queue and running update, but now we are done only after we done.

Fixup for b6c80a2
  • Loading branch information
hurricup committed Aug 17, 2024
1 parent 6aa0ece commit 492517c
Showing 1 changed file with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.jetbrains.annotations.TestOnly;

import java.util.*;
import com.intellij.util.concurrency.Semaphore;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BooleanSupplier;

Expand All @@ -56,6 +57,7 @@ public class MojoProjectManager implements Disposable {
private final @NotNull MergingUpdateQueue myUpdateQueue;
private final @NotNull AtomicBoolean myUpdatingModel = new AtomicBoolean(false);
private volatile @NotNull Model myModel = new Model(Collections.emptySet());
private volatile Semaphore myTestSemaphore;

public MojoProjectManager(@NotNull Project project) {
myProject = project;
Expand Down Expand Up @@ -140,7 +142,16 @@ private void updateModel() {
scheduleUpdate();
}
else {
doUpdateModel();
try{
LOG.debug("Performing model update");
doUpdateModel();
}
finally {
if( myTestSemaphore != null) {
myTestSemaphore.up();
myTestSemaphore = null;
}
}
}
});
}
Expand Down Expand Up @@ -281,7 +292,9 @@ private Set<MojoProject> getProjects() {

@TestOnly
public BooleanSupplier updateInTestMode() {
var semaphore = new Semaphore(1);
myTestSemaphore = semaphore;
updateModel();
return ()-> myUpdateQueue.isEmpty() && !myUpdatingModel.get();
return semaphore::isUp;
}
}

0 comments on commit 492517c

Please sign in to comment.