47
Concurrency: model-based design ©Magee/ Kramer Claus Brabrand [email protected] University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer Claus Brabrand [email protected] University of Aarhus Model-Based Design Concurrency

  • View
    220

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Claus Brabrand

[email protected]

University of Aarhus

Model-Based Design

Concurrency

Page 2: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Repetition

Safety and Liveness Properties

Page 3: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Safety & Liveness Properties

Concepts:Properties: true for every possible

executionSafety: nothing bad happensLiveness: something good eventually

happens

Models:Safety: no reachable ERROR/STOP stateProgress: an action is eventually executed

(fair choice and action priority)

Practice: Threads and monitors

Aim: property satisfaction.

Aim: property satisfaction.

Page 4: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

7.1 Safety

ERROR conditions state what is not required (~ exceptions).

In complex systems, it is usually better to specify safety properties by stating directly what is required. property SAFE_ACTUATOR = (command ->

respond -> SAFE_ACTUATOR).

command

respond

command

respond

-1 0 1

ACTUATOR = (command -> (respond -> ACTUATOR |command -> ERROR) |respond -> ERROR).

A safety property asserts that nothing bad happens.

Page 5: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

7.2 Single Lane Bridge Problem

Page 6: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Single Lane Bridge - Safety Property “ONEWAY”

property ONEWAY = (red[ID].enter -> RED[1] |blue[ID].enter -> BLUE[1]),

RED[i:ID] = (red[ID].enter -> RED[i+1] |when (i==1) red[ID].exit -> ONEWAY |when (i>1) red[ID].exit -> RED[i-1]),

BLUE[j:ID]= (blue[ID].enter -> BLUE[j+1] |when (j==1) blue[ID].exit -> ONEWAY |when (j>1) blue[ID].exit -> BLUE[j-1]).

We now specify a safety property to check that cars do not collide!:

When the bridge is empty, either a red or a blue car may enter. While red cars are on the bridge only red cars can enter; similarly for blue cars.

Page 7: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Single Lane Bridge

Trace to property violation in ONEWAY:red.1.enterblue.1.enter

Withoutbridge =>controller

Page 8: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

7.3 Liveness

A safety property asserts that nothing bad happens.

A liveness property asserts that something good eventually happens.Does every car eventually get an opportunity to cross the bridge (i.e., make progress)?

A progress property asserts that it is always the case that an action is eventually executed.

Progress is the opposite of starvation = the name given to a concurrent programming situation in which an action is never executed.

Page 9: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

pick

pick toss

heads

toss

toss

tails

heads

0 1 2 3 4 5

Progress Properties

Suppose that there were two possible coins that could be picked up: a regular coin and a trick coin

TWOCOIN = (pick->COIN | pick->TRICK),COIN = (toss->heads->COIN | toss->tails->COIN).TRICK = (toss->heads->TRICK),

progress HEADS = {heads} ?

progress TAILS = {tails} ?

TerminalTransitionSet

Page 10: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Revised single lane bridge implementation - FairBridge

||CongestedBridge = (SingleLaneBridge) >> {red[ID].exit,blue[ID].exit}.

Low/high action priorities: P >> {low1, …, lowN}P << {high1, …, highN}

Page 11: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

7.5 Readers and Writers

Page 12: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Chapter 8

Model-Based Design

Page 13: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Model-Based Design

Concepts: design process:

- from requirements to models- from models to implementations

Models: check properties of interest:

- safety of the appropriate (sub-)system- progress of the overall system

Practice: model “interpretation”:

- to infer actual system behavior active threads and passive monitors

Aim: rigorous design process.

Aim: rigorous design process.

Case Study: cruise controller

Page 14: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

From Requirements to Models

Page 15: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

goals of the system scenarios (Use-Case models) properties of interest

8.1 From Requirements to Models

Requirements

Model

1. identify the main events, actions, and interactions

2. identify and define the main processes

3. identify and define the properties of interest

4. structure the processes into an architecture

check traces of interest check properties of interest

Any appropriat

