Java Code Examples for java.util.concurrent.atomic.AtomicReferenceFieldUpdater

The following examples show how to use java.util.concurrent.atomic.AtomicReferenceFieldUpdater. These examples are extracted from open source projects. 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 Project: servicetalk   Source File: SubscriberUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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 2
Source Project: servicetalk   Source File: CopyOnWriteAsyncContextMap.java    License: Apache License 2.0 6 votes vote down vote up
@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 3
Source Project: reactor-core   Source File: OperatorDisposables.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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 4
Source Project: servicetalk   Source File: CopyOnWriteAsyncContextMap.java    License: Apache License 2.0 6 votes vote down vote up
@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 Project: j2objc   Source File: AtomicReferenceFieldUpdaterTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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 6
Source Project: servicetalk   Source File: CopyOnWriteAsyncContextMap.java    License: Apache License 2.0 6 votes vote down vote up
@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 7
Source Project: servicetalk   Source File: CopyOnWriteAsyncContextMap.java    License: Apache License 2.0 6 votes vote down vote up
@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 8
Source Project: servicetalk   Source File: CopyOnWriteAsyncContextMap.java    License: Apache License 2.0 6 votes vote down vote up
@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 9
/**
 * 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 10
Source Project: reactor-core   Source File: Disposables.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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 11
Source Project: reactor-core   Source File: Operators.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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 12
Source Project: hadoop-ozone   Source File: AbstractFuture.java    License: Apache License 2.0 5 votes vote down vote up
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 13
Source Project: reactor-core   Source File: Operators.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * A generic utility to atomically replace a subscription or cancel the replacement
 * if the current subscription is marked as already cancelled (as in
 * {@link #cancelledSubscription()}).
 *
 * @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 replace(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)) {
			return true;
		}
	}
}
 
Example 14
Source Project: j2objc   Source File: AtomicReferenceFieldUpdaterTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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 15
Source Project: java-katas   Source File: TestKataCompareAndSet.java    License: MIT License 5 votes vote down vote up
@Test
@Tag("PASSING")
@Order(2)
public void compareAndSetUsingAtomicReferenceFieldUpdater() {

    final AtomicReferenceFieldUpdater<TestKataCompareAndSet, Integer> valueUpdater =
            AtomicReferenceFieldUpdater.newUpdater(TestKataCompareAndSet.class,
                    Integer.class,
                    "privateVolatile");

    boolean exchanged = valueUpdater.compareAndSet(this, currentValue, newValue);

    assertTrue(exchanged,
            "The value should have been changed to 7, " +
                    "hence exchanged should be true"
            );

    assertEquals(newValue,
            valueUpdater.get(this),
            "The value of the privateVolatile should now be 7");

    exchanged = valueUpdater.compareAndSet(this, 2, 33);

    assertFalse(exchanged,
            "The value should not have changed since the expected value " +
                    "did not match, hence exchanged should be false"
            );

    assertEquals(newValue,
            valueUpdater.get(this),
            "The value of the privateVolatile should still be 7");
}
 
Example 16
Source Project: reactor-core   Source File: Operators.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Sets the given subscription once and returns true if successful, false
 * if the field has a subscription already or has been cancelled.
 * <p>
 * If the field already has a subscription, it is cancelled and the duplicate
 * subscription is reported (see {@link #reportSubscriptionSet()}).
 *
 * @param <F> the instance type containing the field
 * @param field the field accessor
 * @param instance the parent instance
 * @param s the subscription to set once
 * @return true if successful, false if the target was not empty or has been cancelled
 */
public static <F> boolean setOnce(AtomicReferenceFieldUpdater<F, Subscription> field, F instance, Subscription s) {
	Objects.requireNonNull(s, "subscription");
	Subscription a = field.get(instance);
	if (a == CancelledSubscription.INSTANCE) {
		s.cancel();
		return false;
	}
	if (a != null) {
		s.cancel();
		reportSubscriptionSet();
		return false;
	}

	if (field.compareAndSet(instance, null, s)) {
		return true;
	}

	a = field.get(instance);

	if (a == CancelledSubscription.INSTANCE) {
		s.cancel();
		return false;
	}

	s.cancel();
	reportSubscriptionSet();
	return false;
}
 
