From: prascle Date: Wed, 5 Oct 2005 20:23:08 +0000 (+0000) Subject: PR: design simplification X-Git-Tag: BR_UnitTests_20051010~4 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=c1538a94ef22d85caa6deb141ee9fc9f2ac2ef5b;p=modules%2Fkernel.git PR: design simplification --- diff --git a/src/SALOMELocalTrace/BaseTraceCollector.cxx b/src/SALOMELocalTrace/BaseTraceCollector.cxx index 942f07307..de6121897 100644 --- a/src/SALOMELocalTrace/BaseTraceCollector.cxx +++ b/src/SALOMELocalTrace/BaseTraceCollector.cxx @@ -38,6 +38,7 @@ using namespace std; BaseTraceCollector* BaseTraceCollector::_singleton = 0; pthread_mutex_t BaseTraceCollector::_singletonMutex; +sem_t BaseTraceCollector::_sem; int BaseTraceCollector::_threadToClose = 0; pthread_t* BaseTraceCollector::_threadId = 0; // used to control single run diff --git a/src/SALOMELocalTrace/BaseTraceCollector.hxx b/src/SALOMELocalTrace/BaseTraceCollector.hxx index c05b2fe2b..dad4d9e0b 100644 --- a/src/SALOMELocalTrace/BaseTraceCollector.hxx +++ b/src/SALOMELocalTrace/BaseTraceCollector.hxx @@ -28,6 +28,7 @@ #define _BASETRACECOLLECTOR_HXX_ #include +#include #if defined SALOMELOCALTRACE_EXPORTS @@ -59,6 +60,7 @@ class SALOMELOCALTRACE_EXPORT BaseTraceCollector static BaseTraceCollector* _singleton; static pthread_mutex_t _singletonMutex; static pthread_t* _threadId; + static sem_t _sem; }; #endif diff --git a/src/SALOMELocalTrace/FileTraceCollector.cxx b/src/SALOMELocalTrace/FileTraceCollector.cxx index 5dd1074be..5bceaeaab 100644 --- a/src/SALOMELocalTrace/FileTraceCollector.cxx +++ b/src/SALOMELocalTrace/FileTraceCollector.cxx @@ -58,14 +58,15 @@ BaseTraceCollector* FileTraceCollector::instance(const char *fileName) { // the lock after the first test DEVTRACE("FileTraceCollector:: instance()"); _singleton = new FileTraceCollector(); - _fileName = fileName; DEVTRACE(" _fileName: " << _fileName); + sem_init(&_sem,0,0); // to wait until run thread is initialized pthread_t traceThread; int bid; int re2 = pthread_create(&traceThread, NULL, FileTraceCollector::run, (void *)bid); + sem_wait(&_sem); DEVTRACE("FileTraceCollector:: instance()-end"); } ret = pthread_mutex_unlock(&_singletonMutex); // release lock @@ -86,93 +87,71 @@ BaseTraceCollector* FileTraceCollector::instance(const char *fileName) void* FileTraceCollector::run(void *bid) { - int isOKtoRun = 0; - int ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone + _threadId = new pthread_t; + *_threadId = pthread_self(); + sem_post(&_sem); // unlock instance - if (! _threadId) // only one run - { - isOKtoRun = 1; - if(_threadId == 0) - { - _threadId = new pthread_t; - } - *_threadId = pthread_self(); - } - else cerr << "--- FileTraceCollector::run-serious design problem..." <toCollect() ) + { + if (_threadToClose) + DEVTRACE("FileTraceCollector _threadToClose"); - while ((!_threadToClose) || myTraceBuffer->toCollect() ) + int fullBuf = myTraceBuffer->retrieve(myTrace); + if (myTrace.traceType == ABORT_MESS) { - if (_threadToClose) - DEVTRACE("FileTraceCollector _threadToClose"); - - int fullBuf = myTraceBuffer->retrieve(myTrace); - if (myTrace.traceType == ABORT_MESS) - { #ifndef WNT - traceFile << "INTERRUPTION from thread " << myTrace.threadId - << " : " << myTrace.trace; + traceFile << "INTERRUPTION from thread " << myTrace.threadId + << " : " << myTrace.trace; #else - traceFile << "INTERRUPTION from thread " - << (void*)(&myTrace.threadId) - << " : " << myTrace.trace; + traceFile << "INTERRUPTION from thread " + << (void*)(&myTrace.threadId) + << " : " << myTrace.trace; #endif - traceFile.close(); - cout << flush ; + traceFile.close(); + cout << flush ; #ifndef WNT - cerr << "INTERRUPTION from thread " << myTrace.threadId - << " : " << myTrace.trace; + cerr << "INTERRUPTION from thread " << myTrace.threadId + << " : " << myTrace.trace; #else - cerr << "INTERRUPTION from thread " << (void*)(&myTrace.threadId) - << " : " << myTrace.trace; + cerr << "INTERRUPTION from thread " << (void*)(&myTrace.threadId) + << " : " << myTrace.trace; #endif - cerr << flush ; - exit(1); - } - else - { + cerr << flush ; + exit(1); + } + else + { #ifndef WNT - traceFile << "th. " << myTrace.threadId - << " " << myTrace.trace; + traceFile << "th. " << myTrace.threadId + << " " << myTrace.trace; #else - traceFile << "th. " << (void*)(&myTrace.threadId) - << " " << myTrace.trace; + traceFile << "th. " << (void*)(&myTrace.threadId) + << " " << myTrace.trace; #endif - } } - DEVTRACE("traceFile.close()"); - traceFile.close(); - DEVTRACE("traceFile.close()_end"); - pthread_exit(NULL); } - //return NULL; + DEVTRACE("traceFile.close()"); + traceFile.close(); + DEVTRACE("traceFile.close()_end"); + pthread_exit(NULL); } // ============================================================================ diff --git a/src/SALOMELocalTrace/LocalTraceCollector.cxx b/src/SALOMELocalTrace/LocalTraceCollector.cxx index e19e2252a..6386998c5 100644 --- a/src/SALOMELocalTrace/LocalTraceCollector.cxx +++ b/src/SALOMELocalTrace/LocalTraceCollector.cxx @@ -54,10 +54,12 @@ BaseTraceCollector* LocalTraceCollector::instance() { // the lock after the first test _singleton = new LocalTraceCollector(); + sem_init(&_sem,0,0); // to wait until run thread is initialized pthread_t traceThread; int bid; int re2 = pthread_create(&traceThread, NULL, LocalTraceCollector::run, (void *)bid); + sem_wait(&_sem); } ret = pthread_mutex_unlock(&_singletonMutex); // release lock } @@ -77,66 +79,45 @@ BaseTraceCollector* LocalTraceCollector::instance() void* LocalTraceCollector::run(void *bid) { - int isOKtoRun = 0; - int ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone + _threadId = new pthread_t; + *_threadId = pthread_self(); + sem_post(&_sem); // unlock instance - if (! _threadId) // only one run - { - isOKtoRun = 1; - if(_threadId == 0) - { - _threadId = new pthread_t; - } - *_threadId = pthread_self(); - } - else cerr << "--- LocalTraceCollector::run-serious design problem..." <toCollect() ) + { + if (_threadToClose) + DEVTRACE("FileTraceCollector _threadToClose"); - while ((!_threadToClose) || myTraceBuffer->toCollect() ) + int fullBuf = myTraceBuffer->retrieve(myTrace); + if (myTrace.traceType == ABORT_MESS) { - //if (_threadToClose) - // cerr << "FileTraceCollector _threadToClose" << endl << flush; - - int fullBuf = myTraceBuffer->retrieve(myTrace); - if (myTrace.traceType == ABORT_MESS) - { - cout << flush ; + cout << flush ; #ifndef WNT - cerr << "INTERRUPTION from thread " << myTrace.threadId - << " : " << myTrace.trace; + cerr << "INTERRUPTION from thread " << myTrace.threadId + << " : " << myTrace.trace; #else - cerr << "INTERRUPTION from thread " << (void*)(&myTrace.threadId) - << " : " << myTrace.trace; + cerr << "INTERRUPTION from thread " << (void*)(&myTrace.threadId) + << " : " << myTrace.trace; #endif - cerr << flush ; - exit(1); - } - else - { - cout << flush ; + cerr << flush ; + exit(1); + } + else + { + cout << flush ; #ifndef WNT - cerr << "th. " << myTrace.threadId << " " << myTrace.trace; + cerr << "th. " << myTrace.threadId << " " << myTrace.trace; #else - cerr << "th. " << (void*)(&myTrace.threadId) - << " " << myTrace.trace; + cerr << "th. " << (void*)(&myTrace.threadId) + << " " << myTrace.trace; #endif - cerr << flush ; - } + cerr << flush ; } } pthread_exit(NULL); @@ -155,7 +136,7 @@ LocalTraceCollector:: ~LocalTraceCollector() ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone if (_singleton) { - //cerr << "LocalTraceCollector:: ~LocalTraceCollector()" << endl <insert(NORMAL_MESS,"end of trace\n"); // to wake up thread @@ -163,7 +144,7 @@ LocalTraceCollector:: ~LocalTraceCollector() { int ret = pthread_join(*_threadId, NULL); if (ret) cerr << "error close LocalTraceCollector : "<< ret << endl; - //else cerr << "LocalTraceCollector destruction OK" << endl; + else DEVTRACE("LocalTraceCollector destruction OK"); _threadId = 0; _threadToClose = 0; } diff --git a/src/SALOMETraceCollector/SALOMETraceCollector.cxx b/src/SALOMETraceCollector/SALOMETraceCollector.cxx index de0331d96..fbb0e322a 100644 --- a/src/SALOMETraceCollector/SALOMETraceCollector.cxx +++ b/src/SALOMETraceCollector/SALOMETraceCollector.cxx @@ -65,10 +65,12 @@ BaseTraceCollector* SALOMETraceCollector::instance() char ** argv = &_argv; _orb = CORBA::ORB_init (argc, argv); + sem_init(&_sem,0,0); // to wait until run thread is initialized pthread_t traceThread; int bid; int re2 = pthread_create(&traceThread, NULL, SALOMETraceCollector::run, (void *)bid); + sem_wait(&_sem); } ret = pthread_mutex_unlock(&_singletonMutex); // release lock } @@ -88,90 +90,72 @@ BaseTraceCollector* SALOMETraceCollector::instance() void* SALOMETraceCollector::run(void *bid) { - int isOKtoRun = 0; - int ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone - if (! _threadId) // only one run - { - isOKtoRun = 1; - if(_threadId == 0) - { - _threadId = new pthread_t; - } - - *_threadId = pthread_self(); - } - else cerr << "-- SALOMETraceCollector::run-serious design problem..." <putMessage(LogMsg); + DEVTRACE("Logger server found"); + } - obj = TraceCollector_WaitForServerReadiness(_orb,"Logger"); - if (!CORBA::is_nil(obj)) - m_pInterfaceLogger = SALOME_Logger::Logger::_narrow(obj); - if (CORBA::is_nil(m_pInterfaceLogger)) - { - cerr << "Logger server not found ! Abort" << endl; - cerr << flush ; - exit(1); - } - else - { - CORBA::String_var LogMsg = - CORBA::string_dup("\n---Init logger trace---\n"); - m_pInterfaceLogger->putMessage(LogMsg); - DEVTRACE("Logger server found"); - } + // --- Loop until there is no more buffer to print, + // and no ask for end from destructor. - // --- Loop until there is no more buffer to print, - // and no ask for end from destructor. + while ((!_threadToClose) || myTraceBuffer->toCollect() ) + { + if (_threadToClose) + DEVTRACE("SALOMETraceCollector _threadToClose"); - while ((!_threadToClose) || myTraceBuffer->toCollect() ) + int fullBuf = myTraceBuffer->retrieve(myTrace); + if (!CORBA::is_nil(_orb)) { - int fullBuf = myTraceBuffer->retrieve(myTrace); - if (!CORBA::is_nil(_orb)) + if (myTrace.traceType == ABORT_MESS) { - if (myTrace.traceType == ABORT_MESS) - { - stringstream abortMessage(""); + stringstream abortMessage(""); #ifndef WNT - abortMessage << "INTERRUPTION from thread " - << myTrace.threadId << " : " << myTrace.trace; + abortMessage << "INTERRUPTION from thread " + << myTrace.threadId << " : " << myTrace.trace; #else - abortMessage << "INTERRUPTION from thread " - << (void*)&myTrace.threadId - << " : " << myTrace.trace; + abortMessage << "INTERRUPTION from thread " + << (void*)&myTrace.threadId + << " : " << myTrace.trace; #endif - CORBA::String_var LogMsg = - CORBA::string_dup(abortMessage.str().c_str()); - m_pInterfaceLogger->putMessage(LogMsg); - exit(1); - } - else - { - stringstream aMessage(""); + CORBA::String_var LogMsg = + CORBA::string_dup(abortMessage.str().c_str()); + m_pInterfaceLogger->putMessage(LogMsg); + exit(1); + } + else + { + stringstream aMessage(""); #ifndef WNT - aMessage << "th. " << myTrace.threadId + aMessage << "th. " << myTrace.threadId #else - aMessage << "th. " << (void*)&myTrace.threadId + aMessage << "th. " << (void*)&myTrace.threadId #endif - << " " << myTrace.trace; - CORBA::String_var LogMsg = - CORBA::string_dup(aMessage.str().c_str()); - m_pInterfaceLogger->putMessage(LogMsg); - } + << " " << myTrace.trace; + CORBA::String_var LogMsg = + CORBA::string_dup(aMessage.str().c_str()); + m_pInterfaceLogger->putMessage(LogMsg); } } }