Presentation is loading. Please wait.

Presentation is loading. Please wait.

9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics.

Similar presentations


Presentation on theme: "9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics."— Presentation transcript:

1 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

2 9/10/2015 CS2104, Lecture 8 2 Reminder of the Last Lecture abstract data types declarative concurrency design a concurrent program execution of concurrent programs

3 9/10/2015 CS2104, Lecture 8 3 Overview Thread Semantics Extend the abstract machine to execute multiple threads

4 9/10/2015CS2104, Lecture 84 Thread Semantics

5 9/10/2015 CS2104, Lecture 8 5 Semantics for Threads. (Interleaving Semantics) Interleaving: two different threads can interleave their computation steps Interleaving semantics:  Language viewpoint: threads can do interleaving execution; each computation step is atomic.  Implementation viewpoint: might execute several threads in parallel, however must execute as if one thread at a time; that is, whatever the parallel execution is, there is always at least one interleaving that is observationally equivalent to it.

6 9/10/2015 CS2104, Lecture 8 6 Causal Order of Computation Steps For a given program, all computation steps form a partial order, called the causal order. A computation step occurs before another step if in all possible executions of the program it happens before the other. Similarly for a computation step that occurs after another step. Sometimes a step is neither before nor after another step, that is, the two steps are concurrent.

7 9/10/2015 CS2104, Lecture 8 7 Causal Order and Interleaving Executions

8 9/10/2015 CS2104, Lecture 8 8 Declarative Nondeterminism Nondeterministic execution: there is an execution state in which there is a choice of what to do next, i.e., a choice of which thread to reduce. Example: After I a, there is a choice of either I 1 or I b Declarative concurrent model: the nondeterminism isn’t visible.  dataflow variables can be bound to only one value. The nondeterminism affects only the exact moment when each binding takes place; it does not affect the plain fact that the binding does take place.  any operation that needs the value of a variable has no choice but to wait until the variable is bound.

9 9/10/2015 CS2104, Lecture 8 9 Semantics for Threads (Scheduling) The choice of which thread to execute next is done by the scheduler. A thread is ready (or runnable), if its statement has all the information it needs to execute at least one computation step. Once a thread is ready, it stays ready indefinitely. So, thread reduction in declarative concurrent model is monotonic. A thread is suspended (or blocked), if it is not ready. That is, its first statement cannot continue because it does not have all the information it needs.

10 9/10/2015 CS2104, Lecture 8 10 Monotonicity (Example) thread if B then … else … end end When B is bound, thread will eventually run When B is bound, the value of B is fixed  value of B is independent of when thread executes

11 9/10/2015 CS2104, Lecture 8 11 Monotonicity: Simplicity Result is scheduling independent  unless attempt to put inconsistent information to store  Example: non-determinism ( failure exception) thread X=1 end thread X=2 end  which value for X ? Different with explicit mutable state (JAVA)  if variable values changed over time, result would depend on order in which threads run

12 9/10/2015 CS2104, Lecture 8 12 Dependencies Suspension and resumption driven by variable bindings Progress, only if for each variable, its value is supplied Typical error-scenario: deadlock  thread depends on X, supposed to bind Y  thread depends on Y, supposed to bind X Example: declare X Y thread if X==1 then Y=1 {Browse 1} end end thread if Y==1 then X=1 {Browse 2} end end

13 9/10/2015 CS2104, Lecture 8 13 Extend Abstract Machine Extend to execute multiple threads  shared store: all threads share common store  semantic stack: corresponds to a thread  thread creation: create a new semantic stack Orthogonal: scheduling policy  scheduling policy: which thread to execute?  consider only non-suspended threads!

14 9/10/2015 CS2104, Lecture 8 14 Abstract Machine Concepts (Reminder) Single-assignment store Environment Semantic statement Execution state Computation

15 9/10/2015 CS2104, Lecture 8 15 Abstract Machine (Reminder) Performs a computation Computation is a sequence of execution states Execution state  stack of semantic statements  single assignment store Semantic statement  statement  environment Environment maps variable identifiers to store entities