e design approach

can be used

Determine appropriate level of abstraction

Page 16: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Example: A Cruise Control System - Requirements

When the car ignition is switched on and the ‘on’ button is pressed, the current speed is recorded and the system is enabled:

Pressing ‘resume’ or ‘on’ re-enables the system.

Pressing the ‘brake’, ‘accelerator’ or ‘off’ button disables the system.button

sCruise control

Cruise control display

It maintains the speed of the car at the recorded setting.

Requirements:

Page 17: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

A Cruise Control System - Hardware

Wheel Revolution Sensor generates interrupts to enable the car speed to be calculated.

Parallel Interface Adapter (PIA) is polled every 100msec. It records the actions of the sensors:

• buttons (on, off, resume)

• brake (pressed)

• accelerator (pressed)

• engine (on, off).

buttons

engine

accelerator

brakePIA

polled

wheel interrupt

CPU

throttleD/A

Output: The cruise control system controls the car speed by setting the throttle via the digital-to-analogue (D/A) converter.

throttle

Page 18: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Model - Design

buttons

engine

accelerator

brakePIA

polled

wheel interrupt

CPU

throttleD/A

1. Identify the main events, actions, and interactions:engineOn, engineOff, accelerator, brake,

on, off, resume,

speed, setThrottle, zoom,

clearSpeed,recordSpeed,

enableControl,disableControl.

Page 19: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Model - Design

1. Identify the main events, actions, and interactions:engineOn, engineOff, accelerator, brake,

on, off, resume,

speed, setThrottle, zoom

clearSpeed,recordSpeed,

enableControl,disableControl.

set Sensors = {engineOn, engineOff, accelerator, brake, on, off, resume}

set Engine = {engineOn, engineOff}

set Prompts = {enableControl, disableControl, clearSpeed, recordSpeed}

Page 20: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Model - Outline Design

2. Identify and define the main processes:

Input Speed monitors the speed (when the engine is on), and provides the current speed readings to the speed control

Input Speed monitors the speed (when the engine is on), and provides the current speed readings to the speed control

Sensor Scan monitors the buttons, brake, accelerator and engine events

Sensor Scan monitors the buttons, brake, accelerator and engine events

Cruise State Controllertriggers clearSpeed and recordSpeed, and enables-/disables the speed control

Cruise State Controllertriggers clearSpeed and recordSpeed, and enables-/disables the speed control

Speed Control clears and records the speed, and sets the throttle accordingly when enabled

Speed Control clears and records the speed, and sets the throttle accordingly when enabled

Throttle sets the actual throttle

Throttle sets the actual throttle

Sensors

PromptsEngine

speed

setThrottle

set Sensors = {engineOn, engineOff, accelerator, brake, on, off, resume}set Engine = {engineOn, engineOff} set Prompts = {enableControl, disableControl, clearSpeed, recordSpeed}

Controlling the state(of the controller)

Controllingthe throttle

buttons

engine

accelerator

brakePIA

polled

wheel interrupt

CPU

throttleD/A

Page 21: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Model – Design (Step 1+2+3)

1. Identify the main events, actions, and interactions:on, off, resume, brake, accelerator

engineOn, engineOff,

speed, setThrottle

clearSpeed,recordSpeed,

enableControl,disableControl

2. Identify and define the main processes:Sensor Scan, Input Speed,

Cruise Controller, Speed Control and

Throttle

3. Identify and define the main properties of interest:safety: cruise control disabled when

off, brake or accelerator pressed.

Sensors

Prompts

Page 22: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Model - Design (Step 4)

SENSORSCAN

CRUISECONTROLLER

Sensors

INPUTSPEED

SPEEDCONTROL

setThrottle

speed

Engine Prompts

CONTROL CRUISECONTROLSYSTEM

THROTTLE

The CONTROL system is structured as two processes:- CRUISECONTROLLER (controlling the state); and - SPEEDCONTROL (controlling the throttle)

4. Structure the processes into an architecture:

Page 23: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Model Elaboration - Process Definitions

