preserving architectural properties in multithreaded code ... · abstraction the program topology...

21
Preserving Architectural Properties in Multithreaded Code Generation Marco Bernardo and Marco Bernardo and Edoardo Bontà Edoardo Bontà Università di Urbino “Carlo Bo” - Italy Istituto di Scienze e Tecnologie dell’Informazione

Upload: others

Post on 01-Aug-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Preserving Architectural Properties in Multithreaded Code ... · abstraction the program topology in terms of thread instances, their interactions, and their behavior. 2. Use a translator

Preserving Architectural Properties

in Multithreaded Code Generation

Marco Bernardo and Marco Bernardo and Edoardo BontàEdoardo Bontà

Università di Urbino “Carlo Bo” - Italy

Istituto di Scienze e Tecnologie dell’Informazione

Page 2: Preserving Architectural Properties in Multithreaded Code ... · abstraction the program topology in terms of thread instances, their interactions, and their behavior. 2. Use a translator

Coordination 2005 © Marco Bernardo & Edoardo Bontà 2

OverviewOverview

• Multithreading provides an adequate support for concurrent programming, but requires the software developer to take care of the correct synchronization and exchange of data among threads.

• The software developer should be provided with a suitable support for being confident in the correctness of the way in which the thread coordination and behavior is dealt with.

• A complicated combination of several different synchronization techniques – sleep and wakeup primitives, semaphores, monitors, etc. – may be necessary.

Page 3: Preserving Architectural Properties in Multithreaded Code ... · abstraction the program topology in terms of thread instances, their interactions, and their behavior. 2. Use a translator

Coordination 2005 © Marco Bernardo & Edoardo Bontà 3

BackgroundBackground

An Architecture-driven Approach

• In [WICSA2004] we presented a Java package called Sync that takes care of the details of the thread

synchronization in a way that is transparent to the

software developer.

• The components of the package are inspired by the main

architectural concepts: architectures, components,

connectors, ports.

• The package is consistent with a rich synchronization

model.

Page 4: Preserving Architectural Properties in Multithreaded Code ... · abstraction the program topology in terms of thread instances, their interactions, and their behavior. 2. Use a translator

Coordination 2005 © Marco Bernardo & Edoardo Bontà 4

BackgroundBackground

Application Steps of the Approach

1. Provide an architectural description in PADL of the multithreaded program, which specifies at a high level of abstraction the program topology in terms of thread instances, their interactions, and their behavior.

2. Use a translator called PADL2Java that, from the PADL specification of the multithreaded Java program automatically generates (a skeleton of) the program based on the Java package.

Page 5: Preserving Architectural Properties in Multithreaded Code ... · abstraction the program topology in terms of thread instances, their interactions, and their behavior. 2. Use a translator

Coordination 2005 © Marco Bernardo & Edoardo Bontà 5

Java packageJava package Sync:

LayerLayer Architecture

Advantages deriving from inheritance:

• Hierarchy

• Composition

• Concurrency

Architecture

ThreadElem

Port

Connector

public abstract class Architecture

extends ThreadElem {…}

Page 6: Preserving Architectural Properties in Multithreaded Code ... · abstraction the program topology in terms of thread instances, their interactions, and their behavior. 2. Use a translator

Coordination 2005 © Marco Bernardo & Edoardo Bontà 6

Java packageJava package Sync:

LayerLayer ThreadElem

• Constructors of the Thread class

redefined in the ThreadElem

class with the same parameters.

• Additional methods for easily

generating Java code for the thread.

Architecture

ThreadElem

Port

Connector

public abstract class ThreadElem

extends Thread {…}

Page 7: Preserving Architectural Properties in Multithreaded Code ... · abstraction the program topology in terms of thread instances, their interactions, and their behavior. 2. Use a translator

Coordination 2005 © Marco Bernardo & Edoardo Bontà 7

Java packageJava package Sync:

LayerLayer Port

Synchronous / Asynchronous Ports:

uni - sender and - sender or - sender

uni - receiver and - receiver or - receiver

Architecture

ThreadElem

Port

Connector

Page 8: Preserving Architectural Properties in Multithreaded Code ... · abstraction the program topology in terms of thread instances, their interactions, and their behavior. 2. Use a translator

Coordination 2005 © Marco Bernardo & Edoardo Bontà 8

Java packageJava package Sync:

LayerLayer Connector

Connector types:

• asynchronous-to-asynchronous

• asynchronous-to-synchronous

• synchronous-to-asynchronous

• synchronous-to-synchronous

Architecture

ThreadElem

Port

Connector