16 9/10/2015 CS2104, Lecture 8 16 Single Assignment Store (Reminder) Single assignment store   set of store variables  partitioned into sets of variables that are equal but unbound variables bound to value Example store{x 1, x 2 =x 3, x 4 = a |x 2 }  x 1 unbound  x 2, x 3 equal and unbound  x 4 bound to partial value a |x 2

17 9/10/2015 CS2104, Lecture 8 17 Environment (Reminder) EnvironmentE  maps variable identifiers to entities in store   written as set of pairs X  x variable identifier X store variablex Example environment{ X  x, Y  y }  maps identifier X to store variable x  maps identifier Y to store variable y

18 9/10/2015 CS2104, Lecture 8 18 Environment and Store (Reminder) Given: environment E, store  Looking up value for variable identifier X :  find store variable in environmentE( X )  take value from  for E( X ) Example:  ={x 1, x 2 =x 3, x 4 = a |x 2 } E = { X  x 1, Y  x 4 }  E( X ) = x 1 and no information in  on x 1  E( Y ) = x 4 and  binds x 4 to a |x 2

19 9/10/2015 CS2104, Lecture 8 19 Environment Adjunction (Reminder) Given: Environment E E + {  x  1  x 1, …,  x  n  x n } is new environment E ’ with mappings added:  always take store entity from new mappings  might overwrite old mappings

20 9/10/2015 CS2104, Lecture 8 20 Semantic Statements (Reminder) To actually execute statement:  environment to map identifiers modified with execution of each statement each statement has its own environment  store to find values all statements modify same store single store Semantic statement(  s , E )  pair of (statement, environment)

21 9/10/2015 CS2104, Lecture 8 21 Semantic Stack (Reminder) Execution maintains stack of semantic statementsST [(  s  1, E 1 ), …, (  s  n, E n )]  always topmost statement (  s  1, E 1 ) executes first  rest of stack: what needs to be done

22 9/10/2015 CS2104, Lecture 8 22 Semantic Stack States (Reminder) Semantic stack can be in run-time states  terminatedstack is empty  runnablecan do execution step  suspendedstack not empty, no execution step possible Statements  non-suspendingcan always execute  suspendingneed values from store dataflow behavior

23 9/10/2015 CS2104, Lecture 8 23 Summary Single assignment store  EnvironmentsE  adjunctionE + {…} Semantic statements(  s , E) Semantic stacks[(  s , E) … ] Execution state(ST,  ) Program execution  runnable, terminated, suspended Statements  suspending, non-suspending

24 9/10/2015 CS2104, Lecture 8 24 Executing if (Reminder) Semantic statement is ( if  x  then  s  1 else  s  2 end, E) If activation condition “  x  bound” is true  if E(  x  ) bound to true push  s  1  if E(  x  ) bound to false push  s  2  otherwise, raise error Otherwise, suspend…

25 9/10/2015 CS2104, Lecture 8 25 Procedure Call (Reminder) Semantic statement is ( {  x   y  1 …  y  n }, E) where  E(  x  ) is to be called   y  1, …,  y  n are actual parameters Suspending statement, suspension condition  E(  x  ) is determined

26 9/10/2015 CS2104, Lecture 8 26 Summary so far Semantic statement executes by  popping itselfalways  creating environment local  manipulating store local, =  pushing new statements local, if sequential composition Semantic statement can suspend  activation condition if, {…}, case  read store

27 9/10/2015 CS2104, Lecture 8 27 Multiple Semantic Stacks Abstract machine has multiple semantic stacks  each semantic stack represents one thread Number of semantic stacks change over time  increases:new threads created  decreases:threads terminate

28 9/10/2015 CS2104, Lecture 8 28 Multisets Collection of semantic stacks called multiset of semantic stacks Multiset: like a set, but maintains multiplicity  ordinary set: element can be contained at most once {1,2,3}  multiset: element can be contained many times {1,1,1,2,2,3} is different from {1,1,2,3,3}  just think of: bag, collection, bunch of something  same thread is allowed to occur more than once

29 9/10/2015 CS2104, Lecture 8 29 Execution State ( Multiset of semantic stacks, store ) ({ ST 1, …, ST n },  ) we write multisets with normal set parentheses: { and }

