Compare commits

..

No commits in common. "25297b686fe43cf2e93cb3cf6e4aead6ec956816" and "bde868ad4746fca3a867774a0bc5b2594b998c26" have entirely different histories.

2 changed files with 86 additions and 133 deletions

View File

@ -1,144 +1,97 @@
package pp; package pp;
import java.util.concurrent.locks.Lock; public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
import java.util.concurrent.locks.ReentrantLock; private Node<T> first;
private final Object headLock = new Object();
public class ThreadsafeSimplifiedList<T> private static class Node<U> {
implements SimplifiedList<T> { U element;
Node<U> prev;
Node<U> next;
final Object lock = new Object();
private Node<T> head; Node(U element, Node<U> prev, Node<U> next) {
private final Lock listLock = new ReentrantLock();
private class Node<U> {
private U element;
private Node<U> next;
private final Lock nodeLock;
private Node(U element, Node<U> prev, Node<U> next) {
super();
this.element = element; this.element = element;
this.prev = prev;
this.next = next; this.next = next;
this.nodeLock = new ReentrantLock();
} }
} }
/** public ThreadsafeSimplifiedList() {
* Returns the element at the specified position in this list. this.first = null;
*
* @param index index of the element to return
* @return the element at the specified position in this list
*/
@Override
public T get(int index) {
this.listLock.lock();
var ptr = this.head;
if (ptr == null) {
this.listLock.unlock();
throw new IndexOutOfBoundsException(index + " out of bounds (empty list)");
}
ptr.nodeLock.lock();
this.listLock.unlock();
for (var i = 0; i < index; i++) {
if (ptr.next != null) {
ptr.next.nodeLock.lock();
var savePtr = ptr;
ptr = ptr.next;
savePtr.nodeLock.unlock();
} else {
ptr.nodeLock.unlock();
throw new IndexOutOfBoundsException(index + " out of bounds");
}
} }
try {
return delay(ptr.element);
} finally {
ptr.nodeLock.unlock();
}
}
/**
* Appends the specified element to the end of this list. There are no
* limitations on what elements may be added to this list.
*
* @param element element to be appended to this list
* @return true
* @see java.util.Collection#add(Object)
*
*/
@Override @Override
public boolean add(T element) { public boolean add(T element) {
this.listLock.lock(); Node<T> curr;
if (this.head != null) {
var ptr = this.head; synchronized (headLock) {
ptr.nodeLock.lock(); if (first == null) {
this.listLock.unlock(); first = new Node<>(element, null, null);
while (ptr.next != null) {
ptr.next.nodeLock.lock();
var savePtr = ptr;
ptr = ptr.next;
savePtr.nodeLock.unlock();
}
ptr.next = new Node<>(element, ptr, null);
ptr.nodeLock.unlock();
} else {
this.head = new Node<>(element, null, null);
this.listLock.unlock();
}
return true; return true;
} }
curr = first;
}
while (true) {
Node<T> next;
synchronized (curr.lock) {
next = curr.next;
if (next == null) {
curr.next = new Node<>(element, curr, null);
return true;
}
}
curr = next;
}
}
@Override
public T get(int index) {
Node<T> curr;
synchronized (headLock) {
if (first == null) throw new IndexOutOfBoundsException();
curr = first;
}
synchronized (curr.lock) {
for (int i = 0; i < index; i++) {
Node<T> next = curr.next;
if (next == null) throw new IndexOutOfBoundsException();
synchronized (next.lock) {
curr = next;
}
}
return delay(curr.element);
}
}
/**
* Replaces the element at the specified position in this list with the
* specified element.
*
* @param index index of the element to replace
* @param element element to be stored at the specified position
* @return the element previously at the specified position
*/
@Override @Override
public T set(int index, T element) { public T set(int index, T element) {
this.listLock.lock(); Node<T> curr;
var ptr = this.head; synchronized (headLock) {
if (ptr == null) { if (first == null) throw new IndexOutOfBoundsException();
this.listLock.unlock(); curr = first;
throw new IndexOutOfBoundsException(index + " out of bounds (empty list)");
}
ptr.nodeLock.lock();
this.listLock.unlock();
for (var i = 0; i < index; i++) {
if (ptr.next != null) {
ptr.next.nodeLock.lock();
var savePtr = ptr;
ptr = ptr.next;
savePtr.nodeLock.unlock();
} else {
ptr.nodeLock.unlock();
throw new IndexOutOfBoundsException(index + " out of bounds");
}
} }
try { synchronized (curr.lock) {
T old = ptr.element; for (int i = 0; i < index; i++) {
ptr.element = element; Node<T> next = curr.next;
if (next == null) throw new IndexOutOfBoundsException();
synchronized (next.lock) {
curr = next;
}
}
T old = curr.element;
curr.element = element;
return old; return old;
} finally {
ptr.nodeLock.unlock();
} }
} }
/**
* Returns true if this list contains no elements.
*
* @return true if this list contains no elements
*/
@Override @Override
public boolean isEmpty() { public boolean isEmpty() {
this.listLock.lock(); synchronized (headLock) {
try { return first == null;
return this.head == null;
} finally {
this.listLock.unlock();
} }
} }
} }