Shared Objects & Mutual Exclusion DM519 Concurrent Programming - - PowerPoint PPT Presentation

shared objects mutual exclusion
SMART_READER_LITE
LIVE PREVIEW

Shared Objects & Mutual Exclusion DM519 Concurrent Programming - - PowerPoint PPT Presentation

Chapter 4 Shared Objects & Mutual Exclusion DM519 Concurrent Programming 1 1 Repetition (Finite State Processes; FSP) Finite State Processes (FSP) can be defined using: P = x -> Q // action Q // other process variable


slide-1
SLIDE 1

DM519 Concurrent Programming

Chapter 4

Shared Objects & Mutual Exclusion

1

1

slide-2
SLIDE 2

DM519 Concurrent Programming

Repetition (Finite State Processes; FSP)

Finite State Processes (FSP) can be defined using: P = – x -> Q // action – Q // other process variable – STOP // termination – Q | R // choice – when (...) x -> Q // guard – ... + {write[0..3]} // alphabet extension – X[i:0..N] =x[N-i] -> P // process & action index – BUFF(N=3) // process parameter

2

2

slide-3
SLIDE 3

DM519 Concurrent Programming

Repetition (Finite State Processes; FSP)

Finite State Processes (FSP) can be defined using: P = – x -> Q // action – Q // other process variable – STOP // termination – Q | R // choice – when (...) x -> Q // guard – ... + {write[0..3]} // alphabet extension – X[i:0..N] =x[N-i] -> P // process & action index – BUFF(N=3) // process parameter

const N = 3

// constant definitions

range R = 0..N

// range definitions

set S = {a,b,c}

// set definitions

2

2

slide-4
SLIDE 4

DM519 Concurrent Programming

Repetition (Finite State Processes; FSP)

Finite State Processes (FSP) can be defined using: P = – x -> Q // action – Q // other process variable – STOP // termination – Q | R // choice – when (...) x -> Q // guard – ... + {write[0..3]} // alphabet extension – X[i:0..N] =x[N-i] -> P // process & action index – BUFF(N=3) // process parameter

const N = 3

// constant definitions

range R = 0..N

// range definitions

set S = {a,b,c}

// set definitions

2

range T = 0..3 BUFF = (in[i:T]->out[i]->BUFF).

2

slide-5
SLIDE 5

DM519 Concurrent Programming

Repetition (Finite State Processes; FSP)

Finite State Processes (FSP) can be defined using: P = – x -> Q // action – Q // other process variable – STOP // termination – Q | R // choice – when (...) x -> Q // guard – ... + {write[0..3]} // alphabet extension – X[i:0..N] =x[N-i] -> P // process & action index – BUFF(N=3) // process parameter

const N = 3

// constant definitions

range R = 0..N

// range definitions

set S = {a,b,c}

// set definitions

2

range T = 0..3 BUFF = (in[i:T]->out[i]->BUFF).

2

slide-6
SLIDE 6

DM519 Concurrent Programming

Repetition (FSP)

3

3

slide-7
SLIDE 7

DM519 Concurrent Programming

Repetition (FSP)

FSP: –P || Q // parallel composition –a:P // process labelling (1 process/prefix) –{…}::P // process sharing (1 process w/all prefixes) –P / {x/y} // action relabelling –P \ {…} // hiding –P @ {…} // keeping (hide complement)

3

3

slide-8
SLIDE 8

DM519 Concurrent Programming

Repetition (FSP)

FSP: –P || Q // parallel composition –a:P // process labelling (1 process/prefix) –{…}::P // process sharing (1 process w/all prefixes) –P / {x/y} // action relabelling –P \ {…} // hiding –P @ {…} // keeping (hide complement)

3

||TWOBUF = (a:BUFF||b:BUFF) /{in/a.in, a.out/b.in,

  • ut/b.out}

@{in,out}.

3

slide-9
SLIDE 9

DM519 Concurrent Programming

Repetition (FSP)

FSP: –P || Q // parallel composition –a:P // process labelling (1 process/prefix) –{…}::P // process sharing (1 process w/all prefixes) –P / {x/y} // action relabelling –P \ {…} // hiding –P @ {…} // keeping (hide complement)

3

||TWOBUF = (a:BUFF||b:BUFF) /{in/a.in, a.out/b.in,

  • ut/b.out}

@{in,out}.

3

slide-10
SLIDE 10

DM519 Concurrent Programming

Repetition (FSP)

FSP: –P || Q // parallel composition –a:P // process labelling (1 process/prefix) –{…}::P // process sharing (1 process w/all prefixes) –P / {x/y} // action relabelling –P \ {…} // hiding –P @ {…} // keeping (hide complement) Structure Diagrams:

a:BUFF b:BUFF a.out TWOBUFF

  • ut

in in

  • ut

in

  • ut

3

||TWOBUF = (a:BUFF||b:BUFF) /{in/a.in, a.out/b.in,

  • ut/b.out}

@{in,out}.

3

slide-11
SLIDE 11

DM519 Concurrent Programming

Structure Diagrams - Resource Sharing

RESOURCE = (acquire->release->RESOURCE). USER = (printer.acquire->use->printer.release->USER).

4

4

slide-12
SLIDE 12

DM519 Concurrent Programming

Structure Diagrams - Resource Sharing

RESOURCE = (acquire->release->RESOURCE). USER = (printer.acquire->use->printer.release->USER). ||PRINTER_SHARE = (a:USER || b:USER || {a,b}::printer:RESOURCE).

4

4

slide-13
SLIDE 13

DM519 Concurrent Programming

Structure Diagrams - Resource Sharing

a:USER

printer

b:USER

printer

printer: RESOURCE

acquire release

PRINTER_SHARE