30 9/10/2015 CS2104, Lecture 8 30 Execution State ( Multiset of semantic stacks, store ) ({ ST 1, …, ST n },  ) MST Multisets of stacks are denotedMST

31 9/10/2015 CS2104, Lecture 8 31 Initial Execution State Given statement  s , start execution as before with empty environment, empty store and just one thread ( { [ (  s ,  ) ] },  )

32 9/10/2015 CS2104, Lecture 8 32 Initial Execution State Given statement  s , start execution as before with empty environment, empty store and just one thread ( { [ (  s ,  ) ] },  ) execution state

33 9/10/2015 CS2104, Lecture 8 33 Initial Execution State Given statement  s , start execution as before with empty environment, empty store and just one thread ( { [ (  s ,  ) ] },  ) store (empty)

34 9/10/2015 CS2104, Lecture 8 34 Initial Execution State Given statement  s , start execution as before with empty environment, empty store and just one thread ( { [ (  s ,  ) ] },  ) multiset of threads (just one)

35 9/10/2015 CS2104, Lecture 8 35 Initial Execution State Given statement  s , start execution as before with empty environment, empty store and just one thread ( { [ (  s ,  ) ] },  ) thread (just one)

36 9/10/2015 CS2104, Lecture 8 36 Initial Execution State Given statement  s , start execution as before with empty environment, empty store and just one thread ( { [ (  s ,  ) ] },  ) semantic statement

37 9/10/2015 CS2104, Lecture 8 37 Initial Execution State Given statement  s , start execution as before with empty environment, empty store and just one thread ( { [ (  s ,  ) ] },  ) statement (initial)

38 9/10/2015 CS2104, Lecture 8 38 Initial Execution State Given statement  s , start execution as before with empty environment, empty store and just one thread ( { [ (  s ,  ) ] },  ) empty environment

39 9/10/2015 CS2104, Lecture 8 39 Execution Execution steps (MST 1,  1 )  (MST 2,  2 )  … At each step  select runnable semantic stack ST i from MST i  execute topmost semantic statement of ST i resulting in ST’ i  continue with threads MST i+1 = {ST’ i }  (MST i – {ST i })

40 9/10/2015 CS2104, Lecture 8 40 Execution Execution steps (MST 1,  1 )  (MST 2,  2 )  … At each step  select runnable semantic stack ST i from MST i  execute topmost semantic statement of ST i resulting in ST’ i  continue with threads MST i+1 = {ST’ i }  (MST i – {ST i })

41 9/10/2015 CS2104, Lecture 8 41 Statements  s  ::= skip |  x  =  y  |  x  =  v  |  s  1  s  2 | local  x  in  s  end | if  x  then  s  1 else  s  2 end | {  x   y  1 …  y  n }

42 9/10/2015 CS2104, Lecture 8 42 Statements with Thread Creation  s  ::= skip |  x  =  y  |  x  =  v  |  s  1  s  2 | local  x  in  s  end | if  x  then  s  1 else  s  2 end | {  x   y  1 …  y  n } | thread  s  end

43 9/10/2015 CS2104, Lecture 8 43 Sketch of Computation Multiple threads sharing store  ST 1 ST n …

44 9/10/2015 CS2104, Lecture 8 44 Sketch of Computation Thread creation statement…  ST n … ST i (thread  s  end, E )

45 9/10/2015 CS2104, Lecture 8 45 Sketch of Computation …new semantic stack running  s   ST n … ST i (  s ,E)

46 9/10/2015 CS2104, Lecture 8 46 Example local B X in thread if B then X=1 else X=2 end end B=true end

47 9/10/2015 CS2104, Lecture 8 47 The Abstract Machine with Threads ( { [ ( local B X in thread if B then X=1 else X=2 end end B=true end,  ) ] },  ) We need to apply the local statement

48 9/10/2015 CS2104, Lecture 8 48 The Abstract Machine with Threads ( { [ ( thread if B then X=1 else X=2 end end B=true, { B  b, X  x }) ] }, { b, x } ) We need to apply the sequential composition

