Skip to content
Open

HW2 #258

Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions csc/2019/2.LockFreeSet/EslikovAV/LockFreeSetImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicMarkableReference;

public class LockFreeSetImpl<T extends Comparable<T>> implements LockFreeSet<T> {

private final Node head;

public LockFreeSetImpl() {
this.head = new Node();
}

@Override
public boolean add(T value) {
while (true) {
Window window = find(value);
if (window.current != null && window.current.value != null && window.current.value.compareTo(value) == 0) {
return false;
} else {
Node node = new Node(value, window.current);
if (window.previous.next.compareAndSet(window.current, node, false, false)) {
return true;
}
}
}
}

@Override
public boolean remove(T value) {
while (true) {
Window window = find(value);
if (window.current == null || window.current.value.compareTo(value) != 0) {
return false;
} else {
Node successor = window.current.next.getReference();
if (!window.current.next.attemptMark(successor, true)) {
continue;
} else {
window.previous.next.compareAndSet(window.current, successor, false, false);
return true;
}
}
}
}

@Override
public boolean contains(T value) {
Node current = head.next.getReference();

while (current != null
&& current.value.compareTo(value) < 0) {
current = current.next.getReference();
}

return current != null
&& !current.next.isMarked()
&& current.value.compareTo(value) == 0;
}

@Override
public boolean isEmpty() {
return head.next.getReference() == null;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

достаточно ли этого?

}

@Override
public Iterator<T> iterator() {
return null;
}

private Window find(T value) {
while (true) {
Node previous = head;
Node current = previous.next.getReference();
Node successor;

while (true) {
if (current == null) {
return new Window(previous, null);
}
successor = current.next.getReference();
if (current.next.isMarked()) {
if (!previous.next.compareAndSet(current, successor, false, false)) {
break;
}
current = successor;

} else {
if (current.value.compareTo(value) > 0) {
return new Window(previous, current);
}
previous = current;
current = successor;
}
}
}
}

private final class Window {
private final Node previous;
private final Node current;

Window(Node previous, Node current) {
this.previous = previous;
this.current = current;

}
}

private final class Node {
private final T value;
private final AtomicMarkableReference<Node> next;

Node() {
this.value = null;
this.next = new AtomicMarkableReference<>(null, false);
}

Node(T value, Node next) {
this.value = value;
this.next = new AtomicMarkableReference<>(next, false);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import com.devexperts.dxlab.lincheck.LinChecker;
import com.devexperts.dxlab.lincheck.LoggingLevel;
import com.devexperts.dxlab.lincheck.Options;
import com.devexperts.dxlab.lincheck.annotations.Operation;
import com.devexperts.dxlab.lincheck.annotations.Param;
import com.devexperts.dxlab.lincheck.paramgen.IntGen;
import com.devexperts.dxlab.lincheck.strategy.stress.StressOptions;
import org.junit.Test;

@Param(name = "key", gen = IntGen.class)
public class LockFreeSetLinearizabilityTest {
private LockFreeSetImpl<Integer> set = new LockFreeSetImpl<>();

@Operation
public Boolean put(@Param(name = "key") int key) {
return set.add(key);
}

@Operation
public Boolean delete(@Param(name = "key") int key) {
return set.remove(key);
}

@Operation
public Boolean contains(@Param(name = "key") int key) {
return set.contains(key);
}

@Operation
public Boolean isEmpty() {
return set.isEmpty();
}

@Test
public void test() {
Options opts = new StressOptions()
.iterations(5)
.threads(2)
.logLevel(LoggingLevel.DEBUG);
LinChecker.check(LockFreeSetLinearizabilityTest.class, opts);
}
}