RESOURCE = (acquire->release->RESOURCE). USER = (printer.acquire->use->printer.release->USER). ||PRINTER_SHARE = (a:USER || b:USER || {a,b}::printer:RESOURCE).

4

4

slide-14
SLIDE 14

DM519 Concurrent Programming

How To Create The Parallel Composed LTS

MAKE1 = (make->ready->STOP). USE1 = (ready->use->STOP). ||MAKE1_USE1 = (MAKE1 || USE1).

5

make ready make make ready ready ready ready ready use use use 0,0 0,1 0,2 1,0 1,2 1,1 2,0 2,2 2,1 For any state reachable from the initial state (0,0), consider the possible actions and draw edges to the corresponding new states (i,j). Remember to consider shared actions.

5

slide-15
SLIDE 15

DM519 Concurrent Programming

How To Create The Parallel Composed LTS

MAKE1 = (make->ready->STOP). USE1 = (ready->use->STOP). ||MAKE1_USE1 = (MAKE1 || USE1).

5

make ready make make ready ready ready ready ready use use use 0,0 0,1 0,2 1,0 1,2 1,1 2,0 2,2 2,1 For any state reachable from the initial state (0,0), consider the possible actions and draw edges to the corresponding new states (i,j). Remember to consider shared actions.

5

slide-16
SLIDE 16

DM519 Concurrent Programming

How To Create The Parallel Composed LTS

MAKE1 = (make->ready->STOP). USE1 = (ready->use->STOP). ||MAKE1_USE1 = (MAKE1 || USE1).

5

make ready make make ready ready ready ready ready use use use 0,0 0,1 0,2 1,0 1,2 1,1 2,0 2,2 2,1 For any state reachable from the initial state (0,0), consider the possible actions and draw edges to the corresponding new states (i,j). Remember to consider shared actions.

5

slide-17
SLIDE 17

DM519 Concurrent Programming

How To Create The Parallel Composed LTS

MAKE1 = (make->ready->STOP). USE1 = (ready->use->STOP). ||MAKE1_USE1 = (MAKE1 || USE1).

5

make ready make make ready ready ready ready ready use use use 0,0 0,1 0,2 1,0 1,2 1,1 2,0 2,2 2,1 For any state reachable from the initial state (0,0), consider the possible actions and draw edges to the corresponding new states (i,j). Remember to consider shared actions.

5

slide-18
SLIDE 18

DM519 Concurrent Programming

How To Create The Parallel Composed LTS

WORKDAY = HOME, HOME = (bus -> WORK), WORK = (dostuff-> WORK | bus -> HOME). ALSORUN = (bus -> run -> ALSORUN). ||DAY = (WORKDAY || ALSORUN).

6

For any state reachable from the initial state (0,0), consider the possible actions and draw edges to the corresponding new states (i,j). Remember to consider shared actions.

6

slide-19
SLIDE 19

DM519 Concurrent Programming

How To Create The Parallel Composed LTS

WORKDAY = HOME, HOME = (bus -> WORK), WORK = (dostuff-> WORK | bus -> HOME). ALSORUN = (bus -> run -> ALSORUN). ||DAY = (WORKDAY || ALSORUN).

6

For any state reachable from the initial state (0,0), consider the possible actions and draw edges to the corresponding new states (i,j). Remember to consider shared actions.

6

slide-20
SLIDE 20

DM519 Concurrent Programming

How To Create The Parallel Composed LTS

WORKDAY = HOME, HOME = (bus -> WORK), WORK = (dostuff-> WORK | bus -> HOME). ALSORUN = (bus -> run -> ALSORUN). ||DAY = (WORKDAY || ALSORUN).

6

For any state reachable from the initial state (0,0), consider the possible actions and draw edges to the corresponding new states (i,j). Remember to consider shared actions.

6

slide-21
SLIDE 21

DM519 Concurrent Programming

How To Create The Parallel Composed LTS

WORKDAY = HOME, HOME = (bus -> WORK), WORK = (dostuff-> WORK | bus -> HOME). ALSORUN = (bus -> run -> ALSORUN). ||DAY = (WORKDAY || ALSORUN).

6

For any state reachable from the initial state (0,0), consider the possible actions and draw edges to the corresponding new states (i,j). Remember to consider shared actions. 0,0

6

slide-22
SLIDE 22

DM519 Concurrent Programming

How To Create The Parallel Composed LTS

WORKDAY = HOME, HOME = (bus -> WORK), WORK = (dostuff-> WORK | bus -> HOME). ALSORUN = (bus -> run -> ALSORUN). ||DAY = (WORKDAY || ALSORUN).

6

For any state reachable from the initial state (0,0), consider the possible actions and draw edges to the corresponding new states (i,j). Remember to consider shared actions. 0,0 1,1

6

slide-23
SLIDE 23

DM519 Concurrent Programming

How To Create The Parallel Composed LTS

WORKDAY = HOME, HOME = (bus -> WORK), WORK = (dostuff-> WORK | bus -> HOME). ALSORUN = (bus -> run -> ALSORUN). ||DAY = (WORKDAY || ALSORUN).

6

For any state reachable from the initial state (0,0), consider the possible actions and draw edges to the corresponding new states (i,j). Remember to consider shared actions. 0,0 1,1 1,0

6

slide-24
SLIDE 24

DM519 Concurrent Programming

How To Create The Parallel Composed LTS

WORKDAY = HOME, HOME = (bus -> WORK), WORK = (dostuff-> WORK | bus -> HOME). ALSORUN = (bus -> run -> ALSORUN). ||DAY = (WORKDAY || ALSORUN).

6

For any state reachable from the initial state (0,0), consider the possible actions and draw edges to the corresponding new states (i,j). Remember to consider shared actions. 0,0 1,1 1,0 0,1

6

slide-25
SLIDE 25

DM519 Concurrent Programming

