Skip to content

Fix for potential NPE in lock scopes #172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,45 @@
*/
public abstract class LockScope
{
private final transient LockedExecutor executor = LockedExecutor.New();
private transient volatile LockedExecutor executor;

protected LockScope()
{
super();
}

/**
* Lazy initializes the executor.
*/
private LockedExecutor executor()
{
/*
* Double-checked locking to reduce the overhead of acquiring a lock
* by testing the locking criterion.
* The field (this.executor) has to be volatile.
*/
LockedExecutor executor = this.executor;
if(executor == null)
{
synchronized(this)
{
if((executor = this.executor) == null)
{
executor = this.executor = LockedExecutor.New();
}
}
}
return executor;
}

/**
* Executes an operation protected by a read lock.
*
* @param action the action to execute
*/
protected void read(final Action action)
{
this.executor.read(action);
this.executor().read(action);
}

/**
Expand All @@ -50,7 +74,7 @@ protected void read(final Action action)
*/
protected <R> R read(final Producer<R> producer)
{
return this.executor.read(producer);
return this.executor().read(producer);
}

/**
Expand All @@ -60,7 +84,7 @@ protected <R> R read(final Producer<R> producer)
*/
protected void write(final Action action)
{
this.executor.write(action);
this.executor().write(action);
}

/**
Expand All @@ -72,7 +96,7 @@ protected void write(final Action action)
*/
protected <R> R write(final Producer<R> producer)
{
return this.executor.write(producer);
return this.executor().write(producer);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,37 @@
*/
public abstract class StripeLockScope
{
private final transient StripeLockedExecutor executor = StripeLockedExecutor.New(this.stripeCount());
private transient volatile StripeLockedExecutor executor;

protected StripeLockScope()
{
super();
}

/**
* Lazy initializes the executor.
*/
private StripeLockedExecutor executor()
{
/*
* Double-checked locking to reduce the overhead of acquiring a lock
* by testing the locking criterion.
* The field (this.executor) has to be volatile.
*/
StripeLockedExecutor executor = this.executor;
if(executor == null)
{
synchronized(this)
{
if((executor = this.executor) == null)
{
executor = this.executor = StripeLockedExecutor.New(this.stripeCount());
}
}
}
return executor;
}

/**
* Gets the maximum number of stripes used for the {@link StripeLockedExecutor}.
*
Expand All @@ -50,7 +74,7 @@ protected int stripeCount()
*/
protected void read(final Object mutex, final Action action)
{
this.executor.read(mutex, action);
this.executor().read(mutex, action);
}

/**
Expand All @@ -63,7 +87,7 @@ protected void read(final Object mutex, final Action action)
*/
protected <R> R read(final Object mutex, final Producer<R> producer)
{
return this.executor.read(mutex, producer);
return this.executor().read(mutex, producer);
}

/**
Expand All @@ -74,7 +98,7 @@ protected <R> R read(final Object mutex, final Producer<R> producer)
*/
protected void write(final Object mutex, final Action action)
{
this.executor.write(mutex, action);
this.executor().write(mutex, action);
}

/**
Expand All @@ -87,7 +111,7 @@ protected void write(final Object mutex, final Action action)
*/
protected <R> R write(final Object mutex, final Producer<R> producer)
{
return this.executor.write(mutex, producer);
return this.executor().write(mutex, producer);
}

}