Salome HOME
[EDF30062] : Forward of current directory mecanism
[modules/kernel.git] / src / SALOMELocalTrace / LocalTraceBufferPool.cxx
index 484d9391c19542db46a2d2d2f13979bbfdaf9191..afba1c4b9c80621404c99af7cedc3ad9b8c70de4 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -6,7 +6,7 @@
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 // License as published by the Free Software Foundation; either
-// version 2.1 of the License.
+// version 2.1 of the License, or (at your option) any later version.
 //
 // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -80,8 +80,7 @@ LocalTraceBufferPool* LocalTraceBufferPool::instance()
 {
   if (_singleton == 0) // no need of lock when singleton already exists
     {
-      int ret;
-      ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
+      pthread_mutex_lock(&_singletonMutex);    // acquire lock to be alone
       if (_singleton == 0)                     // another thread may have got
         {                                      // the lock after the first test
           DEVTRACE("New buffer pool");
@@ -113,12 +112,16 @@ LocalTraceBufferPool* LocalTraceBufferPool::instance()
 #ifndef WIN32
               void* handle;
               std::string impl_name = std::string ("lib") + traceKind 
+#ifdef __APPLE__
+                + std::string("TraceCollector.dylib");
+#else
                 + std::string("TraceCollector.so");
-              handle = dlopen( impl_name.c_str() , RTLD_LAZY ) ;
+#endif
+              handle = dlopen( impl_name.c_str() , RTLD_LAZY | RTLD_GLOBAL ) ;
 #else
               HINSTANCE handle;
-              std::string impl_name = std::string ("lib") + traceKind + std::string(".dll");
-              handle = LoadLibrary( impl_name.c_str() );
+              std::string impl_name = std::string ("lib") + traceKind + std::string(".dll");                                
+              handle = LoadLibraryA( impl_name.c_str() );
 #endif
               if ( handle )
                 {
@@ -149,7 +152,7 @@ LocalTraceBufferPool* LocalTraceBufferPool::instance()
             }
           DEVTRACE("New buffer pool: end");
         }
-      ret = pthread_mutex_unlock(&_singletonMutex); // release lock
+      pthread_mutex_unlock(&_singletonMutex); // release lock
     }
   return _singleton;
 }
@@ -173,13 +176,16 @@ int LocalTraceBufferPool::insert(int traceType, const char* msg)
 
   // wait until there is a free buffer in the pool
 
+#ifdef __APPLE__
+  dispatch_semaphore_wait(_freeBufferSemaphore, DISPATCH_TIME_FOREVER);
+#else
   int ret = -1;
   while (ret)
     {
       ret = sem_wait(&_freeBufferSemaphore);
       if (ret) perror(" LocalTraceBufferPool::insert, sem_wait");
     }
-
+#endif
   // get the next free buffer available (mutex protected) 
 
   unsigned long myInsertPos = lockedIncrement(_insertPos);
@@ -197,12 +203,20 @@ int LocalTraceBufferPool::insert(int traceType, const char* msg)
   // increment the full buffer semaphore
   // (if previously 0, awake thread in charge of trace)
 
+#ifdef __APPLE__
+  dispatch_semaphore_signal(_fullBufferSemaphore);
+#else
   ret = sem_post(&_fullBufferSemaphore);
+#endif
 
-  // returns the number of free buffers
 
+  // returns the number of free buffers
+#ifdef __APPLE__
+  return 0;
+#else
   sem_getvalue(&_freeBufferSemaphore, &ret);
   return ret;  
+#endif
 }
 
 // ============================================================================
@@ -218,13 +232,16 @@ int LocalTraceBufferPool::retrieve(LocalTrace_TraceInfo& aTrace)
 
   // wait until there is a buffer in the pool, with a message to print
 
+#ifdef __APPLE__
+    dispatch_semaphore_wait(_fullBufferSemaphore, DISPATCH_TIME_FOREVER);
+#else
   int ret = -1;
   while (ret)
     {
       ret = sem_wait(&_fullBufferSemaphore);
       if (ret) MESSAGE (" LocalTraceBufferPool::retrieve, sem_wait");
     }