Chapter 4: Shared Objects & Mutual Exclusion

7

7

slide-26
SLIDE 26

DM519 Concurrent Programming

Chapter 4: Shared Objects & Mutual Exclusion

uConcepts:

lProcess interference lMutual exclusion

7

7

slide-27
SLIDE 27

DM519 Concurrent Programming

Chapter 4: Shared Objects & Mutual Exclusion

uConcepts:

lProcess interference lMutual exclusion

uModels:

lModel-checking for interference lModelling mutual exclusion

7

7

slide-28
SLIDE 28

DM519 Concurrent Programming

Chapter 4: Shared Objects & Mutual Exclusion

uConcepts:

lProcess interference lMutual exclusion

uModels:

lModel-checking for interference lModelling mutual exclusion

uPractice:

lThread interference in shared objects in Java lMutual exclusion in Java lSynchronised objects, methods, and statements

7

7

slide-29
SLIDE 29

DM519 Concurrent Programming

4.1 Interference

People enter an ornamental garden through either of two

  • turnstiles. Management wishes to know how many are in the

garden at any time. (Nobody can exit). The ”Ornamental Garden Problem ”: Counter

8

8

slide-30
SLIDE 30

DM519 Concurrent Programming

4.1 Interference

People enter an ornamental garden through either of two

  • turnstiles. Management wishes to know how many are in the

garden at any time. (Nobody can exit). The ”Ornamental Garden Problem ”: Counter 1

8

8

slide-31
SLIDE 31

DM519 Concurrent Programming

4.1 Interference

People enter an ornamental garden through either of two

  • turnstiles. Management wishes to know how many are in the

garden at any time. (Nobody can exit). The ”Ornamental Garden Problem ”: Counter 1 2

8

8

slide-32
SLIDE 32

DM519 Concurrent Programming

4.1 Ornamental Garden Problem (cont’d)

Counter

9

9

slide-33
SLIDE 33

DM519 Concurrent Programming

4.1 Ornamental Garden Problem (cont’d)

Java implementation: Counter

9

9

slide-34
SLIDE 34

DM519 Concurrent Programming

4.1 Ornamental Garden Problem (cont’d)

Java implementation: The concurrent program consists of: Counter

9

9

slide-35
SLIDE 35

DM519 Concurrent Programming

4.1 Ornamental Garden Problem (cont’d)

Java implementation: The concurrent program consists of:

  • two concurrent threads (west & east); and

Counter

9

9

slide-36
SLIDE 36

DM519 Concurrent Programming

4.1 Ornamental Garden Problem (cont’d)

Java implementation: The concurrent program consists of:

  • two concurrent threads (west & east); and
  • a shared counter object

Counter 2

9

9

slide-37
SLIDE 37

DM519 Concurrent Programming

Class Diagram

counter

40 20 20

10

10

slide-38
SLIDE 38

DM519 Concurrent Programming

Ornamental Garden Program

The go() method of the Garden applet… …creates the shared Counter object & the Turnstile threads. class Garden extends Applet { NumberCanvas counterD, westD, eastD; Turnstile east, west; ... private void go() { counter = new Counter(counterD); west = new Turnstile(westD,counter); east = new Turnstile(eastD,counter); west.start(); east.start(); } }

11

11

slide-39
SLIDE 39

DM519 Concurrent Programming

The Turnstile Class

class Turnstile extends Thread { NumberCanvas display; Counter counter; public void run() { try { display.setvalue(0); for (int i=1; i<=Garden.MAX; i++) { Thread.sleep(1000); display.setvalue(i); counter.increment(); } } catch (InterruptedException _) {} } }

12

12

slide-40
SLIDE 40

DM519 Concurrent Programming

The Turnstile Class

class Turnstile extends Thread { NumberCanvas display; Counter counter; public void run() { try { display.setvalue(0); for (int i=1; i<=Garden.MAX; i++) { Thread.sleep(1000); display.setvalue(i); counter.increment(); } } catch (InterruptedException _) {} } } The Turnstile thread simulates periodic arrival of visitors by invoking the counter

  • bject’s increment() method every second

12

12

slide-41
SLIDE 41

DM519 Concurrent Programming

The Shared Counter Class

class Counter { int value; NumberCanvas display; void increment() { value = value + 1; display.setvalue(value); } } The increment() method of the Counter class increments its internal value and updates the display.

13

13

slide-42
SLIDE 42

DM519 Concurrent Programming

Running The Applet

After the East and West turnstile threads each have incremented the counter 20 times, the garden people counter is not always the sum of the counts displayed.

39 20 20

14

14

slide-43
SLIDE 43

DM519 Concurrent Programming

Running The Applet

After the East and West turnstile threads each have incremented the counter 20 times, the garden people counter is not always the sum of the counts displayed. Why?

39 20 20

14

14

slide-44
SLIDE 44

DM519 Concurrent Programming

The Shared Counter Class (cont’d)

class Counter { int value; NumberCanvas display; void increment() { value = value + 1; display.setvalue(value); } }

15

15

slide-45
SLIDE 45

DM519 Concurrent Programming

The Shared Counter Class (cont’d)

class Counter { int value; NumberCanvas display; void increment() { value = value + 1; display.setvalue(value); } }

15

javac Counter.java javap -c Counter > Counter.bc

15

slide-46
SLIDE 46

DM519 Concurrent Programming

The Shared Counter Class (cont’d)

class Counter { int value; NumberCanvas display; void increment() { value = value + 1; display.setvalue(value); } }

15

javac Counter.java javap -c Counter > Counter.bc

15

slide-47
SLIDE 47

DM519 Concurrent Programming

The Shared Counter Class (cont’d)