49 9/10/2015 CS2104, Lecture 8 49 The Abstract Machine with Threads ( { [ ( thread if B then X=1 else X=2 end end, { B  b, X  x }) ( B=true, { B  b, X  x }) ] }, { b, x } ) We need to apply thread … end

50 9/10/2015 CS2104, Lecture 8 50 The Abstract Machine with Threads ( { [ ( if B then X=1 else X=2 end, { B  b, X  x }) ] [ ( B=true, { B  b, X  x }) ] }, { b, x } ) We need to select a runnable semantic stack from the multiset of semantic stacks First semantic stack cannot run because B is unbound!

51 9/10/2015 CS2104, Lecture 8 51 The Abstract Machine with Threads ( { [ ( if B then X=1 else X=2 end, { B  b, X  x }) ] [ ] }, { b= true, x } ) After we run the second semantic stack The first semantic stack can run now because the if statement is not suspended anymore!

52 9/10/2015 CS2104, Lecture 8 52 The Abstract Machine with Threads ( { [ ( X=1, { B  b, X  x }) ] [ ] }, { b= true, x } ) We need to execute the variable-value equality

53 9/10/2015 CS2104, Lecture 8 53 The Abstract Machine with Threads ( { [ ] [ ] }, { b= true, x= 1 } ) We conclude that B and X have been bound to true and 1, respectively.

54 9/10/2015 CS2104, Lecture 8 54 Statements with Thread Creation  s  ::= skip |  x  =  y  |  x  =  v  |  s  1  s  2 | local  x  in  s  end | if  x  then  s  1 else  s  2 end | {  x   y  1 …  y  n } | thread  s  end | {ByNeed  x   y  }

55 9/10/2015 CS2104, Lecture 8 55 Sketch of Computation Thread creation statement…  ST n … ST i ({ByNeed  x   y  }, E )

56 9/10/2015 CS2104, Lecture 8 56 Sketch of Computation The store is  the variable-store +  the trigger- store  ST n … ST i ({ByNeed  x   y  }, E ) 

57 9/10/2015 CS2104, Lecture 8 57 Triggers A by-need trigger is a pair trig(F, X) :  a zero-argument function F  a variable X Additionally to single-assignment store , we consider a new store , called the trigger-store The trigger store contains all by-need triggers and is initially empty. The execution state becomes a triple (MST, ,  ).

58 9/10/2015 CS2104, Lecture 8 58 Trigger Creation (Executing ByNeed ) Semantic statement is ({ ByNeed  x   y  }, E)   x  is a one-argument procedure   y  is a variable If  y  is not determined (unbound)  Add the trigger trig( E(  x  ), E(  y  ) ) to the trigger store If  y  is determined (bound)  Create a new thread with initial semantic stack [({  x   y  }, E)]

59 9/10/2015 CS2104, Lecture 8 59 Trigger Activation (Executing ByNeed ) If trig(x, y) is in the trigger-store and a need on y is detected, then  there is either a thread that is suspended waiting for y to be determined, or  an attempt to bind y to make it determined then  Remove the trigger from the trigger store.  Create a new thread with initial semantic stack: [ ({ },{  x,  y}) ]  {…} is a procedure call!

60 9/10/2015 CS2104, Lecture 8 60 The Abstract Machine with By-Need ( { [ ( local X Z in X={ByNeed fun {$} 4 end} {Browse X} Z=X+1 end,  ) ] }, ,  )  is the trigger-store We need to apply the local statement

61 9/10/2015 CS2104, Lecture 8 61 The Abstract Machine with By-Need ( { [ ( X={ByNeed fun {$} 4 end} {Browse X} Z=X+1, { X  x, Z  z }) ] }, { x, z },  ) We need to apply the sequential composition

62 9/10/2015 CS2104, Lecture 8 62 The Abstract Machine with By-Need ( { [ ( X={ByNeed fun {$} 4 end}, { X  x, Z  z }) ( {Browse X} Z=X+1, { X  x, Z  z }) ] }, { x, z },  ) We need to apply the trigger creation

63 9/10/2015 CS2104, Lecture 8 63 The Abstract Machine with By-Need ( { [( {Browse X} Z=X+1, { X  x, Z  z }) ] }, { x, z }, trig ( fun {$} 4 end, x )) We need to apply the sequential composition

