concurrency/synchronization using uml state models

21
Concurrency/ synchronization using UML state models April 14 th , 2008 Michigan State University

Upload: ciara

Post on 24-Feb-2016

78 views

Category:

Documents


0 download

DESCRIPTION

Concurrency/synchronization using UML state models. April 14 th , 2008 Michigan State University. Overview. State models of monitor objects Monitor-process pattern Heuristic for “generating” correct synchronization code from a state model - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Concurrency/synchronization using UML state models

Concurrency/synchronization using UML state models

April 14th, 2008Michigan State University

Page 2: Concurrency/synchronization using UML state models

OverviewState models of monitor objects Monitor-process pattern Heuristic for “generating” correct

synchronization code from a state model

Use of signal vs. broadcast to notify waiting threads

ExamplesSynchronization that is more difficult to model using state diagrams

Page 3: Concurrency/synchronization using UML state models

Method for using models to design concurrent software

State model for each system object Passive objects modeled using monitor-

process patternModels then refined into code skeletons Active objects incorporate infrastructure

needed to host a thread: E.g., a distinguished run method whose activation

corresponds to the lifetime of a thread Passive objects designed to behave as

monitors Design pattern for passive-object model

guarantees monitor-object pattern can be applied to develop code

Guarded transitions in state model engender condition synchronization in code

Page 4: Concurrency/synchronization using UML state models

Simple monitor-process pattern

Simple pattern: Distinguished initial state Idle Composite state for each monitor operation

Reachable only from Idle via an accept-call action Entry is named according to the name of the

operation Possibly guarded with an enabling condition

Must transition back to Idle via a reply action upon exit

Names the entry to which it corresponds Includes return value (if any)

No accept-call actions within the composite stateVariations on this pattern will relax some of these restrictions

Page 5: Concurrency/synchronization using UML state models

Simple monitor-process pattern

Idle

[guard] / accept-call ( op(parms) )

/ reply (op, result )

Monitor process

Operation body

Page 6: Concurrency/synchronization using UML state models

Bounded Buffer Example

/ reply ( pull, rv )

BoundedBuffer

Idle

[queue_.size != 0] / accept-call ( pull )

Pullingdo/ rv := queue_.pull

Pushingdo/ queue_.push(x)

/ reply ( push )[queue_.size != MAX] / accept-call ( push(x) )

Page 7: Concurrency/synchronization using UML state models

From simple monitor process to monitor object (code)

Class declares a private mutex variable lockEach operation state in the model becomes a monitor method in the code

Method body bracketed by acquire/release of lockIf any transition out of Idle is guarded:

Class must declare a condition variable, associated with lock, to wait on when guard condition is NOT satisfied

Method body includes a conditionwait loop immediately following acquisition of lock

All other methods must notify this condition variable if they might serve to satisfy this guard condition

Notification could be signal or broadcast

Page 8: Concurrency/synchronization using UML state models

Example: Code for Buffer::push

void Buffer::push(int x){ lock_.acquire();

while (queue_.size() == MAX) { full_.wait(); }

queue_.push(x);

empty_.signal(); lock_.release();}

Idle[queue_.size() != MAX]/ accept-call (push(x))

Pushingdo/ queue_.push(x)

/ reply (push)

Page 9: Concurrency/synchronization using UML state models

Example: Code for Buffer::push

void Buffer::push(int x){ lock_.acquire();

while (queue_.size() == MAX) { full_.wait(); }

queue_.push(x);

empty_.signal(); lock_.release();}

Idle[queue_.size() != MAX]/ accept-call (push(x))

Pushingdo/ queue_.push(x)

/ reply (push)

Page 10: Concurrency/synchronization using UML state models

Example: Code for Buffer::push

void Buffer::push(int x){ lock_.acquire();

while (queue_.size() == MAX) { full_.wait(); }

queue_.push(x);

empty_.signal(); lock_.release();}

Idle[queue_.size() != MAX]/ accept-call (push(x))

Pushingdo/ queue_.push(x)

/ reply (push)