class Counter { int value; NumberCanvas display; void increment() { value = value + 1; display.setvalue(value); } } aload_0 // push “this” onto stack getfield #2 // get value of “this.value” iconst_1 // push 1 onto stack iadd // add two top stack elements putfield #2 // put result into “this.value”

15

javac Counter.java javap -c Counter > Counter.bc

15

slide-48
SLIDE 48

DM519 Concurrent Programming

The Shared Counter Class (cont’d)

class Counter { int value; NumberCanvas display; void increment() { value = value + 1; display.setvalue(value); } } aload_0 // push “this” onto stack getfield #2 // get value of “this.value” iconst_1 // push 1 onto stack iadd // add two top stack elements putfield #2 // put result into “this.value”

Thread switch?

15

javac Counter.java javap -c Counter > Counter.bc

15

slide-49
SLIDE 49

DM519 Concurrent Programming

Concurrent Method Activation

Thus, threads east and west may be executing the code for the increment method at the same time.

east west

program counter program counter PC PC Shared code: Java method activation is not atomic! Counter.class: aload_0 // this getfield #2 // x iconst_1 iadd putfield #2 // x

16

16

slide-50
SLIDE 50

DM519 Concurrent Programming

Pedagogification; The Counter Class (cont’d)

class Counter { void increment() { value = value + 1; display.setvalue(value); } }

17

17

slide-51
SLIDE 51

DM519 Concurrent Programming

Pedagogification; The Counter Class (cont’d)

