PassiveToCyclical Aspect
[Activation Group]

This aspect is defined in file: ../../AspectXLib/RealTime/PassiveToCyclical/aspects/PassiveToCyclical.ax

Aspect Description

This sample aspect transforms passive classes into active classes with cyclical activation. A passive class is a class whose object instances do not have their own execution thread. An active class is instead a class whose instances have an execution thread associated to them. This aspect demonstrates how to turn a passive class into a cyclical class i.e. a class with its own thread of execution that is activated on a cyclical basis.

The base code upon which the aspect operates is a small but self-contained application. This application consists of the following four classes: main, MicroTime, Task1, and Task2. The two task classes are the classes that are initially passive and that must be provided with activation code. These two classes expose a method called activate. This is the method through which the classes should be activated.

The main class contains a small main program that consists of an endless loop that, at every iteration, calls the activate methods on the two task classes.

The MicroTime class is just a helper class that encapsulates access to timing service. This class is not affected by the aspect transformation.

The most important transformations performed by aspect program are: In all cases, the activation code uses synchronization primitives provided by the POSIX standard. This as also the reason why sleep() function is used instead of sleep_until().
Author:
A. Pasetti, O. Rohlik
See also:
PassiveToSporadic

Pointcuts

unittargetInitializationUnit
  Points to the unit that contains main() method
blocktargetSchedulerMethodBody
 Points to the body of the function scheduler()
classtargetTaskClassDeclaration
  Points to target classes to which declarations of _activate() method should be added
unittargetTaskClassDefinition
  Points to the target units where are definitions of methods classes identified by pointcut targetTaskClassDeclaration

Advices

addaddCyclicalActivateDeclaraion
  Add declaration of the _activate() method to the class declaration
addaddCyclicalActivateDefinition
  Add definition of _activate() method to class definition files
addaddActivationHelperFunctions
  Adds definition of two methods that activate the threads of active objects
addaddIncludesToMain
  Add the #include "main
aroundreplaceSchedulerBody
  Replaces the body of schedule() function

Pointcut Documentation

unit targetInitializationUnit (source)
Points to the unit that contains main() method. In srcML, a unit is a source file (a definition file, a declaration file, or an inline file). The pointcut is restricted to include only those units that contain function whose full name is main.
block targetSchedulerMethodBody (source)
Points to the body of the function scheduler().
class targetTaskClassDeclaration (source)
Points to target classes to which declarations of _activate() method should be added. The classes that must be modified are identified by their full name. The name have to contain substring Task.
unit targetTaskClassDefinition (source)
Points to the target units where are definitions of methods classes identified by pointcut targetTaskClassDeclaration.

Advice Documentation

add addCyclicalActivateDeclaraion (source)

Triplet: class add declaration

Add declaration of the _activate() method to the class declaration. This method is to be called as a POSIX thread. Therefore its return value has to be a pointer (void *) and has to have one untyped pointer as a parameter.

Refers to global pointcut: class targetTaskClassDeclaration

add addCyclicalActivateDefinition (source)

Triplet: unit add definition

Add definition of _activate() method to class definition files. The full name of the method is constructed using dollar variable ${className}.

The method body contains a loop that call the activate() method of the same class with period of 7 seconds. The activate() method is called at the beginning of the loop. After the method call the thread is suspended for time remaining to the end of the period.

Refers to global pointcut: unit targetTaskClassDefinition

add addActivationHelperFunctions (source)

Triplet: unit add definition

Adds definition of two methods that activate the threads of active objects. The threads execution (in pthread_create()) is associated with the two helper functions _activate1() and _activate2() that pass the calls to the respective methods in classes Task1 and Task2. These helper functions are used because POSIX function pthread_create() does not allow to use C++ method as a parameter - only C functions are accepted.

Refers to global pointcut: unit targetInitializationUnit

add addIncludesToMain (source)

Triplet: unit add include

Add the #include "main.h" preprocessor instruction. Header file main.h contains signatures of two functions (_activate1() and _activate2()) added to the main module. Since both functions are woven at the end of the file their declarations have to be inserted at the beginning of the unit using the header file.

Refers to global pointcut: unit targetInitializationUnit

around replaceSchedulerBody (source)

Triplet: block around codeFragment

Replaces the body of schedule() function. It inserts code that initializes the two threads and suspends the execution of the main thread.

The threads are executed using helper functions _activate1() and _activate2() that pass the calls to the respective methods in classes Task1 and Task2.

Refers to global pointcut: block targetSchedulerMethodBody