-
+#endif
   // get the next buffer to print
 
   unsigned long myRetrievePos = lockedIncrement(_retrievePos);
@@ -241,12 +258,20 @@ int LocalTraceBufferPool::retrieve(LocalTrace_TraceInfo& aTrace)
   // threads are waiting to put a trace: the waken up thread is not
   // necessarily the first thread to wait.
 
+#ifdef __APPLE__
+  dispatch_semaphore_signal(_freeBufferSemaphore);
+#else
   ret = sem_post(&_freeBufferSemaphore);
+#endif
 
   // returns the number of full buffers
 
+#ifdef __APPLE__
+  return 0;
+#else
   sem_getvalue(&_fullBufferSemaphore, &ret);
   return ret;
+#endif
 }
 
 // ============================================================================
@@ -282,10 +307,16 @@ LocalTraceBufferPool::LocalTraceBufferPool()
   for (int i=0; i<TRACE_BUFFER_SIZE; i++)
     strcpy(&(_myBuffer[i].trace[MAXMESS_LENGTH]),TRUNCATED_MESSAGE);
   int ret;
+#ifdef __APPLE__
+  dispatch_semaphore_t* sem1 = &_freeBufferSemaphore, *sem2 = &_fullBufferSemaphore;
+  *sem1 = dispatch_semaphore_create(TRACE_BUFFER_SIZE);
+  *sem2 = dispatch_semaphore_create(0);
+#else
   ret=sem_init(&_freeBufferSemaphore, 0, TRACE_BUFFER_SIZE); // all buffer free
   if (ret!=0) IMMEDIATE_ABORT(ret);
   ret=sem_init(&_fullBufferSemaphore, 0, 0);                 // 0 buffer full
   if (ret!=0) IMMEDIATE_ABORT(ret);
+#endif
   ret=pthread_mutex_init(&_incrementMutex,NULL); // default = fast mutex
   if (ret!=0) IMMEDIATE_ABORT(ret);
 
@@ -300,20 +331,24 @@ LocalTraceBufferPool::LocalTraceBufferPool()
 
 LocalTraceBufferPool::~LocalTraceBufferPool()
 {
-  int ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
+  pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
   if (_singleton)
     {
       DEVTRACE("LocalTraceBufferPool::~LocalTraceBufferPool()");
       delete (_myThreadTrace);
       _myThreadTrace = 0;
-      int ret;
-      ret=sem_destroy(&_freeBufferSemaphore);
-      ret=sem_destroy(&_fullBufferSemaphore);
-      ret=pthread_mutex_destroy(&_incrementMutex);
+#ifdef __APPLE__
+      dispatch_release(_freeBufferSemaphore);
+      dispatch_release(_fullBufferSemaphore);
+#else
+      sem_destroy(&_freeBufferSemaphore);
+      sem_destroy(&_fullBufferSemaphore);
+#endif
+      pthread_mutex_destroy(&_incrementMutex);
       DEVTRACE("LocalTraceBufferPool::~LocalTraceBufferPool()-end");
       _singleton = 0;
     }
-  ret = pthread_mutex_unlock(&_singletonMutex); // release lock
+  pthread_mutex_unlock(&_singletonMutex); // release lock
 }
 
 // ============================================================================
@@ -324,10 +359,8 @@ LocalTraceBufferPool::~LocalTraceBufferPool()
 
 unsigned long LocalTraceBufferPool::lockedIncrement(unsigned long& pos)
 {
-  int ret;
-  ret = pthread_mutex_lock(&_incrementMutex);   // lock access to counters
+  pthread_mutex_lock(&_incrementMutex);   // lock access to counters
   unsigned long mypos = ++pos;
-  ret = pthread_mutex_unlock(&_incrementMutex); // release lock
+  pthread_mutex_unlock(&_incrementMutex); // release lock
   return mypos;
 }
-