class Counter { void increment() { int temp = value; // read Simulate.HWinterrupt(); value = temp + 1; // write display.setvalue(value); } }

18

18

slide-52
SLIDE 52

DM519 Concurrent Programming

Pedagogification; The Counter Class (cont’d)

class Counter { void increment() { int temp = value; // read Simulate.HWinterrupt(); value = temp + 1; // write display.setvalue(value); } } The counter simulates a hardware interrupt during an increment(), between reading and writing to the shared counter value.

18

18

slide-53
SLIDE 53

DM519 Concurrent Programming

Pedagogification; The Counter Class (cont’d)

class Counter { void increment() { int temp = value; // read Simulate.HWinterrupt(); value = temp + 1; // write display.setvalue(value); } } The counter simulates a hardware interrupt during an increment(), between reading and writing to the shared counter value. class Simulate { // randomly force thread switch! public static void HWinterrupt() { if (random()<0.5) Thread.yield(); } }

18

18

slide-54
SLIDE 54

DM519 Concurrent Programming

Running The Applet

Now the erroneous behaviour occurs almost all the time!

19

19

slide-55
SLIDE 55

DM519 Concurrent Programming

Garden Model (Structure Diagram)

GARDEN

20

20

slide-56
SLIDE 56

DM519 Concurrent Programming

Garden Model (Structure Diagram)

VAR: models read and write access to the shared counter value. GARDEN

20

20

slide-57
SLIDE 57

DM519 Concurrent Programming

Garden Model (Structure Diagram)

VAR: models read and write access to the shared counter value. TURNSTILE: Increment is modelled inside TURNSTILE, since Java method activation is not atomic (i.e., thread objects east and west may interleave their read and write actions). GARDEN

20

20

slide-58
SLIDE 58

DM519 Concurrent Programming

Ornamental Garden Model (FSP)

21

21

slide-59
SLIDE 59

DM519 Concurrent Programming

Ornamental Garden Model (FSP)

const N = 4 range T = 0..N

21

21

slide-60
SLIDE 60

DM519 Concurrent Programming

Ornamental Garden Model (FSP)

const N = 4 range T = 0..N VAR = VAR[0], VAR[u:T] = (read[u] -> VAR[u] | write[v:T] -> VAR[v]).

21

21

slide-61
SLIDE 61

DM519 Concurrent Programming

Ornamental Garden Model (FSP)

const N = 4 range T = 0..N VAR = VAR[0], VAR[u:T] = (read[u] -> VAR[u] | write[v:T] -> VAR[v]). TURNSTILE = (go -> RUN), RUN = (arrive -> INCREMENT | end -> TURNSTILE), INCREMENT = (value.read[x:T] -> value.write[x+1] -> RUN)

21

21

slide-62
SLIDE 62

DM519 Concurrent Programming

Ornamental Garden Model (FSP)

const N = 4 range T = 0..N VAR = VAR[0], VAR[u:T] = (read[u] -> VAR[u] | write[v:T] -> VAR[v]). TURNSTILE = (go -> RUN), RUN = (arrive -> INCREMENT | end -> TURNSTILE), INCREMENT = (value.read[x:T] -> value.write[x+1] -> RUN) +{value.write[0]}.

21

21

slide-63
SLIDE 63

DM519 Concurrent Programming

Ornamental Garden Model (FSP)

const N = 4 range T = 0..N VAR = VAR[0], VAR[u:T] = (read[u] -> VAR[u] | write[v:T] -> VAR[v]). TURNSTILE = (go -> RUN), RUN = (arrive -> INCREMENT | end -> TURNSTILE), INCREMENT = (value.read[x:T] -> value.write[x+1] -> RUN) +{value.write[0]}. DISPLAY =(value.read[T]->DISPLAY)+{value.write[T]}.

21

21

slide-64
SLIDE 64

DM519 Concurrent Programming

Ornamental Garden Model (FSP)

const N = 4 range T = 0..N VAR = VAR[0], VAR[u:T] = (read[u] -> VAR[u] | write[v:T] -> VAR[v]). TURNSTILE = (go -> RUN), RUN = (arrive -> INCREMENT | end -> TURNSTILE), INCREMENT = (value.read[x:T] -> value.write[x+1] -> RUN) +{value.write[0]}. DISPLAY =(value.read[T]->DISPLAY)+{value.write[T]}. ||GARDEN = (east:TURNSTILE || west:TURNSTILE || display:DISPLAY || {east,west,display}::value:VAR)

21

21

slide-65
SLIDE 65

DM519 Concurrent Programming

Ornamental Garden Model (FSP)

const N = 4 range T = 0..N VAR = VAR[0], VAR[u:T] = (read[u] -> VAR[u] | write[v:T] -> VAR[v]). TURNSTILE = (go -> RUN), RUN = (arrive -> INCREMENT | end -> TURNSTILE), INCREMENT = (value.read[x:T] -> value.write[x+1] -> RUN) +{value.write[0]}. DISPLAY =(value.read[T]->DISPLAY)+{value.write[T]}. ||GARDEN = (east:TURNSTILE || west:TURNSTILE || display:DISPLAY || {east,west,display}::value:VAR) /{ go / {east,west}.go , end / {east,west}.end}.

21

21

slide-66
SLIDE 66

DM519 Concurrent Programming

Ornamental Garden Model (FSP)

const N = 4 range T = 0..N VAR = VAR[0], VAR[u:T] = (read[u] -> VAR[u] | write[v:T] -> VAR[v]). TURNSTILE = (go -> RUN), RUN = (arrive -> INCREMENT | end -> TURNSTILE), INCREMENT = (value.read[x:T] -> value.write[x+1] -> RUN) +{value.write[0]}. DISPLAY =(value.read[T]->DISPLAY)+{value.write[T]}. ||GARDEN = (east:TURNSTILE || west:TURNSTILE || display:DISPLAY || {east,west,display}::value:VAR) /{ go / {east,west}.go , end / {east,west}.end}.

α(VAR) ?

21

21

slide-67
SLIDE 67

DM519 Concurrent Programming

Ornamental Garden Model (FSP)

const N = 4 range T = 0..N VAR = VAR[0], VAR[u:T] = (read[u] -> VAR[u] | write[v:T] -> VAR[v]). TURNSTILE = (go -> RUN), RUN = (arrive -> INCREMENT | end -> TURNSTILE), INCREMENT = (value.read[x:T] -> value.write[x+1] -> RUN) +{value.write[0]}. DISPLAY =(value.read[T]->DISPLAY)+{value.write[T]}. ||GARDEN = (east:TURNSTILE || west:TURNSTILE || display:DISPLAY || {east,west,display}::value:VAR) /{ go / {east,west}.go , end / {east,west}.end}.

α(VAR) ? α(value:VAR) ?

21

21

slide-68
SLIDE 68

DM519 Concurrent Programming

Ornamental Garden Model (FSP)

const N = 4 range T = 0..N VAR = VAR[0], VAR[u:T] = (read[u] -> VAR[u] | write[v:T] -> VAR[v]). TURNSTILE = (go -> RUN), RUN = (arrive -> INCREMENT | end -> TURNSTILE), INCREMENT = (value.read[x:T] -> value.write[x+1] -> RUN) +{value.write[0]}. DISPLAY =(value.read[T]->DISPLAY)+{value.write[T]}. ||GARDEN = (east:TURNSTILE || west:TURNSTILE || display:DISPLAY || {east,west,display}::value:VAR) /{ go / {east,west}.go , end / {east,west}.end}.

α(VAR) ? α(value:VAR) ? α({east,west,display}::value:VAR) ?

21

21

slide-69
SLIDE 69

DM519 Concurrent Programming

Ornamental Garden Model (FSP)

const N = 4 range T = 0..N VAR = VAR[0], VAR[u:T] = (read[u] -> VAR[u] | write[v:T] -> VAR[v]). TURNSTILE = (go -> RUN), RUN = (arrive -> INCREMENT | end -> TURNSTILE), INCREMENT = (value.read[x:T] -> value.write[x+1] -> RUN) +{value.write[0]}. DISPLAY =(value.read[T]->DISPLAY)+{value.write[T]}. ||GARDEN = (east:TURNSTILE || west:TURNSTILE || display:DISPLAY || {east,west,display}::value:VAR) /{ go / {east,west}.go , end / {east,west}.end}.

α(VAR) ? α(TURNSTILE) ? α(value:VAR) ? α({east,west,display}::value:VAR) ?

21

21

slide-70
SLIDE 70

DM519 Concurrent Programming

Ornamental Garden Model (FSP)

const N = 4 range T = 0..N VAR = VAR[0], VAR[u:T] = (read[u] -> VAR[u] | write[v:T] -> VAR[v]). TURNSTILE = (go -> RUN), RUN = (arrive -> INCREMENT | end -> TURNSTILE), INCREMENT = (value.read[x:T] -> value.write[x+1] -> RUN) +{value.write[0]}. DISPLAY =(value.read[T]->DISPLAY)+{value.write[T]}. ||GARDEN = (east:TURNSTILE || west:TURNSTILE || display:DISPLAY || {east,west,display}::value:VAR) /{ go / {east,west}.go , end / {east,west}.end}.

α(VAR) ? α(TURNSTILE) ? α(value:VAR) ? α({east,west,display}::value:VAR) ? α(east:TURNSTILE) ?

21

21

slide-71
SLIDE 71

DM519 Concurrent Programming

Ornamental Garden Model (FSP)

const N = 4 range T = 0..N VAR = VAR[0], VAR[u:T] = (read[u] -> VAR[u] | write[v:T] -> VAR[v]). TURNSTILE = (go -> RUN), RUN = (arrive -> INCREMENT | end -> TURNSTILE), INCREMENT = (value.read[x:T] -> value.write[x+1] -> RUN) +{value.write[0]}. DISPLAY =(value.read[T]->DISPLAY)+{value.write[T]}. ||GARDEN = (east:TURNSTILE || west:TURNSTILE || display:DISPLAY || {east,west,display}::value:VAR) /{ go / {east,west}.go , end / {east,west}.end}.

α(VAR) ? α(TURNSTILE) ? α(value:VAR) ? α({east,west,display}::value:VAR) ? α(east:TURNSTILE) ?

21

α(display:DISPLAY) ?

21

slide-72
SLIDE 72

DM519 Concurrent Programming

Checking For Errors - Animation

Scenario checking - use animation to produce a trace.

22

22

slide-73
SLIDE 73

DM519 Concurrent Programming

Checking For Errors - Animation

Scenario checking - use animation to produce a trace. Is the model correct?

22

22

slide-74
SLIDE 74

DM519 Concurrent Programming

Checking For Errors - Animation

Scenario checking - use animation to produce a trace. Is the model correct?

“Never send a human to do a machine’s job”

