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

Preview:

Citation preview

04/19/23 CS2104, Lecture 8 1

Programming Language Concepts, COSC-3308-01Lecture 8  

(Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 2

Reminder of the Last Lecture

abstract data types declarative concurrency design a concurrent program execution of concurrent programs

04/19/23 CS2104, Lecture 8 3

Overview

Thread Semantics Extend the abstract machine to execute

multiple threads

04/19/23 CS2104, Lecture 8 4

Thread Semantics

04/19/23 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.

04/19/23 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.

04/19/23 CS2104, Lecture 8 7

Causal Order and Interleaving Executions

04/19/23 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 Ia, there is a choice of either I1 or Ib 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.

04/19/23 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.

04/19/23 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

04/19/23 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

04/19/23 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

04/19/23 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!

04/19/23 CS2104, Lecture 8 14

Abstract Machine Concepts (Reminder) Single-assignment store Environment Semantic statement Execution state Computation

04/19/23 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

04/19/23 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 {x1, x2=x3, x4=a|x2} x1 unbound x2, x3 equal and unbound x4 bound to partial value a|x2

04/19/23 CS2104, Lecture 8 17

Environment (Reminder)

Environment E maps variable identifiers to entities in store written as set of pairs X x

variable identifier X store variable x

Example environment { X x, Y y } maps identifier X to store variable x maps identifier Y to store variable y

04/19/23 CS2104, Lecture 8 18

Environment and Store (Reminder) Given: environment E, store Looking up value for variable identifier X:

find store variable in environment E(X) take value from for E(X)

Example: ={x1, x2=x3, x4=a|x2} E = { X x1, Y x4 }

E(X) = x1 and no information in on x1

E(Y) = x4 and binds x4 to a|x2

04/19/23 CS2104, Lecture 8 19

Environment Adjunction (Reminder) Given: Environment E

E + {x1x1, …, xnxn}

is new environment E ’ with mappings added: always take store entity from new mappings might overwrite old mappings

04/19/23 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)

04/19/23 CS2104, Lecture 8 21

Semantic Stack (Reminder)

Execution maintains stack of semantic statements ST

[(s1, E1), …, (sn, En)] always topmost statement (s1, E1) executes first rest of stack: what needs to be done

04/19/23 CS2104, Lecture 8 22

Semantic Stack States (Reminder) Semantic stack can be in run-time states

terminated stack is empty runnable can do execution step suspended stack not empty, no execution

step possible

Statements non-suspending can always execute suspending need values from store

dataflow behavior

04/19/23 CS2104, Lecture 8 23

Summary

Single assignment store Environments E

adjunction E + {…} Semantic statements (s, E) Semantic stacks [(s, E) … ] Execution state (ST, ) Program execution

runnable, terminated, suspended Statements

suspending, non-suspending

04/19/23 CS2104, Lecture 8 24

Executing if (Reminder)

Semantic statement is

(if x then s1 else s2 end, E)

If activation condition “x bound” is true if E(x) bound to true push s1

if E(x) bound to false push s2

otherwise, raise error

Otherwise, suspend…

04/19/23 CS2104, Lecture 8 25

Procedure Call (Reminder)

Semantic statement is

({x y1 … yn}, E)

where E(x) is to be called y1, …, yn are actual parameters

Suspending statement, suspension condition E(x) is determined

04/19/23 CS2104, Lecture 8 26

Summary so far

Semantic statement executes by popping itself always creating environment local manipulating store local, = pushing new statements local, if

sequential composition Semantic statement can suspend

activation condition if, {…}, case read store

04/19/23 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

04/19/23 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

04/19/23 CS2104, Lecture 8 29

Execution State

( Multiset of semantic stacks, store )

( { ST1, …, STn }, )

we write multisets with normal set parentheses: { and }

04/19/23 CS2104, Lecture 8 30

Execution State

( Multiset of semantic stacks, store )

( { ST1, …, STn }, )

MST Multisets of stacks are denoted MST

04/19/23 CS2104, Lecture 8 31

Initial Execution State

Given statement s, start execution as before with empty environment, empty store and just one thread

( { [ (s, ) ] }, )

04/19/23 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

04/19/23 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)

04/19/23 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)

04/19/23 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)

04/19/23 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

04/19/23 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)

04/19/23 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

04/19/23 CS2104, Lecture 8 39

Execution

Execution steps

(MST1, 1) (MST2, 2) …

At each step select runnable semantic stack STi from MSTi

execute topmost semantic statement of STi resulting in ST’i

continue with threads MSTi+1 = {ST’i} (MSTi – {STi})

04/19/23 CS2104, Lecture 8 40

Execution

Execution steps

(MST1, 1) (MST2, 2) …

At each step select runnable semantic stack STi from MSTi

execute topmost semantic statement of STi resulting in ST’i

continue with threadsMSTi+1 = {ST’i} (MSTi – {STi})

04/19/23 CS2104, Lecture 8 41

Statements

s ::= skip

| x = y | x = v

| s1 s2

| local x in s end | if x then s1 else s2 end

| {x y1 … yn}

04/19/23 CS2104, Lecture 8 42

