java.util.concurrent.atomic.AtomicMarkableReference Java Examples

The following examples show how to use java.util.concurrent.atomic.AtomicMarkableReference. 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: AtomicMarkableReferenceTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * get returns the last values of reference and mark set
 */
public void testGetSet() {
    boolean[] mark = new boolean[1];
    AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
    assertSame(one, ai.getReference());
    assertFalse(ai.isMarked());
    assertSame(one, ai.get(mark));
    assertFalse(mark[0]);
    ai.set(two, false);
    assertSame(two, ai.getReference());
    assertFalse(ai.isMarked());
    assertSame(two, ai.get(mark));
    assertFalse(mark[0]);
    ai.set(one, true);
    assertSame(one, ai.getReference());
    assertTrue(ai.isMarked());
    assertSame(one, ai.get(mark));
    assertTrue(mark[0]);
}
 
Example #2
Source File: AtomicMarkableReferenceTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * repeated weakCompareAndSet succeeds in changing values when equal
 * to expected
 */
public void testWeakCompareAndSet() {
    boolean[] mark = new boolean[1];
    AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
    assertSame(one, ai.get(mark));
    assertFalse(ai.isMarked());
    assertFalse(mark[0]);

    do {} while (!ai.weakCompareAndSet(one, two, false, false));
    assertSame(two, ai.get(mark));
    assertFalse(mark[0]);

    do {} while (!ai.weakCompareAndSet(two, m3, false, true));
    assertSame(m3, ai.get(mark));
    assertTrue(mark[0]);
}
 
Example #3
Source File: AtomicMarkableReferenceTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * get returns the last values of reference and mark set
 */
public void testGetSet() {
    boolean[] mark = new boolean[1];
    AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
    assertSame(one, ai.getReference());
    assertFalse(ai.isMarked());
    assertSame(one, ai.get(mark));
    assertFalse(mark[0]);
    ai.set(two, false);
    assertSame(two, ai.getReference());
    assertFalse(ai.isMarked());
    assertSame(two, ai.get(mark));
    assertFalse(mark[0]);
    ai.set(one, true);
    assertSame(one, ai.getReference());
    assertTrue(ai.isMarked());
    assertSame(one, ai.get(mark));
    assertTrue(mark[0]);
}
 
Example #4
Source File: AtomicMarkableReferenceTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * compareAndSet in one thread enables another waiting for mark value
 * to succeed
 */
public void testCompareAndSetInMultipleThreads2() throws Exception {
    final AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
    Thread t = new Thread(new CheckedRunnable() {
        public void realRun() {
            while (!ai.compareAndSet(one, one, true, false))
                Thread.yield();
        }});

    t.start();
    assertTrue(ai.compareAndSet(one, one, false, true));
    t.join(LONG_DELAY_MS);
    assertFalse(t.isAlive());
    assertSame(one, ai.getReference());
    assertFalse(ai.isMarked());
}
 
Example #5
Source File: AtomicMarkableReferenceTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * compareAndSet succeeds in changing values if equal to expected reference
 * and mark else fails
 */
public void testCompareAndSet() {
    boolean[] mark = new boolean[1];
    AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
    assertSame(one, ai.get(mark));
    assertFalse(ai.isMarked());
    assertFalse(mark[0]);

    assertTrue(ai.compareAndSet(one, two, false, false));
    assertSame(two, ai.get(mark));
    assertFalse(mark[0]);

    assertTrue(ai.compareAndSet(two, m3, false, true));
    assertSame(m3, ai.get(mark));
    assertTrue(mark[0]);

    assertFalse(ai.compareAndSet(two, m3, true, true));
    assertSame(m3, ai.get(mark));
    assertTrue(mark[0]);
}
 
Example #6
Source File: AtomicMarkableReferenceTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * compareAndSet in one thread enables another waiting for reference value
 * to succeed
 */
