Queue locks,( Array based locks, CLH, MCS Queue locks, composite locks) Flashcards

1
Q

Why are queue locks good?

A

Cache-coherence traffic is reduced since each thread spins on a different location.
increase CS utilization
fair

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

variables in Alock class

A

class ALock implements Lock {
boolean[] flags={true,false,…,false};
AtomicInteger tail
= new AtomicInteger(0);
ThreadLocal<Integer> mySlot;</Integer>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Lock code for Alock

A

public lock() {
mySlot = tail.getAndIncrement();
while (!flags[mySlot]) {};
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

unlock for Alock

A

public unlock() {
flags[mySlot] = false;
flags[(mySlot+1)] = true;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is good/bad about Alock

A

scalable,simple

not space efficient
1bit per thread

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What keeps track of the queue in CLH?

A

Linked List

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the 2 states of the CLH and what do they mean

A

true: has/want to acquire lock
false: finished + has released it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does each node have in a CLH

A

a track of its predecessors status

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Variables in CLH

A

class CLHLock implements Lock {
AtomicReference<Qnode> tail;
ThreadLocal<Qnode> myNode
= new Qnode();
}}</Qnode></Qnode>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

lock CLH

A

public void lock() {
myNode.locked = true;
Qnode pred
= tail.getAndSet(myNode);
while (pred.locked) {}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

unlock CLH

A

public void unlock() {
myNode.locked.set(false);
myNode = pred;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Whats good about CLH

A

Lock release affects predecessor only
small, constant-sized space

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

MCS Lock idea

A

FIFO
Spin on local memory only
small, constant-size overhead

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Difference between CLH and MCS

A

in CLH = implicit MCS = explicit
each node in the queue has a next field

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

how does the Node look for MCS

A

class Qnode {
boolean locked = false;
qnode next = null;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What variables does MCS lock have?

A

only tail (atomic reference)

17
Q

lock MCS

A

public void lock() {
Qnode qnode = new Qnode();
Qnode pred = tail.getAndSet(qnode);
if (pred != null) {
qnode.locked = true;
pred.next = qnode;
while (qnode.locked) {}
}}

18
Q

unlock MCS

A

public void unlock() {
if (qnode.next == null) {
if (tail.CAS(qnode, null)
return;
while (qnode.next == null) {}
}
qnode.next.locked = false;
}

19
Q

What 2 types of locks do composite locks use?

A

Backoff + Queue

20
Q

Idea of Composite locks

A

Keep small number of waiting threads in a queue and have the rest use exponential backoff

21
Q

how will the Composite lock work?

A

keep fixed sized array of lock nodes
Each thread that tries to acquire the lock selects array node at random.
if in use back off and try again
If node is acquired enqueue in TOLock style queue
wait till its your turn to enter CS

22
Q

node states in Composite lock and what do they mean

A

FREE = available
WAITING = linked into the queue and either in/waiting CS
RELEASED = owner leaves the CS
ABORTED = quit

23
Q

how does a thread try to acquire a lock in a CLock?

A

Acquire node in waiting array
enqueue node in queue
waits until node reaches head of the queue

24
Q

What does tail point to in CLock?

A

either null or last value entered into queue

25
Q

what are the 2 variables in the CLock?

A

tail (AtomicReference<Qnode>)
Qnode[] waiting</Qnode>

26
Q

how does the states look in CLock

A

enum State {FREE, WAITING, RELEASED,
ABORTED}

27
Q

How does the node class look(for the composite lock)?

A

class Qnode {
AtomicReference<State> state;
Qnode pred;
public Qnode() {
state = new
AtomicReference<State>
(State.FREE);
}</State></State>

28
Q

How does the tryLock look in CLock?

A

public boolean tryLock() {
Qnode node = acquireNode();
Qnode pred = spliceNode(node);
waitForPredecessor(pred, node);
}

29
Q

To aquire a node at random how does the function look in CLock

A

private Qnode acquireNode() {
Qnode node = waiting[random.nextInt()];
while (true) {
if (state.compareAndSet(FREE, WAITING))
return node;
currTail = tail;
if (state == ABORTED || state ==
RELEASED) {
if (node == currTail) {
Qnode myPred = null;
if (state == ABORTED)
myPred = node.pred;
if (tail.compareAndSet(
currTail, myPred)
node.state.set(WAITING);
return node;
}
}

30
Q

how is the node spliced into the queue in CLock?

A

private Qnode spliceNode(Qnode node) {
Qnode currTail;
do {
currTail = tail;
if (timeout()) {
node.state.set(FREE);
throw new TimeoutException();
}
} while (!tail.compareAndSet(currTail,
node);
return currTail;
}

31
Q

how does a node wait for its predecessor in CLock?

A

private void waitforPredecessor {
if (pred == null)
return;
State predState = pred.state.get();
while (predState != RELEASED) {
if (predState == ABORTED) {
Qnode temp = pred;
pred = pred.pred;
temp.state.set(FREE);
}
if (timeout()) {
node.pred = pred;
node.state.set(ABORTED);
} throw new TimeoutException();
predState = pred.state.get();
}
pred.state.set(FREE);
return;
}

32
Q

unlock for CLock

A

public void unlock() {
myNode.state.set(RELEASED);
}