Example 17
Source Project: servicetalk   Source File: CopyOnWriteAsyncContextMap.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public <T> T put(final Key<T> key, final T value,
                 CopyOnWriteAsyncContextMap owner,
                 AtomicReferenceFieldUpdater<CopyOnWriteAsyncContextMap, CopyAsyncContextMap> mapUpdater) {
    return mapUpdater.compareAndSet(owner, this, new OneAsyncContextMap(key, value)) ? null :
            owner.put(key, value);
}
 
Example 18
Source Project: servicetalk   Source File: CopyOnWriteAsyncContextMap.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public <T> T remove(final Key<T> key,
                    CopyOnWriteAsyncContextMap owner,
                    AtomicReferenceFieldUpdater<CopyOnWriteAsyncContextMap, CopyAsyncContextMap> mapUpdater) {
    return null;
}
 
Example 19
Source Project: servicetalk   Source File: CopyOnWriteAsyncContextMap.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T> T put(final Key<T> key, final T value, CopyOnWriteAsyncContextMap owner,
                 AtomicReferenceFieldUpdater<CopyOnWriteAsyncContextMap, CopyAsyncContextMap> mapUpdater) {
    assert this.key != null;
    if (this.key.equals(key)) {
        return mapUpdater.compareAndSet(owner, this, new OneAsyncContextMap(this.key, value)) ? (T) this.value :
                owner.put(key, value);
    }
    return mapUpdater.compareAndSet(owner, this, new TwoAsyncContextMap(this.key, this.value, key, value)) ?
            null : owner.put(key, value);
}
 
Example 20
Source Project: servicetalk   Source File: CopyOnWriteAsyncContextMap.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Nullable
@Override
public <T> T remove(final Key<T> key, CopyOnWriteAsyncContextMap owner,
                    AtomicReferenceFieldUpdater<CopyOnWriteAsyncContextMap, CopyAsyncContextMap> mapUpdater) {
    assert this.key != null;
    if (this.key.equals(key)) {
        return mapUpdater.compareAndSet(owner, this, EmptyAsyncContextMap.INSTANCE) ?
                (T) value : owner.remove(key);
    }
    return null;
}
 
Example 21
Source Project: servicetalk   Source File: CopyOnWriteAsyncContextMap.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean removeAll(final Iterable<Key<?>> entries, CopyOnWriteAsyncContextMap owner,
                     AtomicReferenceFieldUpdater<CopyOnWriteAsyncContextMap, CopyAsyncContextMap> mapUpdater) {
    assert this.key != null;
    MutableInt index = new MutableInt();
    entries.forEach(key -> {
        if (this.key.equals(key)) {
            index.value = 1;
        }
    });
    if (index.value != 0) {
        return mapUpdater.compareAndSet(owner, this, EmptyAsyncContextMap.INSTANCE) || owner.removeAll(entries);
    }
    return false;
}
 
Example 22
static <T, V> AtomicReferenceFieldUpdater<T, V> newRefUpdater(Class<T> tclass, Class<V> vclass, String fieldName) {
    if (AVAILABLE) {
        return AtomicReferenceFieldUpdater.newUpdater(tclass, vclass, fieldName);
    } else {
        return null;
    }
}
 