public void testCompareAndSetInMultipleThreads() throws Exception {
    final AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
    Thread t = new Thread(new CheckedRunnable() {
        public void realRun() {
            while (!ai.compareAndSet(two, three, false, false))
                Thread.yield();
        }});

    t.start();
    assertTrue(ai.compareAndSet(one, two, false, false));
    t.join(LONG_DELAY_MS);
    assertFalse(t.isAlive());
    assertSame(three, ai.getReference());
    assertFalse(ai.isMarked());
}
 
Example #7
Source File: AtomicMarkableReferenceTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * compareAndSet in one thread enables another waiting for mark value
 * to succeed
 */
public void testCompareAndSetInMultipleThreads2() throws Exception {
    final AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
    Thread t = new Thread(new CheckedRunnable() {
        public void realRun() {
            while (!ai.compareAndSet(one, one, true, false))
                Thread.yield();
        }});

    t.start();
    assertTrue(ai.compareAndSet(one, one, false, true));
    t.join(LONG_DELAY_MS);
    assertFalse(t.isAlive());
    assertSame(one, ai.getReference());
    assertFalse(ai.isMarked());
}
 
Example #8
Source File: AtomicMarkableReferenceTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * repeated weakCompareAndSet succeeds in changing values when equal
 * to expected
 */
public void testWeakCompareAndSet() {
    boolean[] mark = new boolean[1];
    AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
    assertSame(one, ai.get(mark));
    assertFalse(ai.isMarked());
    assertFalse(mark[0]);

    do {} while (!ai.weakCompareAndSet(one, two, false, false));
    assertSame(two, ai.get(mark));
    assertFalse(mark[0]);

    do {} while (!ai.weakCompareAndSet(two, m3, false, true));
    assertSame(m3, ai.get(mark));
    assertTrue(mark[0]);
}
 
Example #9
Source File: AtomicMarkableReferenceTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * compareAndSet in one thread enables another waiting for reference value
 * to succeed
 */
public void testCompareAndSetInMultipleThreads() throws Exception {
    final AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
    Thread t = new Thread(new CheckedRunnable() {
        public void realRun() {
            while (!ai.compareAndSet(two, three, false, false))
                Thread.yield();
        }});

    t.start();
    assertTrue(ai.compareAndSet(one, two, false, false));
    t.join(LONG_DELAY_MS);
    assertFalse(t.isAlive());
    assertSame(three, ai.getReference());
    assertFalse(ai.isMarked());
}
 
Example #10
Source File: LockFreeList.java    From TAOMP with MIT License 6 votes vote down vote up
@Override
public boolean add(T item) {
    int key = item.hashCode();
    while (true) {
        Window window = find(head, key);
        Node<T> pred = window.pred, curr = window.curr;
        if (curr.key == key) {
            return false;
        } else {
            Node<T> node = new Node<>(item);
            node.next = new AtomicMarkableReference<>(curr, false);
            if (pred.next.compareAndSet(curr, node, false, false)) {
                return true;
            }
        }
    }
}
 
Example #11
Source File: AtomicMarkableReferenceTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * compareAndSet succeeds in changing values if equal to expected reference
 * and mark else fails
 */
public void testCompareAndSet() {
    boolean[] mark = new boolean[1];
    AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
    assertSame(one, ai.get(mark));
    assertFalse(ai.isMarked());
    assertFalse(mark[0]);

    assertTrue(ai.compareAndSet(one, two, false, false));
    assertSame(two, ai.get(mark));
    assertFalse(mark[0]);

    assertTrue(ai.compareAndSet(two, m3, false, true));
    assertSame(m3, ai.get(mark));
    assertTrue(mark[0]);

    assertFalse(ai.compareAndSet(two, m3, true, true));
    assertSame(m3, ai.get(mark));
    assertTrue(mark[0]);
}
 
Example #12
Source File: Chunk.java    From Oak with Apache License 2.0 6 votes vote down vote up
/**
 * This constructor is only used internally to instantiate a Chunk without a creator and a min-key.
 * The caller should set the creator and min-key before returning the Chunk to the user.
 */