Page 9: Preserving Architectural Properties in Multithreaded Code ... · abstraction the program topology in terms of thread instances, their interactions, and their behavior. 2. Use a translator

Coordination 2005 © Marco Bernardo & Edoardo Bontà 9

ContributionContribution

Property Preservation

• Separate the thread synchronization management from the

thread behavior translation.

• How to translate the specification of the thread behavior

into Java code in a way that preserves the properties

proved at the architectural level?

• Only a partial translation based on stubs is possible, with

the property preservation depending on the way in which

the stubs are filled in.

• Extending PADL2Java to provide support for the thread

behavior translation and the stub generation.

Page 10: Preserving Architectural Properties in Multithreaded Code ... · abstraction the program topology in terms of thread instances, their interactions, and their behavior. 2. Use a translator

Coordination 2005 © Marco Bernardo & Edoardo Bontà 10

File Structure of the Generated CodeFile Structure of the Generated Code

automaticallyderivedclasses

automaticallygeneratedstubs

program (-p) / applet (-a)

Architecture-derived class

...

...

ThreadElem-derived classes

...T1

Sync package

IBT2 IBTn

ET1 ET2 ETn

IBT1

T2 Tn

Page 11: Preserving Architectural Properties in Multithreaded Code ... · abstraction the program topology in terms of thread instances, their interactions, and their behavior. 2. Use a translator

Coordination 2005 © Marco Bernardo & Edoardo Bontà 11

Generating CodeGenerating Code

class <Architecture Type Name>extends Architecture {

Sections:

• DECLARING THREADS (element types)

• DECLARING ARCHITECTURAL INTERACTIONS

• DEFINING CONSTRUCTOR

• BUILDING ARCHITECTURE

• RUNNING ARCHITECTURE

}

class <Element Type Name>extends ThreadElem {

Sections:

• DEFINING CONSTRUCTOR

• DEFINING BEHAVIOR

• INSTANTIATING INPUT INTERACTIONS

• INSTANTIATING OUTPUT INTERACTIONS

}

PADL2Java [-p/-c/-a] file1.padl Java_package_name

import Sync.*;

Page 12: Preserving Architectural Properties in Multithreaded Code ... · abstraction the program topology in terms of thread instances, their interactions, and their behavior. 2. Use a translator

Coordination 2005 © Marco Bernardo & Edoardo Bontà 12

TranslatingTranslating the the ThreadThread BehaviorBehavior

• Method run() guides the execution together with methods

behavEqNext(), behavEqCall(), and behavStop().

• Each behavioral equation of the PADL specification is

translated into a behavioral method.

• Each behavioral method is generated by proceeding by

induction on the algebraic structure of the corresponding

behavioral equation.

Page 13: Preserving Architectural Properties in Multithreaded Code ... · abstraction the program topology in terms of thread instances, their interactions, and their behavior. 2. Use a translator

Coordination 2005 © Marco Bernardo & Edoardo Bontà 13

Flow (behavior) control:

run(), behavEqNext(), behavStop(), …

Behavioral Invocations &Behavioral Invocations &

Process Term stopProcess Term stop

Thread_1

eq#0

eq#2 eq#1

stop

public void run() {

<IBT instantiation>

<ET instantiation>

behavEqNext(0, <eq#0 params>);

while (behavEqCall());

}

public void <eq#n>() {

<…>

behavEqNext(k, <eq#k params>);

<…>

behavStop();

}p1

p2

p3

Page 14: Preserving Architectural Properties in Multithreaded Code ... · abstraction the program topology in terms of thread instances, their interactions, and their behavior. 2. Use a translator

Coordination 2005 © Marco Bernardo & Edoardo Bontà 14

Flow (behavior) control:

run(), behavEqNext(), behavStop(), …

Action PrefixAction Prefix

IBT_Thread_1

<internal action a>

<internal action z>

eq#0

eq#2 eq#1

stop

Thread_1

p1

p2

p3

Page 15: Preserving Architectural Properties in Multithreaded Code ... · abstraction the program topology in terms of thread instances, their interactions, and their behavior. 2. Use a translator

Coordination 2005 © Marco Bernardo & Edoardo Bontà 15

Flow (behavior) control:

run(), behavEqNext(), behavStop(), …

Interactions Interactions –– ExceptionsExceptions

UnattachedPortException

AsyncPortNotReadyException

Thread_2

IBT_Thread_1

<internal action a>

<internal action z>

ET_Thread_1<exception α>

<exception ω>

…eq#0

eq#2 eq#1

stop

Architecture_k

No exception to handle (synchronous, attached port)

Thread_1( instance of )

p1

p2

p3

Page 16: Preserving Architectural Properties in Multithreaded Code ... · abstraction the program topology in terms of thread instances, their interactions, and their behavior. 2. Use a translator

Coordination 2005 © Marco Bernardo & Edoardo Bontà 16

Flow (behavior) control:

run(), behavEqNext(), behavStop(), …

ChoiceChoice

eq#0

eq#2 eq#1

stop

Thread_1

public void <eq#0>() {

<...>

switch(choice(<choice list>)) {

case 0:

act_Thread_1.<action a(params a)>;

break;

case 1:

try {

p1.send(<params p1>);

} catch (AsyncPortNotReadyException e) {

exc_Thread_1.<exception α(params p1)>;

}

break;

default:

behavStop();

}

}

