activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject svn commit: r816370 [1/2] - in /activemq/activemq-cpp/trunk/activemq-cpp: ./ src/main/ src/main/decaf/internal/lang/unix/ src/main/decaf/internal/lang/windows/ src/main/decaf/internal/util/concurrent/ src/main/decaf/internal/util/concurrent/unix/ src/m...
Date Thu, 17 Sep 2009 20:21:27 GMT
Author: tabish
Date: Thu Sep 17 20:21:25 2009
New Revision: 816370

URL: http://svn.apache.org/viewvc?rev=816370&view=rev
Log:
Remove More APR code and implement our own Threading code, reduces the memory foot print of a mutex and improves performance of mutex lock, unlock, waits and notify significantly.  

Added:
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/lang/unix/ThreadHandle.h   (with props)
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/lang/windows/ThreadHandle.h   (with props)
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/ConditionImpl.h   (with props)
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/MutexImpl.h   (with props)
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/ConditionHandle.h   (with props)
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/ConditionImpl.cpp   (with props)
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/MutexHandle.h   (with props)
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/MutexImpl.cpp   (with props)
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/ConditionHandle.h   (with props)
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/ConditionImpl.cpp   (with props)
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/MutexHandle.h   (with props)
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/MutexImpl.cpp   (with props)
Modified:
    activemq/activemq-cpp/trunk/activemq-cpp/configure.ac
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/Makefile.am
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/lang/unix/ThreadImpl.cpp
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/lang/windows/ThreadImpl.cpp
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/Mutex.cpp
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/Mutex.h
    activemq/activemq-cpp/trunk/activemq-cpp/src/test/testRegistry.cpp

