-
Notifications
You must be signed in to change notification settings - Fork 188
#4673 Make AtWithRho.get() thread-safe #5023
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
f5a720f
a0a29a3
1346a5f
4278545
ecec171
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,18 +5,13 @@ | |
|
|
||
| package org.eolang; | ||
|
|
||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import java.util.concurrent.locks.ReentrantLock; | ||
|
|
||
| /** | ||
| * The attribute that tries to copy object and set \rho to it if it has not already set. | ||
| * This attribute is NOT thread safe! | ||
| * | ||
| * @since 0.36.0 | ||
| * @todo #4673:30min The {@link AtWithRho#get()} is not thread safe. If multiple threads | ||
| * call get() concurrently when the underlying object lacks RHO, each thread will: | ||
| * 1. Pass the !ret.hasRho() check | ||
| * 2. Create its own copy via ret.copy() | ||
| * 3. Attempt to set RHO on its copy | ||
| * This results in different threads receiving different copies, violating the expectation | ||
| * that get() returns a consistent view of the attribute's value. | ||
| */ | ||
| final class AtWithRho implements Attr { | ||
| /** | ||
|
|
@@ -29,6 +24,16 @@ final class AtWithRho implements Attr { | |
| */ | ||
| private final Phi rho; | ||
|
|
||
| /** | ||
| * Cached result of {@link #get()} to guarantee a consistent view across threads. | ||
| */ | ||
| private final AtomicReference<Phi> cached; | ||
|
|
||
| /** | ||
| * Lock guarding the first initialization of {@link #cached}. | ||
| */ | ||
| private final ReentrantLock lock; | ||
|
|
||
| /** | ||
| * Ctor. | ||
| * @param attr Attribute | ||
|
|
@@ -37,6 +42,8 @@ final class AtWithRho implements Attr { | |
| AtWithRho(final Attr attr, final Phi rho) { | ||
| this.original = attr; | ||
| this.rho = rho; | ||
| this.cached = new AtomicReference<>(); | ||
| this.lock = new ReentrantLock(); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -49,16 +56,48 @@ public Attr copy(final Phi self) { | |
|
|
||
| @Override | ||
| public Phi get() { | ||
| Phi ret = this.original.get(); | ||
| if (!ret.hasRho()) { | ||
| ret = ret.copy(); | ||
| ret.put(Phi.RHO, this.rho); | ||
| final Phi ret = this.cached.get(); | ||
| if (ret != null) { | ||
| return ret; | ||
| } | ||
| return ret; | ||
| return this.initialize(); | ||
| } | ||
|
|
||
| @Override | ||
| public void put(final Phi phi) { | ||
| this.cached.set(null); | ||
| this.original.put(phi); | ||
|
Comment on lines
68
to
70
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Synchronize cache invalidation with initialization.
🔒 Proposed fix: serialize `put()` with `get()` initialization `@Override`
public void put(final Phi phi) {
- this.cached.set(null);
- this.original.put(phi);
+ this.lock.lock();
+ try {
+ this.cached.set(null);
+ this.original.put(phi);
+ } finally {
+ this.lock.unlock();
+ }
}🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| /** | ||
| * Initialize the cached value under the lock, using double-checked locking. | ||
| * @return Cached {@link Phi} | ||
| */ | ||
| private Phi initialize() { | ||
| this.lock.lock(); | ||
| try { | ||
| Phi ret = this.cached.get(); | ||
| if (ret == null) { | ||
| ret = this.withRho(this.original.get()); | ||
| this.cached.set(ret); | ||
| } | ||
| return ret; | ||
| } finally { | ||
| this.lock.unlock(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Return the given object with {@link Phi#RHO} attached, copying it if needed. | ||
| * @param phi Original object | ||
| * @return Object with {@code \rho} set | ||
| */ | ||
| private Phi withRho(final Phi phi) { | ||
| if (phi.hasRho()) { | ||
| return phi; | ||
| } | ||
| final Phi copy = phi.copy(); | ||
| copy.put(Phi.RHO, this.rho); | ||
| return copy; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,6 +4,11 @@ | |||||||
| */ | ||||||||
| package org.eolang; | ||||||||
|
|
||||||||
| import java.util.Set; | ||||||||
| import java.util.concurrent.ConcurrentHashMap; | ||||||||
| import java.util.concurrent.CountDownLatch; | ||||||||
| import java.util.concurrent.ExecutorService; | ||||||||
| import java.util.concurrent.Executors; | ||||||||
| import org.hamcrest.MatcherAssert; | ||||||||
| import org.hamcrest.Matchers; | ||||||||
| import org.junit.jupiter.api.Test; | ||||||||
|
|
@@ -97,4 +102,49 @@ | |||||||
| Matchers.not(Matchers.is(obj)) | ||||||||
| ); | ||||||||
| } | ||||||||
|
|
||||||||
| @Test | ||||||||
| void returnsSameInstanceToConcurrentCallers() throws InterruptedException { | ||||||||
| MatcherAssert.assertThat( | ||||||||
| "AtWithRho.get() must return the same instance for all concurrent callers", | ||||||||
| AtWithRhoTest.collectConcurrentGet( | ||||||||
| new AtWithRho( | ||||||||
| new AtComposite(new PhDefault(), phi -> phi), | ||||||||
| new PhDefault() | ||||||||
| ), | ||||||||
| 16 | ||||||||
| ), | ||||||||
| Matchers.hasSize(1) | ||||||||
| ); | ||||||||
| } | ||||||||
|
|
||||||||
| /** | ||||||||
| * Invoke {@link Attr#get()} concurrently from many threads released | ||||||||
| * simultaneously and return the distinct instances observed. | ||||||||
| * @param attr Attribute to query | ||||||||
| * @param threads Number of concurrent callers | ||||||||
| * @return Distinct {@link Phi} instances returned across threads | ||||||||
| * @throws InterruptedException If interrupted while waiting | ||||||||
| */ | ||||||||
| private static Set<Phi> collectConcurrentGet(final Attr attr, final int threads) | ||||||||
| throws InterruptedException { | ||||||||
|
Check warning on line 130 in eo-runtime/src/test/java/org/eolang/AtWithRhoTest.java
|
||||||||
|
Comment on lines
+130
to
+144
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the unused Nothing in the method body throws 🔧 Proposed fix- private static int distinctConcurrentGets(final Attr attr, final int threads)
- throws InterruptedException {
+ private static int distinctConcurrentGets(final Attr attr, final int threads) {📝 Committable suggestion
Suggested change
🧰 Tools🪛 GitHub Check: SonarCloud Code Analysis[warning] 130-130: Remove the declaration of thrown exception 'java.lang.InterruptedException', as it cannot be thrown from method's body. 🤖 Prompt for AI Agents |
||||||||
| final Set<Phi> results = ConcurrentHashMap.newKeySet(); | ||||||||
| final CountDownLatch start = new CountDownLatch(1); | ||||||||
| try (ExecutorService pool = Executors.newFixedThreadPool(threads)) { | ||||||||
| for (int idx = 0; idx < threads; ++idx) { | ||||||||
| pool.submit( | ||||||||
| () -> { | ||||||||
| try { | ||||||||
| start.await(); | ||||||||
| results.add(attr.get()); | ||||||||
| } catch (final InterruptedException ex) { | ||||||||
| Thread.currentThread().interrupt(); | ||||||||
| } | ||||||||
| } | ||||||||
| ); | ||||||||
| } | ||||||||
| start.countDown(); | ||||||||
| } | ||||||||
| return results; | ||||||||
| } | ||||||||
| } | ||||||||
Uh oh!
There was an error while loading. Please reload this page.