activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject svn commit: r559051 - in /activemq/activemq-cpp/trunk/src/decaf/src/main/decaf: lang/Thread.cpp lang/Thread.h util/concurrent/Mutex.h
Date Tue, 24 Jul 2007 13:51:27 GMT
Author: tabish
Date: Tue Jul 24 06:51:25 2007
New Revision: 559051

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

APRified Thread Class, and mutex cleanups

Modified:
    activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Thread.cpp
    activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Thread.h
    activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/concurrent/Mutex.h

Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Thread.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Thread.cpp?view=diff&rev=559051&r1=559050&r2=559051
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Thread.cpp (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Thread.cpp Tue Jul 24 06:51:25
2007
@@ -17,11 +17,8 @@
 
 #include "Thread.h"
 
-#ifdef HAVE_PTHREAD_H
-    #include <errno.h>
-#else
-    #include <process.h> // _endthreadex
-#endif
+#include <apr_time.h>
+#include <apr_portable.h>
 
 #include <decaf/lang/Exception.h>
 #include <decaf/lang/exceptions/RuntimeException.h>
@@ -31,60 +28,53 @@
 using namespace decaf::lang::exceptions;
 
 ////////////////////////////////////////////////////////////////////////////////
-Thread::Thread()
-{
-    task = this;
-    started = false;
-    joined = false;
+Thread::Thread() {
+    this->task = this;
+    this->started = false;
+    this->joined = false;
+    this->pool = NULL;
+    this->threadHandle = NULL;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-Thread::Thread( Runnable* task )
-{
+Thread::Thread( Runnable* task ) {
     this->task = task;
-    started = false;
-    joined = false;
+    this->started = false;
+    this->joined = false;
+    this->pool = NULL;
+    this->threadHandle = NULL;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-Thread::~Thread()
-{
+Thread::~Thread(){
+    if( pool != NULL ) {
+        apr_pool_destroy( pool );
+    }
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 void Thread::start() throw ( Exception )
 {
-    if (this->started) {
-        throw Exception( __FILE__, __LINE__,
-            "Thread already started");
+    if( this->started ) {
+        throw Exception(
+            __FILE__, __LINE__,
+            "Thread::start - Thread already started");
     }
 
-#ifdef HAVE_PTHREAD_H
-
-    ::pthread_attr_init (&attributes);
-    ::pthread_attr_setdetachstate (&attributes, PTHREAD_CREATE_JOINABLE);
-    int err = ::pthread_create (
+    apr_pool_create( &pool, NULL );
+    apr_status_t err = apr_thread_create(
         &this->threadHandle,
-        &attributes,
+        NULL,
         runCallback,
-        this);
-    if (err != 0) {
-        throw Exception( __FILE__, __LINE__,
-            "Coud not start thread");
-    }
-
-#else
+        this,
+        pool );
 
-    unsigned int threadId = 0;
-    this->threadHandle =
-        (HANDLE)::_beginthreadex(NULL, 0, runCallback, this, 0, &threadId);
-    if (this->threadHandle == NULL) {
-        throw Exception( __FILE__, __LINE__,
-            "Coud not start thread");
+    if( err != APR_SUCCESS ) {
+        throw Exception(
+            __FILE__, __LINE__,
+            "Thread::start - Coud not start thread");
     }
 
-#endif
-
     // Mark the thread as started.
     started = true;
 }
@@ -92,58 +82,34 @@
 ////////////////////////////////////////////////////////////////////////////////
 void Thread::join() throw( Exception )
 {
-    if (!this->started) {
+    if( !this->started ) {
         throw Exception( __FILE__, __LINE__,
             "Thread::join() called without having called Thread::start()");
     }
-    if (!this->joined) {
-
-#ifdef HAVE_PTHREAD_H
-        ::pthread_join(this->threadHandle, NULL);
-#else
-        ::WaitForSingleObject (this->threadHandle, INFINITE);
-#endif
 
+    if( !this->joined ) {
+        apr_status_t threadReturn;
+        if( apr_thread_join( &threadReturn, this->threadHandle ) != APR_SUCCESS )
{
+            throw Exception( __FILE__, __LINE__,
+                "Thread::join() - Failed to Join the Thread");
+        }
     }
     this->joined = true;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-void Thread::sleep( int millisecs )
-{
-#ifdef HAVE_PTHREAD_H
-    struct timespec rec, rem;
-    rec.tv_sec = millisecs / 1000;
-    rec.tv_nsec = (millisecs % 1000) * 1000000;
-    while( nanosleep( &rec, &rem ) == -1 ){
-        if( errno != EINTR ){
-            break;
-        }
-    }
-
-#else
-    ::Sleep (millisecs);
-#endif
+void Thread::sleep( int millisecs ) {
+    apr_sleep( (apr_interval_time_t)(millisecs * 1000) );
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-unsigned long Thread::getId(void)
-{
-   #ifdef HAVE_PTHREAD_H
-      return (long)(pthread_self());
-   #else
-      return GetCurrentThreadId();
-   #endif
+unsigned long Thread::getId() {
+    return (long)( apr_os_thread_current() );
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-#ifdef HAVE_PTHREAD_H
-    void*
-#else
-    unsigned int WINAPI
-#endif
-Thread::runCallback( void* param )
-{
+void* APR_THREAD_FUNC Thread::runCallback( apr_thread_t* self, void* param ) {
+
     // Get the instance.
     Thread* thread = (Thread*)param;
 
@@ -151,22 +117,13 @@
     try{
         thread->task->run();
     } catch( ... ){
-        RuntimeException ex(__FILE__, __LINE__, "unhandled exception bubbled up to Thread::run");
+        RuntimeException ex(
+            __FILE__, __LINE__,
+            "unhandled exception bubbled up to Thread::run");
         ex.printStackTrace();
     }
 
-#ifdef HAVE_PTHREAD_H
-    ::pthread_attr_destroy( &thread->attributes );
+    // Indicate we are done.
+    apr_thread_exit( self, APR_SUCCESS );
     return NULL;
-#else
-
-    // Needed when using threads and CRT in Windows. Otherwise memleak can appear.
-    ::_endthreadex(0);
-
-    // _endthreadex (unlike _endthread) does not automatically close the thread handle
-    // so we need to do this manually.
-    ::CloseHandle(thread->threadHandle);
-
-    return 0;
-#endif
 }

Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Thread.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Thread.h?view=diff&rev=559051&r1=559050&r2=559051
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Thread.h (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Thread.h Tue Jul 24 06:51:25
2007
@@ -23,11 +23,8 @@
 #include <stdexcept>
 #include <assert.h>
 
-#ifdef HAVE_PTHREAD_H
-    #include <pthread.h>
-#else
-    #include <windows.h>
-#endif
+#include <apr_pools.h>
+#include <apr_thread_proc.h>
 
 namespace decaf{
 namespace lang{
@@ -47,12 +44,15 @@
          */
         Runnable* task;
 
-        #ifdef HAVE_PTHREAD_H
-            pthread_attr_t attributes;
-            pthread_t threadHandle;
-        #else
-            HANDLE threadHandle;
-        #endif
+        /**
+         * APR Pool to allocate thread from
+         */
+        apr_pool_t* pool;
+
+        /**
+         * APR Thread Handle
+         */
+        apr_thread_t* threadHandle;
 
         /**
          * Started state of this thread.
@@ -116,16 +116,12 @@
          * Obtains the Thread Id of the current thread
          * @return Thread Id
          */
-        static unsigned long getId(void);
+        static unsigned long getId();
 
     private:
 
         // Internal thread handling
-        #ifdef HAVE_PTHREAD_H
-            static void* runCallback (void* param);
-        #else
-            static unsigned int WINAPI runCallback (void* param);
-        #endif
+        static void* APR_THREAD_FUNC runCallback( apr_thread_t* self, void* param );
 
     };
 

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=559051&r1=559050&r2=559051
==============================================================================
--- 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 Tue Jul 24
06:51:25 2007
@@ -18,21 +18,16 @@
 #ifndef _DECAF_CONCURRENT_MUTEX_H_
 #define _DECAF_CONCURRENT_MUTEX_H_
 
-// Includes.
 #include <decaf/util/concurrent/Synchronizable.h>
 #include <decaf/util/concurrent/Concurrent.h>
 #include <decaf/lang/Thread.h>
 #include <decaf/util/Config.h>
-#include <list>
 
 #include <apr_pools.h>
 #include <apr_thread_mutex.h>
 #include <apr_thread_cond.h>
 
-#ifdef HAVE_SYS_TIME_H
-    #include <sys/time.h>
-#endif
-
+#include <list>
 #include <assert.h>
 
 namespace decaf{



Mime
View raw message