cs510 concurrent systems class 2 a lock-free multiprocessor os kernel

Post on 20-Dec-2015

224 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

CS510 Concurrent Systems Class 2

A Lock-Free Multiprocessor OS Kernel

CS510 - Concurrent Systems 2

The Synthesis kernel

Locking

CS510 - Concurrent Systems 3

So why not use locking?

CS510 - Concurrent Systems 4

Is there an alternative?

Use lock-free, “optimistic” synchronizationo Execute the critical section unconstrained, and check

at the end to see if you were the only oneo If so, continue. If not roll back and retry

Synthesis uses no locks at all!

Goal: Show that Lock-Free synchronization is...o Sufficient for all OS synchronization needso Practicalo High performance

CS510 - Concurrent Systems 5

Locking is pessimistic

CS510 - Concurrent Systems 6

Optimistic synchronization

CS510 - Concurrent Systems 7

How does it work?

CS510 - Concurrent Systems 8

Example – stack pop

Pop() { retry: old_SP = SP; new_SP = old_SP + 1; elem = *old_SP; if (CAS(old_SP, new_SP, &SP) == FAIL) goto retry; return elem;}

CS510 - Concurrent Systems 9

Example – stack pop

Pop() { retry: old_SP = SP; new_SP = old_SP + 1; elem = *old_SP; if (CAS(old_SP, new_SP, &SP) == FAIL) goto retry; return elem;}

CS510 - Concurrent Systems 10

loop

Example – stack pop

Pop() { retry: old_SP = SP; new_SP = old_SP + 1; elem = *old_SP; if (CAS(old_SP, new_SP, &SP) == FAIL) goto retry; return elem;}

CS510 - Concurrent Systems 11

Locals -won’t change!

Locals -won’t change!

Global - may change any

time!

Global - may change any

time!

“Atomic”read-modify-write

instruction

“Atomic”read-modify-write

instruction

CAS

CAS – single word Compare and Swapo An atomic read-modify-write instructiono Semantics of the single atomic instruction are:

CAS(copy, update, mem_addr)

{

if (*mem_addr == copy) {

*mem_addr = update;

return SUCCESS;

} else

return FAIL;

}

CS510 - Concurrent Systems 12

Example – stack pop

Pop() { retry: old_SP = SP; new_SP = old_SP + 1; elem = *old_SP; if (CAS(old_SP, new_SP, &SP) == FAIL) goto retry; return elem;}

CS510 - Concurrent Systems 13

Example – stack pop

Pop() { retry: old_SP = SP; new_SP = old_SP + 1; elem = *old_SP; if (CAS(old_SP, new_SP, &SP) == FAIL) goto retry; return elem;}

CS510 - Concurrent Systems 14

Do Work

Example – stack pop

Pop() { retry: old_SP = SP; new_SP = old_SP + 1; elem = *old_SP; if (CAS(old_SP, new_SP, &SP) == FAIL) goto retry; return elem;}

CS510 - Concurrent Systems 15

Example – stack pop

Pop() { retry: old_SP = SP; new_SP = old_SP + 1; elem = *old_SP; if (CAS(old_SP, new_SP, &SP) == FAIL) goto retry; return elem;}

CS510 - Concurrent Systems 16

Do Work

What made it work?

It works because we can atomically commit the new stack pointer value and compare the old stack pointer with the one at commit time

This allows us to verify no other thread has accessed the stack concurrently with our operation

o i.e. since we took the copyo Well, at least we know the address in the stack pointer is

the same as it was when we started• Does this guarantee there was no concurrent activity?• Does it matter?• We have to be careful !

CS510 - Concurrent Systems 17

Stack push

Push(elem) {

retry:

old_SP = SP;

new_SP = old_SP – 1;

old_val = *new_SP;

if(CAS2(old_SP, old_val, new_SP, elem, &SP, new_SP)

== FAIL) goto retry;

}

CS510 - Concurrent Systems 18

Stack push

Push(elem) {

retry:

old_SP = SP;

new_SP = old_SP – 1;

old_val = *new_SP;

if(CAS2(old_SP, old_val, new_SP, elem, &SP, new_SP)

== FAIL) goto retry;

}

CS510 - Concurrent Systems 19

Copy

Stack push

Push(elem) {

retry:

old_SP = SP;

new_SP = old_SP – 1;

old_val = *new_SP;

if(CAS2(old_SP, old_val, new_SP, elem, &SP, new_SP)

== FAIL) goto retry;

}

CS510 - Concurrent Systems 20

Do Work

Stack push

Push(elem) {

retry:

old_SP = SP;

new_SP = old_SP – 1;

old_val = *new_SP;

if(CAS2(old_SP, old_val, new_SP, elem, &SP, new_SP)

== FAIL) goto retry;

}

CS510 - Concurrent Systems 21

Stack push

Push(elem) {

retry:

old_SP = SP;

new_SP = old_SP – 1;

old_val = *new_SP;

if(CAS2(old_SP, old_val, new_SP, elem, &SP, new_SP)

== FAIL) goto retry;

}

CS510 - Concurrent Systems 22

Note: this is a double compare and swap!Its needed to atomically update both thenew item and the new stack pointer

Unnecessary

Compare

CAS2

CAS2 = double compare and swapo Sometimes referred to as DCAS

CAS2(copy1, copy2, update1, update2, addr1, addr2)

{

if(addr1 == copy1 && addr2 == copy2) {

*addr1 = update1;

*addr2 = update2;

return SUCCEED;

} else

return FAIL;

}

CS510 - Concurrent Systems 23

Stack push

Push(elem) {

retry:

old_SP = SP;

new_SP = old_SP – 1;

old_val = *new_SP;

if(CAS2(old_SP, old_val, new_SP, elem, &SP, new_SP)

== FAIL) goto retry;

}

CS510 - Concurrent Systems 24

Do Work

Optimistic synchronization in Synthesis

CS510 - Concurrent Systems 25

Approach

CS510 - Concurrent Systems 26

Why is this trickier than it seems?

CS510 - Concurrent Systems 27

The fall-back position

CS510 - Concurrent Systems 28

Lock vs lock-free critical sections

CS510 - Concurrent Systems 29

Lock_based_Pop() {

spin_lock(&lock);

elem = *SP; SP = SP + 1;

spin_unlock(&lock);

return elem;

}

Lock_free_Pop() {

retry:

old_SP = SP;

new_SP = old_SP + 1; elem = *old_SP;

if (CAS(old_SP, new_SP, &SP) == FAIL)

goto retry;

return elem;

}

CS510 - Concurrent Systems 30

Conclusions

top related