Concurrent Stacks презентация

Содержание

Слайд 2

Outline

Quick reminder of the Stack structure.
The Unbounded Lock-Free Stack.
The Elimination Backoff Stack.

Слайд 3

Concurrent Stack

The Stack class is a collection of items (of type T) that

provides the following methods:
push(x)
pop()
Satisfying the Last-In-First-Out (LIFO) property:
The last item pushed is the first popped.

Слайд 4

Empty Stack

Top

Слайд 6

Push

Top

CAS

Слайд 10

Push

Top

CAS

Слайд 13

Pop

Top

CAS

Слайд 14

Pop

Top

CAS

mine!

Слайд 15

Pop

Top

CAS

Слайд 17

The LockfreeStack class

The lock-free stack is a linked list, where the top field

points to the first node (or null if the stack is empty).
A pop() call uses compareAndSet() to try to remove the first node from the stack.
A push() call uses compareAndSet() to try to insert a new node into the top of the stack.

Слайд 18

public class LockFreeStack {
private AtomicReference top = new AtomicReference(null);
static final

int MIN_DELAY = …;
static final int MAX_DELAY = …;
Backoff backoff = new Backoff(MIN_DELAY, MAX_DELAY);
public boolean tryPush(Node node){
Node oldTop = top.get();
node.next = oldTop;
return(top.compareAndSet(oldTop, node))
}
public void push(T value) {
Node node = new Node(value);
while (true) {
if (tryPush(node)) {
return;
} else backoff.backoff();
}
}

Слайд 19

public boolean tryPop() throws EmptyException {
Node oldTop = top.get();
if (oldTop ==

null) {
throw new EmptyException();
}
Node newTop = oldTop.next;
if (top.compareAndSet(oldTop, newTop)) {
return oldTop;
} else {
return null;
}
}
public T pop() throws EmptyException {
while (true) {
Node returnNode = tryPop();
if (returnNode != null) {
return returnNode.value;
} else backoff.backoff();
}

Слайд 20

Lock-free Stack

Good
No locking
Bad
huge contention at top
No parallelism

Слайд 21

Elimination-Backoff Stack

Ways to solve it :
exponential backoff (reduces contention but does not solve

the bottleneck problem).
elimination backoff

The LockFreeStack implementation scales poorly, not so much because the stack’s top field is a source of contention, but primarily because it is a sequential bottleneck.

Слайд 22

Observation

Push( )

Pop()

linearizable stack

After an equal number
of pushes and pops,
stack stays the

same

Yes!

Слайд 23

Idea: Elimination Array

Push( )

Pop()

stack

Pick at
random

Pick at
random

Elimination
Array

Слайд 24

Push Collides With Pop

Push( )

Pop()

stack

continue

No need to
access stack

Yes!

Слайд 25

No Collision

Push( )

Pop()

stack

If no collision,
access stack

If pushes collide or pops collide

access stack

Слайд 26

Elimination-Backoff Stack

A union of the LockFreeStack class with the elimination array
Access Lock-free stack,


If uncontended, apply operation
if contended, back off to elimination array and attempt elimination

Слайд 27

Elimination-Backoff Stack

Push( )

Pop()

CAS

If CAS fails, back off

Слайд 28

Dynamic Range and Delay

Push( )

Pick random range and max time to wait for

collision based on level of
contention encountered

Слайд 29

Linearizability

The combined data structure, array, and shared stack, is linearizable because the shared

stack is linearizable, and the eliminated calls can be ordered as if they happened at the point in which they exchanged values.
Un-eliminated calls
linearized as before
Eliminated calls:
linearize pop() immediately after matching push()
Combination is a linearizable stack

Слайд 30

Un-Eliminated Linearizability

push(v1)

linearizable

pop(v1)

Слайд 31

Eliminated Linearizability

pop(v2)

push(v1)

push(v2)

linearizable

pop(v2)

pop(v1)

Red calls are
eliminated

Слайд 32

Backoff Has Dual Effect

Elimination introduces parallelism
Backoff onto array cuts contention on lock-free stack
Elimination

in array cuts down total number of threads ever accessing lock-free stack

Слайд 33

public class EliminationArray {
private static final int duration = ...;
private static

final int timeUnit = ...;
Exchanger[] exchanger;
public EliminationArray(int capacity) {
exchanger = new Exchanger[capacity];
for (int i = 0; i < capacity; i++)
exchanger[i] = new Exchanger();

}

}

Elimination Array

Слайд 34

public class Exchanger {
AtomicStampedReference slot
= new AtomicStampedReference(null, 0);

A Lock-Free Exchanger

Слайд 35

Atomic Stamped Reference

address

S

Stamp

Reference

Слайд 36

Exchanger Status

enum Status {EMPTY, WAITING, BUSY};

Слайд 37

Lock-free Exchanger

Слайд 38

Lock-free Exchanger

CAS

Слайд 39

Lock-free Exchanger

Слайд 40

Lock-free Exchanger

In search of partner …

Слайд 41

Lock-free Exchanger

Slot

Still waiting …

Try to exchange item and set state to BUSY

CAS

Слайд 42

Lock-free Exchanger

Slot

Partner showed up, take item and reset to EMPTY

item

stamp/state

Слайд 43

Lock-free Exchanger

Slot

item

stamp/state

Partner showed up, take item and reset to EMPTY

Слайд 44

The Exchanger Slot

Exchanger is lock-free
Because the only way an exchange can fail

is if others repeatedly succeeded or no-one showed up

Слайд 45

public class EliminationArray {

public T visit(T value, int Range) throws TimeoutException {
int

slot = random.nextInt(Range);
int nanodur = convertToNanos(duration, timeUnit));
return (exchanger[slot].exchange(value, nanodur)
}}

Elimination Array

Слайд 46

public void push(T value) {
...
while (true) {
if (tryPush(node)) {
return;

} else try {
T otherValue = eliminationArray.visit(value,policy.Range);
if (otherValue == null) {
return;
}
}

Elimination Stack Push

Слайд 47

public T pop() {
...
while (true) {
if (tryPop()) {

return returnNode.value;
} else
try {
T otherValue =
eliminationArray.visit(null,policy.Range);
if (otherValue != null) {
return otherValue;
}
}
}}

Elimination Stack Pop

Слайд 48

Summary

Quick reminder of the Stack structure.
The Unbounded Lock-Free Stack.
The Elimination Backoff Stack.

Имя файла: Concurrent-Stacks.pptx
Количество просмотров: 103
Количество скачиваний: 0