/** * get returns the last values of reference and stamp set */ public void testGetSet() { int[] mark = new int[1]; AtomicStampedReference ai = new AtomicStampedReference(one, 0); assertSame(one, ai.getReference()); assertEquals(0, ai.getStamp()); assertSame(one, ai.get(mark)); assertEquals(0, mark[0]); ai.set(two, 0); assertSame(two, ai.getReference()); assertEquals(0, ai.getStamp()); assertSame(two, ai.get(mark)); assertEquals(0, mark[0]); ai.set(one, 1); assertSame(one, ai.getReference()); assertEquals(1, ai.getStamp()); assertSame(one, ai.get(mark)); assertEquals(1, mark[0]); }
/** * compareAndSet succeeds in changing values if equal to expected reference * and stamp else fails */ public void testCompareAndSet() { int[] mark = new int[1]; AtomicStampedReference ai = new AtomicStampedReference(one, 0); assertSame(one, ai.get(mark)); assertEquals(0, ai.getStamp()); assertEquals(0, mark[0]); assertTrue(ai.compareAndSet(one, two, 0, 0)); assertSame(two, ai.get(mark)); assertEquals(0, mark[0]); assertTrue(ai.compareAndSet(two, m3, 0, 1)); assertSame(m3, ai.get(mark)); assertEquals(1, mark[0]); assertFalse(ai.compareAndSet(two, m3, 1, 1)); assertSame(m3, ai.get(mark)); assertEquals(1, mark[0]); }
/** * compareAndSet in one thread enables another waiting for reference value * to succeed */ public void testCompareAndSetInMultipleThreads() throws Exception { final AtomicStampedReference ai = new AtomicStampedReference(one, 0); Thread t = new Thread(new CheckedRunnable() { public void realRun() { while (!ai.compareAndSet(two, three, 0, 0)) Thread.yield(); }}); t.start(); assertTrue(ai.compareAndSet(one, two, 0, 0)); t.join(LONG_DELAY_MS); assertFalse(t.isAlive()); assertSame(three, ai.getReference()); assertEquals(0, ai.getStamp()); }
/** * compareAndSet in one thread enables another waiting for stamp value * to succeed */ public void testCompareAndSetInMultipleThreads2() throws Exception { final AtomicStampedReference ai = new AtomicStampedReference(one, 0); Thread t = new Thread(new CheckedRunnable() { public void realRun() { while (!ai.compareAndSet(one, one, 1, 2)) Thread.yield(); }}); t.start(); assertTrue(ai.compareAndSet(one, one, 0, 1)); t.join(LONG_DELAY_MS); assertFalse(t.isAlive()); assertSame(one, ai.getReference()); assertEquals(2, ai.getStamp()); }
/** * repeated weakCompareAndSet succeeds in changing values when equal * to expected */ public void testWeakCompareAndSet() { int[] mark = new int[1]; AtomicStampedReference ai = new AtomicStampedReference(one, 0); assertSame(one, ai.get(mark)); assertEquals(0, ai.getStamp()); assertEquals(0, mark[0]); do {} while (!ai.weakCompareAndSet(one, two, 0, 0)); assertSame(two, ai.get(mark)); assertEquals(0, mark[0]); do {} while (!ai.weakCompareAndSet(two, m3, 0, 1)); assertSame(m3, ai.get(mark)); assertEquals(1, mark[0]); }
private AtomicStampedReference<KAryBaseNode> searchKey(int key, KAryInternalNode l){ if(l.keys[0] == Integer.MAX_VALUE) return l.children.get(0); int left = 0; int right = l.keyCount-1; while(right > left){ int mid = (left+right)/2; if( key < l.keys[mid]){ right = mid; } else{ left = mid+1; } } if(left == l.keyCount-1 && (key >= l.keys[left]) ) return l.children.get(l.keyCount); return l.children.get(left); }
public KAryInternalNode(int K, boolean root){ this.keys = new int[K-1]; this.keyCount = K-1; for(int i = 0; i < keyCount; ++i){ this.keys[i] = Integer.MAX_VALUE; } if(root){ this.children = new ArrayList<AtomicStampedReference<KAryBaseNode>>(K); this.children.add(new AtomicStampedReference<KAryBaseNode>(new KAryInternalNode(K,false),0 ) ); for(int i = 1; i < K; ++i ){ this.children.add(new AtomicStampedReference<KAryBaseNode>(new KAryLeafNode(K),0) ); } } else{ this.children = new ArrayList<AtomicStampedReference<KAryBaseNode>>(K); for(int i = 0; i < K; ++i ){ this.children.add(new AtomicStampedReference<KAryBaseNode>(new KAryLeafNode(K),0)); } } }
public KAryInternalNode(int key, KAryBaseNode leaf){ this.keyCount = leaf.keyCount; int i; for(i = 0; i < keyCount; ++i){ if( key < leaf.keys[i]) break; } this.children = new ArrayList<AtomicStampedReference<KAryBaseNode>>(keyCount+1); for(int j = 0; j < i; ++j){ //this.children.set(j, new KAryLeafNode(leaf.keys[j],true)); this.children.set(j, new AtomicStampedReference<KAryBaseNode>(new KAryLeafNode(leaf.keys[j],true),0)); } //this.children.set(i, new KAryLeafNode(key,true)); this.children.set(i, new AtomicStampedReference<KAryBaseNode>(new KAryLeafNode(key,true),0)); for(int j = i ; j < keyCount; ++j){ //this.children.set(j+1, new KAryLeafNode(leaf.keys[j],true)); this.children.set(j+1, new AtomicStampedReference<KAryBaseNode>(new KAryLeafNode(leaf.keys[j],true),0)); } this.keys = new int[keyCount]; int []stamp = new int[1]; for(int j = 0; j < keyCount; ++j){ //this.keys[j] = this.children.get(j+1).keys[0]; this.keys[j] = this.children.get(j+1).get(stamp).keys[0]; } }
public void setRunning() { AtomicStampedReference<State> stateRef = state; int newStamp = stamp.incrementAndGet(); int[] receiver = new int[1]; // Keep trying until stateRef is set with our stamp for (;;) { State was = stateRef.get(receiver); if (was != State.STARTING) { // AS7-1103 only transition to running from STARTING break; } synchronized (service) { State newState = restartRequiredFlag ? State.RESTART_REQUIRED : State.RUNNING; if (state.compareAndSet(was, newState, receiver[0], newStamp)) { service.stateChanged(newState); break; } } } }
public Object setReloadRequired() { if (!reloadSupported) { return setRestartRequired(); } AtomicStampedReference<State> stateRef = state; int newStamp = stamp.incrementAndGet(); int[] receiver = new int[1]; // Keep trying until stateRef is RELOAD_REQUIRED with our stamp for (;;) { State was = stateRef.get(receiver); if (was == State.STARTING || was == State.STOPPING || was == State.RESTART_REQUIRED) { break; } synchronized (service) { if (stateRef.compareAndSet(was, State.RELOAD_REQUIRED, receiver[0], newStamp)) { service.stateChanged(State.RELOAD_REQUIRED); break; } } } return Integer.valueOf(newStamp); }
public Object setRestartRequired() { AtomicStampedReference<State> stateRef = state; int newStamp = stamp.incrementAndGet(); int[] receiver = new int[1]; // Keep trying until stateRef is RESTART_REQUIRED with our stamp for (;;) { State was = stateRef.get(receiver); if (was == State.STARTING || was == State.STOPPING) { break; } synchronized (service) { if (stateRef.compareAndSet(was, State.RESTART_REQUIRED, receiver[0], newStamp)) { restartRequiredFlag = true; service.stateChanged(State.RESTART_REQUIRED); break; } } } return Integer.valueOf(newStamp); }
@Override public WorkloadStatusSnapshot status() throws MetricsCollectionException { if ( null != alreadyShutdownPolicy ) { alreadyShutdownPolicy.apply(); } AtomicStampedReference<WorkloadStatusSnapshot> statusSnapshotReference = eventHandler.statusSnapshot(); int oldStamp = statusSnapshotReference.getStamp(); ringBuffer.publishEvent( getWorkloadStatusTranslator ); while ( statusSnapshotReference.getStamp() <= oldStamp ) { LockSupport.parkNanos( TimeUnit.MILLISECONDS.toNanos( 100 ) ); } return statusSnapshotReference.getReference(); }
@Override public WorkloadResultsSnapshot results() throws MetricsCollectionException { if ( null != alreadyShutdownPolicy ) { alreadyShutdownPolicy.apply(); } AtomicStampedReference<WorkloadResultsSnapshot> resultsSnapshotReference = eventHandler.resultsSnapshot(); int oldStamp = resultsSnapshotReference.getStamp(); ringBuffer.publishEvent( getWorkloadResultsTranslator ); while ( resultsSnapshotReference.getStamp() <= oldStamp ) { LockSupport.parkNanos( TimeUnit.MILLISECONDS.toNanos( 100 ) ); } return resultsSnapshotReference.getReference(); }
@Test public void should_create_error_message_for_AtomicStampedReference() throws Exception { // GIVEN AtomicStampedReference<String> actual = new AtomicStampedReference<>("foo", 123); // WHEN String message = shouldHaveReference(actual, actual.getReference(), "bar").create(TEST_DESCRIPTION, CONFIGURATION_PROVIDER.representation()); // THEN assertThat(message).isEqualTo(format("[TEST] %n" + "Expecting%n" + " <AtomicStampedReference[stamp=123, reference=\"foo\"]>%n" + "to have reference:%n" + " <\"bar\">%n" + "but had:%n" + " <\"foo\">")); }
/** * constructor initializes to given reference and stamp */ public void testConstructor() { AtomicStampedReference ai = new AtomicStampedReference(one, 0); assertSame(one, ai.getReference()); assertEquals(0, ai.getStamp()); AtomicStampedReference a2 = new AtomicStampedReference(null, 1); assertNull(a2.getReference()); assertEquals(1, a2.getStamp()); }
/** * attemptStamp succeeds in single thread */ public void testAttemptStamp() { int[] mark = new int[1]; AtomicStampedReference ai = new AtomicStampedReference(one, 0); assertEquals(0, ai.getStamp()); assertTrue(ai.attemptStamp(one, 1)); assertEquals(1, ai.getStamp()); assertSame(one, ai.get(mark)); assertEquals(1, mark[0]); }
public static void main(String[] args) { AtomicInteger atomicInteger = new AtomicInteger(); System.out.println(atomicInteger.get()); System.out.println(atomicInteger.incrementAndGet()); AtomicStampedReference atomicStampedReference = new AtomicStampedReference(1,0); System.out.println(atomicInteger); }
public boolean containsKey(int key){ if(key == -1) return false; int[] stamp = new int[1]; KAryBaseNode l = root.children.get(0).get(stamp); while( l!= null && !l.isLeaf()){ AtomicStampedReference<KAryBaseNode> baseNode = searchKey(key, (KAryInternalNode)l); l = baseNode.get(stamp); } if(l == null){ return false; } return ((KAryLeafNode)l).hasKey(key); }
private void helpMarked(DeleteInfo info){ int[]stamp = new int[1]; KAryBaseNode other = info.p.children.get(info.p.keyCount).get(stamp); int index = 0; for(int i = 0; i < info.p.keyCount ;++i){ KAryBaseNode u = info.p.children.get(i).getReference(); if( u!= null && u.keyCount > 0 && u != info.oldChild ){ other = u; index = i; break; } } boolean casValue = false; casValue = info.gp.children.get(info.gpindex).compareAndSet(info.p, other, info.parentStamp, info.parentStamp+1); if(casValue){ KAryInternalNode parentNode = (KAryInternalNode)info.p; for(int i = 0; i < K ; ++i){ AtomicStampedReference<KAryBaseNode> recycledStamp; KAryBaseNode recycledNode; if(i != info.pindex){ recycledStamp = parentNode.children.get(i); recycledNode = recycledStamp.getReference(); //recycledStamp.set(null,stamp[0]+1); if(recycledNode != null){ for(int j = 0; j < K-1; ++j ){ recycledNode.keys[j] = Integer.MAX_VALUE; } recycledNode.keyCount = 0; moveToLeafFree(recycledNode); } } } parentNode.info = new Clean(); parentNode.keyCount = 0; moveToInternalFree(parentNode); infoUpdater.compareAndSet((KAryInternalNode)info.gp, info, new Clean()); } }
@SuppressWarnings("unchecked") @Override public AtomicStampedReference readObject(OInput in, Class<AtomicStampedReference> clazz) throws IOException { Object obj = in.readObject(); int stamp = in.readInt(); AtomicStampedReference ref = new AtomicStampedReference(obj, stamp); return ref; }
/** * Creates a Bloom filter that can store up to an expected maximum capacity with an acceptable probability * that a membership query will result in a false positive. The filter will size itself based on the given * parameters. * * @param capacity The expected maximum number of elements to be inserted into the Bloom filter. * @param probability The acceptable false positive probability for membership queries. */ public ConcurrentBloomFilter(int capacity, float probability) { if ((capacity <= 0) || (probability <= 0) || (probability >= 1)) { throw new IllegalArgumentException(); } this.capacity = max(capacity, Long.SIZE); this.bits = bits(capacity, probability); this.length = bits / Long.SIZE; this.lock = new ReentrantLock(); this.hashes = numberOfHashes(capacity, bits); this.probability = probability(hashes, capacity, bits); this.ref = new AtomicStampedReference<long[]>(new long[length], 0); }
/** * Returns standard the {@code toString} representation of the given object. It may or not the object's own * implementation of {@code toString}. * * @param object the given object. * @return the {@code toString} representation of the given object. */ @Override public String toStringOf(Object object) { if (object == null) return null; if (hasCustomFormatterFor(object)) return customFormat(object); if (object instanceof Calendar) return toStringOf((Calendar) object); if (object instanceof Class<?>) return toStringOf((Class<?>) object); if (object instanceof Date) return toStringOf((Date) object); if (object instanceof AtomicBoolean) return toStringOf((AtomicBoolean) object); if (object instanceof AtomicInteger) return toStringOf((AtomicInteger) object); if (object instanceof AtomicLong) return toStringOf((AtomicLong) object); if (object instanceof AtomicReference) return toStringOf((AtomicReference<?>) object); if (object instanceof AtomicMarkableReference) return toStringOf((AtomicMarkableReference<?>) object); if (object instanceof AtomicStampedReference) return toStringOf((AtomicStampedReference<?>) object); if (object instanceof AtomicIntegerFieldUpdater) return AtomicIntegerFieldUpdater.class.getSimpleName(); if (object instanceof AtomicLongFieldUpdater) return AtomicLongFieldUpdater.class.getSimpleName(); if (object instanceof AtomicReferenceFieldUpdater) return AtomicReferenceFieldUpdater.class.getSimpleName(); if (object instanceof Number) return toStringOf((Number) object); if (object instanceof File) return toStringOf((File) object); if (object instanceof String) return toStringOf((String) object); if (object instanceof Character) return toStringOf((Character) object); if (object instanceof Comparator) return toStringOf((Comparator<?>) object); if (object instanceof SimpleDateFormat) return toStringOf((SimpleDateFormat) object); if (object instanceof PredicateDescription) return toStringOf((PredicateDescription) object); if (object instanceof CompletableFuture) return toStringOf((CompletableFuture<?>) object); if (isArray(object)) return formatArray(object); if (object instanceof Collection<?>) return smartFormat((Collection<?>) object); if (object instanceof Map<?, ?>) return toStringOf((Map<?, ?>) object); if (object instanceof Tuple) return toStringOf((Tuple) object); if (object instanceof MapEntry) return toStringOf((MapEntry<?, ?>) object); if (object instanceof Method) return ((Method) object).toGenericString(); if (object instanceof InsertDelta<?>) return toStringOf((InsertDelta<?>) object); if (object instanceof ChangeDelta<?>) return toStringOf((ChangeDelta<?>) object); if (object instanceof DeleteDelta<?>) return toStringOf((DeleteDelta<?>) object); return object == null ? null : fallbackToStringOf(object); }
@Test public void should_create_error_message() throws Exception { // GIVEN AtomicStampedReference<String> actual = new AtomicStampedReference<>("foo", 1234); // WHEN String message = shouldHaveStamp(actual, 5678).create(new TestDescription("TEST"), CONFIGURATION_PROVIDER.representation()); // THEN assertThat(message).isEqualTo(format("[TEST] %n" + "Expecting%n" + " <AtomicStampedReference[stamp=1234, reference=\"foo\"]>%n" + "to have stamp:%n" + " <5678>%n" + "but had:%n" + " <1234>")); }
@Test public void should_fail_if_expected_value_is_null_and_does_not_contains_expected_value() throws Exception { AtomicStampedReference<String> actual = new AtomicStampedReference<>("actual", 1234); thrown.expectAssertionError(shouldHaveReference(actual, actual.getReference(), null).create()); assertThat(actual).hasReference(null); }
@Test public void should_fail_if_atomicStampedReference_does_not_contain_expected_value() throws Exception { AtomicStampedReference<String> actual = new AtomicStampedReference<>("actual", 1234); thrown.expectAssertionError(shouldHaveReference(actual, actual.getReference(), expectedValue).create()); assertThat(actual).hasReference(expectedValue); }
@Test public void should_fail_if_atomicStampedReference_contains_expected_value_and_hasStamp_does_not() throws Exception { int actualStamp = 1234; int expectedStamp = 5678; AtomicStampedReference<String> actual = new AtomicStampedReference<>(expectedValue, actualStamp); thrown.expectAssertionError(shouldHaveStamp(actual, expectedStamp).create()); assertThat(actual).hasReference(expectedValue).hasStamp(expectedStamp); }
UpdateInfo(Info info, int state){ infoRef = new AtomicStampedReference<Info>(info, state); }
@Override public void writeObject(OOutput out, AtomicStampedReference ref) throws IOException { out.writeObject(ref.getReference()); out.writeInt(ref.getStamp()); }
AtomicStampedReference<WorkloadStatusSnapshot> statusSnapshot() { return statusSnapshotReference; }
AtomicStampedReference<WorkloadResultsSnapshot> resultsSnapshot() { return resultsSnapshotReference; }
private ShouldHaveStamp(AtomicStampedReference<?> actual, int expectedStamp) { super(SHOULD_HAVE_STAMP, actual, expectedStamp, actual.getStamp()); }
public static ErrorMessageFactory shouldHaveStamp(AtomicStampedReference<?> actual, int expectedStamp) { return new ShouldHaveStamp(actual, expectedStamp); }
public AtomicStampedReferenceAssert(AtomicStampedReference<VALUE> actual) { super(actual, AtomicStampedReferenceAssert.class, true); }