<aspect xmlns:cpp="http://www.sdml.info/srcML/cpp" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://control.ee.ethz.ch/XWeaver/AspectX" xmlns:src="http://www.sdml.info/srcML/src" xsi:schemaLocation="http://control.ee.ethz.ch/XWeaver/AspectX ../../../../src/xsd/aspectX.xsd" name="Entry">
        
<description>
          This aspect inserts synchronization code into the base code 
          to implement methods as entries (in Ada95 sense of word).
          
<i>Synchronization code</i> is code 
      that protects access to certain code segments. In this example
      synchronization code provides protection against simultaneous access to a class 
<i>method</i>
      by more than one thread of execution.
        
<p />
        A protected entry is guaranteed to execute in mutual exclusion and is 
        guarded by a boolean expression (called a 
<i>barrier</i>). If 
        this barrier evaluates to false when the entry call is made, the calling thread 
        is suspended until the barrier evaluates to true and no other threads are 
        active inside the protected object. Hence protected entry calls can be used to 
        implement condition synchronization.
        
<p />
      This sample aspect adds synchronization code to the body of the method that
      has to be protected. 
      It uses a value of 
<code>trigger</code> as a barrier.
      The barrier is accessed in mutual exclusion. It uses 
<code>mutex</code> object to 
      protect the method (entry) against multiple access to the method and 
      
<i>condition variable</i> <code>condvar</code> to control the access to the barrier.
        
<p />
      The activation code uses synchronization primitives provided by the POSIX standard.      
      
<author>A. Pasetti, O. Rohlik</author>
      
<see>Synchronization</see>
      
<see>PassiveToSporadic</see>
    
</description>
        
        

        
<pointcut name="targetClassDeclaration" type="src:class" constraint="src:name='UniqueResource'">
                
<description>
                        Points to the target class declaration.
                        The class is identified by its full name: 
<code>UniqueResource</code>.
                
</description>
        
</pointcut>

        
<pointcut name="targetClassDefinition" type="src:unit">
                
<description>
            Points to the target unit where method of the given class are defined.
            The class if specified by a pointcut 
<code>targetClassDeclaration</code>.
                
</description>
                
<restriction type="isDefinitionOf">
                    
<pointcutRef ref="targetClassDeclaration" type="src:class" />
                
</restriction>
        
</pointcut>
                        
         
<pointcut name="targetClassUnit" type="src:unit">
                
<description>
            Points to the target unit where class identified 
            by pointcut 
<code>targetClassDeclaration</code> is declared. 
        
</description>
        
<restriction type="contain">
            
<pointcutRef type="src:class" ref="targetClassDeclaration" />
        
</restriction>
        
</pointcut> 
        
        
<pointcut name="targetSynchronizedFunction" type="src:function" constraint="src:name='UniqueResource::activate'"> 
                
<description>
                    Point to the function definition that has to be synchronized. The function
                    is identified by its full name: 
<code>UniqueResource::activate()</code>
                
</description>
        
</pointcut>
        
        
<pointcut name="targetInitializationUnit" type="src:unit">
                
<description>
                    Points to the 
<i>unit</i> that contains <code>main()</code> method. 
                    The pointcut is restricted to include only those units that contain
                        function whose full name is 'main'. 
        
</description>
        
<restriction type="contain">
            
<pointcut type="src:function" constraint="src:name='main'" />
        
</restriction>
        
</pointcut>

        
<advice type="add" name="addSynchronizationDefinition">
            
<description>
                Adds declaration and initialization of three variables 
                needed to implement the mutual exclusive access
                to the condition variable 
<code>trigger</code>.
            
<p />            
            This advice has to precede other advices that modify code
            in the main unit.
        
</description>
                
<pointcutRef ref="targetClassDefinition" type="src:unit" />
                
<codeModifier type="declaration">
                    
<text>extern pthread_mutex_t mutex;</text>
                    
<text>extern pthread_cond_t condvar;</text>
                    
<text>extern int trigger;</text>
                    
<text />
                
</codeModifier>
        
</advice>
    
        
<advice type="add" name="addSynchronizationDeclaration">
            
<description>
                Adds external declaration of three variables 
                needed to implement the mutual exclusive access
                to the condition variable 
<code>trigger</code>.
        
</description>
                
<pointcutRef ref="targetInitializationUnit" type="src:unit" />
                
<codeModifier type="declaration">
                    
<text>pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;</text>
                    
<text>pthread_cond_t condvar=PTHREAD_COND_INITIALIZER;</text>
                    
<text>int trigger = 0;</text>
                    
<text />
                
</codeModifier>
        
</advice>        
        
        
<advice type="add" name="addInclude">
            
<description>
                Add the 
<code>#include &lt;pthread.h&gt;</code> preprocessor instruction.
            Header file 
<code>pthread.h</code> contain declaration
            of methods and data types implementing mutual 
            exclusive access policy according to the POSIX standard.
        
</description>
                
<pointcutRef ref="targetClassDefinition" type="src:unit" />
                
<codeModifier type="include">
                    
<text>#include &lt;pthread.h&gt; // added by aspect  </text>
                
</codeModifier>
        
</advice>
                
  
<advice type="begin" name="atBegin">
    
<description>
         Inserts the code that locks the access to the method body 
         by acquiring the 
<code>mutex</code>. The code is inserted at the very 
         beginning of the target method. 
    
</description>  
    
<pointcutRef ref="targetSynchronizedFunction" type="src:function" />
    
<codeModifier type="codeFragment">
      
<text>cout &lt;&lt; "Caller " &lt;&lt; param &lt;&lt; " at guard at time:     " &lt;&lt; MicroTime::getMicroTime() &lt;&lt; endl &lt;&lt; flush;
pthread_mutex_lock(&amp;mutex);
while (!trigger) { // non-zero value == do action, otherwise wait
        pthread_cond_wait(&amp;condvar,&amp;mutex); // waits for notification
}
cout &lt;&lt; "Caller " &lt;&lt; param &lt;&lt; " passed guard at time: " &lt;&lt; MicroTime::getMicroTime() &lt;&lt; endl &lt;&lt; flush; 
      
</text>    
    
</codeModifier>
  
</advice>
  
  
  
<advice type="end" name="atEnd">
    
<description>
         Inserts the code that release the access to the method body 
         by releasing the 
<code>mutex</code>. The code is inserted at the very 
         end of the target method.     
    
</description>
    
<pointcutRef ref="targetSynchronizedFunction" type="src:function" />
    
<codeModifier type="codeFragment">
      
<text> 
pthread_cond_signal(&amp;condvar); // notify others that trigger is available
pthread_mutex_unlock(&amp;mutex);  // release mutex
</text>
    
</codeModifier>
  
</advice>
     
  
<advice type="replace" name="guardConditionChange">
    
<description>
         Inserts the code to the 
<code>main()</code> function 
         that changes the 
<i>barrier</i> by setting the 
         
<code>trigger</code> variable to non-zero value. 
         This has to be done in mutual exclusion. Note 
         that in this example the 
<i>barrier</i> is the condition
         
<code>(!trigger)</code>.     
    
</description>
    
<pointcut type="src:comment" constraint="text()='/* This is a hook where aspect will insert code that changes the guard in UniqueResource class */'" />
    
<codeModifier type="codeFragment">
      
<text> 
pthread_mutex_lock(&amp;mutex);
trigger = 1; // set up condvar to non-zero value
pthread_cond_signal(&amp;condvar); 
pthread_mutex_unlock(&amp;mutex);
pthread_cond_signal(&amp;condvar);
          
</text>
    
</codeModifier>
  
</advice>     
     
</aspect>






































v