Page 11: Concurrency/synchronization using UML state models

Example: Code for Buffer::push

void Buffer::push(int x){ lock_.acquire();

while (queue_.size() == MAX) { full_.wait(); }

queue_.push(x);

empty_.signal(); lock_.release();}

Idle[queue_.size() != MAX]/ accept-call (push(x))

Pushingdo/ queue_.push(x)

/ reply (push)

Page 12: Concurrency/synchronization using UML state models

Example: Code for Buffer::pull

int Buffer::pull(){ lock_.acquire();

while (queue_.size() == 0) { empty_.wait(); } int rv = queue_.pull();

full_.signal(); lock_.release(); return rv;}

Idle[queue_.size() != 0]/ accept-call (pull)

Pushingdo/ rv :=

queue_.pull()

/ reply (pull, rv)

Page 13: Concurrency/synchronization using UML state models

More complex guard conditions

Consider a banking application that allows multiple clients to deposit and withdraw funds from shared accountsGoals: Protect against data races, so that money is

not accidentally created or destroyed Prevent overdrafts by making withdraw

requests block if account has insufficient funds

Question: How should we model the behavior of an account object?

Page 14: Concurrency/synchronization using UML state models

Monitor-process model of BankAccount

/ reply ( deposit )

BankAccount

Idle

/ accept-call ( deposit(amount) )

Depositingdo/ balance_ += amount;

Withdrawingdo/ balance_ -= amount;

/ reply ( withdraw )

[ amount <= balance_ ] / accept-call ( withdraw(amount) )

Page 15: Concurrency/synchronization using UML state models

Code for BankAccount::withdraw

voidBankAccount::withdraw(int amount){ lock_.acquire();

while (amount > balance_) { okToWithdraw_.wait(); }

balance_ -= amount;

lock_.release();}

Idle[amount <= balance_]/ accept-call (withdraw(amount))

Withdrawingdo/ balance_ -=

amount;

/ reply (withdraw)

Page 16: Concurrency/synchronization using UML state models

Code for BankAccount::deposit

voidBankAccount::deposit(int amount){ lock_.acquire();

balance_ += amount;

okToWithdraw_.broadcast();

lock_.release();}

Idle/ accept-call (deposit(amount))

Depositingdo/ balance_ += amount;

/ reply (deposit)

Page 17: Concurrency/synchronization using UML state models

Signal vs. BroadcastWhen one thread changes the value of a condition upon which others might be waiting, the modifier is obliged to notify these waiting threadsAlways safest, though perhaps not very efficient, to use broadcast to notify waiting threads after a changeQuestion: When is it safe to use signal?

Page 18: Concurrency/synchronization using UML state models

More complex monitors…Simple monitor-process pattern insufficient for handling some forms of synchronization E.g., barriers, using which two or more

threads must arrive before either may pass Not so easily modeled using guarded

transitionsMay require: Embedding accept-call actions involving

other operations within an operation state Explicit modeling, storage, and retrieval of

request objects associated with calls into an entry

Page 19: Concurrency/synchronization using UML state models

Party-admission problemModels the admission of (boy–girl) couples to a party

Boys and girls arrive independently but are blocked from entering the party except as couples

When a boy arrives, he must block unless and until a girl has arrived for him to hook up with

Example of something called a barrier synchronization

Requires embedding accept-call action for girlArrives inside operation state for boyArrives and vice versa

Violates our simple monitor-process conventions Still, may be modeled without too much trouble and

without any use of guards on transitions

Page 20: Concurrency/synchronization using UML state models

Example: Part of the model

/ accept-call (boyArrives)

BoyWaiting

/ reply (boyArrives)

/ accept-call (girlArrives)

/ reply (girlArrives)

BoyMeetsGirlIdle

Page 21: Concurrency/synchronization using UML state models

Example: Other part of the model

/ accept-call (girlArrives)

GirlWaiting

/ reply (girlArrives)

/ accept-call (boyArrives)

/ reply (boyArrives)

BoyMeetsGirlIdle