shared objects mutual exclusion
play

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


  1. Chapter 4: Shared Objects & Mutual Exclusion u Concepts: l Process interference l Mutual exclusion DM519 Concurrent Programming 7 7

  2. 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 DM519 Concurrent Programming 7 7

  3. 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 DM519 Concurrent Programming 7 7

  4. 4.1 Interference The ”Ornamental Garden Problem ”: 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). 0 Counter DM519 Concurrent Programming 8 8

  5. 4.1 Interference The ”Ornamental Garden Problem ”: 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). 1 0 Counter DM519 Concurrent Programming 8 8

  6. 4.1 Interference The ”Ornamental Garden Problem ”: 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). 1 0 2 Counter DM519 Concurrent Programming 8 8

  7. 4.1 Ornamental Garden Problem (cont’d) Counter DM519 Concurrent Programming 9 9

  8. 4.1 Ornamental Garden Problem (cont’d) Counter Java implementation: DM519 Concurrent Programming 9 9

  9. 4.1 Ornamental Garden Problem (cont’d) Counter Java implementation: The concurrent program consists of: DM519 Concurrent Programming 9 9

  10. 4.1 Ornamental Garden Problem (cont’d) Counter Java implementation: The concurrent program consists of: • two concurrent threads (west & east); and DM519 Concurrent Programming 9 9

  11. 4.1 Ornamental Garden Problem (cont’d) Counter Java implementation: The concurrent program consists of: • two concurrent threads (west & east); and 2 • a shared counter object DM519 Concurrent Programming 9 9

  12. Class Diagram 20 40 20 counter DM519 Concurrent Programming 10 10

  13. Ornamental Garden Program The go() method of the Garden applet… 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(); } } …creates the shared Counter object & the Turnstile threads. DM519 Concurrent Programming 11 11

  14. 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 _) {} } } DM519 Concurrent Programming 12 12

  15. The Turnstile Class class Turnstile extends Thread { NumberCanvas display; The Turnstile thread simulates periodic Counter counter; arrival of visitors by invoking the counter public void run() { object’s increment() method every second try { display.setvalue(0); for (int i=1; i<=Garden.MAX; i++) { Thread.sleep(1000); display.setvalue(i); counter.increment(); } } catch (InterruptedException _) {} } } DM519 Concurrent Programming 12 12

  16. The Shared Counter Class The increment() method of the Counter class increments its internal value and updates the display. class Counter { int value; NumberCanvas display; void increment() { value = value + 1; display.setvalue(value); } } DM519 Concurrent Programming 13 13

  17. Running The Applet 39 20 20 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. DM519 Concurrent Programming 14 14

  18. Running The Applet 39 20 20 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? DM519 Concurrent Programming 14 14

  19. The Shared Counter Class (cont’d) class Counter { int value; NumberCanvas display; void increment() { value = value + 1; display.setvalue(value); } } DM519 Concurrent Programming 15 15

  20. The Shared Counter Class (cont’d) class Counter { int value; NumberCanvas display; void increment() { value = value + 1; display.setvalue(value); } javac Counter.java } javap -c Counter > Counter.bc DM519 Concurrent Programming 15 15

  21. The Shared Counter Class (cont’d) class Counter { int value; NumberCanvas display; void increment() { value = value + 1; display.setvalue(value); } javac Counter.java } javap -c Counter > Counter.bc DM519 Concurrent Programming 15 15

  22. The Shared Counter Class (cont’d) class Counter { int value; NumberCanvas display; void increment() { value = value + 1; display.setvalue(value); } javac Counter.java } javap -c Counter > Counter.bc 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” DM519 Concurrent Programming 15 15

  23. The Shared Counter Class (cont’d) class Counter { int value; NumberCanvas display; void increment() { value = value + 1; display.setvalue(value); } javac Counter.java } javap -c Counter > Counter.bc aload_0 // push “this” onto stack getfield #2 // get value of “this.value” Thread switch ? iconst_1 // push 1 onto stack iadd // add two top stack elements putfield #2 // put result into “this.value” DM519 Concurrent Programming 15 15

  24. Concurrent Method Activation Java method activation is not atomic ! Thus, threads east and west may be executing the code for the increment method at the same time. west Shared code: east Counter.class: PC PC program aload_0 // this program counter getfield #2 // x counter iconst_1 iadd putfield #2 // x DM519 Concurrent Programming 16 16

  25. Pedagogification; The Counter Class (cont’d) class Counter { void increment() { value = value + 1; display.setvalue(value); } } DM519 Concurrent Programming 17 17

  26. Pedagogification; The Counter Class (cont’d) class Counter { void increment() { int temp = value; // read Simulate.HWinterrupt(); value = temp + 1; // write display.setvalue(value); } } DM519 Concurrent Programming 18 18

  27. 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 . DM519 Concurrent Programming 18 18

  28. 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(); } } DM519 Concurrent Programming 18 18

  29. Running The Applet Now the erroneous behaviour occurs almost all the time! DM519 Concurrent Programming 19 19

  30. Garden Model (Structure Diagram) GARDEN DM519 Concurrent Programming 20 20

  31. Garden Model (Structure Diagram) GARDEN VAR: models read and write access to the shared counter value. DM519 Concurrent Programming 20 20

  32. Garden Model (Structure Diagram) GARDEN 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 ) . DM519 Concurrent Programming 20 20

  33. Ornamental Garden Model (FSP) DM519 Concurrent Programming 21 21

  34. Ornamental Garden Model (FSP) const N = 4 range T = 0..N DM519 Concurrent Programming 21 21

  35. 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]). DM519 Concurrent Programming 21 21

  36. 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) DM519 Concurrent Programming 21 21

  37. 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]}. DM519 Concurrent Programming 21 21

  38. 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]}. DM519 Concurrent Programming 21 21

  39. 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) DM519 Concurrent Programming 21 21

  40. 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}. DM519 Concurrent Programming 21 21

  41. 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) ? DM519 Concurrent Programming 21 21

  42. 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) ? DM519 Concurrent Programming 21 21

  43. 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) ? DM519 Concurrent Programming 21 21

  44. 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) ? α (TURNSTILE) ? DM519 Concurrent Programming 21 21

  45. 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) ? α (TURNSTILE) ? α (east:TURNSTILE) ? DM519 Concurrent Programming 21 21

  46. 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) ? α (TURNSTILE) ? α (east:TURNSTILE) ? α (display:DISPLAY) ? DM519 Concurrent Programming 21 21

  47. Checking For Errors - Animation Scenario checking - use animation to produce a trace. DM519 Concurrent Programming 22 22

  48. Checking For Errors - Animation Scenario checking - use animation to produce a trace. Is the model correct? DM519 Concurrent Programming 22 22

  49. 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) DM519 Concurrent Programming 22 22

  50. 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: DM519 Concurrent Programming 23 23

  51. 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: 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]), DM519 Concurrent Programming 23 23

  52. 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: 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)). DM519 Concurrent Programming 23 23

  53. Checking For Errors - Exhaustive Analysis ||TESTGARDEN = (GARDEN || TEST). Use LTSA to perform an exhaustive search for ERROR : DM519 Concurrent Programming 24 24

  54. 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 DM519 Concurrent Programming 24 24

  55. 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 LTSA produces west.arrive the shortest west.value.read.0 path to reach east.value.write.1 the ERROR state. west.value.write.1 end display.value.read.1 wrong DM519 Concurrent Programming 24 24

  56. Interference And Mutual Exclusion Destructive update, caused by the arbitrary interleaving of read and write actions, is termed interference . DM519 Concurrent Programming 25 25

  57. Interference And Mutual Exclusion Destructive update, caused by the arbitrary interleaving of read and write actions, is termed interference . Interference bugs are extremely difficult to locate. DM519 Concurrent Programming 25 25

  58. Interference And Mutual Exclusion Destructive update, caused by the arbitrary interleaving of read and write actions, is termed interference . Interference bugs are extremely difficult to locate. The general solution is: • Give methods mutually exclusive access to shared objects. DM519 Concurrent Programming 25 25

  59. Interference And Mutual Exclusion Destructive update, caused by the arbitrary interleaving of read and write actions, is termed interference . 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. DM519 Concurrent Programming 25 25

  60. 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 . DM519 Concurrent Programming 26 26

  61. 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 . We correct the Counter class by deriving a class from it and making its increment method synchronized: DM519 Concurrent Programming 26 26

  62. 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 . We correct the Counter class by deriving a class from it and making its increment method synchronized: class SynchronizedCounter extends Counter { SynchronizedCounter(NumberCanvas n) { super(n); } synchronized void increment() { super.increment(); } } DM519 Concurrent Programming 26 26

  63. 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(); } } DM519 Concurrent Programming 27 27

  64. Mutual Exclusion - The Ornamental Garden DM519 Concurrent Programming 28 28

  65. Mutual Exclusion - The Ornamental Garden Java associates a lock with every object. DM519 Concurrent Programming 28 28

  66. Mutual Exclusion - The Ornamental Garden Java associates a lock with every object. The Java compiler inserts code to: DM519 Concurrent Programming 28 28

  67. 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 DM519 Concurrent Programming 28 28

  68. 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. DM519 Concurrent Programming 28 28

  69. 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. DM519 Concurrent Programming 28 28

  70. Java Synchronized Statement Synchronized methods: synchronized void increment() { super.increment(); } synchronized void decrement() { super.decrement(); } DM519 Concurrent Programming 29 29

  71. Java Synchronized Statement Synchronized methods: synchronized void increment() { super.increment(); } synchronized void decrement() { super.decrement(); } Variant - the synchronized statement : class Turnstile{ ... public void run() { ... synchronized(counter) { counter.increment(); } ... DM519 Concurrent Programming 29 29

  72. Java Synchronized Statement Synchronized methods: synchronized void increment() { super.increment(); } synchronized void decrement() { super.decrement(); } Variant - the synchronized statement : object reference class Turnstile{ ... public void run() { ... synchronized(counter) { counter.increment(); } ... DM519 Concurrent Programming 29 29

  73. Java Synchronized Statement Synchronized methods: synchronized void increment() { super.increment(); } synchronized void decrement() { super.decrement(); } Variant - the synchronized statement : object reference class Turnstile{ Use synch methods ... public void run() { whenever possible. ... synchronized(counter) { counter.increment(); } ... DM519 Concurrent Programming 29 29

  74. Java -> Java Bytecode DM519 Concurrent Programming 30 30

  75. Java -> Java Bytecode 1 class X { 2 int x; 3 void m() { 4 synchronized(this) { 5 x++; 6 } 7 } 8 } DM519 Concurrent Programming 30 30

Recommend


More recommend