Skip to content

Commit

Permalink
Merge pull request #1035 from rubenporras/remv_synchronization
Browse files Browse the repository at this point in the history
Double-checked locking to reduce overhead of lazy-initialization
  • Loading branch information
rubenporras authored Nov 21, 2024
2 parents db67361 + a962a5f commit 34cd443
Showing 1 changed file with 31 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,37 +35,48 @@ public class DefaultXtextTargetPlatformManager implements IXtextTargetPlatformMa
private IXtextTargetPlatformFactory platformFactory;

/** The current platform. */
private IXtextTargetPlatform platform;
private volatile IXtextTargetPlatform platform; // NOPMD (volatile)

/** Flag indicating that the target platform manager is shutting down. */
private volatile boolean shutdownInProgress; // NOPMD (volatile)

private final Object lock = new Object();

protected IXtextTargetPlatformFactory getPlatformFactory() {
return platformFactory;
}

@Override
public synchronized IXtextTargetPlatform getPlatform() {
ensureLoaded();
return platform;
public IXtextTargetPlatform getPlatform() {
return ensureLoaded();
}

/**
* Return the platform, but without making sure it's loaded. Just return the raw attribute.
*
* @return the current target platform, or null if none set.
*/
protected synchronized IXtextTargetPlatform basicGetPlatform() {
protected IXtextTargetPlatform basicGetPlatform() {
return platform;
}

/**
* Make sure the platform is loaded.
*
* @returns the loaded platform
*/
protected void ensureLoaded() {
if (platform == null) {
load(new NullProgressMonitor());
protected IXtextTargetPlatform ensureLoaded() {
IXtextTargetPlatform localRef = platform; // access volatile field only once when initialized
if (localRef == null) {
synchronized (this) {
localRef = platform;
if (localRef == null) {
load(new NullProgressMonitor());
localRef = platform;
}
}
}
return localRef;
}

/**
Expand Down Expand Up @@ -156,19 +167,21 @@ public void setPlatform(final IXtextTargetPlatform newPlatform) {
* @param mustRebuild
* whether a rebuild is required in any case.
*/
public synchronized void setPlatform(final IXtextTargetPlatform newPlatform, final Collection<IResourceDescription.Delta> deltas, final boolean mustRebuild) {
IXtextTargetPlatform oldPlatform = platform;
public void setPlatform(final IXtextTargetPlatform newPlatform, final Collection<IResourceDescription.Delta> deltas, final boolean mustRebuild) {
synchronized (lock) {
IXtextTargetPlatform oldPlatform = platform;

if (oldPlatform == null && newPlatform == null) {
return; // may occur during initialization...
}
if (oldPlatform == null && newPlatform == null) {
return; // may occur during initialization...
}

if (newPlatform == null) {
this.platform = new NullXtextTargetPlatform();
} else {
this.platform = newPlatform;
if (newPlatform == null) {
this.platform = new NullXtextTargetPlatform();
} else {
this.platform = newPlatform;
}
notifyListeners(platform, deltas, mustRebuild);
}
notifyListeners(platform, deltas, mustRebuild);
}

}

0 comments on commit 34cd443

Please sign in to comment.