  • Agent Smith (1999)

22

22

slide-75
SLIDE 75

DM519 Concurrent Programming

Checking For Errors - Compose With Error Detector

Exhaustive checking - compose the model with a TEST process which sums the arrivals and checks against the display value:

23

23

slide-76
SLIDE 76

DM519 Concurrent Programming

Checking For Errors - Compose With Error Detector

TEST = TEST[0], TEST[v:T] = (when (v<N) west.arrive->TEST[v+1] |when (v<N) east.arrive->TEST[v+1] |end -> CHECK[v]), Exhaustive checking - compose the model with a TEST process which sums the arrivals and checks against the display value:

23

23

slide-77
SLIDE 77

DM519 Concurrent Programming

Checking For Errors - Compose With Error Detector

TEST = TEST[0], TEST[v:T] = (when (v<N) west.arrive->TEST[v+1] |when (v<N) east.arrive->TEST[v+1] |end -> CHECK[v]), CHECK[v:T] = (display.value.read[u:T] -> (when (u==v) right -> TEST[v] |when (u!=v) wrong -> ERROR)). Exhaustive checking - compose the model with a TEST process which sums the arrivals and checks against the display value:

23

23

slide-78
SLIDE 78

DM519 Concurrent Programming

Checking For Errors - Exhaustive Analysis

||TESTGARDEN = (GARDEN || TEST). Use LTSA to perform an exhaustive search for ERROR:

24

24

slide-79
SLIDE 79

DM519 Concurrent Programming

Checking For Errors - Exhaustive Analysis

||TESTGARDEN = (GARDEN || TEST). Use LTSA to perform an exhaustive search for ERROR: Trace to property violation in TEST: go east.arrive east.value.read.0 west.arrive west.value.read.0 east.value.write.1 west.value.write.1 end display.value.read.1 wrong

24

24

slide-80
SLIDE 80

DM519 Concurrent Programming

Checking For Errors - Exhaustive Analysis

||TESTGARDEN = (GARDEN || TEST). Use LTSA to perform an exhaustive search for ERROR: Trace to property violation in TEST: go east.arrive east.value.read.0 west.arrive west.value.read.0 east.value.write.1 west.value.write.1 end display.value.read.1 wrong LTSA produces the shortest path to reach the ERROR state.

24

24

slide-81
SLIDE 81

DM519 Concurrent Programming

Interference And Mutual Exclusion

Destructive update, caused by the arbitrary interleaving of read and write actions, is termed interference.

25

25

slide-82
SLIDE 82

DM519 Concurrent Programming

Interference And Mutual Exclusion

Interference bugs are extremely difficult to locate. Destructive update, caused by the arbitrary interleaving of read and write actions, is termed interference.

25

25

slide-83
SLIDE 83

DM519 Concurrent Programming

Interference And Mutual Exclusion

Interference bugs are extremely difficult to locate. The general solution is:

  • Give methods mutually exclusive access to

shared objects. Destructive update, caused by the arbitrary interleaving of read and write actions, is termed interference.

25

25

slide-84
SLIDE 84

DM519 Concurrent Programming

Interference And Mutual Exclusion

Interference bugs are extremely difficult to locate. The general solution is:

  • Give methods mutually exclusive access to

shared objects. Mutual exclusion can be modelled as atomic actions. Destructive update, caused by the arbitrary interleaving of read and write actions, is termed interference.

25

25

slide-85
SLIDE 85

DM519 Concurrent Programming

4.2 Mutual Exclusion In Java

Concurrent activations of a method in Java can be made mutually exclusive by prefixing the method with the keyword synchronized.

26

26

slide-86
SLIDE 86

DM519 Concurrent Programming

4.2 Mutual Exclusion In Java

We correct the Counter class by deriving a class from it and making its increment method synchronized: Concurrent activations of a method in Java can be made mutually exclusive by prefixing the method with the keyword synchronized.

26

26

slide-87
SLIDE 87

DM519 Concurrent Programming

4.2 Mutual Exclusion In Java

class SynchronizedCounter extends Counter { SynchronizedCounter(NumberCanvas n) { super(n); } synchronized void increment() { super.increment(); } } We correct the Counter class by deriving a class from it and making its increment method synchronized: Concurrent activations of a method in Java can be made mutually exclusive by prefixing the method with the keyword synchronized.

26

26

slide-88
SLIDE 88

DM519 Concurrent Programming

The Garden Class (revisited)

If the fixit checkbox is ticked, the go() method creates a SynchronizedCounter: class Garden extends Applet { private void go() { if (!fixit.getState()) counter = new Counter(counterD); else counter = new SynchCounter(counterD); west = new Turnstile(westD,counter); east = new Turnstile(eastD,counter); west.start(); east.start(); } }

27

27

slide-89
SLIDE 89

DM519 Concurrent Programming

Mutual Exclusion - The Ornamental Garden

28

28

slide-90
SLIDE 90

DM519 Concurrent Programming

Mutual Exclusion - The Ornamental Garden

Java associates a lock with every object.

28

28

slide-91
SLIDE 91

DM519 Concurrent Programming

Mutual Exclusion - The Ornamental Garden

Java associates a lock with every object. The Java compiler inserts code to:

28

28

slide-92
SLIDE 92

DM519 Concurrent Programming

Mutual Exclusion - The Ornamental Garden

Java associates a lock with every object. The Java compiler inserts code to:

