Outline 0024 Spring 2010 21 :: 2 Source [1] Talking About - - PowerPoint PPT Presentation

outline
SMART_READER_LITE
LIVE PREVIEW

Outline 0024 Spring 2010 21 :: 2 Source [1] Talking About - - PowerPoint PPT Presentation

Outline 0024 Spring 2010 21 :: 2 Source [1] Talking About Executions Why? Can t we specify the linearization point of each operation without describing an execution? Not Always In some


slide-1
SLIDE 1
slide-2
SLIDE 2

– 21 :: 2 – Source [1] 0024 Spring 2010

Outline

slide-3
SLIDE 3

– 21 :: 3 – Source [1] 0024 Spring 2010

Talking About Executions

Why?

Can’t we specify the linearization point of each operation

without describing an execution?

Not Always

In some cases, linearization point depends on the execution

slide-4
SLIDE 4

– 21 :: 4 – Source [1] 0024 Spring 2010

Formal Model of Executions

Define precisely what we mean

Ambiguity is bad when intuition is weak

Allow reasoning

Formal But mostly informal

In the long run, actually more important Ask me why!

slide-5
SLIDE 5

– 21 :: 5 – Source [1] 0024 Spring 2010

Split Method Calls into Two Events

Invocation

method name & args

  • q. enq( x)

Response

result or exception

  • q. enq( x) returns voi d
  • q. deq( ) returns x
  • q. deq( ) throws em

pt y

slide-6
SLIDE 6

– 21 :: 6 – Source [1] 0024 Spring 2010

Invocation Notation

A q.enq(x)

(4)

slide-7
SLIDE 7

– 21 :: 7 – Source [1] 0024 Spring 2010

Invocation Notation

A q.enq(x) thread

(4)

slide-8
SLIDE 8

– 21 :: 8 – Source [1] 0024 Spring 2010

Invocation Notation

A q.enq(x) thread method

(4)

slide-9
SLIDE 9

– 21 :: 9 – Source [1] 0024 Spring 2010

Invocation Notation

A q.enq(x) thread

  • bject

(4)

method

slide-10
SLIDE 10

– 21 :: 10 – Source [1] 0024 Spring 2010

Invocation Notation

A q.enq(x) thread

  • bject

method arguments

(4)

slide-11
SLIDE 11

– 21 :: 11 – Source [1] 0024 Spring 2010

Response Notation

A q: void

(2)

slide-12
SLIDE 12

– 21 :: 12 – Source [1] 0024 Spring 2010

Response Notation

A q: void thread

(2)

slide-13
SLIDE 13

– 21 :: 13 – Source [1] 0024 Spring 2010

Response Notation

A q: void thread result

(2)

slide-14
SLIDE 14

– 21 :: 14 – Source [1] 0024 Spring 2010

Response Notation

A q: void thread

  • bject

result

(2)

slide-15
SLIDE 15

– 21 :: 15 – Source [1] 0024 Spring 2010

Response Notation

A q: void thread

  • bject

result

(2)

slide-16
SLIDE 16

– 21 :: 16 – Source [1] 0024 Spring 2010

Response Notation

A q: empty() thread

  • bject

(2)

exception

slide-17
SLIDE 17

– 21 :: 17 – Source [1] 0024 Spring 2010

History - Describing an Execution

A q. enq( 3) A q: voi d A q. enq( 5) B p. enq( 4) B p: voi d B q. deq( ) B q: 3 Sequence of invocations and responses H =

slide-18
SLIDE 18

– 21 :: 18 – Source [1] 0024 Spring 2010

Definition

Invocation & response match if

A q. enq( 3) A q: voi d Thread names agree Object names agree

Method call

(1)

slide-19
SLIDE 19

– 21 :: 19 – Source [1] 0024 Spring 2010

Object Projections

A q. enq( 3) A q: voi d B p. enq( 4) B p: voi d B q. deq( ) B q: 3

H =

slide-20
SLIDE 20

– 21 :: 20 – Source [1] 0024 Spring 2010

Object Projections

A q. enq( 3) A q: voi d B p. enq( 4) B p: voi d B q. deq( ) B q: 3

H|q =

slide-21
SLIDE 21

– 21 :: 21 – Source [1] 0024 Spring 2010

Thread Projections

A q. enq( 3) A q: voi d B p. enq( 4) B p: voi d B q. deq( ) B q: 3