SENSORSCAN = ({Sensors} -> SENSORSCAN).

// monitor speed when engine onINPUTSPEED = (engineOn -> CHECKSPEED),CHECKSPEED = (speed -> CHECKSPEED // monitor speed |engineOff -> INPUTSPEED).

// zoom when throttle setTHROTTLE =(setThrottle -> zoom -> THROTTLE).

// perform speed control when enabledSPEEDCONTROL = DISABLED,DISABLED =({speed,clearSpeed,recordSpeed} -> DISABLED |enableControl -> ENABLED), ENABLED = (speed -> setThrottle -> ENABLED |{recordSpeed,enableControl} -> ENABLED |disableControl -> DISABLED).

SENSORSCAN

CRUISECONTROLLER

Sensors

INPUTSPEED

SPEEDCONTROL

setThrottle

speed

Engine Prompts

CONTROL CRUISECONTROLSYSTEM

THROTTLE

Page 24: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Model Elaboration - Process Definitions

CRUISECONTROLLER = INACTIVE,

INACTIVE =(engineOn -> clearSpeed -> ACTIVE),

ACTIVE =(engineOff -> INACTIVE |on-> recordSpeed-> enableControl-> CRUISING),

// enable speed control when cruising, // disable when off, brake, or accelerator pressed

CRUISING =(engineOff -> INACTIVE |{off,brake,accelerator} ->disableControl-> STANDBY |on-> recordSpeed-> enableControl-> CRUISING),

STANDBY =(engineOff -> INACTIVE |resume -> enableControl-> CRUISING |on-> recordSpeed-> enableControl-> CRUISING).

SENSORSCAN

CRUISECONTROLLER

Sensors

INPUTSPEED

SPEEDCONTROL

setThrottle

speed

Engine Prompts

CONTROL CRUISECONTROLSYSTEM

THROTTLE

Page 25: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Model - CONTROL Sub-System

||CONTROL = (CRUISECONTROLLER||SPEEDCONTROL).

- Is control enabled after the engine is switched on and the ‘on’ button is pressed?- Is control disabled when the brake is then pressed?- Is control re-enabled when resume is then pressed?

Testing (animation in LTSA) will check particular traces:

Safety: Is the control always disabled when off, brake, or accelerator is pressed?

Progress: Can every action eventually be selected?

Verification (aka. model-checking) will exhaustively analyze all possible traces:

SENSORSCAN

CRUISECONTROLLER

Sensors

INPUTSPEED

SPEEDCONTROL

setThrottle

speed

Engine Prompts

CONTROL CRUISECONTROLSYSTEM

THROTTLE

Page 26: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Model - Safety Properties

Thus: Safety properties should be composed with the appropriate (sub-)system to which the property refers.

Safety checks are compositional!

If there is no violation at a sub-system level, then there cannot be a violation when the sub-system is composed with other sub-systems.

This is because, if the ERROR state of a particular safety property is unreachable in the LTS of the sub-system, it remains unreachable in any subsequent parallel composition which includes the sub-system.

P |= S Q |= S => (P||Q) |= S

/* safety */ property S = ...

Page 27: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Model - Safety Properties

Verify CRUISESAFETY?

||CONTROL = (CRUISECONTROLLER||SPEEDCONTROL||CRUISESAFETY).

property CRUISESAFETY = {on,resume} -> SAFETYCHECK, |{off,accelerator,brake,disableControl} -> CRUISESAFETY

SAFETYCHECK = ({off,accelerator,brake} -> SAFETYACTION |disableControl -> CRUISESAFETY |{on,resume} -> SAFETYCHECK),

SAFETYACTION = (disableControl->CRUISESAFETY).

Composition with CONTROL processes:

SENSORSCAN

CRUISECONTROLLER

Sensors

INPUTSPEED

SPEEDCONTROL

setThrottle

speed

Engine Prompts

CONTROL CRUISECONTROLSYSTEM

THROTTLE

Is the control always disabled when off/brake/acc pressed?