  • acquire the lock before executing a synchronized method

28

28

slide-93
SLIDE 93

DM519 Concurrent Programming

Mutual Exclusion - The Ornamental Garden

Java associates a lock with every object. The Java compiler inserts code to:

  • acquire the lock before executing a synchronized method
  • release the lock after the synchronized method returns.

28

28

slide-94
SLIDE 94

DM519 Concurrent Programming

Mutual Exclusion - The Ornamental Garden

Java associates a lock with every object. The Java compiler inserts code to:

  • acquire the lock before executing a synchronized method
  • release the lock after the synchronized method returns.

Concurrent threads are blocked until the lock is released.

28

28

slide-95
SLIDE 95

DM519 Concurrent Programming

Java Synchronized Statement

synchronized void increment() { super.increment(); } synchronized void decrement() { super.decrement(); } Synchronized methods:

29

29

slide-96
SLIDE 96

DM519 Concurrent Programming

Java Synchronized Statement

class Turnstile{ ... public void run() { ... synchronized(counter) { counter.increment(); } ... synchronized void increment() { super.increment(); } synchronized void decrement() { super.decrement(); } Synchronized methods: Variant - the synchronized statement :

29

29

slide-97
SLIDE 97

DM519 Concurrent Programming

Java Synchronized Statement

class Turnstile{ ... public void run() { ... synchronized(counter) { counter.increment(); } ... synchronized void increment() { super.increment(); } synchronized void decrement() { super.decrement(); } Synchronized methods: Variant - the synchronized statement :

  • bject reference

29

29

slide-98
SLIDE 98

DM519 Concurrent Programming

Java Synchronized Statement

class Turnstile{ ... public void run() { ... synchronized(counter) { counter.increment(); } ... synchronized void increment() { super.increment(); } synchronized void decrement() { super.decrement(); } Synchronized methods: Variant - the synchronized statement :

Use synch methods whenever possible.

  • bject reference

29

29

slide-99
SLIDE 99

DM519 Concurrent Programming

Java -> Java Bytecode

30

30

slide-100
SLIDE 100

DM519 Concurrent Programming

Java -> Java Bytecode

1 class X { 2 int x; 3 void m() { 4 synchronized(this) { 5 x++; 6 } 7 } 8 } 30

30

slide-101
SLIDE 101

DM519 Concurrent Programming

Java -> Java Bytecode

Method void m() >> max_stack=3, max_locals=3 << 0 aload_0 1 dup 2 astore_1 3 monitorenter 4 aload_0 5 dup 6 getfield #2 <Field X.x:int> 9 iconst_1 10 iadd 11 putfield #2 <Field X.x:int> 14 aload_1 15 monitorexit 16 goto 24 19 astore_2 20 aload_1 21 monitorexit 22 aload_2 23 athrow 24 return Exception table: from to target type 4 16 19 any 19 22 19 any 1 class X { 2 int x; 3 void m() { 4 synchronized(this) { 5 x++; 6 } 7 } 8 }

compile

30

30

slide-102
SLIDE 102

DM519 Concurrent Programming

4.3 Modelling Mutual Exclusion

||GARDEN = (east:TURNSTILE || west:TURNSTILE || {east,west,display}::value:LOCKVAR)

31

31

slide-103
SLIDE 103

DM519 Concurrent Programming

Define a mutual exclusion LOCK process:

4.3 Modelling Mutual Exclusion

LOCK = (acq -> rel -> LOCK).

||GARDEN = (east:TURNSTILE || west:TURNSTILE || {east,west,display}::value:LOCKVAR)

31

31

slide-104
SLIDE 104

DM519 Concurrent Programming

Define a mutual exclusion LOCK process:

4.3 Modelling Mutual Exclusion

LOCK = (acq -> rel -> LOCK). ||LOCKVAR = (LOCK || VAR). …and compose it with the shared VAR in the Garden:

||GARDEN = (east:TURNSTILE || west:TURNSTILE || {east,west,display}::value:LOCKVAR)

31

31

slide-105
SLIDE 105

DM519 Concurrent Programming

Define a mutual exclusion LOCK process:

4.3 Modelling Mutual Exclusion

LOCK = (acq -> rel -> LOCK). TURNSTILE = (go -> RUN), RUN = (arrive -> INCREMENT | end -> TURNSTILE), INCREMENT = (value.acq

  • > value.read[x:T]
  • > value.write[x+1]
  • > value.rel->RUN )+{value.write[0]}.

Modify TURNSTILE to acquire and release the lock: ||LOCKVAR = (LOCK || VAR). …and compose it with the shared VAR in the Garden:

||GARDEN = (east:TURNSTILE || west:TURNSTILE || {east,west,display}::value:LOCKVAR)

31

31

slide-106
SLIDE 106

DM519 Concurrent Programming

Revised Ornamental Garden Model - Checking For Errors

A sample trace: go east.arrive east.value.acq east.value.read.0 east.value.write.1 east.value.rel west.arrive west.value.acq west.value.read.1 west.value.write.2 west.value.rel end display.value.read.2 right

32

32

slide-107
SLIDE 107

DM519 Concurrent Programming

Revised Ornamental Garden Model - Checking For Errors

A sample trace: Use LTSA to perform an exhaustive check: “is TEST satisfied”? go east.arrive east.value.acq east.value.read.0 east.value.write.1 east.value.rel west.arrive west.value.acq west.value.read.1 west.value.write.2 west.value.rel end display.value.read.2 right

32

32

slide-108
SLIDE 108

DM519 Concurrent Programming

COUNTER: Abstraction Using Action Hiding

const N = 4 range T = 0..N VAR = VAR[0], VAR[u:T] = ( read[u]->VAR[u] | write[v:T]->VAR[v]). LOCK = (acquire->release->LOCK). INCREMENT = (acquire->read[x:T]

  • > write[x+1]
  • > release->increment->INCREMENT)

+{read[T],write[T]}. ||COUNTER = (INCREMENT||LOCK||VAR)@{increment}.

33

33

slide-109
SLIDE 109

DM519 Concurrent Programming

COUNTER: Abstraction Using Action Hiding

We can abstract the details by hiding. For SynchronizedCounter we hide read, write, acquire, release actions. const N = 4 range T = 0..N VAR = VAR[0], VAR[u:T] = ( read[u]->VAR[u] | write[v:T]->VAR[v]). LOCK = (acquire->release->LOCK). INCREMENT = (acquire->read[x:T]

  • > write[x+1]
  • > release->increment->INCREMENT)

+{read[T],write[T]}. ||COUNTER = (INCREMENT||LOCK||VAR)@{increment}.

33

33

slide-110
SLIDE 110

DM519 Concurrent Programming

COUNTER: Abstraction Using Action Hiding

COUNTER = COUNTER[0] COUNTER[v:T] = (when (v<N) increment -> COUNTER[v+1]).

34

34

slide-111
SLIDE 111

DM519 Concurrent Programming

COUNTER: Abstraction Using Action Hiding

Minimised LTS: COUNTER = COUNTER[0] COUNTER[v:T] = (when (v<N) increment -> COUNTER[v+1]).

34

34

slide-112
SLIDE 112

DM519 Concurrent Programming

COUNTER: Abstraction Using Action Hiding

Minimised LTS: We can give a more abstract, simpler description of a COUNTER which generates the same LTS: COUNTER = COUNTER[0] COUNTER[v:T] = (when (v<N) increment -> COUNTER[v+1]).

34

34

slide-113
SLIDE 113

DM519 Concurrent Programming

COUNTER: Abstraction Using Action Hiding

Minimised LTS: We can give a more abstract, simpler description of a COUNTER which generates the same LTS: This therefore exhibits “equivalent” behaviour, i.e., has the same