Example 23
Source Project: reactor-core   Source File: OperatorDisposables.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Atomically set the field to the given non-null {@link Disposable} and return true,
 * or return false if the field is non-null.
 * If the target field contains the common {@link #DISPOSED} instance, the supplied disposable
 * is disposed. If the field contains other non-null {@link Disposable}, an {@link IllegalStateException}
 * is signalled to the {@code errorCallback}.
 *
 * @param updater the target field updater
 * @param holder the target instance holding the field
 * @param newValue the new Disposable to set, not null
 * @return true if the operation succeeded, false
 */
public static <T> boolean setOnce(AtomicReferenceFieldUpdater<T, Disposable> updater, T holder, Disposable newValue,
		Consumer<RuntimeException> errorCallback) {
	Objects.requireNonNull(newValue, "newValue is null");
	if (!updater.compareAndSet(holder, null, newValue)) {
		newValue.dispose();
		if (updater.get(holder) != DISPOSED) {
			errorCallback.accept(new IllegalStateException("Disposable already pushed"));
		}
		return false;
	}
	return true;
}
 
Example 24
Source Project: servicetalk   Source File: CopyOnWriteAsyncContextMap.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Nullable
@Override
public <T> T remove(final Key<T> key, CopyOnWriteAsyncContextMap owner,
                    AtomicReferenceFieldUpdater<CopyOnWriteAsyncContextMap, CopyAsyncContextMap> mapUpdater) {
    assert keyOne != null && keyTwo != null && keyThree != null && keyFour != null;
    if (keyOne.equals(key)) {
        return mapUpdater.compareAndSet(owner, this,
                new ThreeAsyncContextMap(keyTwo, valueTwo, keyThree, valueThree, keyFour, valueFour)) ?
                    (T) valueOne : owner.remove(key);
    }
    if (keyTwo.equals(key)) {
        return mapUpdater.compareAndSet(owner, this,
                new ThreeAsyncContextMap(keyOne, valueOne, keyThree, valueThree, keyFour, valueFour)) ?
                    (T) valueTwo : owner.remove(key);
    }
    if (keyThree.equals(key)) {
        return mapUpdater.compareAndSet(owner, this,
                new ThreeAsyncContextMap(keyOne, valueOne, keyTwo, valueTwo, keyFour, valueFour)) ?
                    (T) valueThree : owner.remove(key);
    }
    if (keyFour.equals(key)) {
        return mapUpdater.compareAndSet(owner, this,
                new ThreeAsyncContextMap(keyOne, valueOne, keyTwo, valueTwo, keyThree, valueThree)) ?
                    (T) valueFour : owner.remove(key);
    }
    return null;
}
 
Example 25
Source Project: servicetalk   Source File: CopyOnWriteAsyncContextMap.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Nullable
@Override
public <T> T put(final Key<T> key, final T value, CopyOnWriteAsyncContextMap owner,
                 AtomicReferenceFieldUpdater<CopyOnWriteAsyncContextMap, CopyAsyncContextMap> mapUpdater) {
    assert keyOne != null && keyTwo != null && keyThree != null && keyFour != null && keyFive != null;
    if (keyOne.equals(key)) {
        return mapUpdater.compareAndSet(owner, this,
                new FiveAsyncContextMap(keyOne, value, keyTwo, valueTwo, keyThree, valueThree,
                keyFour, valueFour, keyFive, valueFive)) ? (T) valueOne : owner.put(key, value);
    } else if (keyTwo.equals(key)) {
        return mapUpdater.compareAndSet(owner, this,
                new FiveAsyncContextMap(keyOne, valueOne, keyTwo, value, keyThree, valueThree,
                keyFour, valueFour, keyFive, valueFive)) ? (T) valueTwo : owner.put(key, value);
    } else if (keyThree.equals(key)) {
        return mapUpdater.compareAndSet(owner, this,
                new FiveAsyncContextMap(keyOne, valueOne, keyTwo, valueTwo, keyThree, value, keyFour, valueFour,
                keyFive, valueFive)) ? (T) valueThree : owner.put(key, value);
    } else if (keyFour.equals(key)) {
        return mapUpdater.compareAndSet(owner, this, new FiveAsyncContextMap(keyOne, valueOne, keyTwo, valueTwo,
                keyThree, valueThree, keyFour, value, keyFive, valueFive)) ?
                (T) valueFour : owner.put(key, value);
    } else if (keyFive.equals(key)) {
        return mapUpdater.compareAndSet(owner, this,
                new FiveAsyncContextMap(keyOne, valueOne, keyTwo, valueTwo, keyThree, valueThree,
                keyFour, valueFour, keyFive, value)) ? (T) valueFive : owner.put(key, value);
    }
    return mapUpdater.compareAndSet(owner, this,
            new SixAsyncContextMap(keyOne, valueOne, keyTwo, valueTwo, keyThree, valueThree, keyFour, valueFour,
            keyFive, valueFive, key, value)) ? null : owner.put(key, value);
}
 
Example 26
Source Project: servicetalk   Source File: CopyOnWriteAsyncContextMap.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Nullable
@Override
public <T> T remove(final Key<T> key, CopyOnWriteAsyncContextMap owner,
                    AtomicReferenceFieldUpdater<CopyOnWriteAsyncContextMap, CopyAsyncContextMap> mapUpdater) {
    assert keyOne != null && keyTwo != null && keyThree != null && keyFour != null && keyFive != null;
    if (keyOne.equals(key)) {
        return mapUpdater.compareAndSet(owner, this, new FourAsyncContextMap(keyTwo, valueTwo, keyThree,
                valueThree, keyFour, valueFour, keyFive, valueFive)) ? (T) valueOne : owner.remove(key);
    }
    if (keyTwo.equals(key)) {
        return mapUpdater.compareAndSet(owner, this, new FourAsyncContextMap(keyOne, valueOne, keyThree,
                valueThree, keyFour, valueFour, keyFive, valueFive)) ? (T) valueTwo : owner.remove(key);
    }
    if (keyThree.equals(key)) {
        return mapUpdater.compareAndSet(owner, this, new FourAsyncContextMap(keyOne, valueOne, keyTwo, valueTwo,
                keyFour, valueFour, keyFive, valueFive)) ? (T) valueThree : owner.remove(key);
    }
    if (keyFour.equals(key)) {
        return mapUpdater.compareAndSet(owner, this, new FourAsyncContextMap(keyOne, valueOne, keyTwo, valueTwo,
                keyThree, valueThree, keyFive, valueFive)) ? (T) valueFour : owner.remove(key);
    }
    if (keyFive.equals(key)) {
        return mapUpdater.compareAndSet(owner, this, new FourAsyncContextMap(keyOne, valueOne, keyTwo, valueTwo,
                keyThree, valueThree, keyFour, valueFour)) ? (T) valueFive : owner.remove(key);
    }
    return null;
}
 
Example 27
Source Project: servicetalk   Source File: CopyOnWriteAsyncContextMap.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Nullable
@Override
public <T> T put(final Key<T> key, final T value, CopyOnWriteAsyncContextMap owner,
                 AtomicReferenceFieldUpdater<CopyOnWriteAsyncContextMap, CopyAsyncContextMap> mapUpdater) {
    assert keyOne != null && keyTwo != null && keyThree != null && keyFour != null && keyFive != null &&
            keySix != null;
    if (keyOne.equals(key)) {
        return mapUpdater.compareAndSet(owner, this, new SixAsyncContextMap(keyOne, value, keyTwo, valueTwo,
                keyThree, valueThree, keyFour, valueFour, keyFive, valueFive, keySix, valueSix)) ?
                    (T) valueOne : owner.put(key, value);
    } else if (keyTwo.equals(key)) {
        return mapUpdater.compareAndSet(owner, this, new SixAsyncContextMap(keyOne, valueOne, keyTwo, value,
                keyThree, valueThree, keyFour, valueFour, keyFive, valueFive, keySix, valueSix)) ?
                    (T) valueTwo : owner.put(key, value);
    } else if (keyThree.equals(key)) {
        return mapUpdater.compareAndSet(owner, this, new SixAsyncContextMap(keyOne, valueOne, keyTwo, valueTwo,
                keyThree, value, keyFour, valueFour, keyFive, valueFive, keySix, valueSix)) ?
                    (T) valueThree : owner.put(key, value);
    } else if (keyFour.equals(key)) {
        return mapUpdater.compareAndSet(owner, this, new SixAsyncContextMap(keyOne, valueOne, keyTwo, valueTwo,
                keyThree, valueThree, keyFour, value, keyFive, valueFive, keySix, valueSix)) ?
                    (T) valueFour : owner.put(key, value);
    } else if (keyFive.equals(key)) {
        return mapUpdater.compareAndSet(owner, this, new SixAsyncContextMap(keyOne, valueOne, keyTwo, valueTwo,
                keyThree, valueThree, keyFour, valueFour, keyFive, value, keySix, valueSix)) ?
                    (T) valueFive : owner.put(key, value);
    } else if (keySix.equals(key)) {
        return mapUpdater.compareAndSet(owner, this, new SixAsyncContextMap(keyOne, valueOne, keyTwo, valueTwo,
                keyThree, valueThree, keyFour, valueFour, keyFive, valueFive, keySix, value)) ?
                    (T) valueSix : owner.put(key, value);
    }
    return mapUpdater.compareAndSet(owner, this, new SevenOrMoreAsyncContextMap(keyOne, valueOne, keyTwo,
            valueTwo, keyThree, valueThree, keyFour, valueFour, keyFive, valueFive, keySix, valueSix,
            key, value)) ? null : owner.put(key, value);
}
 
Example 28
Source Project: servicetalk   Source File: CopyOnWriteAsyncContextMap.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Nullable
@Override
public <T> T remove(final Key<T> key, CopyOnWriteAsyncContextMap owner,
                    AtomicReferenceFieldUpdater<CopyOnWriteAsyncContextMap, CopyAsyncContextMap> mapUpdater) {
    assert keyOne != null && keyTwo != null && keyThree != null && keyFour != null && keyFive != null &&
            keySix != null;
    if (keyOne.equals(key)) {
        return mapUpdater.compareAndSet(owner, this, new FiveAsyncContextMap(keyTwo, valueTwo, keyThree,
                valueThree, keyFour, valueFour, keyFive, valueFive, keySix, valueSix)) ?
                    (T) valueOne : owner.remove(key);
    }
    if (keyTwo.equals(key)) {
        return mapUpdater.compareAndSet(owner, this, new FiveAsyncContextMap(keyOne, valueOne, keyThree,
                valueThree, keyFour, valueFour, keyFive, valueFive, keySix, valueSix)) ?
                    (T) valueTwo : owner.remove(key);
    }
    if (keyThree.equals(key)) {
        return mapUpdater.compareAndSet(owner, this, new FiveAsyncContextMap(keyOne, valueOne, keyTwo, valueTwo,
                keyFour, valueFour, keyFive, valueFive, keySix, valueSix)) ? (T) valueThree : owner.remove(key);
    }
    if (keyFour.equals(key)) {
        return mapUpdater.compareAndSet(owner, this, new FiveAsyncContextMap(keyOne, valueOne, keyTwo, valueTwo,
            keyThree, valueThree, keyFive, valueFive, keySix, valueSix)) ? (T) valueFour : owner.remove(key);
    }
    if (keyFive.equals(key)) {
        return mapUpdater.compareAndSet(owner, this, new FiveAsyncContextMap(keyOne, valueOne, keyTwo, valueTwo,
            keyThree, valueThree, keyFour, valueFour, keySix, valueSix)) ? (T) valueFive : owner.remove(key);
    }
    if (keySix.equals(key)) {
        return mapUpdater.compareAndSet(owner, this, new FiveAsyncContextMap(keyOne, valueOne, keyTwo, valueTwo,
            keyThree, valueThree, keyFour, valueFour, keyFive, valueFive)) ? (T) valueSix : owner.remove(key);
    }
    return null;
}
 
Example 29
Source Project: servicetalk   Source File: CopyOnWriteAsyncContextMap.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean removeAll(final Iterable<Key<?>> entries, CopyOnWriteAsyncContextMap owner,
                     AtomicReferenceFieldUpdater<CopyOnWriteAsyncContextMap, CopyAsyncContextMap> mapUpdater) {
    assert keyOne != null && keyTwo != null && keyThree != null && keyFour != null && keyFive != null &&
            keySix != 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;
        } else if (keyFive.equals(k)) {
            removeIndexMask.value |= 0x10;
        } else if (keySix.equals(k)) {
            removeIndexMask.value |= 0x20;
        }
    });

    CopyAsyncContextMap newMap = removeAll(removeIndexMask);
    if (newMap == null) {
        return false;
    }
    return mapUpdater.compareAndSet(owner, this, newMap) || owner.removeAll(entries);
}
 
Example 30
Source Project: sofa-tracer   Source File: SequenceGroups.java    License: Apache License 2.0 5 votes vote down vote up
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;
}