private Chunk(int maxItems, AtomicInteger externalSize, MemoryManager memoryManager, OakComparator<K> comparator,
              OakSerializer<K> keySerializer, OakSerializer<V> valueSerializer, ValueUtils valueOperator) {
    this.maxItems = maxItems;
    this.externalSize = externalSize;
    this.comparator = comparator;
    this.entrySet = new EntrySet<>(memoryManager, maxItems, keySerializer, valueSerializer, valueOperator);
    // if not zero, sorted count keeps the entry index of the last
    // subsequent and ordered entry in the entries array
    this.sortedCount = new AtomicInteger(0);
    this.minKey = new KeyBuffer();
    this.creator = new AtomicReference<>(null);
    this.state = new AtomicReference<>(State.NORMAL);
    this.next = new AtomicMarkableReference<>(null, false);
    this.pendingOps = new AtomicInteger();
    this.rebalancer = new AtomicReference<>(null); // to be updated on rebalance
    this.statistics = new Statistics();
}
 
Example #13
Source File: LockFreeDeque.java    From lin-check with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void pushRight(T x) {
    Node node = new Node(x, null, null);
    Node next = tail;
    Node prev = next.prev.getReference();
    while (true) {
        if (!prev.next.compareAndSet(next, next, false, false)) {
            // concurrent push inserted -> get new prev
            prev = helpInsert(prev, next, "concurrentPushRight");
            continue;
        }
        // 0 push step
        node.prev = new AtomicMarkableReference<>(prev, false);
        node.next = new AtomicMarkableReference<>(next, false);
        // 1 push step
        if (prev.next.compareAndSet(next, node, false, false)) {
            break;
        }
    }
    // 2 push step
    pushCommon(node, next);
}
 
Example #14
Source File: LockFreeDeque.java    From lin-check with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void pushLeft(T x) {
    Node node = new Node(x, null, null);
    Node prev = head;
    Node next = prev.next.getReference();
    while (true) {
        if (!prev.next.compareAndSet(next, next, false, false)) {
            next = prev.next.getReference();
            continue;
        }
        node.prev = new AtomicMarkableReference<>(prev, false);
        node.next = new AtomicMarkableReference<>(next, false);

        if (prev.next.compareAndSet(next, node, false, false)) {
            break;
        }
    }
    pushCommon(node, next);
}
 
Example #15
Source File: AtomicMarkableReferenceUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
void givenTheSameObjectReference_whenUsingAttemptMarkMethod_thenMarkShouldBeUpdated() {
    Employee employee = new Employee(123, "Mike");
    AtomicMarkableReference<Employee> employeeNode = new AtomicMarkableReference<Employee>(employee, true);

    Assertions.assertTrue(employeeNode.attemptMark(employee, false));
    Assertions.assertFalse(employeeNode.isMarked());
}
 
Example #16
Source File: AtomicMarkableReferenceUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
void givenNotCurrentReferenceAndCurrentMark_whenUsingCompareAndSet_thenReferenceAndMarkShouldNotBeUpdated() {
    Employee employee = new Employee(123, "Mike");
    AtomicMarkableReference<Employee> employeeNode = new AtomicMarkableReference<Employee>(employee, true);
    Employee newEmployee = new Employee(124, "John");

    Assertions.assertFalse(employeeNode.compareAndSet(new Employee(1234, "Steve"), newEmployee, true, false));
    Assertions.assertEquals(employee, employeeNode.getReference());
    Assertions.assertTrue(employeeNode.isMarked());
}
 
Example #17
Source File: AtomicMarkableReferenceUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
void givenNewReferenceAndMark_whenUsingSetMethod_thenCurrentReferenceAndMarkShouldBeUpdated() {
    Employee employee = new Employee(123, "Mike");
    AtomicMarkableReference<Employee> employeeNode = new AtomicMarkableReference<Employee>(employee, true);

    Employee newEmployee = new Employee(124, "John");
    employeeNode.set(newEmployee, false);

    Assertions.assertEquals(newEmployee, employeeNode.getReference());
    Assertions.assertFalse(employeeNode.isMarked());
}
 