Modified: activemq/activemq-cpp/trunk/activemq-cpp/configure.ac
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/configure.ac?rev=816370&r1=816369&r2=816370&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/configure.ac (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/configure.ac Thu Sep 17 20:21:25 2009
@@ -112,6 +112,7 @@
 AC_CHECK_HEADERS([sys/wait.h])
 AC_CHECK_HEADERS([sys/resource.h])
 AC_CHECK_HEADERS([pthread.h])
+AC_CHECK_HEADERS([errno.h])
 
 AC_CHECK_FUNCS([ioctl select gettimeofday time ftime])
 

Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/Makefile.am
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/Makefile.am?rev=816370&r1=816369&r2=816370&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/Makefile.am (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/Makefile.am Thu Sep 17 20:21:25 2009
@@ -495,6 +495,8 @@
     decaf/internal/util/HexStringParser.cpp \
     decaf/internal/util/TimerTaskHeap.cpp \
     decaf/internal/util/concurrent/SynchronizableImpl.cpp \
+    decaf/internal/util/concurrent/unix/ConditionImpl.cpp \
+    decaf/internal/util/concurrent/unix/MutexImpl.cpp \
     decaf/io/BlockingByteArrayInputStream.cpp \
     decaf/io/BufferedInputStream.cpp \
     decaf/io/BufferedOutputStream.cpp \
@@ -1083,6 +1085,8 @@
     decaf/internal/io/StandardInputStream.h \
     decaf/internal/io/StandardOutputStream.h \
     decaf/internal/lang/ThreadImpl.h \
+    decaf/internal/lang/unix/ThreadHandle.h \
+    decaf/internal/lang/windows/ThreadHandle.h \
     decaf/internal/net/URIEncoderDecoder.h \
     decaf/internal/net/URIHelper.h \
     decaf/internal/net/URIType.h \
@@ -1098,7 +1102,11 @@
     decaf/internal/util/ByteArrayAdapter.h \
     decaf/internal/util/HexStringParser.h \
     decaf/internal/util/TimerTaskHeap.h \
+    decaf/internal/util/concurrent/ConditionImpl.h \
+    decaf/internal/util/concurrent/MutexImpl.h \
     decaf/internal/util/concurrent/SynchronizableImpl.h \
+    decaf/internal/util/concurrent/unix/ConditionHandle.h \
+    decaf/internal/util/concurrent/unix/MutexHandle.h \
     decaf/io/BlockingByteArrayInputStream.h \
     decaf/io/BufferedInputStream.h \
     decaf/io/BufferedOutputStream.h \

Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/lang/unix/ThreadHandle.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/lang/unix/ThreadHandle.h?rev=816370&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/lang/unix/ThreadHandle.h (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/lang/unix/ThreadHandle.h Thu Sep 17 20:21:25 2009
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+#ifndef _DECAF_LANG_THREADHANDLE_H_
+#define _DECAF_LANG_THREADHANDLE_H_
+
+#include <decaf/util/Config.h>
+
+#if HAVE_PTHREAD_H
+#include <pthread.h>
+#endif
+
+namespace decaf {
+namespace lang {
+
+    /**
+     * Platform definition of a Thread Handle on Unix, contains the PThread
+     * constructs and additional data necessary to implement a thread on
+     * a Unix based OS.
+     *
+     * @since 1.0
+     */
+    class DECAF_API ThreadHandle {
+    public:
+
+        typedef void (*threadEntry)( decaf::lang::ThreadHandle* self, void* data );
+
+        ThreadHandle() {
+            pthread_attr_init( &attributes );
+            running = false;
+            returnStatus = false;
+            userArg = NULL;
+            entryFunctionPtr = NULL;
+        }
+
+        ~ThreadHandle() {
+            pthread_attr_destroy( &attributes );
+        }
+
+        pthread_t thread;
+        pthread_attr_t attributes;
+        bool returnStatus;
+        threadEntry entryFunctionPtr;
+        void* userArg;
+        bool running;
+
+    };
+
+}}
+
+#endif /* _DECAF_LANG_THREADHANDLE_H_ */

Propchange: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/lang/unix/ThreadHandle.h
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/lang/unix/ThreadImpl.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/lang/unix/ThreadImpl.cpp?rev=816370&r1=816369&r2=816370&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/lang/unix/ThreadImpl.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/lang/unix/ThreadImpl.cpp Thu Sep 17 20:21:25 2009
@@ -16,6 +16,7 @@
  */
 
 #include <decaf/internal/lang/ThreadImpl.h>
+#include <decaf/internal/lang/unix/ThreadHandle.h>
 
 #include <decaf/lang/Math.h>
 #include <decaf/lang/Thread.h>
@@ -53,38 +54,6 @@
 using namespace decaf::util::concurrent;
 
 ////////////////////////////////////////////////////////////////////////////////
-namespace decaf{
-namespace lang{
-
-    class ThreadHandle {
-    public:
-
-        typedef void (*threadEntry)( decaf::lang::ThreadHandle* self, void* data );
-
-        ThreadHandle() {
-            pthread_attr_init( &attributes );
-            running = false;
-            returnStatus = false;
-            userArg = NULL;
-            entryFunctionPtr = NULL;
-        }
-
-        ~ThreadHandle() {
-            pthread_attr_destroy( &attributes );
-        }
-
-        pthread_t thread;
-        pthread_attr_t attributes;
-        bool returnStatus;
-        threadEntry entryFunctionPtr;
-        void* userArg;
-        bool running;
-
-    };
-
-}}
-
-////////////////////////////////////////////////////////////////////////////////
 namespace{
 
     void* threadWorker( void* arg ) {
@@ -118,16 +87,16 @@
 
     int result = pthread_create( &( handle->thread ), &( handle->attributes ), threadWorker, handle.get() );
 
-    // Only try and set this if its not the default value.
-    if( parent->getPriority() != Thread::NORM_PRIORITY ) {
-        setPriority( handle.get(), parent->getPriority() );
-    }
-
     if( result != 0 ) {
         throw RuntimeException(
             __FILE__, __LINE__, "Failed to create new Thread." );
     }
 
+    // Only try and set this if its not the default value.
+    if( parent->getPriority() != Thread::NORM_PRIORITY ) {
+        setPriority( handle.get(), parent->getPriority() );
+    }
+
     return handle.release();
 }
 

Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/lang/windows/ThreadHandle.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/lang/windows/ThreadHandle.h?rev=816370&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/lang/windows/ThreadHandle.h (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/lang/windows/ThreadHandle.h Thu Sep 17 20:21:25 2009
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+#ifndef _DECAF_LANG_THREADHANDLE_H_
+#define _DECAF_LANG_THREADHANDLE_H_
+
+#include <decaf/util/Config.h>
+
+namespace decaf {
+namespace lang {
+
+    /**
+     * Platform definition of a Thread Handle on Windows, contains the type
+     * constructs and additional data necessary to implement a thread on
+     * a Windows based OS.
+     *
+     * @since 1.0
+     */
+    class DECAF_API ThreadHandle {
+    public:
+
+        typedef void (*threadEntry)( decaf::lang::ThreadHandle* self, void* data );
+
+        ThreadHandle() {
+            running = false;
+            returnStatus = false;
+            userArg = NULL;
+            entryFunctionPtr = NULL;
+            handle = NULL;
+        }
+
+        ~ThreadHandle() {
+            ::CloseHandle( handle );
+        }
+
+        HANDLE handle;
+        bool returnStatus;
+        threadEntry entryFunctionPtr;
+        void* userArg;
+        bool running;
+
+    };
+
+}}
+
+#endif /* _DECAF_LANG_THREADHANDLE_H_ */

Propchange: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/lang/windows/ThreadHandle.h
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/lang/windows/ThreadImpl.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/lang/windows/ThreadImpl.cpp?rev=816370&r1=816369&r2=816370&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/lang/windows/ThreadImpl.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/lang/windows/ThreadImpl.cpp Thu Sep 17 20:21:25 2009
@@ -16,6 +16,7 @@
  */
 
 #include <decaf/internal/lang/ThreadImpl.h>
+#include <decaf/internal/lang/windows/ThreadHandle.h>
 
 #include <decaf/lang/Math.h>
 #include <decaf/lang/Thread.h>
@@ -35,36 +36,6 @@
 using namespace decaf::util::concurrent;
 
 ////////////////////////////////////////////////////////////////////////////////
-namespace decaf{
-namespace lang{
-
-    class ThreadHandle {
-    public:
-
-        typedef void (*threadEntry)( decaf::lang::ThreadHandle* self, void* data );
-
-        ThreadHandle() {
-            running = false;
-            returnStatus = false;
-            userArg = NULL;
-            entryFunctionPtr = NULL;
-            handle = NULL;
-        }
-
-        ~ThreadHandle() {
-        }
-
-        HANDLE handle;
-        bool returnStatus;
-        threadEntry entryFunctionPtr;
-        void* userArg;
-        bool running;
-
-    };
-
-}}
-
-////////////////////////////////////////////////////////////////////////////////
 namespace{
 
     unsigned int __stdcall threadWorker( void* arg ) {
@@ -151,7 +122,6 @@
 void ThreadImpl::join( decaf::lang::ThreadHandle* handle, long long mills, long long nanos ) {
 
     unsigned int rv = WaitForSingleObject( handle->handle, (DWORD)mills );
-    ::CloseHandle( handle->handle );
 }
 
 ////////////////////////////////////////////////////////////////////////////////

Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/ConditionImpl.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/ConditionImpl.h?rev=816370&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/ConditionImpl.h (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/ConditionImpl.h Thu Sep 17 20:21:25 2009
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _DECAF_INTERNAL_UTIL_CONCURRENT_CONDITIONIMPL_H_
+#define _DECAF_INTERNAL_UTIL_CONCURRENT_CONDITIONIMPL_H_
+
+#include <decaf/util/Config.h>
+
+namespace decaf {
+namespace util{
+namespace concurrent{
+    class MutexHandle;
+    class ConditionHandle;
+}}
+namespace internal {
+namespace util {
+namespace concurrent {
+
+    class DECAF_API ConditionImpl {
+    private:
+
+        ConditionImpl() {}
+
+    public:
+
+        /**
+         * Creates the Condition object and attaches it to the given MutexHandle.
+         *
+         * @param mutex the Mutex handle that this Condition is attached to.
+         *
+         * @return a newly constructed Condition handle that is attached to the given handle.
+         */
+        static decaf::util::concurrent::ConditionHandle* create( decaf::util::concurrent::MutexHandle* mutex );
+
+        /**
+         * Waits for the condition to be signaled.
+         *
+         * @param condition the handle to the condition to wait on.
+         */
+        static void wait( decaf::util::concurrent::ConditionHandle* condition );
+
+        /**
+         * Waits for the condition to be signaled or for the time specified to ellapse.
+         *
+         * @param condition the handle to the condition to wait on.
+         * @param mills the time in milliseconds to wait for the condition to be signaled.
+         * @param nanos additional time in nanoseconds to wait for the thread to be signaled.
+         */
+        static void wait( decaf::util::concurrent::ConditionHandle* condition, long long mills, long long nanos );
+
+        /**
+         * Signals one Thread that is waiting on this condition to wake up.
+         *
+         * @param condition the handle to the condition to wait on.
+         */
+        static void notify( decaf::util::concurrent::ConditionHandle* condition );
+
+        /**
+         * Signals all Threads that is waiting on this condition to wake up.
+         *
+         * @param condition the handle to the condition to wait on.
+         */
+        static void notifyAll( decaf::util::concurrent::ConditionHandle* condition );
+
+    };
+
+}}}}
+
+#endif /* _DECAF_INTERNAL_UTIL_CONCURRENT_CONDITIONIMPL_H_ */

Propchange: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/ConditionImpl.h
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/MutexImpl.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/MutexImpl.h?rev=816370&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/MutexImpl.h (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/MutexImpl.h Thu Sep 17 20:21:25 2009
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _DECAF_INTERNAL_UTIL_CONCURRENT_MUTEXIMPL_H_
+#define _DECAF_INTERNAL_UTIL_CONCURRENT_MUTEXIMPL_H_
+
+#include <decaf/util/Config.h>
+
+namespace decaf {
+namespace util {
+namespace concurrent {
+    class Mutex;
+    class MutexHandle;
+}}
+namespace internal {
+namespace util {
+namespace concurrent {
+
+    class DECAF_API MutexImpl {
+    private:
+
+        MutexImpl();
+
+    public:
+
+        /**
+         * Creates a Reentrant Mutex and returns the handle, throws a Runtime Exception
+         * if the Mutex cannot be created for some reason.
+         *
+         * @returns handle to a newly created Mutex.
+         */
+        static decaf::util::concurrent::MutexHandle* create();
+
+        /**
+         * Locks the Mutex.  If the Mutex is already locked by another thread this method
+         * blocks until the Mutex becomes unlocked and this thread acquires the lock.
+         *
+         * @param handle the handle to the Mutex to Lock.
+         */
+        static void lock( decaf::util::concurrent::MutexHandle* handle );
+
+        /**
+         * Tries to lock the Mutex.  If the Mutex is unlocked this Thread acquires the lock on
+         * the Mutex and this method returns true, if the Mutex is already locked then the lock
+         * is not acquired and this method returns false.
+         *
+         * @param handle the handle to the Mutex to attempt to Lock.
+         *
+         * @return true if the lock was acquired false otherwise.
+         */
+        static bool trylock( decaf::util::concurrent::MutexHandle* handle );
+
+        /**
+         * Unlocks the Mutex allowing other Thread to then acquire the Lock on it.
+         *
+         * @param handle the handle to the Mutex to attempt to Lock.
+         */
+        static void unlock( decaf::util::concurrent::MutexHandle* handle );
+
+    };
+
+}}}}
+
+#endif /* _DECAF_INTERNAL_UTIL_CONCURRENT_MUTEXIMPL_H_ */

Propchange: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/MutexImpl.h
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/ConditionHandle.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/ConditionHandle.h?rev=816370&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/ConditionHandle.h (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/ConditionHandle.h Thu Sep 17 20:21:25 2009
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+#ifndef _DECAF_UTIL_CONCURRENT_CONDITIONHANDLE_H_
+#define _DECAF_UTIL_CONCURRENT_CONDITIONHANDLE_H_
+
+#include <decaf/util/Config.h>
+
+#if HAVE_PTHREAD_H
+#include <pthread.h>
+#endif
+
+namespace decaf {
+namespace util {
+namespace concurrent {
+
+    class MutexHandle;
+
+    class DECAF_API ConditionHandle {
+    public:
+
+        ConditionHandle() {
+            mutex = NULL;
+        }
+
+        ~ConditionHandle() {
+            pthread_cond_destroy( &condition );
+        }
+
+        // The actual condition object
+        pthread_cond_t condition;
+
+        // The mutex object associated with this condition.
+        MutexHandle* mutex;
+
+
+    };
+
+}}}
+
+#endif /* _DECAF_UTIL_CONCURRENT_CONDITIONHANDLE_H_ */

Propchange: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/ConditionHandle.h
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/ConditionImpl.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/ConditionImpl.cpp?rev=816370&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/ConditionImpl.cpp (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/ConditionImpl.cpp Thu Sep 17 20:21:25 2009
@@ -0,0 +1,153 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <decaf/internal/util/concurrent/ConditionImpl.h>
+#include <decaf/internal/util/concurrent/unix/ConditionHandle.h>
+#include <decaf/internal/util/concurrent/unix/MutexHandle.h>
+
+#include <decaf/lang/exceptions/RuntimeException.h>
+#include <decaf/internal/lang/ThreadImpl.h>
+#include <decaf/util/concurrent/TimeUnit.h>
+
+#include <list>
+#include <memory>
+
+#if HAVE_PTHREAD_H
+#include <pthread.h>
+#endif
+#if HAVE_SIGNAL_H
+#include <signal.h>
+#endif
+#if HAVE_STRING_H
+#include <string.h>
+#endif
+#if HAVE_SCHED_H
+#include <sched.h>
+#endif
+#if HAVE_SYS_TIME_H
+#include <sys/time.h>
+#endif
+#if HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#if HAVE_TIME_H
+#include <time.h>
+#endif
+#if HAVE_ERRNO_H
+#include <errno.h>
+#endif
+
+using namespace decaf;
+using namespace decaf::util;
+using namespace decaf::util::concurrent;
+using namespace decaf::internal;
+using namespace decaf::internal::util;
+using namespace decaf::internal::util::concurrent;
+using namespace decaf::lang;
+using namespace decaf::lang::exceptions;
+
+////////////////////////////////////////////////////////////////////////////////
+ConditionHandle* ConditionImpl::create( MutexHandle* mutex ) {
+
+    std::auto_ptr<ConditionHandle> handle( new ConditionHandle );
+
+    handle->mutex = mutex;
+
+    if( pthread_cond_init( &handle->condition, NULL ) != 0 ) {
+        throw RuntimeException(
+            __FILE__, __LINE__, "Failed to initialize OS Condition object." );
+    }
+
+    return handle.release();
+}
+
+///////////////////////////////////////////////////////////////////////////////
+void ConditionImpl::wait( ConditionHandle* condition ) {
+
+    // 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.
+    long long lock_owner = condition->mutex->lock_owner;
+    long long lock_count = condition->mutex->lock_count;
+
+    condition->mutex->lock_owner = 0;
+    condition->mutex->lock_count = 0;
+
+    if( pthread_cond_wait( &condition->condition, &condition->mutex->mutex ) != 0 ) {
+        throw RuntimeException(
+            __FILE__, __LINE__, "Failed to wait on OS Condition object." );
+    }
+
+    // restore the owner
+    condition->mutex->lock_owner = lock_owner;
+    condition->mutex->lock_count = lock_count;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ConditionImpl::wait( ConditionHandle* condition, long long mills, long long nanos ) {
+
+    // 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.
+    long long lock_owner = condition->mutex->lock_owner;
+    long long lock_count = condition->mutex->lock_count;
+
+    condition->mutex->lock_owner = 0;
+    condition->mutex->lock_count = 0;
+
+    // Get time now as nanoseconds.
+    struct timeval tv;
+    gettimeofday( &tv, NULL );
+    long long timeNow = TimeUnit::SECONDS.toNanos( tv.tv_sec ) +
+                        TimeUnit::MICROSECONDS.toNanos( tv.tv_usec );
+
+    // Convert delay to nanoseconds and add it to now.
+    long long delay = TimeUnit::MILLISECONDS.toNanos( mills ) + nanos + timeNow;
+
+    struct timespec abstime;
+    abstime.tv_sec = TimeUnit::NANOSECONDS.toSeconds( delay );
+    abstime.tv_nsec = delay % 1000000000;
+
+    unsigned int result = pthread_cond_timedwait( &condition->condition, &condition->mutex->mutex, &abstime );
+
+    // restore the owner
+    condition->mutex->lock_owner = lock_owner;
+    condition->mutex->lock_count = lock_count;
+
+    if( result != 0 && result != ETIMEDOUT ) {
+        throw RuntimeException(
+            __FILE__, __LINE__, "Failed to wait on OS Condition object." );
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ConditionImpl::notify( ConditionHandle* condition ) {
+
+    if( pthread_cond_signal( &condition->condition ) ) {
+        throw RuntimeException(
+            __FILE__, __LINE__, "Failed to signal OS Condition object." );
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ConditionImpl::notifyAll( ConditionHandle* condition ) {
+
+    if( pthread_cond_broadcast( &condition->condition ) ) {
+        throw RuntimeException(
+            __FILE__, __LINE__, "Failed to broadcast signal OS Condition object." );
+    }
+}

Propchange: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/ConditionImpl.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/MutexHandle.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/MutexHandle.h?rev=816370&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/MutexHandle.h (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/MutexHandle.h Thu Sep 17 20:21:25 2009
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+#ifndef _DECAF_UTIL_CONCURRENT_MUTEXHANDLE_H_
+#define _DECAF_UTIL_CONCURRENT_MUTEXHANDLE_H_
+
+#include <decaf/util/Config.h>
+
+#if HAVE_PTHREAD_H
+#include <pthread.h>
+#endif
+
+namespace decaf {
+namespace util {
+namespace concurrent {
+
+    class DECAF_API MutexHandle {
+    public:
+
+        MutexHandle() {
+        }
+
+        ~MutexHandle() {
+            pthread_mutex_destroy( &mutex );
+        }
+
+        // The mutex object.
+        pthread_mutex_t mutex;
+
+        // List of waiting threads
+        // std::list<pthread_cond_t*> eventQ;
+
+        // Lock Status Members
+        volatile long long lock_owner;
+        volatile long long lock_count;
+
+    };
+
+}}}
+
+#endif /* _DECAF_UTIL_CONCURRENT_MUTEXHANDLE_H_ */

Propchange: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/MutexHandle.h
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/MutexImpl.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/MutexImpl.cpp?rev=816370&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/MutexImpl.cpp (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/MutexImpl.cpp Thu Sep 17 20:21:25 2009
@@ -0,0 +1,117 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <decaf/internal/util/concurrent/MutexImpl.h>
+#include <decaf/internal/util/concurrent/unix/MutexHandle.h>
+
+#include <decaf/lang/exceptions/RuntimeException.h>
+#include <decaf/internal/lang/ThreadImpl.h>
+
+#include <list>
+#include <memory>
+
+#if HAVE_PTHREAD_H
+#include <pthread.h>
+#endif
+
+using namespace decaf;
+using namespace decaf::util;
+using namespace decaf::util::concurrent;
+using namespace decaf::internal;
+using namespace decaf::internal::lang;
+using namespace decaf::internal::util;
+using namespace decaf::internal::util::concurrent;
+using namespace decaf::lang;
+using namespace decaf::lang::exceptions;
+
+////////////////////////////////////////////////////////////////////////////////
+MutexHandle* MutexImpl::create() {
+
+    std::auto_ptr<MutexHandle> handle( new MutexHandle );
+
+    if( pthread_mutex_init( &( handle->mutex ), NULL ) != 0 ) {
+
+        throw RuntimeException(
+            __FILE__, __LINE__, "Failed to create Mutex object." );
+    }
+
+    handle->lock_count = 0;
+    handle->lock_owner = 0;
+
+    return handle.release();
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void MutexImpl::lock( MutexHandle* handle ) {
+
+    long long threadId = ThreadImpl::getThreadId();
+
+    if( threadId == handle->lock_owner ) {
+        handle->lock_count++;
+    } else {
+
+        if( pthread_mutex_lock( &( handle->mutex ) ) != 0 ) {
+            throw RuntimeException(
+                __FILE__, __LINE__, "Failed to Lock OS Mutex" );
+        }
+
+        handle->lock_owner = threadId;
+        handle->lock_count = 1;
+    }
+
+}
+
+////////////////////////////////////////////////////////////////////////////////
+bool MutexImpl::trylock( MutexHandle* handle ) {
+
+    long long threadId = ThreadImpl::getThreadId();
+
+    if( threadId == handle->lock_owner ) {
+        handle->lock_count++;
+    } else {
+
+        if( pthread_mutex_lock( &( handle->mutex ) ) == 0 ) {
+            handle->lock_owner = threadId;
+            handle->lock_count = 1;
+        } else {
+            return false;
+        }
+    }
+
+    return true;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void MutexImpl::unlock( MutexHandle* handle ) {
+
+    if( handle->lock_owner == 0 ) {
+        return;
+    }
+
+    if( handle->lock_owner != ThreadImpl::getThreadId() ) {
+        throw RuntimeException(
+            __FILE__, __LINE__,
+            "Unlock Failed, this thread is not the Lock Owner!" );
+    }
+
+    handle->lock_count--;
+
+    if( handle->lock_count == 0 ) {
+        handle->lock_owner = 0;
+        pthread_mutex_unlock( &( handle->mutex ) );
+    }
+}

Propchange: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/MutexImpl.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/ConditionHandle.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/ConditionHandle.h?rev=816370&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/ConditionHandle.h (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/ConditionHandle.h Thu Sep 17 20:21:25 2009
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+#ifndef _DECAF_UTIL_CONCURRENT_CONDITIONHANDLE_H_
+#define _DECAF_UTIL_CONCURRENT_CONDITIONHANDLE_H_
+
+#include <decaf/util/Config.h>
+
+namespace decaf {
+namespace util {
+namespace concurrent {
+
+    class MutexHandle;
+
+    class DECAF_API ConditionHandle {
+    public:
+
+        ConditionHandle() : numWaiting(0), numWake(0), generation(0), mutex(NULL) {
+        }
+
+        ~ConditionHandle() {
+            CloseHandle( semaphore );
+            DestroyCriticalSection( &mutex );
+        }
+
+        // The actual condition object
+        HANDLE semaphore;
+        CRITICAL_SECTION criticalSection;
+        unsigned int numWaiting;
+        unsigned int numWake;
+        unsigned int generation;
+
+        // The mutex object associated with this condition.
+        MutexHandle* mutex;
+
+    };
+
+}}}
+
+#endif /* _DECAF_UTIL_CONCURRENT_CONDITIONHANDLE_H_ */

Propchange: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/ConditionHandle.h
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/ConditionImpl.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/ConditionImpl.cpp?rev=816370&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/ConditionImpl.cpp (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/ConditionImpl.cpp Thu Sep 17 20:21:25 2009
@@ -0,0 +1,194 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <decaf/internal/util/concurrent/ConditionImpl.h>
+#include <decaf/internal/util/concurrent/MutexImpl.h>
+#include <decaf/internal/util/concurrent/windows/ConditionHandle.h>
+#include <decaf/internal/util/concurrent/windows/MutexHandle.h>
+
+#include <decaf/lang/exceptions/RuntimeException.h>
+#include <decaf/internal/lang/ThreadImpl.h>
+#include <decaf/util/concurrent/TimeUnit.h>
+
+#include <list>
+#include <memory>
+
+#include <limits.h>
+
+using namespace decaf;
+using namespace decaf::util;
+using namespace decaf::util::concurrent;
+using namespace decaf::internal;
+using namespace decaf::internal::util;
+using namespace decaf::internal::util::concurrent;
+using namespace decaf::lang;
+using namespace decaf::lang::exceptions;
+
+////////////////////////////////////////////////////////////////////////////////
+ConditionHandle* ConditionImpl::create( MutexHandle* mutex ) {
+
+    std::auto_ptr<ConditionHandle> handle( new ConditionHandle );
+
+    handle->mutex = mutex;
+
+    handle->semaphore = CreateSemaphore( NULL, 0, LONG_MAX, NULL );
+    if( handle->semaphore == NULL ) {
+        throw RuntimeException(
+            __FILE__, __LINE__, "Failed to initialize OS Condition object." );
+    }
+
+    try{
+        InitializeCriticalSection( &handle->criticalSection );
+    } catch(...) {
+        throw RuntimeException(
+            __FILE__, __LINE__, "Failed to initialize OS Condition object." );
+    }
+
+    return handle.release();
+}
+
+///////////////////////////////////////////////////////////////////////////////
+void ConditionImpl::wait( ConditionHandle* condition ) {
+
+    ConditionImpl::wait( condition, INFINITE, 0 );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ConditionImpl::wait( ConditionHandle* condition, long long mills, long long nanos ) {
+
+    DWORD res = 0;
+    bool doWake = false;
+    unsigned int generation = 0;
+
+    // Store the state of the condition before waiting.
+    EnterCriticalSection( &condition->criticalSection );
+
+    condition->numWaiting++;
+    generation = condition->generation;
+
+    LeaveCriticalSection( &condition->criticalSection );
+
+    // 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.  These
+    // values are on the calling threads stack now so when it re-acquires
+    // the lock its essentially restoring its own state data back to the
+    // loack object.
+    long long lock_owner = condition->mutex->lock_owner;
+    long long lock_count = condition->mutex->lock_count;
+
+    condition->mutex->lock_owner = 0;
+    condition->mutex->lock_count = 0;
+
+    // Circumvent the MutexImpl Implementation and unlock the Critical Section ourself.
+    // We have the state preserved here so we can restore it once the condition has ]
+    // been signaled, the default Mutex unlock behavior is of a recursive lock so
+    // if we don't do this here it might not unlock if this thread had locked it more
+    // than once.
+    LeaveCriticalSection( &condition->mutex->mutex );
+
+    do {
+
+        res = WaitForSingleObject( condition->semaphore, mills );
+
+        EnterCriticalSection( &condition->criticalSection );
+
+        if( condition->numWake ) {
+
+            if( condition->generation != generation ) {
+                condition->numWake--;
+                condition->numWaiting--;
+                break;
+            } else {
+                wake = 1;
+            }
+        }
+        else if( res != WAIT_OBJECT_0 ) {
+            condition->numWaiting--;
+            break;
+        }
+
+        LeaveCriticalSection( &condition->criticalSection );
+
+        if( doWake ) {
+            doWake = false;
+            ReleaseSemaphore( condition->semaphore, 1, NULL );
+        }
+
+    } while( true );
+
+    LeaveCriticalSection( &condition->criticalSection );
+
+    // Perform the reciprocal of the Mutex unlock done above.
+    EnterCriticalSection( &condition->mutex->mutex );
+
+    // restore the owner now that its been re-locked.
+    condition->mutex->lock_owner = lock_owner;
+    condition->mutex->lock_count = lock_count;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ConditionImpl::notify( ConditionHandle* condition ) {
+
+    try {
+
+        bool doWake = false;
+
+        EnterCriticalSection( &condition->criticalSection );
+
+        if( condition->numWaiting > condition->numWake ) {
+            doWake = true;
+            condition->numWake++;
+            condition->generation++;
+        }
+
+        LeaveCriticalSection( &condition->criticalSection );
+
+        if( doWake ) {
+            ReleaseSemaphore( condition->semaphore, 1, NULL );
+        }
+
+    } catch(...) {
+        throw RuntimeException(
+            __FILE__, __LINE__, "Failed to signal OS Condition object." );
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ConditionImpl::notifyAll( ConditionHandle* condition ) {
+
+    unsigned int numWake = 0;
+
+    EnterCriticalSection( &condition->criticalSection );
+
+    if( condition->numWaiting > condition->numWake ) {
+        numWake = condition->numWaiting - condition->numWake;
+        condition->numWake = cond->numWaiting;
+        condition->generation++;
+    }
+
+    LeaveCriticalSection( &condition->criticalSection );
+
+    if( numWake ) {
+        ReleaseSemaphore( condition->semaphore, numWake, NULL );
+    }
+
+    if( pthread_cond_broadcast( &condition->condition ) ) {
+        throw RuntimeException(
+            __FILE__, __LINE__, "Failed to broadcast signal OS Condition object." );
+    }
+}

Propchange: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/ConditionImpl.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/MutexHandle.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/MutexHandle.h?rev=816370&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/MutexHandle.h (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/MutexHandle.h Thu Sep 17 20:21:25 2009
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+#ifndef _DECAF_UTIL_CONCURRENT_MUTEXHANDLE_H_
+#define _DECAF_UTIL_CONCURRENT_MUTEXHANDLE_H_
+
+#include <decaf/util/Config.h>
+
+namespace decaf {
+namespace util {
+namespace concurrent {
+
+    class DECAF_API MutexHandle {
+    public:
+
+        MutexHandle() : lock_owner(0), lock_count(0) {
+        }
+
+        ~MutexHandle() {
+            DeleteCriticalSection( &mutex );
+        }
+
+        // The mutex object.
+        CRITICAL_SECTION mutex;
+
+        // Lock Status Members
+        volatile long long lock_owner;
+        volatile long long lock_count;
+
+    };
+
+}}}
+
+#endif /* _DECAF_UTIL_CONCURRENT_MUTEXHANDLE_H_ */

Propchange: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/MutexHandle.h
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/MutexImpl.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/MutexImpl.cpp?rev=816370&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/MutexImpl.cpp (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/MutexImpl.cpp Thu Sep 17 20:21:25 2009
@@ -0,0 +1,130 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <decaf/internal/util/concurrent/MutexImpl.h>
+#include <decaf/internal/util/concurrent/windows/MutexHandle.h>
+
+#include <decaf/lang/exceptions/RuntimeException.h>
+#include <decaf/internal/lang/ThreadImpl.h>
+
+#include <memory>
+
+using namespace decaf;
+using namespace decaf::util;
+using namespace decaf::util::concurrent;
+using namespace decaf::internal;
+using namespace decaf::internal::lang;
+using namespace decaf::internal::util;
+using namespace decaf::internal::util::concurrent;
+using namespace decaf::lang;
+using namespace decaf::lang::exceptions;
+
+////////////////////////////////////////////////////////////////////////////////
+MutexHandle* MutexImpl::create() {
+
+    std::auto_ptr<MutexHandle> handle( new MutexHandle );
+
+    try{
+        InitializeCriticalSection( &handle->mutex );
+    } catch(...) {
+        throw RuntimeException(
+            __FILE__, __LINE__, "Failed to create OS Mutex object." );
+    }
+
+    handle->lock_count = 0;
+    handle->lock_owner = 0;
+
+    return handle.release();
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void MutexImpl::lock( MutexHandle* handle ) {
+
+    long long threadId = ThreadImpl::getThreadId();
+
+    if( threadId == handle->lock_owner ) {
+        handle->lock_count++;
+    } else {
+
+        try{
+            EnterCriticalSection( &handle->mutex );
+        } catch(...) {
+            throw RuntimeException(
+                __FILE__, __LINE__, "Failed to Lock OS Mutex" );
+        }
+
+        handle->lock_owner = threadId;
+        handle->lock_count = 1;
+    }
+
+}
+
+////////////////////////////////////////////////////////////////////////////////
+bool MutexImpl::trylock( MutexHandle* handle ) {
+
+    long long threadId = ThreadImpl::getThreadId();
+
+    if( threadId == handle->lock_owner ) {
+        handle->lock_count++;
+    } else {
+
+        unsigned int result = 0;
+
+        try{
+            result = TryEnterCriticalSection( &handle->mutex );
+        } catch(...) {
+            throw RuntimeException(
+                __FILE__, __LINE__, "Failed to Lock OS Mutex" );
+        }
+
+        if( result != 0 ) {
+            handle->lock_owner = threadId;
+            handle->lock_count = 1;
+        } else {
+            return false;
+        }
+    }
+
+    return true;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void MutexImpl::unlock( MutexHandle* handle ) {
+
+    if( handle->lock_owner == 0 ) {
+        return;
+    }
+
+    if( handle->lock_owner != ThreadImpl::getThreadId() ) {
+        throw RuntimeException(
+            __FILE__, __LINE__,
+            "Unlock Failed, this thread is not the Lock Owner!" );
+    }
+
+    handle->lock_count--;
+
+    if( handle->lock_count == 0 ) {
+        handle->lock_owner = 0;
+
+        try{
+            LeaveCriticalSection( &handle->mutex );
+        } catch(...) {
+            throw RuntimeException(
+                __FILE__, __LINE__, "Failed to Unlock OS Mutex" );
+        }
+    }
+}

Propchange: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/MutexImpl.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/Mutex.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/Mutex.cpp?rev=816370&r1=816369&r2=816370&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/Mutex.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/Mutex.cpp Thu Sep 17 20:21:25 2009
@@ -17,17 +17,23 @@
 
 #include <decaf/util/concurrent/Mutex.h>
 
-#include <apr_errno.h>
-#include <apr_time.h>
-#include <apr_thread_mutex.h>
-#include <apr_thread_cond.h>
+//#include <apr_errno.h>
+//#include <apr_time.h>
+//#include <apr_thread_mutex.h>
+//#include <apr_thread_cond.h>
+//
+//#include <decaf/internal/AprPool.h>
 
-#include <decaf/internal/AprPool.h>
+#include <decaf/internal/util/concurrent/MutexImpl.h>
+#include <decaf/internal/util/concurrent/ConditionImpl.h>
 
 #include <list>
+#include <memory>
 
 using namespace decaf;
 using namespace decaf::internal;
+using namespace decaf::internal::util;
+using namespace decaf::internal::util::concurrent;
 using namespace decaf::util;
 using namespace decaf::util::concurrent;
 
@@ -40,23 +46,24 @@
     public:
 
         MutexProperties() {
-            this->mutex = NULL;
-            this->lock_owner = 0;
-            this->lock_count = 0;
+//            this->mutex = NULL;
+//            this->lock_owner = 0;
+//            this->lock_count = 0;
         }
 
         // Our one and only APR Pool
-        internal::AprPool aprPool;
+//        internal::AprPool aprPool;
 
         // The mutex object.
-        apr_thread_mutex_t* mutex;
+        std::auto_ptr<MutexHandle> mutex;
+        std::auto_ptr<ConditionHandle> condition;
 
         // List of waiting threads
-        std::list<apr_thread_cond_t*> eventQ;
-
-        // Lock Status Members
-        volatile long long lock_owner;
-        volatile long long lock_count;
+//        std::list<apr_thread_cond_t*> eventQ;
+//
+//        // Lock Status Members
+//        volatile long long lock_owner;
+//        volatile long long lock_count;
 
     };
 
@@ -68,9 +75,11 @@
     this->properties.reset( new MutexProperties );
 
     // Allocate the OS Mutex Implementation.
-    apr_thread_mutex_create( &( properties->mutex ),
-                             APR_THREAD_MUTEX_NESTED,
-                             properties->aprPool.getAprPool() );
+    this->properties->mutex.reset( MutexImpl::create() );
+    this->properties->condition.reset( ConditionImpl::create( this->properties->mutex.get() ) );
+//    apr_thread_mutex_create( &( properties->mutex ),
+//                             APR_THREAD_MUTEX_NESTED,
+//                             properties->aprPool.getAprPool() );
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -78,158 +87,168 @@
 
     // Unlock the mutex, the destruction of the AprPool will take care
     // of cleaning up the apr_thread_mutex_t allocated in the ctor.
-    unlock();
-}
-
-////////////////////////////////////////////////////////////////////////////////
-bool Mutex::isLockOwner() const {
-    return properties->lock_owner == lang::Thread::getId();
+    //unlock();
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 void Mutex::lock() throw( lang::Exception ) {
 
-    long long threadId = lang::Thread::getId();
-
-    if( threadId == properties->lock_owner ) {
-        properties->lock_count++;
-    } else {
-        apr_thread_mutex_lock( properties->mutex );
-        properties->lock_owner = threadId;
-        properties->lock_count = 1;
-    }
+    MutexImpl::lock( this->properties->mutex.get() );
+//    long long threadId = lang::Thread::getId();
+//
+//    if( threadId == properties->lock_owner ) {
+//        properties->lock_count++;
+//    } else {
+//        apr_thread_mutex_lock( properties->mutex );
+//        properties->lock_owner = threadId;
+//        properties->lock_count = 1;
+//    }
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 bool Mutex::tryLock() throw( lang::Exception ) {
 
-    long long threadId = lang::Thread::getId();
-
-    if( threadId == properties->lock_owner ) {
-        properties->lock_count++;
-    } else {
-
-        if( apr_thread_mutex_trylock( properties->mutex ) == APR_SUCCESS ) {
-            properties->lock_owner = threadId;
-            properties->lock_count = 1;
-        } else {
-            return false;
-        }
-    }
+    return MutexImpl::trylock( this->properties->mutex.get() );
 
-    return true;
+//    long long threadId = lang::Thread::getId();
+//
+//    if( threadId == properties->lock_owner ) {
+//        properties->lock_count++;
+//    } else {
+//
+//        if( apr_thread_mutex_trylock( properties->mutex ) == APR_SUCCESS ) {
+//            properties->lock_owner = threadId;
+//            properties->lock_count = 1;
+//        } else {
+//            return false;
+//        }
+//    }
+//
+//    return true;
 }
 
-
 ////////////////////////////////////////////////////////////////////////////////
 void Mutex::unlock() throw( lang::Exception ) {
 
-    if( properties->lock_owner == 0 ) {
-        return;
-    }
-
-    if( !isLockOwner() ) {
-        throw lang::Exception(
-            __FILE__, __LINE__,
-            "Mutex::unlock - Failed, not Lock Owner!" );
-    }
-
-    properties->lock_count--;
-
-    if( properties->lock_count == 0 ) {
-        properties->lock_owner = 0;
-        apr_thread_mutex_unlock( properties->mutex );
-    }
+    MutexImpl::unlock( this->properties->mutex.get() );
+
+//    if( properties->lock_owner == 0 ) {
+//        return;
+//    }
+//
+//    if( !isLockOwner() ) {
+//        throw lang::Exception(
+//            __FILE__, __LINE__,
+//            "Mutex::unlock - Failed, not Lock Owner!" );
+//    }
+//
+//    properties->lock_count--;
+//
+//    if( properties->lock_count == 0 ) {
+//        properties->lock_owner = 0;
+//        apr_thread_mutex_unlock( properties->mutex );
+//    }
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 void Mutex::wait() throw( lang::Exception ) {
+
     // Delegate to the timed version
-    wait( WAIT_INFINITE );
+    // wait( WAIT_INFINITE, 0 );
+    ConditionImpl::wait( this->properties->condition.get() );
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 void Mutex::wait( long long millisecs ) throw( lang::Exception ) {
 
-    if( !isLockOwner() ) {
-        throw lang::Exception(
-            __FILE__, __LINE__,
-            "Mutex::wait - Failed, not Lock Owner!");
-    }
-
-    // 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.
-    long long lock_owner = this->properties->lock_owner;
-    long long lock_count = this->properties->lock_count;
-
-    this->properties->lock_owner = 0;
-    this->properties->lock_count = 0;
-
-    // Create this threads wait event
-    apr_thread_cond_t* waitEvent = NULL;
-    apr_pool_t *subPool = NULL;
-    apr_pool_create_ex( &subPool, properties->aprPool.getAprPool(), NULL, NULL );
-    apr_thread_cond_create( &waitEvent, subPool );
-
-    // Store the event in the queue so that a notify can
-    // call it and wake up the thread.
-    properties->eventQ.push_back( waitEvent );
-
-    if( millisecs != WAIT_INFINITE ) {
-        apr_interval_time_t wait = millisecs * 1000;
-        apr_thread_cond_timedwait( waitEvent, properties->mutex, wait );
-    } else {
-        apr_thread_cond_wait( waitEvent, properties->mutex );
-    }
-
-    // Be Sure that the event is now removed
-    properties->eventQ.remove( waitEvent );
-
-    // Destroy our wait event now, the notify method will have removed it
-    // from the event queue.
-    apr_thread_cond_destroy( waitEvent );
-    apr_pool_destroy( subPool );
-
-    // restore the owner
-    this->properties->lock_owner = lock_owner;
-    this->properties->lock_count = lock_count;
+    wait( millisecs, 0 );
+
+//    if( !isLockOwner() ) {
+//        throw lang::Exception(
+//            __FILE__, __LINE__,
+//            "Mutex::wait - Failed, not Lock Owner!");
+//    }
+//
+//    // 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.
+//    long long lock_owner = this->properties->lock_owner;
+//    long long lock_count = this->properties->lock_count;
+//
+//    this->properties->lock_owner = 0;
+//    this->properties->lock_count = 0;
+//
+//    // Create this threads wait event
+//    apr_thread_cond_t* waitEvent = NULL;
+//    apr_pool_t *subPool = NULL;
+//    apr_pool_create_ex( &subPool, properties->aprPool.getAprPool(), NULL, NULL );
+//    apr_thread_cond_create( &waitEvent, subPool );
+//
+//    // Store the event in the queue so that a notify can
+//    // call it and wake up the thread.
+//    properties->eventQ.push_back( waitEvent );
+//
+//    if( millisecs != WAIT_INFINITE ) {
+//        apr_interval_time_t wait = millisecs * 1000;
+//        apr_thread_cond_timedwait( waitEvent, properties->mutex, wait );
+//    } else {
+//        apr_thread_cond_wait( waitEvent, properties->mutex );
+//    }
+//
+//    // Be Sure that the event is now removed
+//    properties->eventQ.remove( waitEvent );
+//
+//    // Destroy our wait event now, the notify method will have removed it
+//    // from the event queue.
+//    apr_thread_cond_destroy( waitEvent );
+//    apr_pool_destroy( subPool );
+//
+//    // restore the owner
+//    this->properties->lock_owner = lock_owner;
+//    this->properties->lock_count = lock_count;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 void Mutex::wait( long long millisecs, int nanos DECAF_UNUSED ) throw( lang::Exception ) {
+
     // For now delegate to the single arg version.
-    this->wait( millisecs );
+    // this->wait( millisecs );
+
+    ConditionImpl::wait( this->properties->condition.get(), millisecs, nanos );
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 void Mutex::notify() throw( lang::Exception )
 {
-    if( !isLockOwner() ) {
-        throw lang::Exception(
-            __FILE__, __LINE__,
-            "Mutex::Notify - Failed, not Lock Owner!" );
-    }
-
-    if( !properties->eventQ.empty() ) {
-        apr_thread_cond_t* event = properties->eventQ.front();
-        properties->eventQ.remove( event );
-        apr_thread_cond_signal( event );
-    }
+    ConditionImpl::notify( this->properties->condition.get() );
+
+//    if( !isLockOwner() ) {
+//        throw lang::Exception(
+//            __FILE__, __LINE__,
+//            "Mutex::Notify - Failed, not Lock Owner!" );
+//    }
+//
+//    if( !properties->eventQ.empty() ) {
+//        apr_thread_cond_t* event = properties->eventQ.front();
+//        properties->eventQ.remove( event );
+//        apr_thread_cond_signal( event );
+//    }
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 void Mutex::notifyAll() throw( lang::Exception )
 {
-    if( !isLockOwner() ) {
-        throw lang::Exception(
-            __FILE__, __LINE__,
-            "Mutex::NotifyAll - Failed, not Lock Owner!" );
-    }
-
-    while( !properties->eventQ.empty() ) {
-        apr_thread_cond_t* event = properties->eventQ.front();
-        properties->eventQ.remove( event );
-        apr_thread_cond_signal( event );
-    }
+    ConditionImpl::notifyAll( this->properties->condition.get() );
+
+//    if( !isLockOwner() ) {
+//        throw lang::Exception(
+//            __FILE__, __LINE__,
+//            "Mutex::NotifyAll - Failed, not Lock Owner!" );
+//    }
+//
+//    while( !properties->eventQ.empty() ) {
+//        apr_thread_cond_t* event = properties->eventQ.front();
+//        properties->eventQ.remove( event );
+//        apr_thread_cond_signal( event );
+//    }
 }

Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/Mutex.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/Mutex.h?rev=816370&r1=816369&r2=816370&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/Mutex.h (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/Mutex.h Thu Sep 17 20:21:25 2009
@@ -68,14 +68,6 @@
 
         virtual void notifyAll() throw( lang::Exception );
 
-    private:
-
-        /**
-         * Check if the calling thread is the Lock Owner
-         * @retun true if the caller is the lock owner
-         */
-        bool isLockOwner() const;
-
     };
 
 }}}



Mime
View raw message