activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject svn commit: r553973 - in /activemq/activemq-cpp/trunk/src/main/activemq/concurrent: Mutex.cpp Mutex.h
Date Fri, 06 Jul 2007 17:56:15 GMT
Author: tabish
Date: Fri Jul  6 10:56:14 2007
New Revision: 553973

URL: http://svn.apache.org/viewvc?view=rev&rev=553973
Log:
http://issues.apache.org/activemq/browse/AMQCPP-130

Attempting to fix mutex issues on windows.

Modified:
    activemq/activemq-cpp/trunk/src/main/activemq/concurrent/Mutex.cpp
    activemq/activemq-cpp/trunk/src/main/activemq/concurrent/Mutex.h

Modified: activemq/activemq-cpp/trunk/src/main/activemq/concurrent/Mutex.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/concurrent/Mutex.cpp?view=diff&rev=553973&r1=553972&r2=553973
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/activemq/concurrent/Mutex.cpp (original)
+++ activemq/activemq-cpp/trunk/src/main/activemq/concurrent/Mutex.cpp Fri Jul  6 10:56:14
2007
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
- 
+
 #include <activemq/concurrent/Mutex.h>
 
 using namespace activemq;
@@ -25,13 +25,13 @@
 {
 #ifdef HAVE_PTHREAD_H
     pthread_mutexattr_t attr;
-    pthread_mutexattr_init(&attr);
-    pthread_mutex_init(&mutex, &attr);
-    pthread_mutexattr_destroy(&attr);
+    pthread_mutexattr_init( &attr );
+    pthread_mutex_init( &mutex, &attr );
+    pthread_mutexattr_destroy( &attr );
 #else
-    InitializeCriticalSection(&mutex);            
+    InitializeCriticalSection( &mutex );
 #endif
-  
+
     lock_owner = 0;
     lock_count = 0;
 }
@@ -41,65 +41,60 @@
 {
     // Unlock the mutex.
     unlock();
-  
+
 #ifdef HAVE_PTHREAD_H
-    pthread_mutex_destroy(&mutex);
+    pthread_mutex_destroy( &mutex );
 #else
-    DeleteCriticalSection(&mutex);
+    DeleteCriticalSection( &mutex );
 #endif
 }
-  
+
 ////////////////////////////////////////////////////////////////////////////////
 void Mutex::lock() throw( exceptions::ActiveMQException )
 {
-	int threadId = Thread::getId();
-	
-    if(threadId == lock_owner)
-    {
+    unsigned long threadId = Thread::getId();
+
+    if( threadId == lock_owner ) {
         lock_count++;
-    }
-    else
-    {
-#ifdef HAVE_PTHREAD_H               
-        pthread_mutex_lock(&mutex);
+    } else {
+
+#ifdef HAVE_PTHREAD_H
+        pthread_mutex_lock( &mutex );
 #else
-        EnterCriticalSection(&mutex);
+        EnterCriticalSection( &mutex );
 #endif
 
         lock_owner = threadId;
-        lock_count = 1;        
-    }         
+        lock_count = 1;
+    }
 }
