-
Notifications
You must be signed in to change notification settings - Fork 119
HW2 #258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AlexEslikov
wants to merge
2
commits into
cscenter:master
Choose a base branch
from
AlexEslikov:hw_2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
HW2 #258
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
|
||
| @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); | ||
| } | ||
| } | ||
| } | ||
42 changes: 42 additions & 0 deletions
42
csc/2019/2.LockFreeSet/EslikovAV/LockFreeSetLinearizabilityTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
достаточно ли этого?