Page 28: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Model Analysis

||CONTROL = (CRUISECONTROLLER || SPEEDCONTROL || CRUISESAFETY) @ {Sensors,speed,setThrottle}.

||CRUISECONTROLSYSTEM = (CONTROL || SENSORSCAN || INPUTSPEED || THROTTLE).

We can now compose the whole system:

We know there can be no safety violations (in the whole system); i.e. due to composition

What about deadlocks?

What about progress…?

SENSORSCAN

CRUISECONTROLLER

Sensors

INPUTSPEED

SPEEDCONTROL

setThrottle

speed

Engine Prompts

CONTROL CRUISECONTROLSYSTEM

THROTTLE

No deadlock/errors !

Page 29: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Model - Progress Properties

Thus: Progress checks should be conducted on the complete target system after satisfactory completion of the safety checks.

Progress checks are not compositional !

Even if there is no progress violation at a sub-system level, a progress violation may “appear” when the sub-system is composed with other sub-systems.

This is because an action in the sub-system may satisfy progress yet be unreachable when the sub-system is composed with other sub-systems which constrain system behavior.

P |= L Q |= L => (P||Q) |= L

Assume L (P), L (Q):(P||Q) |= L => P |= L Q |= L

However, we have that:

Page 30: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Model - Progress Properties

Progress violation for actions: {engineOn, clearSpeed, engineOff, on, recordSpeed, enableControl, off, disableControl, brake, accelerator}Path to terminal set of states:

engineOntauontautauengineOffengineOn

Actions in terminal set:{speed, setThrottle, zoom}

Progress check:

(clearSpeed)

(recordSpeed)(enableControl)

When the engine is switched off:

- CruiseController becomes inactive, whereas

- SpeedControl is not disabled!

Page 31: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Cruise Control Model - Minimized LTS

engineOn

engineOff

on

speed

engineOff

on

offbrake

accelerator

speed

engineOff

onresume

speed

engineOn

speed0 1 2 3 4 5

||CRUISEMINIMIZED = (CRUISECONTROLSYSTEM) @ {Sensors,speed}.

Action hiding and minimization can help to reduce the size of the LTS diagram and make it easier to interpret.

Progress violation trace: engineOn -> clearSpeed -> on -> recordSpeed -> enableControl -> engineOff -> engineOn.

Page 32: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Model – Revised Cruise Control System

// enable speed control when cruising, // disable when off, brake, or accelerator pressed

// or when the engine is turned off!!!CRUISING =(engineOff -> INACTIVE |{off,brake,accelerator} ->disableControl-> STANDBY |on-> recordSpeed-> enableControl-> CRUISING),

Fix CRUISECONTROLLER so that it disables the SPEEDCONTROLLER when the engine is switched off:

OK now?

-> disableControl

Page 33: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Model – Revised Cruise Control System (Properties)

property CRUISESAFETYv2 = ({off,accelerator,..., } -> CRUISESAFETYv2 |{on,resume} -> SAFETYCHECK),SAFETYCHECK = ({on,resume} -> SAFETYCHECK |{off,..., } -> SAFETYACTION |disableControl -> CRUISESAFETYv2), SAFETYACTION = (disableControl -> CRUISESAFETYv2).

engineOn

engineOff

on

speed

engineOff

on

offbrake

accelerator

speed

engineOff

onresume

speed0 1 2 3

No deadlocks/errors

No progress violations detected

engineOff

engineOff

Page 34: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Model - System Sensitivities (under Adverse Conditions)

||SPEEDHIGH = CRUISECONTROLSYSTEM << {speed}.

Progress violation for actions: {engineOn, engineOff, on, off, brake, accelerator, resume, setThrottle, zoom}Path to terminal set of states:

engineOntau

Actions in terminal set:{speed}

Indicates that the system may be sensitive to the priority of the action “speed”.

Page 35: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Model Interpretation

Models can be used to indicate system sensitivities!

If it is possible that erroneous situations detected in the model may occur in the implemented system, then the model should be revised to find a design which ensures that those violations are avoided.