H =

slide-22
SLIDE 22

– 21 :: 22 – Source [1] 0024 Spring 2010

Thread Projections

A q. enq( 3) A q: voi d B p. enq( 4) B p: voi d B q. deq( ) B q: 3

H|B =

slide-23
SLIDE 23

– 21 :: 23 – Source [1] 0024 Spring 2010

Complete Subhistory

A q. enq( 3) A q: voi d A q. enq( 5) B p. enq( 4) B p: voi d B q. deq( ) B q: 3 An invocation is pending if it has no matching respnse H =

slide-24
SLIDE 24

– 21 :: 24 – Source [1] 0024 Spring 2010

Complete Subhistory

A q. enq( 3) A q: voi d A q. enq( 5) B p. enq( 4) B p: voi d B q. deq( ) B q: 3 May or may not have taken effect H =

slide-25
SLIDE 25

– 21 :: 25 – Source [1] 0024 Spring 2010

Complete Subhistory

A q. enq( 3) A q: voi d A q. enq( 5) B p. enq( 4) B p: voi d B q. deq( ) B q: 3 discard pending invocations H =

slide-26
SLIDE 26

– 21 :: 26 – Source [1] 0024 Spring 2010

Complete Subhistory

A q. enq( 3) A q: voi d B p. enq( 4) B p: voi d B q. deq( ) B q: 3 Complete(H) =

slide-27
SLIDE 27

– 21 :: 27 – Source [1] 0024 Spring 2010

Sequential Histories

A q. enq( 3) A q: voi d B p. enq( 4) B p: voi d B q. deq( ) B q: 3 A q: enq( 5)

(4)

slide-28
SLIDE 28

– 21 :: 28 – Source [1] 0024 Spring 2010

Sequential Histories

A q. enq( 3) A q: voi d B p. enq( 4) B p: voi d B q. deq( ) B q: 3 A q: enq( 5) match

(4)

slide-29
SLIDE 29

– 21 :: 29 – Source [1] 0024 Spring 2010

Sequential Histories

A q. enq( 3) A q: voi d B p. enq( 4) B p: voi d B q. deq( ) B q: 3 A q: enq( 5) match match

(4)

slide-30
SLIDE 30

– 21 :: 30 – Source [1] 0024 Spring 2010

Sequential Histories

A q. enq( 3) A q: voi d B p. enq( 4) B p: voi d B q. deq( ) B q: 3 A q: enq( 5) match match match

(4)

slide-31
SLIDE 31

– 21 :: 31 – Source [1] 0024 Spring 2010

Sequential Histories

A q. enq( 3) A q: voi d B p. enq( 4) B p: voi d B q. deq( ) B q: 3 A q: enq( 5) match match match Final pending invocation OK

(4)

slide-32
SLIDE 32

– 21 :: 32 – Source [1] 0024 Spring 2010

Sequential Histories

A q. enq( 3) A q: voi d B p. enq( 4) B p: voi d B q. deq( ) B q: 3 A q: enq( 5) match match match Final pending invocation OK

(4)

slide-33
SLIDE 33

– 21 :: 33 – Source [1] 0024 Spring 2010

Well-Formed Histories

H=

A q. enq( 3) B p. enq( 4) B p: voi d B q. deq( ) A q: voi d B q: 3

slide-34
SLIDE 34

– 21 :: 34 – Source [1] 0024 Spring 2010

Well-Formed Histories

H=

A q. enq( 3) B p. enq( 4) B p: voi d B q. deq( ) A q: voi d B q: 3

H| B=

B p. enq( 4) B p: voi d B q. deq( ) B q: 3 Per-thread projections sequential

slide-35
SLIDE 35

– 21 :: 35 – Source [1] 0024 Spring 2010

Well-Formed Histories

H=

A q. enq( 3) B p. enq( 4) B p: voi d B q. deq( ) A q: voi d B q: 3

H| B=

B p. enq( 4) B p: voi d B q. deq( ) B q: 3 A q. enq( 3) A q: voi d

H| A=

Per-thread projections sequential

slide-36
SLIDE 36

– 21 :: 36 – Source [1] 0024 Spring 2010

Equivalent Histories

H=

A q. enq( 3) B p. enq( 4) B p: voi d B q. deq( ) A q: voi d B q: 3 Threads see the same thing in both A q. enq( 3) A q: voi d B p. enq( 4) B p: voi d B q. deq( ) B q: 3

