]> SALOME platform Git repositories - modules/kernel.git/commitdiff
Salome HOME
PR: design simplification
authorprascle <prascle>
Wed, 5 Oct 2005 20:23:08 +0000 (20:23 +0000)
committerprascle <prascle>
Wed, 5 Oct 2005 20:23:08 +0000 (20:23 +0000)
src/SALOMELocalTrace/BaseTraceCollector.cxx
src/SALOMELocalTrace/BaseTraceCollector.hxx
src/SALOMELocalTrace/FileTraceCollector.cxx
src/SALOMELocalTrace/LocalTraceCollector.cxx
src/SALOMETraceCollector/SALOMETraceCollector.cxx

index 942f07307088dae61766d5319199721fc5f57606..de612189795aa8d5146864424325996766a9b9ca 100644 (file)
@@ -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
 
index c05b2fe2b611e296e100c2ae2169b5d8b0c880d0..dad4d9e0bcc74b3ff49cb0931c3f03a8f6b4ce19 100644 (file)
@@ -28,6 +28,7 @@
 #define _BASETRACECOLLECTOR_HXX_
 
 #include <pthread.h>
+#include <semaphore.h>
 
 
 #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
index 5dd1074be9b549fb874427ed8a33cfd66f9f0296..5bceaeaab4eff8c1151778938149902a4f406e75 100644 (file)
@@ -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..." <<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);
 }
 
 // ============================================================================
index e19e2252afb0615eea1bb76b933b03512c36aad4..6386998c55d3cc0597982c613c0cf12086691ba1 100644 (file)
@@ -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..." <<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);
@@ -155,7 +136,7 @@ LocalTraceCollector:: ~LocalTraceCollector()
   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
@@ -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;
        }
index de0331d96a1036bc4e2428b3f6238189707c4711..fbb0e322a17379eef58b8037e909dbebe8d6d7c9 100644 (file)
@@ -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..." <<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);
            }
        }
     }