-
Notifications
You must be signed in to change notification settings - Fork 9
Concurrency helpers #156
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
Concurrency helpers #156
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
base/src/main/java/org/eclipse/serializer/concurrency/LockScope.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package org.eclipse.serializer.concurrency; | ||
|
||
/*- | ||
* #%L | ||
* Eclipse Serializer Base | ||
* %% | ||
* Copyright (C) 2024 MicroStream Software | ||
* %% | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* #L% | ||
*/ | ||
|
||
import org.eclipse.serializer.functional.Action; | ||
import org.eclipse.serializer.functional.Producer; | ||
|
||
/** | ||
* Abstract base class for types, which want to utilize a {@link LockedExecutor}. | ||
* <p> | ||
* All the executor's methods are exposed by abstract equivalents inside this type hierarchy. | ||
*/ | ||
public abstract class LockScope | ||
{ | ||
private final transient LockedExecutor executor = LockedExecutor.New(); | ||
|
||
protected LockScope() | ||
{ | ||
super(); | ||
} | ||
|
||
/** | ||
* Executes an operation protected by a read lock. | ||
* | ||
* @param action the action to execute | ||
*/ | ||
protected void read(final Action action) | ||
{ | ||
this.executor.read(action); | ||
} | ||
|
||
/** | ||
* Executes an operation protected by a read lock. | ||
* | ||
* @param <R> the producer's return type | ||
* @param producer the producer to execute | ||
* @return the producer's result | ||
*/ | ||
protected <R> R read(final Producer<R> producer) | ||
{ | ||
return this.executor.read(producer); | ||
} | ||
|
||
/** | ||
* Executes an operation protected by a write lock. | ||
* | ||
* @param action the action to execute | ||
*/ | ||
protected void write(final Action action) | ||
{ | ||
this.executor.write(action); | ||
} | ||
|
||
/** | ||
* Executes an operation protected by a write lock. | ||
* | ||
* @param <R> the producer's return type | ||
* @param producer the producer to execute | ||
* @return the producer's result | ||
*/ | ||
protected <R> R write(final Producer<R> producer) | ||
{ | ||
return this.executor.write(producer); | ||
} | ||
|
||
} |
224 changes: 224 additions & 0 deletions
224
base/src/main/java/org/eclipse/serializer/concurrency/LockedExecutor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,224 @@ | ||
package org.eclipse.serializer.concurrency; | ||
|
||
/*- | ||
* #%L | ||
* Eclipse Serializer Base | ||
* %% | ||
* Copyright (C) 2024 MicroStream Software | ||
* %% | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* #L% | ||
*/ | ||
|
||
import java.util.concurrent.locks.ReadWriteLock; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock; | ||
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock; | ||
|
||
import org.eclipse.serializer.functional.Action; | ||
import org.eclipse.serializer.functional.Producer; | ||
|
||
|
||
/** | ||
* Facility to execute operations with a reentrant mutual exclusion. | ||
* | ||
* @see ReentrantLock | ||
* @see ReadWriteLock | ||
*/ | ||
public interface LockedExecutor | ||
{ | ||
/** | ||
* Executes an operation protected by a read lock. | ||
* | ||
* @param action the action to execute | ||
*/ | ||
public void read(Action action); | ||
|
||
/** | ||
* Executes an operation protected by a read lock. | ||
* | ||
* @param <R> the producer's return type | ||
* @param producer the producer to execute | ||
* @return the producer's result | ||
*/ | ||
public <R> R read(Producer<R> producer); | ||
|
||
/** | ||
* Executes an operation protected by a write lock. | ||
* | ||
* @param action the action to execute | ||
*/ | ||
public void write(Action action); | ||
|
||
/** | ||
* Executes an operation protected by a write lock. | ||
* | ||
* @param <R> the producer's return type | ||
* @param producer the producer to execute | ||
* @return the producer's result | ||
*/ | ||
public <R> R write(Producer<R> producer); | ||
|
||
|
||
|
||
public static final class Static | ||
{ | ||
private final static Object LOCK = new Object(); | ||
private static volatile LockedExecutor sharedInstance; | ||
|
||
public static LockedExecutor sharedInstance() | ||
{ | ||
/* | ||
* Double-checked locking to reduce the overhead of acquiring a lock | ||
* by testing the locking criterion. | ||
* The field (Static.sharedInstance) has to be volatile. | ||
*/ | ||
LockedExecutor sharedInstance = Static.sharedInstance; | ||
if(sharedInstance == null) | ||
{ | ||
synchronized(LOCK) | ||
{ | ||
if((sharedInstance = Static.sharedInstance) == null) | ||
{ | ||
sharedInstance = Static.sharedInstance = LockedExecutor.New(); | ||
} | ||
} | ||
} | ||
return sharedInstance; | ||
} | ||
|
||
|
||
private Static() | ||
{ | ||
// static only | ||
throw new UnsupportedOperationException(); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Provides a global {@link LockedExecutor} instance. | ||
* <p> | ||
* Only a single one exists for the whole VM process, meaning it can be used to create VM-wide locks. | ||
* | ||
* @return a shared {@link LockedExecutor} instance | ||
*/ | ||
public static LockedExecutor global() | ||
{ | ||
return Static.sharedInstance(); | ||
} | ||
|
||
|
||
/** | ||
* Pseudo-constructor method to create a new {@link LockedExecutor}. | ||
* | ||
* @return a newly created {@link LockedExecutor} | ||
*/ | ||
public static LockedExecutor New() | ||
{ | ||
return new LockedExecutor.Default(); | ||
} | ||
|
||
|
||
public static class Default implements LockedExecutor | ||
{ | ||
private transient volatile ReentrantReadWriteLock reentrantLock; | ||
|
||
Default() | ||
{ | ||
super(); | ||
} | ||
|
||
private ReentrantReadWriteLock reentrantLock() | ||
{ | ||
/* | ||
* Double-checked locking to reduce the overhead of acquiring a lock | ||
* by testing the locking criterion. | ||
* The field (this.reentrantLock) has to be volatile. | ||
*/ | ||
ReentrantReadWriteLock reentrantLock = this.reentrantLock; | ||
if(reentrantLock == null) | ||
{ | ||
synchronized(this) | ||
{ | ||
if((reentrantLock = this.reentrantLock) == null) | ||
{ | ||
reentrantLock = this.reentrantLock = new ReentrantReadWriteLock(); | ||
} | ||
} | ||
} | ||
return reentrantLock; | ||
} | ||
|
||
@Override | ||
public void read(final Action action) | ||
{ | ||
final ReadLock readLock = this.reentrantLock().readLock(); | ||
readLock.lock(); | ||
|
||
try | ||
{ | ||
action.execute(); | ||
} | ||
finally | ||
{ | ||
readLock.unlock(); | ||
} | ||
} | ||
|
||
@Override | ||
public <T> T read(final Producer<T> producer) | ||
{ | ||
final ReadLock readLock = this.reentrantLock().readLock(); | ||
readLock.lock(); | ||
|
||
try | ||
{ | ||
return producer.produce(); | ||
} | ||
finally | ||
{ | ||
readLock.unlock(); | ||
} | ||
} | ||
|
||
@Override | ||
public void write(final Action action) | ||
{ | ||
final WriteLock writeLock = this.reentrantLock().writeLock(); | ||
writeLock.lock(); | ||
|
||
try | ||
{ | ||
action.execute(); | ||
} | ||
finally | ||
{ | ||
writeLock.unlock(); | ||
} | ||
} | ||
|
||
@Override | ||
public <R> R write(final Producer<R> producer) | ||
{ | ||
final WriteLock writeLock = this.reentrantLock().writeLock(); | ||
writeLock.lock(); | ||
|
||
try | ||
{ | ||
return producer.produce(); | ||
} | ||
finally | ||
{ | ||
writeLock.unlock(); | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
93 changes: 93 additions & 0 deletions
93
base/src/main/java/org/eclipse/serializer/concurrency/StripeLockScope.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package org.eclipse.serializer.concurrency; | ||
|
||
/*- | ||
* #%L | ||
* Eclipse Serializer Base | ||
* %% | ||
* Copyright (C) 2024 MicroStream Software | ||
* %% | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* #L% | ||
*/ | ||
|
||
import org.eclipse.serializer.functional.Action; | ||
import org.eclipse.serializer.functional.Producer; | ||
|
||
|
||
/** | ||
* Abstract base class for types, which want to utilize a {@link StripeLockedExecutor}. | ||
* <p> | ||
* All the executor's methods are exposed by abstract equivalents inside this type hierarchy. | ||
*/ | ||
public abstract class StripeLockScope | ||
{ | ||
private final transient StripeLockedExecutor executor = StripeLockedExecutor.New(this.stripeCount()); | ||
|
||
protected StripeLockScope() | ||
{ | ||
super(); | ||
} | ||
|
||
/** | ||
* Gets the maxiimum number of stripes used for the {@link StripeLockedExecutor}. | ||
* | ||
* @return max number of stripes | ||
*/ | ||
protected int stripeCount() | ||
{ | ||
return Runtime.getRuntime().availableProcessors(); | ||
} | ||
|
||
/** | ||
* Executes an operation protected by a read lock. | ||
* | ||
* @param mutex the mutex to lock on, not <code>null</code> | ||
* @param action the action to execute | ||
*/ | ||
protected void read(final Object mutex, final Action action) | ||
{ | ||
this.executor.read(mutex, action); | ||
} | ||
|
||
/** | ||
* Executes an operation protected by a read lock. | ||
* | ||
* @param <R> the producer's return type | ||
* @param mutex the mutex to lock on, not <code>null</code> | ||
* @param producer the producer to execute | ||
* @return the producer's result | ||
*/ | ||
protected <R> R read(final Object mutex, final Producer<R> producer) | ||
{ | ||
return this.executor.read(mutex, producer); | ||
} | ||
|
||
/** | ||
* Executes an operation protected by a write lock. | ||
* | ||
* @param mutex the mutex to lock on, not <code>null</code> | ||
* @param action the action to execute | ||
*/ | ||
protected void write(final Object mutex, final Action action) | ||
{ | ||
this.executor.write(mutex, action); | ||
} | ||
|
||
/** | ||
* Executes an operation protected by a write lock. | ||
* | ||
* @param <R> the producer's return type | ||
* @param mutex the mutex to lock on, not <code>null</code> | ||
* @param producer the producer to execute | ||
* @return the producer's result | ||
*/ | ||
protected <R> R write(final Object mutex, final Producer<R> producer) | ||
{ | ||
return this.executor.write(mutex, producer); | ||
} | ||
|
||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.