Zurück zu Willert.de

How to use Mutex

Rhapsody Model or Codegeneration topics

How to use Mutex

Postby stef85 » September 3rd, 2013, 9:06 am

Hello people.
I did not understand how to use the mutex for a function.
I've set the property CG :: Concurrency :: GUARDED to the function.(my code add GUARD_OPERATION)
Is it enough?
Might I have an example?

Code: Select all
void Log::salvataggioFile(const OMString& arg1, const OMString& arg2, unsigned long long time) {
    GUARD_OPERATION

}
stef85
 
Posts: 6
Joined: August 26th, 2013, 11:12 am

Re: How to use Mutex

Postby shanz » September 3rd, 2013, 10:47 am

To really understand how Rhapsody auto code generates the mutexes, you need to study the proprocessed code since Rhapsody unfortunately uses macros (#defines). If you change your compile command to display preprocessed files then you'll discover how it works.
shanz
 
Posts: 178
Joined: May 7th, 2008, 5:50 pm
Location: Horsham, W Sussex, England

Re: How to use Mutex

Postby fsjunior » September 3rd, 2013, 2:35 pm

The shanz advice is very good. Always try to understand the generated code of the Rhapsody. It helps me a lot. Really.

The shortcut for your question is: if the property that you changed is "CG::Operation::Concurrency" to guarded, then, yes, it is enough.

The CG::Operation::Concurrency setted as Guarded change the class to a guarded one (inserts a mutex in it). The GUARD_OPERATION macro generated by this property gets the mutex, call the lock() method in constructor and unlock method in destructor at the end of the method.

#define GUARD_OPERATION OMGuard _omGuard(getGuard());
fsjunior
 
Posts: 19
Joined: March 22nd, 2013, 8:15 pm

Re: How to use Mutex

Postby stef85 » September 3rd, 2013, 3:18 pm

So if I want exclusive access when the state machine is running from every state by calling the function inside the LOG block (see code below)
FILE: Log.cpp
Code: Select all
//#[ ignore
#define NAMESPACE_PREFIX
//#]

//## auto_generated
#include "Log.h"
//## auto_generated
#include "windows.h"
//## auto_generated
#include "iostream"
//## auto_generated
#include "fstream"
//#[ ignore
#define Default_Log_salvataggioFile_SERIALIZE \
    aomsmethod->addAttribute("arg1", x2String(arg1));\
    aomsmethod->addAttribute("arg2", x2String(arg2));\
    aomsmethod->addAttribute("time", x2String(time));
#define Default_Log_Log_SERIALIZE OM_NO_OP
//#]

//## package Default

//## class Log
Log::Log() {
    NOTIFY_CONSTRUCTOR(Log, Log(), 0, Default_Log_Log_SERIALIZE);
}

Log::~Log() {
    NOTIFY_DESTRUCTOR(~Log, true);
}

void Log::salvataggioFile(const OMString& arg1, const OMString& arg2, unsigned long long time) {
    GUARD_OPERATION
   
    NOTIFY_OPERATION(salvataggioFile, salvataggioFile(OMString,OMString,unsigned long long), 3, Default_Log_salvataggioFile_SERIALIZE);
    //#[ operation salvataggioFile(OMString,OMString,unsigned long long)
    //printf("l'argomento 1 e': %c",arg1);
    //std::cout << arg1;
     std::ofstream f("fileLogSistema.txt", std::ios::app); //apre il file in modalità append, lasciando intatto quello che c'è e scrivendo alla fine
     f << time
       <<","
       <<arg1
       <<","
       <<arg2 <<std::endl;
     
    //#]
}

int Log::getSemaforo() const {
    return semaforo;
}

void Log::setSemaforo(int p_semaforo) {
    GUARD_OPERATION
    semaforo = p_semaforo;
}

#ifdef _OMINSTRUMENT
//#[ ignore
void OMAnimatedLog::serializeAttributes(AOMSAttributes* aomsAttributes) const {
    aomsAttributes->addAttribute("semaforo", x2String(myReal->semaforo));
}
//#]

IMPLEMENT_META_P(Log, Default, Default, false, OMAnimatedLog)
#endif // _OMINSTRUMENT

/*********************************************************************
   File Path   : DefaultComponent\DefaultConfig\Log.cpp
*********************************************************************/


In the states of the statechart made ​​for each block, I call for each states
Code: Select all
getItsContext()->getItsLog()->salvataggioFile(stato1,blocco1,ul.QuadPart);


Questions:
1) Do acces exclusive function works? Or isn't it enough?

Thanks
stef85
 
Posts: 6
Joined: August 26th, 2013, 11:12 am

Re: How to use Mutex

Postby fsjunior » September 3rd, 2013, 7:08 pm

If I am not wrong, I think that yes, it is enough.
fsjunior
 
Posts: 19
Joined: March 22nd, 2013, 8:15 pm

Re: How to use Mutex

Postby stef85 » September 14th, 2013, 6:03 pm

I tried to simulate my project and to save in file.txt

without MUTEX to the function:

13023647348828125000,Idle,LogicaDiSelezioneDelProfDiVelocita
taDatiAdATO,ATC
13023647348828125000,AbilitazioneComando,ATC
13023647349125000000,AttesaRichiestaDatiATC,GestoreInizializzazione
13023647349125000000,InvioDatiAdATC,GestoreInizializzazione
13023647349125000000,AttesaRichiestaTRN,AirgapATS


Loss of data

With MUTEX to the function:

13023648004359375000,Idle,LogicaDiSelezioneDelProfDiVelocita
13023648004500000000,AttesaRichiestaTRN,AirgapATS
13023648004500000000,AttesaRichiestaDatiATC,GestoreInizializzazione
13023648004796875000,Standby,ATC
13023648004812500000,RichiestaDatiAdATO,ATC
13023648004812500000,AbilitazioneComando,ATC
13023648004812500000,ControlloAttesaComandoDiStart,ATC
13023648004828125000,AttesaRichiestaTRN,AirgapATS
stef85
 
Posts: 6
Joined: August 26th, 2013, 11:12 am

Re: How to use Mutex

Postby wvdheiden » September 16th, 2013, 9:45 am

Hi

yes it looks OK, certainly when looking at the debug output. The generated code for C++ looks very different from the C version, which I know better, As already stated before, it helpes to look at the macro source code to understand what happens.

Walter
===============================================
Who is General Failure and why is he reading my drive?
User avatar
wvdheiden
 
Posts: 52
Joined: October 5th, 2007, 10:31 am
Location: Hellendoorn, the Netherlands
Company: Willert Software Tools GmbH


Return to Modelling with Rhapsody

Who is online

Users browsing this forum: No registered users and 5 guests

cron