Salome HOME
PR: merge from branch BR_auto_V310 tag mergefrom_OCC_development_for_3_2_0a2_10mar06
[modules/yacs.git] / src / SALOMELocalTrace / LocalTraceBufferPool.cxx
index d875fe5bee8572ed42f6758b866ac8de2da65814..a6ffdb3c038fadae280734dffb10562e3076276a 100644 (file)
@@ -33,6 +33,7 @@
 #else
 #endif
 
+//#define _DEVDEBUG_
 #include "LocalTraceBufferPool.hxx"
 #include "BaseTraceCollector.hxx"
 #include "LocalTraceCollector.hxx"
@@ -50,12 +51,12 @@ using namespace std;
 // Class static attributes initialisation
 
 LocalTraceBufferPool* LocalTraceBufferPool::_singleton = 0;
-#ifndef WNT
-pthread_mutex_t LocalTraceBufferPool::_singletonMutex;
-#else
+//#ifndef WNT
+//pthread_mutex_t LocalTraceBufferPool::_singletonMutex;
+//#else
 pthread_mutex_t LocalTraceBufferPool::_singletonMutex =
   PTHREAD_MUTEX_INITIALIZER;
-#endif
+//#endif
 BaseTraceCollector *LocalTraceBufferPool::_myThreadTrace = 0;
 
 // ============================================================================
@@ -82,10 +83,12 @@ LocalTraceBufferPool* LocalTraceBufferPool::instance()
       ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
       if (_singleton == 0)                     // another thread may have got
        {                                      // the lock after the first test
-         _singleton = new LocalTraceBufferPool(); 
+         DEVTRACE("New buffer pool");
+         LocalTraceBufferPool* myInstance = new LocalTraceBufferPool(); 
 
          DESTRUCTOR_OF<LocalTraceBufferPool> *ptrDestroy =
-           new DESTRUCTOR_OF<LocalTraceBufferPool> (*_singleton);
+           new DESTRUCTOR_OF<LocalTraceBufferPool> (*myInstance);
+         _singleton = myInstance;
 
          // --- start a trace Collector
 
@@ -140,6 +143,7 @@ LocalTraceBufferPool* LocalTraceBufferPool::instance()
                  exit(1);        // in case assert is deactivated
                }             
            }
+         DEVTRACE("New buffer pool: end");
        }
       ret = pthread_mutex_unlock(&_singletonMutex); // release lock
     }
@@ -165,7 +169,12 @@ int LocalTraceBufferPool::insert(int traceType, const char* msg)
 
   // wait until there is a free buffer in the pool
 
-  int ret = sem_wait(&_freeBufferSemaphore);
+  int ret = -1;
+  while (ret)
+    {
+      ret = sem_wait(&_freeBufferSemaphore);
+      if (ret) perror(" LocalTraceBufferPool::insert, sem_wait");
+    }
 
   // get the next free buffer available (mutex protected) 
 
@@ -205,7 +214,12 @@ int LocalTraceBufferPool::retrieve(LocalTrace_TraceInfo& aTrace)
 
   // wait until there is a buffer in the pool, with a message to print
 
-  int ret = sem_wait(&_fullBufferSemaphore);
+  int ret = -1;
+  while (ret)
+    {
+      ret = sem_wait(&_fullBufferSemaphore);
+      if (ret) perror(" LocalTraceBufferPool::retrieve, sem_wait");
+    }
 
   // get the next buffer to print
 
@@ -294,8 +308,8 @@ LocalTraceBufferPool::~LocalTraceBufferPool()
       ret=pthread_mutex_destroy(&_incrementMutex);
       DEVTRACE("LocalTraceBufferPool::~LocalTraceBufferPool()-end");
       _singleton = 0;
-      ret = pthread_mutex_unlock(&_singletonMutex); // release lock
     }
+  ret = pthread_mutex_unlock(&_singletonMutex); // release lock
 }
 
 // ============================================================================
@@ -308,8 +322,8 @@ unsigned long LocalTraceBufferPool::lockedIncrement(unsigned long& pos)
 {
   int ret;
   ret = pthread_mutex_lock(&_incrementMutex);   // lock access to counters
-  pos++;
+  unsigned long mypos = ++pos;
   ret = pthread_mutex_unlock(&_incrementMutex); // release lock
-  return pos;
+  return mypos;
 }