  • bservable behaviour.

COUNTER = COUNTER[0] COUNTER[v:T] = (when (v<N) increment -> COUNTER[v+1]).

34

34

slide-114
SLIDE 114

DM519 Concurrent Programming

Active & Passive Processes

35

35

slide-115
SLIDE 115

DM519 Concurrent Programming

Active & Passive Processes

35

Comparing FSP and Java – active processes : threads, e.g., TURNSTILE – passive processes: shared objects, e.g., COUNTER

const N = 4 range T = 0..N set VarAlpha = {value.{read[T],write[T],acquire,release}} VAR = VAR[0], VAR[u:T] = (read[u]->VAR[u] | write[v:T]->VAR[v]). LOCK = (acquire->release->LOCK). ||LOCKVAR = (LOCK || VAR). TURNSTILE = (go -> RUN), RUN = (arrive-> INCREMENT | end -> TURNSTILE), INCREMENT = (value.acquire

  • > value.read[x:T]->value.write[x+1]
  • >value.release->RUN)+VarAlpha.

DISPLAY =(value.read[T]->DISPLAY)+{value.{write[T],acquire,release}}. ||GARDEN = (east:TURNSTILE || west:TURNSTILE || display:DISPLAY || {east,west,display}::value:LOCKVAR) /{go /{east,west}.go, end/{east,west}.end}.

35

slide-116
SLIDE 116

DM519 Concurrent Programming

Java Memory Model

36

public class NoVisibility { private static boolean ready; private static int number; private static class ReaderThread extends Thread { public void run() { while (!ready) { yield(); } System.out.println(number); } } public static void main(String[] args) { new ReaderThread().start(); number = 42; ready = true; } }

36

slide-117
SLIDE 117

DM519 Concurrent Programming

Synchronisation In Java Is Not Just Mutual Exclusion; It’s Also About Memory Visibility

37

y=1 lock M x=1 unlock M Thread A Everything before the unlock on M Without synchronisation, there is no such guarantee.

37

slide-118
SLIDE 118

DM519 Concurrent Programming

Synchronisation In Java Is Not Just Mutual Exclusion; It’s Also About Memory Visibility

37

y=1 lock M x=1 unlock M lock M i=x unlock M j=y Thread A Thread B Everything before the unlock on M is visible to everything after the lock

  • n M

Without synchronisation, there is no such guarantee.

37

slide-119
SLIDE 119

DM519 Concurrent Programming

Synchronisation In Java Is Not Just Mutual Exclusion; It’s Also About Memory Visibility

37

y=1 lock M x=1 unlock M lock M i=x unlock M j=y Thread A Thread B Everything before the unlock on M is visible to everything after the lock

  • n M

Without synchronisation, there is no such guarantee. Must be the same lock

37

slide-120
SLIDE 120

DM519 Concurrent Programming

Summary

38

38

slide-121
SLIDE 121

DM519 Concurrent Programming

Summary

uConcepts

lprocess interference lmutual exclusion

38

38

slide-122
SLIDE 122

DM519 Concurrent Programming

Summary

uConcepts

lprocess interference lmutual exclusion

uModels

lmodel checking for interference lmodelling mutual exclusion

38

38

slide-123
SLIDE 123

DM519 Concurrent Programming

Summary

uConcepts

lprocess interference lmutual exclusion

uModels

lmodel checking for interference lmodelling mutual exclusion

uPractice

lthread interference in shared Java objects lmutual exclusion in Java (synchronized objects/methods).

38

38