REF: 多处理器编程的艺术(The Art of Multiprocessor Programming)
说明:我自己的理解为红色(不保证正确),参考书上的为黑色。
最后修改时间: 09-16-2011
-------------------------------------------------------------------------
2.5 Fairness
Definition 2.5.1. A lock is first-come-first-served if, whenever, thread A finishes
its doorway before thread B starts its doorway, then A cannot be overtaken by B.
2.6 Lamport’s Bakery Algorithm
class Bakery implements Lock {
boolean[] flag;
Label[] label;
public Bakery (int n) {
flag = new boolean[n];
label = new Label[n];
for (int i = 0; i < n; i++) {
flag[i] = false; label[i] = 0; //[1]
}
}
public void lock() {
int i = ThreadID.get();
flag[i] = true; //[2]
label[i] = max(label[0], ...,label[n-1]) + 1; //[3]
while ((∃k != i)(flag[k] && (label[k],k) << (label[i],i))) {}; //[4]
}
public void unlock() {
flag[ThreadID.get()] = false; //[5]
}
}
Figure 2.9 The Bakery lock algorithm.
- flag[A] : Indicating whether A wants to enter the critical section.
- label[A] : an integer that indicates the thread’s relative order when entering the Critical Section,
for each thread A.
// label[A]'s task is establishing an order among the contending threads.
- [1]: Init
All threads(0 ~ n-1) don't want to enter the critical section.
Order of each threads is 0.
- [2]: Request to enter the CS(Critical Section).
- [3]: Get a sequence number which is greater by one than the maximal one.
- [4]: Wait until there is no other threads want to enter CS or itself is the earlist one among all
threads waiting at the 'waiting section'.
* stages [2], [3] is called 'doorway section'.
* Since releasing a lock does not reset the label[], it is easy to see that each thread’s labels are
strictly increasing.
- Lemma 2.6.1. The Bakery lock algorithm is deadlock-free.
- Lemma 2.6.2. The Bakery lock algorithm is first-come-first-served.
- Lemma 2.6.3. The Bakery algorithm satisfies mutual exclusion.
2.7 Bounded Timestamps
- Think of assigning a timestamp to a thread as placing that thread’s token on
that timestamp’s node. A thread performs a scan by locating the other threads’
tokens, and it assigns itself a new timestamp by moving its own token to a node
a such that there is an edge from a to every other thread’s node.
- Let G be a precedence graph, and A and B subgraphs of G (possibly single nodes).
We say that A dominates B in G if every node of A has edges directed to every node of B.
- graph multiplication
- Define the graph T^ k inductively to be:
1. T^1 is a single node.
2. T^2 is the three-node graph defined earlier.
3. For k > 2, T^ k = T^2 ◦ T ^(k−1).
- The key to understanding the n-thread labeling algorithm is that the nodes covered by tokens
can never form a cycle.
2.8 Lower Bounds on the Number of Locations
- Any deadlock-free Lock algorithm requires allocating and then reading or writing at least n
distinct locations in the worst case.
++++ We will observe the following important limitation of memory locations accessed solely
by read or write instructions (in practice these are called loads and stores): any information
written by a thread to a given location could be overwritten (stored-to) without any
other thread ever seeing it.
- Definition 2.8.1. A Lock object state s is inconsistent in any global state where
some thread is in the critical section, but the lock state is compatible with a global
state in which no thread is in the critical section or is trying to enter the critical section.
- Lemma 2.8.1. No deadlock-free Lock algorithm can enter an inconsistent state.