G = H| A = G | A H| B = G | B

slide-37
SLIDE 37

– 21 :: 37 – Source [1] 0024 Spring 2010

Sequential Specifications

A sequential specification is some way of telling whether a

Single-thread, single-object history Is legal

For example:

Pre and post-conditions But plenty of other techniques exist …

slide-38
SLIDE 38

– 21 :: 38 – Source [1] 0024 Spring 2010

Legal Histories

A sequential (multi-object) history H is legal if

For every object x H|x is in the sequential spec for x

slide-39
SLIDE 39

– 21 :: 39 – Source [1] 0024 Spring 2010

Precedence

A q. enq( 3) B p. enq( 4) B p. voi d A q: voi d B q. deq( ) B q: 3 A method call precedes another if response event precedes invocation event

Method call Method call

(1)

slide-40
SLIDE 40

– 21 :: 40 – Source [1] 0024 Spring 2010

Non-Precedence

A q. enq( 3) B p. enq( 4) B p. voi d B q. deq( ) A q: voi d B q: 3 Some method calls

  • verlap one another

Method call Method call

(1)

slide-41
SLIDE 41

– 21 :: 41 – Source [1] 0024 Spring 2010

Notation

Given

History H method executions m0 and m1 in H

We say m0 H m1, if

m0 precedes m1

Relation m0 H m1 is a

Partial order Total order if H is sequential

m0 m1

slide-42
SLIDE 42

– 21 :: 42 – Source [1] 0024 Spring 2010

Linearizability

History H is linearizable if it can be extended to G by

Appending zero or more responses to pending invocations Discarding other pending invocations

So that G is equivalent to

Legal sequential history S where G S

slide-43
SLIDE 43

– 21 :: 43 – Source [1] 0024 Spring 2010

What is G S

time a b time

(8)

S c G

G = { ac, bc} S = { ab, ac, bc}

slide-44
SLIDE 44

– 21 :: 44 – Source [1] 0024 Spring 2010

Remarks

Some pending invocations

Took effect, so keep them Discard the rest

Condition G S

Means that S respects “real-time order” of G

slide-45
SLIDE 45

– 21 :: 45 – Source [1] 0024 Spring 2010

A q. enq( 3) B q. enq( 4) B q: voi d B q. deq( ) B q: 4 B q: enq( 6)

Example

time B.q.enq(4)

  • A. q.enq(3)

B.q.deq(4)

  • B. q.enq(6)
slide-46
SLIDE 46

– 21 :: 46 – Source [1] 0024 Spring 2010

Example

Complete this pending invocation

time B.q.enq(4) B.q.deq(4)

  • B. q.enq(6)

A q. enq( 3) B q. enq( 4) B q: voi d B q. deq( ) B q: 4 B q: enq( 6)

  • A. q.enq(3)
slide-47
SLIDE 47

– 21 :: 47 – Source [1] 0024 Spring 2010

Example

Complete this pending invocation

time B.q.enq(4) B.q.deq(4)

  • B. q.enq(6)

A.q.enq(3)

A q. enq( 3) B q. enq( 4) B q: voi d B q. deq( ) B q: 4 B q: enq( 6) A q: voi d

slide-48
SLIDE 48

– 21 :: 48 – Source [1] 0024 Spring 2010

Example

time B.q.enq(4) B.q.deq(4)

  • B. q.enq(6)

A.q.enq(3)

A q. enq( 3) B q. enq( 4) B q: voi d B q. deq( ) B q: 4 B q: enq( 6) A q: voi d discard this one

slide-49
SLIDE 49

– 21 :: 49 – Source [1] 0024 Spring 2010

Example

time B.q.enq(4) B.q.deq(4) A.q.enq(3)

A q. enq( 3) B q. enq( 4) B q: voi d B q. deq( ) B q: 4 A q: voi d discard this one

slide-50
SLIDE 50

– 21 :: 50 – Source [1] 0024 Spring 2010

A q. enq( 3) B q. enq( 4) B q: voi d B q. deq( ) B q: 4 A q: voi d

Example

time B.q.enq(4) B.q.deq(4) A.q.enq(3)

slide-51
SLIDE 51

– 21 :: 51 – Source [1] 0024 Spring 2010

