java.util.concurrent.atomic.AtomicReferenceFieldUpdater Java Examples
The following examples show how to use
java.util.concurrent.atomic.AtomicReferenceFieldUpdater.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CopyOnWriteAsyncContextMap.java From servicetalk with Apache License 2.0 | 6 votes |
@Override public boolean removeAll(final Iterable<Key<?>> entries, CopyOnWriteAsyncContextMap owner, AtomicReferenceFieldUpdater<CopyOnWriteAsyncContextMap, CopyAsyncContextMap> mapUpdater) { assert keyOne != null && keyTwo != null && keyThree != null && keyFour != null; MutableInt removeIndexMask = new MutableInt(); entries.forEach(k -> { if (keyOne.equals(k)) { removeIndexMask.value |= 0x1; } else if (keyTwo.equals(k)) { removeIndexMask.value |= 0x2; } else if (keyThree.equals(k)) { removeIndexMask.value |= 0x4; } else if (keyFour.equals(k)) { removeIndexMask.value |= 0x8; } }); CopyAsyncContextMap newMap = removeAll(removeIndexMask); if (newMap == null) { return false; } return mapUpdater.compareAndSet(owner, this, newMap) || owner.removeAll(entries); }
Example #2
Source File: AtomicReferenceFieldUpdaterTest.java From j2objc with Apache License 2.0 | 6 votes |
/** * compareAndSet in one thread enables another waiting for value * to succeed */ public void testCompareAndSetInMultipleThreads() throws Exception { x = one; final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a; a = updaterFor("x"); Thread t = new Thread(new CheckedRunnable() { public void realRun() { while (!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three)) Thread.yield(); }}); t.start(); assertTrue(a.compareAndSet(this, one, two)); t.join(LONG_DELAY_MS); assertFalse(t.isAlive()); assertSame(three, a.get(this)); }
Example #3
Source File: Disposables.java From reactor-core with Apache License 2.0 | 6 votes |
/** * Atomically set the field to a {@link Disposable} and dispose the old content. * * @param updater the target field updater * @param holder the target instance holding the field * @param newValue the new Disposable to set * @return true if successful, false if the field contains the {@link #DISPOSED} instance. */ static <T> boolean set(AtomicReferenceFieldUpdater<T, Disposable> updater, T holder, @Nullable Disposable newValue) { for (;;) { Disposable current = updater.get(holder); if (current == DISPOSED) { if (newValue != null) { newValue.dispose(); } return false; } if (updater.compareAndSet(holder, current, newValue)) { if (current != null) { current.dispose(); } return true; } } }
Example #4
Source File: CopyOnWriteAsyncContextMap.java From servicetalk with Apache License 2.0 | 6 votes |
@Override public boolean removeAll(final Iterable<Key<?>> entries, CopyOnWriteAsyncContextMap owner, AtomicReferenceFieldUpdater<CopyOnWriteAsyncContextMap, CopyAsyncContextMap> mapUpdater) { assert keyOne != null && keyTwo != null; MutableInt removeIndexMask = new MutableInt(); entries.forEach(k -> { if (keyOne.equals(k)) { removeIndexMask.value |= 0x1; } else if (keyTwo.equals(k)) { removeIndexMask.value |= 0x2; } }); if ((removeIndexMask.value & 0x3) == 0x3) { return mapUpdater.compareAndSet(owner, this, EmptyAsyncContextMap.INSTANCE) || owner.removeAll(entries); } else if ((removeIndexMask.value & 0x2) == 0x2) { return mapUpdater.compareAndSet(owner, this, new OneAsyncContextMap(keyOne, valueOne)) || owner.removeAll(entries); } else if ((removeIndexMask.value & 0x1) == 0x1) { return mapUpdater.compareAndSet(owner, this, new OneAsyncContextMap(keyTwo, valueTwo)) || owner.removeAll(entries); } return false; }
Example #5
Source File: OperatorDisposables.java From reactor-core with Apache License 2.0 | 6 votes |
/** * Atomically set the field to a {@link Disposable} and dispose the old content. * * @param updater the target field updater * @param holder the target instance holding the field * @param newValue the new Disposable to set * @return true if successful, false if the field contains the {@link #DISPOSED} instance. */ public static <T> boolean set(AtomicReferenceFieldUpdater<T, Disposable> updater, T holder, @Nullable Disposable newValue) { for (;;) { Disposable current = updater.get(holder); if (current == DISPOSED) { if (newValue != null) { newValue.dispose(); } return false; } if (updater.compareAndSet(holder, current, newValue)) { if (current != null) { current.dispose(); } return true; } } }
Example #6
Source File: CopyOnWriteAsyncContextMap.java From servicetalk with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") @Override public <T> T put(final Key<T> key, final T value, CopyOnWriteAsyncContextMap owner, AtomicReferenceFieldUpdater<CopyOnWriteAsyncContextMap, CopyAsyncContextMap> mapUpdater) { assert keyOne != null && keyTwo != null; if (keyOne.equals(key)) { return mapUpdater.compareAndSet(owner, this, new TwoAsyncContextMap(keyOne, value, keyTwo, valueTwo)) ? (T) valueOne : owner.put(key, value); } else if (keyTwo.equals(key)) { return mapUpdater.compareAndSet(owner, this, new TwoAsyncContextMap(keyOne, valueOne, keyTwo, value)) ? (T) valueTwo : owner.put(key, value); } return mapUpdater.compareAndSet(owner, this, new ThreeAsyncContextMap(keyOne, valueOne, keyTwo, valueTwo, key, value)) ? null : owner.put(key, value); }
Example #7
Source File: Operators.java From reactor-core with Apache License 2.0 | 6 votes |
/** * A generic utility to atomically replace a subscription or cancel the replacement * if current subscription is marked as cancelled (as in {@link #cancelledSubscription()}) * or was concurrently updated before. * <p> * The replaced subscription is itself cancelled. * * @param field The Atomic container * @param instance the instance reference * @param s the subscription * @param <F> the instance type * * @return true if replaced */ public static <F> boolean set(AtomicReferenceFieldUpdater<F, Subscription> field, F instance, Subscription s) { for (; ; ) { Subscription a = field.get(instance); if (a == CancelledSubscription.INSTANCE) { s.cancel(); return false; } if (field.compareAndSet(instance, a, s)) { if (a != null) { a.cancel(); } return true; } } }
Example #8
Source File: AtomicReferenceFieldUpdaterTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * compareAndSet in one thread enables another waiting for value * to succeed */ public void testCompareAndSetInMultipleThreads() throws Exception { x = one; final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a; a = updaterFor("x"); Thread t = new Thread(new CheckedRunnable() { public void realRun() { while (!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three)) Thread.yield(); }}); t.start(); assertTrue(a.compareAndSet(this, one, two)); t.join(LONG_DELAY_MS); assertFalse(t.isAlive()); assertSame(three, a.get(this)); }
Example #9
Source File: CopyOnWriteAsyncContextMap.java From servicetalk with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") @Nullable @Override public <T> T put(Key<T> key, @Nullable T value, CopyOnWriteAsyncContextMap owner, AtomicReferenceFieldUpdater<CopyOnWriteAsyncContextMap, CopyAsyncContextMap> mapUpdater) { int i = findIndex(Objects.requireNonNull(key)); final Object[] context; if (i < 0) { context = new Object[this.context.length + 2]; arraycopy(this.context, 0, context, 0, this.context.length); context[this.context.length] = key; context[this.context.length + 1] = value; } else { context = new Object[this.context.length]; arraycopy(this.context, 0, context, 0, i + 1); context[i + 1] = value; if (i + 2 < context.length) { arraycopy(this.context, i + 2, context, i + 2, context.length - i - 2); } } return mapUpdater.compareAndSet(owner, this, new SevenOrMoreAsyncContextMap(context)) ? (T) this.context[i + 1] : null; }
Example #10
Source File: CopyOnWriteAsyncContextMap.java From servicetalk with Apache License 2.0 | 6 votes |
@Override public boolean removeAll(Iterable<Key<?>> entries, CopyOnWriteAsyncContextMap owner, AtomicReferenceFieldUpdater<CopyOnWriteAsyncContextMap, CopyAsyncContextMap> mapUpdater) { GrowableIntArray indexesToRemove = new GrowableIntArray(3); entries.forEach(key -> { int keyIndex = findIndex(key); if (keyIndex >= 0) { indexesToRemove.add(keyIndex); } }); CopyAsyncContextMap newMap = removeAll(indexesToRemove); if (newMap == null) { return false; } return mapUpdater.compareAndSet(owner, this, newMap) || owner.removeAll(entries); }
Example #11
Source File: SubscriberUtils.java From servicetalk with Apache License 2.0 | 6 votes |
/** * There are some scenarios where a completion {@link TerminalNotification} can be overridden with an error if * errors are produced asynchronously. * <p> * This method helps set {@link TerminalNotification} atomically providing such an override. * * @param toSet {@link TerminalNotification} to set. * @param overrideComplete Whether exisiting {@link TerminalNotification#complete()} should be overridden with the * {@code toSet}. * @param terminalNotificationUpdater {@link AtomicReferenceFieldUpdater} to access the current * {@link TerminalNotification}. * @param flagOwner instance of {@link R} that owns the current {@link TerminalNotification} field referenced by * {@code terminalNotificationUpdater}. * @param <R> Type of {@code flagOwner}. * @return {@code true} if {@code toSet} is updated as the current {@link TerminalNotification}. */ public static <R> boolean trySetTerminal(TerminalNotification toSet, boolean overrideComplete, AtomicReferenceFieldUpdater<R, TerminalNotification> terminalNotificationUpdater, R flagOwner) { for (;;) { TerminalNotification curr = terminalNotificationUpdater.get(flagOwner); if (curr != null && !overrideComplete) { // Once terminated, terminalNotification will never be set back to null. return false; } else if (curr == null && terminalNotificationUpdater.compareAndSet(flagOwner, null, toSet)) { return true; } else if (curr != null && curr.cause() == null) { // Override complete if (terminalNotificationUpdater.compareAndSet(flagOwner, curr, toSet)) { return true; } } else { return false; } } }
Example #12
Source File: Operators.java From reactor-core with Apache License 2.0 | 5 votes |
/** * Atomically terminates the subscription if it is not already a * {@link #cancelledSubscription()}, cancelling the subscription and setting the field * to the singleton {@link #cancelledSubscription()}. * * @param <F> the instance type containing the field * @param field the field accessor * @param instance the parent instance * @return true if terminated, false if the subscription was already terminated */ public static <F> boolean terminate(AtomicReferenceFieldUpdater<F, Subscription> field, F instance) { Subscription a = field.get(instance); if (a != CancelledSubscription.INSTANCE) { a = field.getAndSet(instance, CancelledSubscription.INSTANCE); if (a != null && a != CancelledSubscription.INSTANCE) { a.cancel(); return true; } } return false; }
Example #13
Source File: Atomic8Test.java From j2objc with Apache License 2.0 | 5 votes |
/** * AtomicReferenceFieldUpdater updateAndGet updates with supplied * function and returns result. */ public void testReferenceFieldUpdaterUpdateAndGet() { AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater(); a.set(this, one); assertEquals(new Integer(18), a.updateAndGet(this, Atomic8Test::addInteger17)); assertEquals(new Integer(35), a.updateAndGet(this, Atomic8Test::addInteger17)); assertEquals(new Integer(35), a.get(this)); assertEquals(new Integer(35), anIntegerField); }
Example #14
Source File: RingBuffer.java From reactor-core with Apache License 2.0 | 5 votes |
static <T> void addSequence(final T holder, final AtomicReferenceFieldUpdater<T, Sequence[]> updater, final Sequence sequence) { Sequence[] updatedSequences; Sequence[] currentSequences; do { currentSequences = updater.get(holder); updatedSequences = copyOf(currentSequences, currentSequences.length + 1); updatedSequences[currentSequences.length] = sequence; } while (!updater.compareAndSet(holder, currentSequences, updatedSequences)); }
Example #15
Source File: AtomicReferenceFieldUpdaterTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * get returns the last value lazySet by same thread */ public void testGetLazySet() { AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a; a = updaterFor("x"); x = one; assertSame(one, a.get(this)); a.lazySet(this, two); assertSame(two, a.get(this)); a.lazySet(this, m3); assertSame(m3, a.get(this)); }
Example #16
Source File: PlatformDependent.java From camunda-bpm-reactor with Apache License 2.0 | 5 votes |
public static <U, W> AtomicReferenceFieldUpdater<U, W> newAtomicReferenceFieldUpdater( Class<U> tclass, String fieldName) { if (hasUnsafe()) { try { return PlatformDependent0.newAtomicReferenceFieldUpdater(tclass, fieldName); } catch (Throwable ignore) { // ignore } } return null; }
Example #17
Source File: OperatorDisposables.java From reactor-core with Apache License 2.0 | 5 votes |
/** * Atomically dispose the {@link Disposable} in the field if not already disposed. * * @param updater the target field updater * @param holder the target instance holding the field * @return true if the {@link Disposable} held by the field was properly disposed */ public static <T> boolean dispose(AtomicReferenceFieldUpdater<T, Disposable> updater, T holder) { Disposable current = updater.get(holder); Disposable d = DISPOSED; if (current != d) { current = updater.getAndSet(holder, d); if (current != d) { if (current != null) { current.dispose(); } return true; } } return false; }
Example #18
Source File: AtomicUpdater.java From Jupiter with Apache License 2.0 | 5 votes |
/** * Creates and returns an updater for objects with the given field. * * @param tClass the class of the objects holding the field. * @param vClass the class of the field * @param fieldName the name of the field to be updated. */ public static <U, W> AtomicReferenceFieldUpdater<U, W> newAtomicReferenceFieldUpdater( Class<U> tClass, Class<W> vClass, String fieldName) { try { if (UnsafeUtil.hasUnsafe()) { return new UnsafeAtomicReferenceFieldUpdater<>(UnsafeUtil.getUnsafeAccessor().getUnsafe(), tClass, fieldName); } else { return AtomicReferenceFieldUpdater.newUpdater(tClass, vClass, fieldName); } } catch (Throwable t) { return AtomicReferenceFieldUpdater.newUpdater(tClass, vClass, fieldName); } }
Example #19
Source File: AtomicTest.java From jtransc with Apache License 2.0 | 5 votes |
static private void testAtomicReferenceFieldUpdater() { System.out.println("AtomicTest.testAtomicReferenceFieldUpdater:"); AtomicTest obj = new AtomicTest(); AtomicReferenceFieldUpdater<AtomicTest, Integer> updater = AtomicReferenceFieldUpdater.newUpdater(AtomicTest.class, Integer.class, "value"); System.out.println(updater.get(obj)); updater.set(obj, 5); System.out.println(updater.get(obj)); updater.compareAndSet(obj, 4, -4); System.out.println(updater.get(obj)); updater.compareAndSet(obj, 5, -5); System.out.println(updater.get(obj)); }
Example #20
Source File: Atomic8Test.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * AtomicReferenceFieldUpdater getAndUpdate returns previous value * and updates result of supplied function */ public void testReferenceFieldUpdaterGetAndUpdate() { AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater(); a.set(this, one); assertEquals((Integer) 1, a.getAndUpdate(this, Atomic8Test::addInteger17)); assertEquals((Integer) 18, a.getAndUpdate(this, Atomic8Test::addInteger17)); assertEquals((Integer) 35, a.get(this)); assertEquals((Integer) 35, anIntegerField); }
Example #21
Source File: AtomicReferenceFieldUpdaterTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * get returns the last value set or assigned */ public void testGetSet() { AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a; a = updaterFor("x"); x = one; assertSame(one, a.get(this)); a.set(this, two); assertSame(two, a.get(this)); a.set(this, m3); assertSame(m3, a.get(this)); }
Example #22
Source File: AtomicReferenceFieldUpdaterTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
public void checkPrivateAccess(AtomicReferenceFieldUpdaterTest obj) { try { AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a = AtomicReferenceFieldUpdater.newUpdater (AtomicReferenceFieldUpdaterTest.class, Integer.class, "privateField"); throw new AssertionError("should throw"); } catch (RuntimeException success) { assertNotNull(success.getCause()); } }
Example #23
Source File: AtomicReferenceFieldUpdaterTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
public void checkPackageAccess(AtomicReferenceFieldUpdaterTest obj) { obj.x = one; AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a = AtomicReferenceFieldUpdater.newUpdater (AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); assertSame(one, a.get(obj)); assertTrue(a.compareAndSet(obj, one, two)); assertSame(two, a.get(obj)); }
Example #24
Source File: AtomicReferenceFieldUpdaterTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
public void checkCompareAndSetProtectedSub() { AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a = AtomicReferenceFieldUpdater.newUpdater (AtomicReferenceFieldUpdaterTest.class, Integer.class, "protectedField"); this.protectedField = one; assertTrue(a.compareAndSet(this, one, two)); assertTrue(a.compareAndSet(this, two, m4)); assertSame(m4, a.get(this)); assertFalse(a.compareAndSet(this, m5, seven)); assertFalse(seven == a.get(this)); assertTrue(a.compareAndSet(this, m4, seven)); assertSame(seven, a.get(this)); }
Example #25
Source File: AtomicReferenceFieldUpdaterTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
public void checkPrivateAccess() { try { AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a = AtomicReferenceFieldUpdater.newUpdater (AtomicReferenceFieldUpdaterTest.class, Integer.class, "privateField"); shouldThrow(); } catch (RuntimeException success) { assertNotNull(success.getCause()); } }
Example #26
Source File: RingBuffer.java From reactor-core with Apache License 2.0 | 5 votes |
static <T> boolean removeSequence(final T holder, final AtomicReferenceFieldUpdater<T, Sequence[]> sequenceUpdater, final Sequence sequence) { int numToRemove; Sequence[] oldSequences; Sequence[] newSequences; do { oldSequences = sequenceUpdater.get(holder); numToRemove = countMatching(oldSequences, sequence); if (0 == numToRemove) { break; } final int oldSize = oldSequences.length; newSequences = new Sequence[oldSize - numToRemove]; for (int i = 0, pos = 0; i < oldSize; i++) { final Sequence testSequence = oldSequences[i]; if (sequence != testSequence) { newSequences[pos++] = testSequence; } } } while (!sequenceUpdater.compareAndSet(holder, oldSequences, newSequences)); return numToRemove != 0; }
Example #27
Source File: Atomic8Test.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * AtomicReferenceFieldUpdater accumulateAndGet updates with * supplied function and returns result. */ public void testReferenceFieldUpdaterAccumulateAndGet() { AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater(); a.set(this, one); assertEquals((Integer) 7, a.accumulateAndGet(this, 6, Atomic8Test::sumInteger)); assertEquals((Integer) 10, a.accumulateAndGet(this, 3, Atomic8Test::sumInteger)); assertEquals((Integer) 10, a.get(this)); assertEquals((Integer) 10, anIntegerField); }
Example #28
Source File: Atomic8Test.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * AtomicReferenceFieldUpdater returns previous value and updates * with supplied function. */ public void testReferenceFieldUpdaterGetAndAccumulate() { AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater(); a.set(this, one); assertEquals((Integer) 1, a.getAndAccumulate(this, 2, Atomic8Test::sumInteger)); assertEquals((Integer) 3, a.getAndAccumulate(this, 3, Atomic8Test::sumInteger)); assertEquals((Integer) 6, a.get(this)); assertEquals((Integer) 6, anIntegerField); }
Example #29
Source File: AbstractFuture.java From hadoop-ozone with Apache License 2.0 | 5 votes |
SafeAtomicHelper( AtomicReferenceFieldUpdater<Waiter, Thread> waiterThreadUpdater, AtomicReferenceFieldUpdater<Waiter, Waiter> waiterNextUpdater, AtomicReferenceFieldUpdater<AbstractFuture, Waiter> waitersUpdater, AtomicReferenceFieldUpdater<AbstractFuture, Listener> listenersUpdater, AtomicReferenceFieldUpdater<AbstractFuture, Object> valueUpdater) { this.waiterThreadUpdater = waiterThreadUpdater; this.waiterNextUpdater = waiterNextUpdater; this.waitersUpdater = waitersUpdater; this.listenersUpdater = listenersUpdater; this.valueUpdater = valueUpdater; }
Example #30
Source File: AtomicReferenceFieldUpdaterTest.java From j2objc with Apache License 2.0 | 5 votes |
/** * get returns the last value set or assigned */ public void testGetSet() { AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a; a = updaterFor("x"); x = one; assertSame(one, a.get(this)); a.set(this, two); assertSame(two, a.get(this)); a.set(this, m3); assertSame(m3, a.get(this)); }