64 9/10/2015 CS2104, Lecture 8 64 The Abstract Machine with By-Need ( { [( {Browse X}, { X  x, Z  z }) ( Z=X+1, { X  x, Z  z }) ] }, { x, z }, trig ( fun {$} 4 end, x )) Browse visualizes the single-assignment store in the Browser window In fact, Browse is running in a system’s thread!

65 9/10/2015 CS2104, Lecture 8 65 The Abstract Machine with By-Need ( { [( {Browse X}, { X  x, Z  z })] [( Z=X+1, { X  x, Z  z })] }, { x, z }, trig ( fun {$} 4 end, x )) From this multiset of threads, Browse is suspended since X is unbound Executing the second semantic stack, X is needed because it appears in the right-hand side of Z=X+1, so trig (…, x ) is removed from  and we create a new thread

66 9/10/2015 CS2104, Lecture 8 66 The Abstract Machine with By-Need ( { [({ fun {$} 4 end X },{ X  x })] [( {Browse X}, { X  x, Z  z })] [( Z=X+1, { X  x, Z  z })] }, { x, z },  ) The second and third semantic stacks are suspended We executed the first semantic stack

67 9/10/2015 CS2104, Lecture 8 67 Zero-Argument Function’s Call local X in {fun {$} 4 end X} {Browse X} end X is an output argument for an anonymous procedure  4 4 Is equivalent with local X Fun1 in proc {Fun1 Result1} Result1 = 4 end {Fun1 X} {Browse X} end

68 9/10/2015 CS2104, Lecture 8 68 The Abstract Machine with By-Need ( {[( {Browse X}, { X  x, Z  z })] [( Z=X+1, { X  x, Z  z })] }, { x= 4, z },  ) Any thread can run now If executing the first semantic stack, then the browser will display 4 If executing the second semantic stack, then …

69 9/10/2015 CS2104, Lecture 8 69 The Abstract Machine with By-Need ( { }, { x= 4, z= 5 },  ) The execution stops by binding x to 4 and z to 5

70 9/10/2015 CS2104, Lecture 8 70 A Variable is Needed if Must be Determined thread X={ByNeed fun {$} 3 end} end thread Y={ByNeed fun {$} 4 end} end thread Z=X+Y end Considering that each thread executes atomically, there are six possible executions. For lazy execution to be declarative, all of these executions must lead to equivalent stores. The addition will wait until the other two triggers are created, and these triggers will then be activated.

71 9/10/2015 CS2104, Lecture 8 71 A Variable is Needed if it is Determined thread X={ByNeed fun {$} 3 end} end thread X=2 end thread Z=X+4 end The correct behavior of the declarative demand-driven concurrent model is that all executions should fail. If X=2 executes last, then the trigger has already been activated, binding X to 3, so this is clear. But if X=2 is executed first, then the trigger should also be activated.

72 9/10/2015 CS2104, Lecture 8 72 Garbage Collection of Threads If a thread is known to be suspended forever, it can be garbage-collected  suspends on variable not in use by any other thread  does not change semantics, just saves memory Approximation, only straight-forward cases  impossible: detect whether a thread will have no effect!  really impossible!

73 9/10/2015 CS2104, Lecture 8 73 Summary so far Threads are organized as multiset of semantic stacks Thread creation inserts new semantic stack  inherits environment  shares store Thread termination removes threads

74 9/10/2015 CS2104, Lecture 8 74 Summary Thread Semantics Extend the abstract machine to execute multiple threads

75 9/10/2015 CS2104, Lecture 8 75 Reading suggestions From [van Roy,Haridi; 2004]  Chapter 4, Sections 4.1-4.6  Exercises 4.11.10-4.11.17

76 9/10/2015 CS2104, Lecture 8 76 Coming up next Stateful programming Object-Oriented Programming  Chapter 5, Sections 5.1-5.3

77 9/10/2015 CS2104, Lecture 8 77 Thank you for your attention! Questions?


Download ppt "9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics."

Similar presentations


Ads by Google