Example #18
Source File: AtomicMarkableReferenceUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
void whenUsingGetMethod_thenCurrentReferenceAndMarkShouldBeReturned() {
    Employee employee = new Employee(123, "Mike");
    AtomicMarkableReference<Employee> employeeNode = new AtomicMarkableReference<Employee>(employee, true);

    boolean[] markHolder = new boolean[1];
    Employee currentEmployee = employeeNode.get(markHolder);

    Assertions.assertEquals(employee, currentEmployee);
    Assertions.assertTrue(markHolder[0]);
}
 
Example #19
Source File: AtomicMarkableReferenceUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
void givenDifferentObjectReference_whenUsingAttemptMarkMethod_thenMarkShouldNotBeUpdated() {
    Employee employee = new Employee(123, "Mike");
    AtomicMarkableReference<Employee> employeeNode = new AtomicMarkableReference<Employee>(employee, true);
    Employee expectedEmployee = new Employee(123, "Mike");

    Assertions.assertFalse(employeeNode.attemptMark(expectedEmployee, false));
    Assertions.assertTrue(employeeNode.isMarked());
}
 
Example #20
Source File: AtomicMarkableReferenceUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
void givenCurrentReferenceAndCurrentMark_whenUsingCompareAndSet_thenReferenceAndMarkShouldBeUpdated() {
    Employee employee = new Employee(123, "Mike");
    AtomicMarkableReference<Employee> employeeNode = new AtomicMarkableReference<Employee>(employee, true);
    Employee newEmployee = new Employee(124, "John");

    Assertions.assertTrue(employeeNode.compareAndSet(employee, newEmployee, true, false));
    Assertions.assertEquals(newEmployee, employeeNode.getReference());
    Assertions.assertFalse(employeeNode.isMarked());
}
 
Example #21
Source File: AtomicMarkableReferenceTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * attemptMark succeeds in single thread
 */
public void testAttemptMark() {
    boolean[] mark = new boolean[1];
    AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
    assertFalse(ai.isMarked());
    assertTrue(ai.attemptMark(one, true));
    assertTrue(ai.isMarked());
    assertSame(one, ai.get(mark));
    assertTrue(mark[0]);
}
 
Example #22
Source File: AtomicMarkableReferenceUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
void givenCurrentReferenceAndNotCurrentMark_whenUsingCompareAndSet_thenReferenceAndMarkShouldNotBeUpdated() {
    Employee employee = new Employee(123, "Mike");
    AtomicMarkableReference<Employee> employeeNode = new AtomicMarkableReference<Employee>(employee, true);
    Employee newEmployee = new Employee(124, "John");

    Assertions.assertFalse(employeeNode.compareAndSet(employee, newEmployee, false, true));
    Assertions.assertEquals(employee, employeeNode.getReference());
    Assertions.assertTrue(employeeNode.isMarked());
}
 
Example #23
Source File: AtomicMarkableReferenceUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
void givenNotCurrentReferenceAndNotCurrentMark_whenUsingCompareAndSet_thenReferenceAndMarkShouldNotBeUpdated() {
    Employee employee = new Employee(123, "Mike");
    AtomicMarkableReference<Employee> employeeNode = new AtomicMarkableReference<Employee>(employee, true);
    Employee newEmployee = new Employee(124, "John");

    Assertions.assertFalse(employeeNode.compareAndSet(new Employee(1234, "Steve"), newEmployee, false, true));
    Assertions.assertEquals(employee, employeeNode.getReference());
    Assertions.assertTrue(employeeNode.isMarked());
}
 