However, if it is considered that the real system will not exhibit this behavior, then no further model revisions are necessary.

Model interpretation and correspondence to the implementation are important in determining the relevance and adequacy of the model design and its analysis.

Page 36: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

From Models to Implementation

Page 37: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

The Central Role of Design Architecture

Behavioural View Implementation View

Architecture

Analysis Program Construction

Performance View

Design architecture describes the gross organization and global structure of the system in terms of its constituent components.

We consider that the implementation should be considered as an elaborated view of the basic design architecture.S = M0 M1 M2 ... M = I // Incremental model

refinement

Page 38: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

8.2 From Models to Implementations

Model

Java

identify the main active entities

- to be implemented as threads

identify the main (shared) passive entities

- to be implemented as monitors

(identify the interactive display environment

- to be implemented as associated classes)

structure the classes as a (UML) class diagram

- to be implemented

Page 39: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Cruise Control System - Class Diagram

SpeedControl interacts with the car simulation via interface CarSpeed.

enableControl()disableControl()recordSpeed()clearSpeed()

Applet

CruiseControl

Controller

brake()accelerator()engineOff()engineOn()on()off()resume()

SpeedControl

CarSimulator

CarSpeed

setThrottle()getSpeed()

Runnable

CruiseDisplay

car

control

sc

disp

disp

cs

CRUISECONTROLLER SPEEDCONTROL

SENSORSCAN

CRUISECONTROLLER

Sensors

INPUTSPEED

SPEEDCONTROL

setThrottle

speed

Engine Prompts

CONTROL CRUISECONTROLSYSTEM

THROTTLE

Page 40: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Class state-Controller

class StateController { final static int INACTIVE = 0, ACTIVE = 1, CRUISING = 2, STANDBY = 3; // controller states

protected int state = INACTIVE; // initial state protected SpeedControl sc;

StateController(CarSpeed cs, CruiseDisplay disp) { sc = new SpeedControl(cs, disp); } synchronized void brake() { if (state == CRUISING) { sc.disableControl(); state = STANDBY; } } synchronized void accelerator() { if (state == CRUISING) { sc.disableControl(); state = STANDBY; } } synchronized void engineOff() { if (state != INACTIVE) { if (state == CRUISING) sc.disableControl(); state = INACTIVE; } }

Controller is a passive entity (it reacts to events) and thus implemented as a monitor

CRUISECONTROLLER = INACTIVE,INACTIVE =(engineOn -> clearSpeed -> ACTIVE),ACTIVE =(engineOff -> INACTIVE |on-> recordSpeed-> enableControl -> CRUISING),CRUISING =(engineOff -> INACTIVE |{off,brake,accelerator} ->disableControl -> STANDBY |on-> recordSpeed-> enableControl -> CRUISING),STANDBY =(engineOff -> disableControl-> INACTIVE |resume -> enableControl -> CRUISING |on-> recordSpeed-> enableControl -> CRUISING).

Page 41: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Class state-Controller

... synchronized void engineOn() { if (state == INACTIVE) { sc.clearSpeed(); state = ACTIVE; } } synchronized void on() { if (state != INACTIVE) { sc.recordSpeed(); sc.enableControl(); state = CRUISING; } } synchronized void off() { if (state == CRUISING) { sc.disableControl(); state = STANDBY; } } synchronized void resume() { if (state == STANDBY) { sc.enableControl(); state = CRUISING; } }}

CRUISECONTROLLER = INACTIVE,INACTIVE =(engineOn -> clearSpeed -> ACTIVE),ACTIVE =(engineOff -> INACTIVE |on-> recordSpeed-> enableControl -> CRUISING),CRUISING =(engineOff -> INACTIVE |{off,brake,accelerator} ->disableControl -> STANDBY |on-> recordSpeed-> enableControl -> CRUISING),STANDBY =(engineOff -> disableControl-> INACTIVE |resume -> enableControl -> CRUISING |on-> recordSpeed-> enableControl -> CRUISING).

