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
#define _BASETRACECOLLECTOR_HXX_
#include <pthread.h>
+#include <semaphore.h>
#if defined SALOMELOCALTRACE_EXPORTS
static BaseTraceCollector* _singleton;
static pthread_mutex_t _singletonMutex;
static pthread_t* _threadId;
+ static sem_t _sem;
};
#endif
{ // 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
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..." <<endl;
-
- ret = pthread_mutex_unlock(&_singletonMutex); // release lock
-
- if (isOKtoRun)
- {
- if (_threadId == 0)
- {
- cerr << "FileTraceCollector::run error!" << endl << flush;
- exit(1);
- }
+ LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
+ LocalTrace_TraceInfo myTrace;
- LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
- LocalTrace_TraceInfo myTrace;
+ // --- opens a file with append mode
+ // so, several processes can share the same file
- // --- opens a file with append mode
- // so, several processes can share the same file
+ ofstream traceFile;
+ const char *theFileName = _fileName.c_str();
+ traceFile.open(theFileName, ios::out | ios::app);
+ if (!traceFile)
+ {
+ cerr << "impossible to open trace file "<< theFileName << endl;
+ exit (1);
+ }
- ofstream traceFile;
- const char *theFileName = _fileName.c_str();
- traceFile.open(theFileName, ios::out | ios::app);
- if (!traceFile)
- {
- cerr << "impossible to open trace file "<< theFileName << endl;
- exit (1);
- }
+ // --- 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("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);
}
// ============================================================================
{ // 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
}
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..." <<endl;
+ LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
+ LocalTrace_TraceInfo myTrace;
- ret = pthread_mutex_unlock(&_singletonMutex); // release lock
-
- if (isOKtoRun)
- {
- if(_threadId == 0)
- {
- cerr << "LocalTraceCollector::run error!" << endl << flush;
- exit(1);
- }
+ // --- Loop until there is no more buffer to print,
+ // and no ask for end from destructor.
- LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
- LocalTrace_TraceInfo myTrace;
-
- // --- Loop until there is no more buffer to print,
- // and no ask for end from destructor.
+ while ((!_threadToClose) || myTraceBuffer->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);
ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
if (_singleton)
{
- //cerr << "LocalTraceCollector:: ~LocalTraceCollector()" << endl <<flush;
+ DEVTRACE("LocalTraceCollector:: ~LocalTraceCollector()");
LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
_threadToClose = 1;
myTraceBuffer->insert(NORMAL_MESS,"end of trace\n"); // to wake up thread
{
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;
}
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
}
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..." <<endl;
+ _threadId = new pthread_t;
+ *_threadId = pthread_self();
+ sem_post(&_sem); // unlock instance
- ret = pthread_mutex_unlock(&_singletonMutex); // release lock
+ LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
+ LocalTrace_TraceInfo myTrace;
- if (isOKtoRun)
- {
- if(_threadId == 0)
- {
- cerr << "SALOMETraceCollector::run error!" << endl << flush;
- exit(1);
- }
-
- LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
- LocalTrace_TraceInfo myTrace;
+ SALOME_Logger::Logger_var m_pInterfaceLogger;
+ CORBA::Object_var obj;
- SALOME_Logger::Logger_var m_pInterfaceLogger;
- CORBA::Object_var obj;
+ 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");
+ }
- 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);
}
}
}