Statements with Thread Creations ::= skip

| x = y | x = v

| s1 s2

| local x in s end | if x then s1 else s2 end

| {x y1 … yn}

| thread s end

04/19/23 CS2104, Lecture 8 43

Sketch of Computation

Multiple threads sharing store

ST1 STn…

04/19/23 CS2104, Lecture 8 44

Sketch of Computation

Thread creation statement…

STn…STi

(threadsend,E)

04/19/23 CS2104, Lecture 8 45

Sketch of Computation

…new semantic stack running s

STn…STi

(s,E)

04/19/23 CS2104, Lecture 8 46

Example

local B X in

thread

if B then X=1 else X=2 end

end

B=true

end

04/19/23 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

04/19/23 CS2104, Lecture 8 48

The Abstract Machine with Threads( { [ (thread if B then X=1 else X=2 end

end

B=true,

{Bb, Xx}) ]

},

{b, x} )

We need to apply the sequential composition

04/19/23 CS2104, Lecture 8 49

The Abstract Machine with Threads( { [ (thread if B then X=1 else X=2 end

end, {Bb, Xx}) (B=true, {Bb, Xx}) ] }, {b, x} )

We need to apply thread … end

04/19/23 CS2104, Lecture 8 50

The Abstract Machine with Threads( { [ (if B then X=1 else X=2 end, {Bb, Xx}) ]

[ (B=true, {Bb, Xx})

]

},

{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!

04/19/23 CS2104, Lecture 8 51

The Abstract Machine with Threads( { [ (if B then X=1 else X=2 end, {Bb, Xx}) ] [ ] }, {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!

04/19/23 CS2104, Lecture 8 52

The Abstract Machine with Threads( { [ (X=1, {Bb, Xx})

]

[

]

},

{b=true, x} )

We need to execute the variable-value equality

04/19/23 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.

04/19/23 CS2104, Lecture 8 54

Statements with Thread Creations ::= skip

| x = y | x = v

| s1 s2

| local x in s end | if x then s1 else s2 end

| {x y1 … yn}

| thread s end | {ByNeed x y}

04/19/23 CS2104, Lecture 8 55

Sketch of Computation

Thread creation statement…

STn…STi

({ByNeed x y},E)

04/19/23 CS2104, Lecture 8 56

Sketch of Computation

The store is the variable-store + the trigger-store

STn…STi

({ByNeed x y},E)

04/19/23 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, , ).

04/19/23 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)]

04/19/23 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>},{<x>x, <y>y}) ] {…} is a procedure call!

04/19/23 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

04/19/23 CS2104, Lecture 8 61

The Abstract Machine with By-Need( { [ (X={ByNeed fun {$} 4 end} {Browse X}

Z=X+1, {Xx, Zz}) ] }, {x, z} , )

We need to apply the sequential composition

04/19/23 CS2104, Lecture 8 62

The Abstract Machine with By-Need( { [ (X={ByNeed fun {$} 4 end}, {Xx, Zz}) ({Browse X} Z=X+1, {Xx, Zz}) ] }, {x, z} , )

We need to apply the trigger creation

04/19/23 CS2104, Lecture 8 63

The Abstract Machine with By-Need( { [({Browse X}

Z=X+1,

{Xx, Zz})

]

},

{x, z} , trig(fun {$} 4 end, x))

We need to apply the sequential composition

04/19/23 CS2104, Lecture 8 64

The Abstract Machine with By-Need( { [({Browse X}, {Xx, Zz}) (Z=X+1, {Xx, Zz}) ] }, {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!

04/19/23 CS2104, Lecture 8 65

The Abstract Machine with By-Need( { [({Browse X}, {Xx, Zz})] [(Z=X+1, {Xx, Zz})] }, {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

04/19/23 CS2104, Lecture 8 66

The Abstract Machine with By-Need( { [({fun {$} 4 end X},{Xx})] [({Browse X}, {Xx, Zz})] [(Z=X+1, {Xx, Zz})] }, {x, z} , )

The second and third semantic stacks are suspended

We executed the first semantic stack

04/19/23 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 Is equivalent withlocal X Fun1 in

proc {Fun1 Result1}

Result1 = 4

end

{Fun1 X}

{Browse X}

end

04/19/23 CS2104, Lecture 8 68

The Abstract Machine with By-Need( {[({Browse X}, {Xx, Zz})]

[(Z=X+1, {Xx, Zz})]

},

{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 …

04/19/23 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

04/19/23 CS2104, Lecture 8 70

A Variable is Needed if Must be Determinedthread 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.

04/19/23 CS2104, Lecture 8 71

A Variable is Needed if it is Determinedthread 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.

04/19/23 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!

04/19/23 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

04/19/23 CS2104, Lecture 8 74

Summary Thread Semantics Extend the abstract machine to execute

multiple threads

04/19/23 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

04/19/23 CS2104, Lecture 8 76

Coming up next

Stateful programming Object-Oriented Programming

Chapter 5, Sections 5.1-5.3

04/19/23 CS2104, Lecture 8 77

Thank you for your attention!

Questions?

Recommended