-  
+
 ////////////////////////////////////////////////////////////////////////////////
 void Mutex::unlock() throw( exceptions::ActiveMQException )
 {
-    if(lock_owner == 0)
-    {
+    if( lock_owner == 0 ) {
         return;
     }
- 
-    if(!isLockOwner())
-    {
-        throw exceptions::ActiveMQException( 
+
+    if( !isLockOwner() ) {
+        throw exceptions::ActiveMQException(
             __FILE__, __LINE__,
             "Mutex::unlock - Failed, not Lock Owner!" );
     }
-     
+
     lock_count--;
- 
-    if(lock_count == 0)
-    {         
+
+    if(lock_count == 0) {
         lock_owner = 0;
 
 #ifdef HAVE_PTHREAD_H
-        pthread_mutex_unlock(&mutex);
+        pthread_mutex_unlock( &mutex );
 #else
-        LeaveCriticalSection(&mutex);
-#endif            
+        LeaveCriticalSection( &mutex );
+#endif
     }
 }
-  
+
 ////////////////////////////////////////////////////////////////////////////////
 void Mutex::wait() throw( exceptions::ActiveMQException )
 {
@@ -108,83 +103,68 @@
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-void Mutex::wait( unsigned long millisecs ) 
+void Mutex::wait( unsigned long millisecs )
     throw( exceptions::ActiveMQException )
 {
-    if(!isLockOwner())
-    {
-        throw exceptions::ActiveMQException( 
+    if( !isLockOwner() ) {
+        throw exceptions::ActiveMQException(
             __FILE__, __LINE__,
             "Mutex::wait - Failed, not Lock Owner!");
     }
 
-    // Save the current owner and Lock count as we are going to 
+    // Save the current owner and Lock count as we are going to
     // unlock and release for someone else to lock on potentially.
-    // When we come back and re-lock we want to restore to the 
+    // When we come back and re-lock we want to restore to the
     // state we were in before.
     unsigned long lock_owner = this->lock_owner;
-    int           lock_count = this->lock_count;
+    int lock_count = this->lock_count;
 
     this->lock_count = 0;
     this->lock_owner = 0;
- 
+
 #ifdef HAVE_PTHREAD_H
 
     // Create this threads wait event
     pthread_cond_t waitEvent;
-    pthread_cond_init(&waitEvent, NULL);
+    pthread_cond_init( &waitEvent, NULL );
 
     // Store the event in the queue so that a notify can
     // call it and wake up the thread.
-    eventQ.push_back(&waitEvent);
+    eventQ.push_back( &waitEvent );
 
     int returnValue = 0;
-    if(millisecs != WAIT_INFINITE)
-    {
+    if( millisecs != WAIT_INFINITE ) {
         timeval now = {0,0};
-        gettimeofday(&now, NULL);
-   
+        gettimeofday( &now, NULL );
+
         timespec wait = {0,0};
         wait.tv_sec = now.tv_sec + (millisecs / 1000);
         wait.tv_nsec = (now.tv_usec * 1000) + ((millisecs % 1000) * 1000000);
-   
-        if(wait.tv_nsec > 1000000000)
-        {
+
+        if( wait.tv_nsec > 1000000000 ) {
             wait.tv_sec++;
             wait.tv_nsec -= 1000000000;
         }
 
-        returnValue =  pthread_cond_timedwait(&waitEvent, &mutex, &wait);
+        returnValue = pthread_cond_timedwait( &waitEvent, &mutex, &wait );
+    } else {
+        returnValue = pthread_cond_wait( &waitEvent, &mutex );
     }
-    else
-    {
-        returnValue = pthread_cond_wait(&waitEvent, &mutex);
-    }
-
-    // If the wait did not succeed for any reason, remove it
-    // from the queue.
-    if( returnValue != 0 ){
-        std::list<pthread_cond_t*>::iterator iter = eventQ.begin();
-        for( ; iter != eventQ.end(); ++iter ){
-            if( *iter == &waitEvent ){
-                eventQ.erase(iter);
-                break;
-            }
-        }
-    }
-    
+
+    // Be Sure that the event is now removed
+    eventQ.remove( &waitEvent );
+
     // Destroy our wait event now, the notify method will have removed it
     // from the event queue.
-    pthread_cond_destroy(&waitEvent);
+    pthread_cond_destroy( &waitEvent );
 
 #else // !defined(HAVE_PTHREAD_H)
 
     // Create the event to wait on
     HANDLE waitEvent = CreateEvent( NULL, false, false, NULL );
 
-    if(waitEvent == NULL)
-    {
-        throw exceptions::ActiveMQException( 
+    if( waitEvent == NULL ) {
+        throw exceptions::ActiveMQException(
             __FILE__, __LINE__,
             "Mutex::Mutex - Failed Creating Event." );
     }
@@ -200,35 +180,38 @@
     // Reaquire the Lock
     EnterCriticalSection( &mutex );
 
+    // Remove the event no matter what
+    eventQ.remove( waitEvent );
+
     // Clean up the event, the notif methods will have
     // already poped it from the queue.
     CloseHandle( waitEvent );
-    
+
 #endif // !defined(HAVE_PTHREAD_H)
- 
+
     // restore the owner
     this->lock_owner = lock_owner;
     this->lock_count = lock_count;
 }
-  
+
 ////////////////////////////////////////////////////////////////////////////////
 void Mutex::notify() throw( exceptions::ActiveMQException )
 {
-    if( !isLockOwner() )
-    {
-        throw exceptions::ActiveMQException( 
+    if( !isLockOwner() ) {
+        throw exceptions::ActiveMQException(
             __FILE__, __LINE__,
             "Mutex::Notify - Failed, not Lock Owner!" );
     }
 
-    if( !eventQ.empty() )
-    {
+    if( !eventQ.empty() ) {
 #ifdef HAVE_PTHREAD_H
-        pthread_cond_signal( eventQ.front() );
-        eventQ.pop_front();
-#else
-        SetEvent( eventQ.front() );
-        eventQ.pop_front();
+        pthread_cond_t* event = eventQ.front();
+        eventQ.remove( event );
+        pthread_cond_signal( event );
+#else
+        HANDLE event = eventQ.front();
+        eventQ.remove( event );
+        SetEvent( event );
 #endif
     }
 }
@@ -236,29 +219,23 @@
 ////////////////////////////////////////////////////////////////////////////////
 void Mutex::notifyAll() throw( exceptions::ActiveMQException )
 {
-    if(!isLockOwner())
-    {
-        throw exceptions::ActiveMQException( 
+    if( !isLockOwner() ) {
+        throw exceptions::ActiveMQException(
             __FILE__, __LINE__,
             "Mutex::NotifyAll - Failed, not Lock Owner!" );
     }
- 
-#ifdef HAVE_PTHREAD_H
-
-    while(!eventQ.empty())
-    {
-         pthread_cond_signal( eventQ.front() );
-         eventQ.pop_front();
-    }
-
-#else
 
-    while(!eventQ.empty())
-    {
-         SetEvent( eventQ.front() );
-         eventQ.pop_front();
-    }
 
+    while( !eventQ.empty() ) {
+#ifdef HAVE_PTHREAD_H
+        pthread_cond_t* event = eventQ.front();
+        eventQ.remove( event );
+        pthread_cond_signal( event );
+#else
+        HANDLE event = eventQ.front();
+        eventQ.remove( event );
+        SetEvent( event );
 #endif
+    }
 }
 

Modified: activemq/activemq-cpp/trunk/src/main/activemq/concurrent/Mutex.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/concurrent/Mutex.h?view=diff&rev=553973&r1=553972&r2=553973
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/activemq/concurrent/Mutex.h (original)
+++ activemq/activemq-cpp/trunk/src/main/activemq/concurrent/Mutex.h Fri Jul  6 10:56:14 2007
@@ -33,7 +33,7 @@
     #include <pthread.h>
 #else
     #include <windows.h>
-   
+
     #if ( !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0400)
         #if ( !defined(WINVER) || WINVER < 0x0400)
             #pragma message ("Unsupported platform, Windows NT 4.0 or later required")
@@ -46,7 +46,7 @@
 
 namespace activemq{
 namespace concurrent{
-    
+
     /**
      * Creates a pthread_mutex_t object. The object is created
      * such that successive locks from the same thread is allowed
@@ -69,9 +69,9 @@
 
             std::list<HANDLE> eventQ;
         #endif
-      
+
         // Lock Status Members
-        volatile int           lock_count;
+        volatile int lock_count;
         volatile unsigned long lock_owner;
 
     public:
@@ -85,19 +85,19 @@
          * Destructor - destroys the mutex object.
          */
         virtual ~Mutex();
-      
+
         /**
          * Locks the object.
          * @throws ActiveMQException
          */
         virtual void lock() throw( exceptions::ActiveMQException );
-      
+
         /**
          * Unlocks the object.
          * @throws ActiveMQException
          */
         virtual void unlock() throw( exceptions::ActiveMQException );
-      
+
         /**
          * Waits on a signal from this object, which is generated
          * by a call to Notify.
@@ -113,9 +113,9 @@
          * @param millisecs the time in milliseconds to wait.
          * @throws ActiveMQException
          */
-        virtual void wait( unsigned long millisecs ) 
+        virtual void wait( unsigned long millisecs )
             throw( exceptions::ActiveMQException );
-      
+
         /**
          * Signals a waiter on this object that it can now wake
          * up and continue.
@@ -131,7 +131,7 @@
         virtual void notifyAll() throw( exceptions::ActiveMQException );
 
     private:
-   
+
         /**
          * Check if the calling thread is the Lock Owner
          * @retun true if the caller is the lock owner
@@ -139,7 +139,7 @@
         bool isLockOwner(){
             return lock_owner == Thread::getId();
         }
-      
+
     };
 
 }}



Mime
View raw message