Page 42: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Class SpeedControl

class SpeedControl implements Runnable { final static int DISABLED = 0; // speed control states final static int ENABLED = 1; protected int state = DISABLED; // initial state protected int set_speed = 0; // initial speed setting protected Thread sc; protected CarSpeed cs; // interface to the car (simulator) protected CruiseDisplay disp;

SpeedControl(CarSpeed c, CruiseDisplay d){ this.cs = c; this.disp = d; d.disable(); d.record(0); }

synchronized void recordSpeed() { set_speed = cs.getSpeed(); disp.record(set_speed); }

synchronized void clearSpeed() { if (state == DISABLED) { set_speed = 0; disp.record(set_speed); } }

SpeedControl is an active entity; when enabled, a new thread is created (which periodically obtains car speed and sets the throttle).

SPEEDCONTROL = DISABLED,DISABLED =({speed,clearSpeed,recordSpeed} -> DISABLED |enableControl -> ENABLED), ENABLED = (speed -> setThrottle -> ENABLED |{recordSpeed,enableControl} -> ENABLED |disableControl -> DISABLED).

Page 43: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Class SpeedControl

...

synchronized void enableControl() { if (state == DISABLED) { disp.enable(); sc = new Thread(this); sc.start(); state = ENABLED; } }

synchronized void disableControl() { if (state == ENABLED) { disp.disable(); state = DISABLED; } }

...

SPEEDCONTROL = DISABLED,DISABLED =({speed,clearSpeed,recordSpeed} -> DISABLED |enableControl -> ENABLED), ENABLED = (speed -> setThrottle -> ENABLED |{recordSpeed,enableControl} -> ENABLED |disableControl -> DISABLED).

Page 44: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Class SpeedControl

synchronized private double speed() { return ...cs.getSpeed()...; } synchronized private void setThrottle(double throttle) { cs.setThrottle(...throttle...); }

public void run() { // the speed controller thread try { while (state == ENABLED) { double s = speed(); setThrottle(s); Thread.sleep(500); } } catch (InterruptedException _) {} sc = null; // throw away SpeedController thread }

SpeedControl is an example of a class that combines both

- synchronized methods (to update local vars); and- a thread.

SPEEDCONTROL = DISABLED,DISABLED =({speed,clearSpeed,recordSpeed} -> DISABLED |enableControl -> ENABLED), ENABLED = (speed -> setThrottle -> ENABLED |{recordSpeed,enableControl} -> ENABLED |disableControl -> DISABLED).

Page 45: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Summary: Model-Based Design

Concepts: design process:

- from requirements to models- from models to implementations

Models: check properties of interest:

- safety on the appropriate (sub)system- progress on the overall system

Practice: model “interpretation”:

- to infer actual system behavior active threads and passive monitors

Aim: rigorous design process.

Aim: rigorous design process.

Page 46: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

class Interleave extends Thread { public static final int K = 10; protected static int x = 0; // shared integer variable

public void run() { for (int i=0; i<K; i++) { // repeat `K' times: x = x + 1; } }

public static void main(String[] args) { // create two threads (processes: p and q) Thread p = new Interleave(); Thread q = new Interleave();

// start the threads...p.start();q.start();

try { // ...and wait for them to finish p.join(); q.join();

// Finally, write value of x to standard out System.out.println("x = " + x); } catch(InterruptedException _) {} }}

Final possible values of x?!

Page 47: Concurrency: model-based design ©Magee/Kramer Claus Brabrand brabrand@daimi.au.dk University of Aarhus Model-Based Design Concurrency

Concurrency: model-based design ©Magee/Kramer

Course Outline

Processes and Threads [MK 1+2]

Concurrent Execution [MK 3]

Shared Objects & Interference [MK 4]

Monitors & Condition Synchronization [MK 5]

Deadlock [MK 6]

Safety and Liveness Properties [MK 7]

Model-Based Design [MK 8]

Mini-Project: groups of 2-3; Wednesday at 15:00

Multiple-choice assignment: individual, “on-site”