A q. enq( 3) B q. enq( 4) B q: voi d B q. deq( ) B q: 4 A q: voi d

Example

time

B q. enq( 4) B q: voi d A q. enq( 3) A q: voi d B q. deq( ) B q: 4

B.q.enq(4) B.q.deq(4) A.q.enq(3)

slide-52
SLIDE 52

– 21 :: 52 – Source [1] 0024 Spring 2010

B.q.enq(4) B.q.deq(4) A.q.enq(3)

A q. enq( 3) B q. enq( 4) B q: voi d B q. deq( ) B q: 4 A q: voi d

Example

time

B q. enq( 4) B q: voi d A q. enq( 3) A q: voi d B q. deq( ) B q: 4 Equivalent sequential history

slide-53
SLIDE 53

– 21 :: 58 – Source [1] 0024 Spring 2010

Composability Theorem

History H is linearizable if and only if

For every object x H|x is linearizable

We care about objects only!

(Materialism?)

slide-54
SLIDE 54

– 21 :: 59 – Source [1] 0024 Spring 2010

Why Does Composability Matter?

Modularity Can prove linearizability of objects in isolation Can compose independently-implemented objects

slide-55
SLIDE 55

– 21 :: 60 – Source [1] 0024 Spring 2010

Reasoning About Linearizability: Locking

publ i c O bj ect deq( ) t hr ows Em pt yExcept i on { l ock. l ock( ) ; t r y { i f ( t ai l == head) t hr ow new Em pt yExcept i on( ) ; O bj ect x = i t em s[ head % i t em

  • s. l engt h] ;

head++; r et ur n x; } f i nal l y { l ock. unl ock( ) ; } }

1 capaci t y- 1 2

head tail

y z

slide-56
SLIDE 56

– 21 :: 61 – Source [1] 0024 Spring 2010

Reasoning About Linearizability: Locking

publ i c O bj ect deq( ) t hr ows Em pt yExcept i on { l ock. l ock( ) ; t r y { i f ( t ai l == head) t hr ow new Em pt yExcept i on( ) ; O bj ect x = i t em s[ head % i t em

  • s. l engt h] ;

head++; r et ur n x; } f i nal l y { l ock. unl ock( ) ; } }

Linearization points are when locks are released

slide-57
SLIDE 57

– 21 :: 62 – Source [1] 0024 Spring 2010

More Reasoning: Lock-free

publ i c cl ass LockFr eeQ ueue { vol at i l e i nt head = 0, t ai l = 0; i t em s = new O bj ect [ capaci t y] ; publ i c voi d enq( O bj ect x) { whi l e ( t ai l - head == capaci t y) ; / / busy- wai t i t em s[ t ai l % capaci t y] = x; t ai l ++; } publ i c I t em deq( ) { whi l e ( t ai l == head) ; / / busy- wai t O bj ect i t em = i t em s[ head % capaci t y] ; head++; r et ur n i t em ; } }

1 capaci t y- 1 2

head tail

y z

slide-58
SLIDE 58

– 21 :: 63 – Source [1] 0024 Spring 2010

publ i c cl ass LockFr eeQ ueue { vol at i l e i nt head = 0, t ai l = 0; i t em s = new O bj ect [ capaci t y] ; publ i c voi d enq( O bj ect x) { whi l e ( t ai l - head == capaci t y) ; / / busy- wai t i t em s[ t ai l % capaci t y] = x; t ai l ++; } publ i c I t em deq( ) { whi l e ( t ai l == head) ; / / busy- wai t O bj ect i t em = i t em s[ head % capaci t y] ; head++; r et ur n i t em ; } }

Linearization order is

  • rder head and tail

fields modified

More Reasoning

slide-59
SLIDE 59

– 21 :: 64 – Source [1] 0024 Spring 2010

More Reasoning

slide-60
SLIDE 60

– 21 :: 65 – Source [1] 0024 Spring 2010

Strategy

Identify one atomic step where method “happens”

Critical section Machine instruction

Doesn’t always work

Might need to define several different steps for a given

method

slide-61
SLIDE 61

– 21 :: 66 – Source [1] 0024 Spring 2010

Linearizability: Summary

Powerful specification tool for shared objects Allows us to capture the notion of objects being “atomic” Don’t leave home without it

slide-62
SLIDE 62

– 21 :: 67 – Source [1] 0024 Spring 2010

Source