p1

p2

p3

Page 17: Preserving Architectural Properties in Multithreaded Code ... · abstraction the program topology in terms of thread instances, their interactions, and their behavior. 2. Use a translator

Coordination 2005 © Marco Bernardo & Edoardo Bontà 17

Preserving Architectural PropertiesPreserving Architectural Properties

• PADL is equipped with a component-oriented technique

based on equivalence checking for verifying the freedom

from architectural mismatches [TCS 2005].

• Are such properties preserved when going to the code

level?

• Three aspects:

1. Code generated for the thread management: Ok

2. Code generated for the behavioral equations: Ok

3. Code provided for filling in the stubs: don’t know…

Page 18: Preserving Architectural Properties in Multithreaded Code ... · abstraction the program topology in terms of thread instances, their interactions, and their behavior. 2. Use a translator

Coordination 2005 © Marco Bernardo & Edoardo Bontà 18

Main ResultMain Result

Theorem. Let T be the process algebraic description of the behavior of a thread

and let a be an internal action occurring in T. Let a1, a2, ..., am be the fresh

actions abstracting the statements into which a is translated and let T’ be the

process algebraic description of the behavior of the thread obtained from T by

replacing every occurrence of a._ with a1.a2. ... .am._ . Let H be the set

of internal actions occurring in T or T’. Whenever T satisfies P and

a.stop / H ~B a1.a2. ... .am.stop / H, then T’ satisfies P as well.

refinement stat1

stat2

abstraction a1

a2

statmam

a

Page 19: Preserving Architectural Properties in Multithreaded Code ... · abstraction the program topology in terms of thread instances, their interactions, and their behavior. 2. Use a translator

Coordination 2005 © Marco Bernardo & Edoardo Bontà 19

Guidelines for Filling in the Stubs Guidelines for Filling in the Stubs (1/2)(1/2)

• No synchronized methods should be defined within the stubs, so that methods like wait() and notify() –which could not be abstracted through internal actions –cannot occur within the stubs.

• No further thread should be created within the stubs, as this would have an observable impact on the system topology and the thread coordination.

• There should be no variables/objects that are visible from several stub classes. This means that all the data shared by several threads should be exchanged only through suitable components of the package Sync.

Page 20: Preserving Architectural Properties in Multithreaded Code ... · abstraction the program topology in terms of thread instances, their interactions, and their behavior. 2. Use a translator

Coordination 2005 © Marco Bernardo & Edoardo Bontà 20

Guidelines for Filling in the Stubs Guidelines for Filling in the Stubs (2/2)(2/2)

• In the stub method associated with the first internal action following an invocation of the method send() (resp. receive()), every object that has been passed in that invocation should be copied, with all the stub methods associated with the subsequent internal actions working on that copy of the object. This avoids interferences among threads stemming from the fact that the method send() always keeps a reference to the passed objects – so that it can be defined in the package Sync in a way that supports arbitrarily many parameters of arbitrary types – and such objects may be modified by the stub method associated with some internal action.

• All the exceptions that can be raised when executing a stub method should be caught or prevented from being raised inside the stub method itself.

• Non-terminating statements should be avoided within the stub methods.

Page 21: Preserving Architectural Properties in Multithreaded Code ... · abstraction the program topology in terms of thread instances, their interactions, and their behavior. 2. Use a translator

Coordination 2005 © Marco Bernardo & Edoardo Bontà 21

ConclusionConclusion

• Proposed an approach for semi-automatically generating

the thread behavior in a way that preserves certain

architectural properties.

• Related approaches: ArchJava, C2SADEL.

• Future work/extensions:

– Experimenting with PADL2Java to assess its effectiveness.

– Integration with ArchJava and C2SADEL to take advantage

of their complementary strengths.