Example #24
Source File: AtomicMarkableReferenceUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
void givenCurrentReferenceAndCurrentMark_whenUsingWeakCompareAndSet_thenReferenceAndMarkShouldBeUpdated() {
    Employee employee = new Employee(123, "Mike");
    AtomicMarkableReference<Employee> employeeNode = new AtomicMarkableReference<Employee>(employee, true);
    Employee newEmployee = new Employee(124, "John");

    Assertions.assertTrue(employeeNode.weakCompareAndSet(employee, newEmployee, true, false));
    Assertions.assertEquals(newEmployee, employeeNode.getReference());
    Assertions.assertFalse(employeeNode.isMarked());
}
 
Example #25
Source File: AtomicMarkableReferenceUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
void givenNotCurrentReferenceAndCurrentMark_whenUsingWeakCompareAndSet_thenReferenceAndMarkShouldNotBeUpdated() {
    Employee employee = new Employee(123, "Mike");
    AtomicMarkableReference<Employee> employeeNode = new AtomicMarkableReference<Employee>(employee, true);
    Employee newEmployee = new Employee(124, "John");

    Assertions.assertFalse(employeeNode.weakCompareAndSet(new Employee(1234, "Steve"), newEmployee, true, false));
    Assertions.assertEquals(employee, employeeNode.getReference());
    Assertions.assertTrue(employeeNode.isMarked());
}
 
Example #26
Source File: AtomicMarkableReferenceUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
void givenCurrentReferenceAndNotCurrentMark_whenUsingWeakCompareAndSet_thenReferenceAndMarkShouldNotBeUpdated() {
    Employee employee = new Employee(123, "Mike");
    AtomicMarkableReference<Employee> employeeNode = new AtomicMarkableReference<Employee>(employee, true);
    Employee newEmployee = new Employee(124, "John");

    Assertions.assertFalse(employeeNode.weakCompareAndSet(employee, newEmployee, false, true));
    Assertions.assertEquals(employee, employeeNode.getReference());
    Assertions.assertTrue(employeeNode.isMarked());
}
 
Example #27
Source File: AtomicMarkableReferenceUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
void givenNotCurrentReferenceAndNotCurrentMark_whenUsingWeakCompareAndSet_thenReferenceAndMarkShouldNotBeUpdated() {
    Employee employee = new Employee(123, "Mike");
    AtomicMarkableReference<Employee> employeeNode = new AtomicMarkableReference<Employee>(employee, true);
    Employee newEmployee = new Employee(124, "John");

    Assertions.assertFalse(employeeNode.weakCompareAndSet(new Employee(1234, "Steve"), newEmployee, false, true));
    Assertions.assertEquals(employee, employeeNode.getReference());
    Assertions.assertTrue(employeeNode.isMarked());
}
 
Example #28
Source File: AtomicMarkableReferenceUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
void whenUsingGetReferenceMethod_thenCurrentReferenceShouldBeReturned() {
    Employee employee = new Employee(123, "Mike");
    AtomicMarkableReference<Employee> employeeNode = new AtomicMarkableReference<Employee>(employee, true);

    Assertions.assertEquals(employee, employeeNode.getReference());
}
 
Example #29
Source File: AtomicMarkableReferenceUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
void givenMarkValueAsFalse_whenUsingIsMarkedMethod_thenMarkValueShouldBeFalse() {
    Employee employee = new Employee(123, "Mike");
    AtomicMarkableReference<Employee> employeeNode = new AtomicMarkableReference<Employee>(employee, false);

    Assertions.assertFalse(employeeNode.isMarked());
}
 
Example #30
Source File: AtomicMarkableReferenceUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
void givenMarkValueAsTrue_whenUsingIsMarkedMethod_thenMarkValueShouldBeTrue() {
    Employee employee = new Employee(123, "Mike");
    AtomicMarkableReference<Employee> employeeNode = new AtomicMarkableReference<Employee>(employee, true);

    Assertions.assertTrue(employeeNode.isMarked());
}