Return-Path: Delivered-To: apmail-activemq-commits-archive@www.apache.org Received: (qmail 98409 invoked from network); 24 Jul 2007 00:12:28 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 24 Jul 2007 00:12:28 -0000 Received: (qmail 6465 invoked by uid 500); 24 Jul 2007 00:12:29 -0000 Delivered-To: apmail-activemq-commits-archive@activemq.apache.org Received: (qmail 6413 invoked by uid 500); 24 Jul 2007 00:12:29 -0000 Mailing-List: contact commits-help@activemq.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@activemq.apache.org Delivered-To: mailing list commits@activemq.apache.org Received: (qmail 6404 invoked by uid 99); 24 Jul 2007 00:12:29 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 23 Jul 2007 17:12:29 -0700 X-ASF-Spam-Status: No, hits=-99.5 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 23 Jul 2007 17:12:27 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id 2F5141A981D; Mon, 23 Jul 2007 17:12:07 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r558901 - in /activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/concurrent: Mutex.cpp Mutex.h Date: Tue, 24 Jul 2007 00:12:06 -0000 To: commits@activemq.apache.org From: tabish@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20070724001207.2F5141A981D@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: tabish Date: Mon Jul 23 17:12:05 2007 New Revision: 558901 URL: http://svn.apache.org/viewvc?view=rev&rev=558901 Log: http://issues.apache.org/activemq/browse/AMQCPP-103 Using APR in Mutex class, not done yet. Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/concurrent/Mutex.cpp activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/concurrent/Mutex.h Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/concurrent/Mutex.cpp URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/concurrent/Mutex.cpp?view=diff&rev=558901&r1=558900&r2=558901 ============================================================================== --- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/concurrent/Mutex.cpp (original) +++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/concurrent/Mutex.cpp Mon Jul 23 17:12:05 2007 @@ -17,62 +17,49 @@ #include +#include +#include + using namespace decaf; using namespace decaf::util; using namespace decaf::util::concurrent; //////////////////////////////////////////////////////////////////////////////// -Mutex::Mutex() -{ -#ifdef HAVE_PTHREAD_H - pthread_mutexattr_t attr; - pthread_mutexattr_init( &attr ); - pthread_mutex_init( &mutex, &attr ); - pthread_mutexattr_destroy( &attr ); -#else - InitializeCriticalSection( &mutex ); -#endif +Mutex::Mutex() { - lock_owner = 0; - lock_count = 0; + apr_pool_create( &pool, NULL ); + apr_thread_mutex_create( &mutex, APR_THREAD_MUTEX_NESTED, pool ); + this->lock_owner = 0; + this->lock_count = 0; } //////////////////////////////////////////////////////////////////////////////// -Mutex::~Mutex() -{ +Mutex::~Mutex() { + // Unlock the mutex. unlock(); -#ifdef HAVE_PTHREAD_H - pthread_mutex_destroy( &mutex ); -#else - DeleteCriticalSection( &mutex ); -#endif + apr_thread_mutex_destroy( mutex ); + apr_pool_destroy( pool ); } //////////////////////////////////////////////////////////////////////////////// -void Mutex::lock() throw( lang::Exception ) -{ +void Mutex::lock() throw( lang::Exception ) { + unsigned long threadId = lang::Thread::getId(); if( threadId == lock_owner ) { lock_count++; } else { - -#ifdef HAVE_PTHREAD_H - pthread_mutex_lock( &mutex ); -#else - EnterCriticalSection( &mutex ); -#endif - + apr_thread_mutex_lock( mutex ); lock_owner = threadId; lock_count = 1; } } //////////////////////////////////////////////////////////////////////////////// -void Mutex::unlock() throw( lang::Exception ) -{ +void Mutex::unlock() throw( lang::Exception ) { + if( lock_owner == 0 ) { return; } @@ -87,12 +74,7 @@ if(lock_count == 0) { lock_owner = 0; - -#ifdef HAVE_PTHREAD_H - pthread_mutex_unlock( &mutex ); -#else - LeaveCriticalSection( &mutex ); -#endif + apr_thread_mutex_unlock( mutex ); } } @@ -113,90 +95,37 @@ "Mutex::wait - Failed, not Lock Owner!"); } - // 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 - // state we were in before. + // Save the current owner 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 state we were in before. unsigned long lock_owner = this->lock_owner; - 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 ); + apr_thread_cond_t* waitEvent = NULL; + apr_thread_cond_create( &waitEvent, pool ); // 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 ) { - timeval now = {0,0}; - 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 ) { - wait.tv_sec++; - wait.tv_nsec -= 1000000000; - } - - returnValue = pthread_cond_timedwait( &waitEvent, &mutex, &wait ); + apr_interval_time_t wait = millisecs * 1000; + apr_thread_cond_timedwait( waitEvent, mutex, wait ); } else { - returnValue = pthread_cond_wait( &waitEvent, &mutex ); + apr_thread_cond_wait( waitEvent, mutex ); } // Be Sure that the event is now removed - eventQ.remove( &waitEvent ); + eventQ.remove( waitEvent ); // Destroy our wait event now, the notify method will have removed it // from the event queue. - 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 lang::Exception( - __FILE__, __LINE__, - "Mutex::wait - Failed Creating Event." ); - } - - eventQ.push_back( waitEvent ); - - // Release the Lock - LeaveCriticalSection( &mutex ); - - // Wait for a signal - if( WaitForSingleObject( waitEvent, millisecs ) != WAIT_OBJECT_0 && millisecs == WAIT_INFINITE ) { - throw lang::Exception( - __FILE__, __LINE__, - "Mutex::wait - Infinite wait aborted for unknown reason." ); - } - - // 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) + apr_thread_cond_destroy( waitEvent ); // restore the owner this->lock_owner = lock_owner; - this->lock_count = lock_count; } //////////////////////////////////////////////////////////////////////////////// @@ -209,15 +138,9 @@ } if( !eventQ.empty() ) { -#ifdef HAVE_PTHREAD_H - pthread_cond_t* event = eventQ.front(); - eventQ.remove( event ); - pthread_cond_signal( event ); -#else - HANDLE event = eventQ.front(); + apr_thread_cond_t* event = eventQ.front(); eventQ.remove( event ); - SetEvent( event ); -#endif + apr_thread_cond_signal( event ); } } @@ -230,16 +153,9 @@ "Mutex::NotifyAll - Failed, not Lock Owner!" ); } - 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(); + apr_thread_cond_t* event = eventQ.front(); eventQ.remove( event ); - SetEvent( event ); -#endif + apr_thread_cond_signal( event ); } } Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/concurrent/Mutex.h URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/concurrent/Mutex.h?view=diff&rev=558901&r1=558900&r2=558901 ============================================================================== --- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/concurrent/Mutex.h (original) +++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/concurrent/Mutex.h Mon Jul 23 17:12:05 2007 @@ -25,23 +25,14 @@ #include #include +#include +#include +#include + #ifdef HAVE_SYS_TIME_H #include #endif -#ifdef HAVE_PTHREAD_H - #include -#else - #include - - #if ( !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0400) - #if ( !defined(WINVER) || WINVER < 0x0400) - #pragma message ("Unsupported platform, Windows NT 4.0 or later required") - #endif - #endif - -#endif - #include namespace decaf{ @@ -54,26 +45,21 @@ * and will be successful. * @see pthread_mutex_t */ - class DECAF_API Mutex : public Synchronizable - { - private: // Data - - /** - * The mutex object. - */ - #ifdef HAVE_PTHREAD_H - pthread_mutex_t mutex; - - std::list eventQ; - #else - CRITICAL_SECTION mutex; + class DECAF_API Mutex : public Synchronizable { + private: + + // APR Pool Allocator + apr_pool_t* pool; + + // The mutex object. + apr_thread_mutex_t* mutex; - std::list eventQ; - #endif + // List of waiting threads + std::list eventQ; // Lock Status Members - volatile int lock_count; volatile unsigned long lock_owner; + volatile unsigned long lock_count; public: