You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ta...@apache.org on 2011/05/06 23:22:41 UTC
svn commit: r1100383 [2/3] - in
/activemq/activemq-cpp/trunk/activemq-cpp/src: examples/
examples/producers/ main/ main/activemq/library/ main/activemq/transport/
main/activemq/wireformat/ main/decaf/internal/
main/decaf/internal/net/ssl/openssl/ main/...
Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/ThreadingTypes.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/ThreadingTypes.h?rev=1100383&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/ThreadingTypes.h (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/ThreadingTypes.h Fri May 6 21:22:39 2011
@@ -0,0 +1,132 @@
+/*
+ * 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_UNIX_THREADTYPES_H_
+#define _DECAF_INTERNAL_UTIL_CONCURRENT_UNIX_THREADTYPES_H_
+
+#include <decaf/util/Config.h>
+
+#ifdef HAVE_PTHREAD_H
+#include <decaf/internal/util/concurrent/unix/PlatformDefs.h>
+#else
+#include <decaf/internal/util/concurrent/windows/PlatformDefs.h>
+#endif
+
+namespace decaf{
+namespace lang{
+ class Thread;
+}
+namespace internal{
+namespace util{
+namespace concurrent{
+
+ /**
+ * This is the main method for thread instances, this value is valid
+ * on any platform, the PlatformThread methods will handle calling this
+ * method and providing it with its assigned arg.
+ *
+ * @param arg
+ * A void* that was given when the thread was started.
+ */
+ typedef PLATFORM_THREAD_CALLBACK_TYPE (PLATFORM_CALLING_CONV *threadMainMethod)(PLATFORM_THREAD_ENTRY_ARG);
+
+ /**
+ * The ThreadHandle contains one of these and it should be the method that
+ * does the actual work for the thread.
+ */
+ typedef void (*threadingTask)(void *);
+
+ // Forward Declare some of the nested types.
+ struct MonitorHandle;
+
+ struct ThreadHandle {
+ decaf::lang::Thread* parent;
+ decaf_thread_t handle;
+ decaf_mutex_t mutex;
+ decaf_condition_t condition;
+ volatile int state;
+ int priority;
+ bool interrupted;
+ bool interruptible;
+ bool timerSet;
+ bool canceled;
+ bool unparked;
+ bool parked;
+ bool sleeping;
+ bool waiting;
+ bool notified;
+ bool blocked;
+ bool suspended;
+ char* name;
+ long long stackSize;
+ void *tls[128];
+ threadingTask threadMain;
+ void* threadArg;
+ long long threadId;
+ bool osThread;
+ ThreadHandle* interruptingThread;
+ int numAttached;
+ ThreadHandle* next;
+ ThreadHandle* joiners;
+ MonitorHandle* monitor;
+ };
+
+ struct MonitorHandle {
+ char* name;
+ decaf_mutex_t mutex;
+ decaf_mutex_t lock;
+ unsigned int count;
+ ThreadHandle* owner;
+ ThreadHandle* waiting;
+ ThreadHandle* blocking;
+ bool initialized;
+ MonitorHandle* next;
+ };
+
+ class CompletionCondition {
+ public:
+
+ virtual ~CompletionCondition() {}
+
+ /**
+ * Called from timed wait condition methods to indicate if the timeout
+ * has occurred, allows this method to take other actions based on the
+ * timeout having occurred or not.
+ *
+ * By default this method just defers to the simple operator() method.
+ *
+ * @param timedOut
+ * Indicates that the calling wait condition timed out or not.
+ */
+ virtual bool operator()(bool timedOut DECAF_UNUSED) {
+ return this->operator()();
+ }
+
+ /**
+ * Called from non-timed wait conditions to determine if the condition
+ * necessary to complete the wait has occurred or not.
+ */
+ virtual bool operator()() {
+ return false;
+ }
+
+ };
+
+}}}}
+
+#endif /* _DECAF_INTERNAL_UTIL_CONCURRENT_UNIX_THREADTYPES_H_ */
Propchange: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/ThreadingTypes.h
------------------------------------------------------------------------------
svn:eol-style = native
Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/PlatformDefs.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/PlatformDefs.h?rev=1100383&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/PlatformDefs.h (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/PlatformDefs.h Fri May 6 21:22:39 2011
@@ -0,0 +1,64 @@
+/*
+ * 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_UNIX_PLATFORMDEFS_H_
+#define _DECAF_INTERNAL_UTIL_CONCURRENT_UNIX_PLATFORMDEFS_H_
+
+#include <decaf/util/Config.h>
+
+#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
+
+namespace decaf{
+namespace internal{
+namespace util{
+namespace concurrent{
+
+ typedef void* PLATFORM_THREAD_ENTRY_ARG;
+ #define PLATFORM_THREAD_RETURN() return 0;
+ #define PLATFORM_THREAD_CALLBACK_TYPE void*
+ #define PLATFORM_DEFAULT_STACK_SIZE 0x8000
+ #define PLATFORM_CALLING_CONV
+
+ typedef pthread_t decaf_thread_t;
+ typedef pthread_key_t decaf_tls_key;
+ typedef pthread_cond_t* decaf_condition_t;
+ typedef pthread_mutex_t* decaf_mutex_t;
+
+
+}}}}
+
+#endif /* _DECAF_INTERNAL_UTIL_CONCURRENT_UNIX_PLATFORMDEFS_H_ */
Propchange: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/PlatformDefs.h
------------------------------------------------------------------------------
svn:eol-style = native
Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/PlatformThread.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/PlatformThread.cpp?rev=1100383&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/PlatformThread.cpp (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/PlatformThread.cpp Fri May 6 21:22:39 2011
@@ -0,0 +1,412 @@
+/*
+ * 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/PlatformThread.h>
+
+#include <decaf/lang/exceptions/RuntimeException.h>
+#include <decaf/util/concurrent/TimeUnit.h>
+#include <memory>
+
+#if HAVE_ERRNO_H
+#include <errno.h>
+#endif
+#if HAVE_LIMITS_H
+#include <limits.h>
+#endif
+#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
+
+using namespace decaf;
+using namespace decaf::lang;
+using namespace decaf::lang::exceptions;
+using namespace decaf::util;
+using namespace decaf::util::concurrent;
+using namespace decaf::internal;
+using namespace decaf::internal::util;
+using namespace decaf::internal::util::concurrent;
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::createMutex(decaf_mutex_t* mutex) {
+
+ *mutex = new pthread_mutex_t;
+
+ if( pthread_mutex_init(*mutex, NULL) != 0 ) {
+ throw RuntimeException(
+ __FILE__, __LINE__, "Failed to create OS Mutex object." );
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::lockMutex(decaf_mutex_t mutex) {
+
+ if (pthread_mutex_lock(mutex) != 0) {
+ throw RuntimeException(
+ __FILE__, __LINE__, "Failed to Lock OS Mutex" );
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+bool PlatformThread::tryLockMutex(decaf_mutex_t mutex) {
+ return pthread_mutex_trylock(mutex) == 0 ? true : false;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::unlockMutex(decaf_mutex_t mutex) {
+
+ if (pthread_mutex_unlock(mutex) != 0) {
+ throw RuntimeException(
+ __FILE__, __LINE__, "Failed to unlock OS Mutex" );
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::destroyMutex(decaf_mutex_t mutex) {
+ pthread_mutex_destroy(mutex);
+ delete mutex;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::createCondition(decaf_condition_t* condition) {
+
+ *condition = new pthread_cond_t;
+
+ if (pthread_cond_init(*condition, NULL) != 0) {
+ throw RuntimeException(
+ __FILE__, __LINE__, "Failed to initialize OS Condition object.");
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::notify(decaf_condition_t condition) {
+ pthread_cond_signal(condition);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::notifyAll(decaf_condition_t condition) {
+ pthread_cond_broadcast(condition);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::waitOnCondition(decaf_condition_t condition, decaf_mutex_t mutex) {
+ pthread_cond_wait(condition, mutex);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+bool PlatformThread::waitOnCondition(decaf_condition_t condition, decaf_mutex_t mutex,
+ long long mills, int nanos) {
+
+ 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;
+
+ if (pthread_cond_timedwait(condition, mutex, &abstime) == ETIMEDOUT) {
+ return true;
+ }
+
+ return false;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::interruptibleWaitOnCondition(decaf_condition_t condition, decaf_mutex_t mutex,
+ CompletionCondition& complete) {
+
+ do {
+
+ pthread_cond_wait(condition, mutex);
+
+ if (complete()) {
+ break;
+ }
+
+ } while(true);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+bool PlatformThread::interruptibleWaitOnCondition(decaf_condition_t condition, decaf_mutex_t mutex,
+ long long mills, int nanos, CompletionCondition& complete) {
+
+ 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;
+
+ bool result = false;
+
+ do {
+
+ if (pthread_cond_timedwait(condition, mutex, &abstime) == ETIMEDOUT) {
+
+ // interruption events take precedence over timeout.
+ if (complete(true)) {
+ break;
+ }
+
+ result = true;
+ break;
+ }
+
+ // check if spurious wake or intentional.
+ if (complete(false)) {
+ break;
+ }
+
+ } while(true);
+
+ return result;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::destroyCondition(decaf_condition_t condition) {
+ pthread_cond_destroy(condition);
+ delete condition;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::initPriorityMapping(int maxPriority, std::vector<int>& mapping) {
+
+ int max = sched_get_priority_max(SCHED_OTHER);
+ int min = sched_get_priority_min(SCHED_OTHER);
+
+ if (max == min) {
+ // get this thread's priority and use that for all threads.
+ struct sched_param schedParam;
+ int currPolicy;
+ pthread_getschedparam(pthread_self(), &currPolicy, &schedParam);
+ max = schedParam.sched_priority;
+ min = max;
+ }
+
+ mapping.clear();
+ mapping.resize(maxPriority + 1);
+
+ int tmpmax = max * 1024;
+ int tmpmin = min * 1024;
+ int mid = (tmpmin + tmpmax) / 2;
+ int midrange = maxPriority / 2;
+
+ mapping[0] = min;
+
+ int delta = (mid - tmpmin) / midrange;
+ for(int i = 1; i < midrange; i++) {
+ mapping[midrange - i] = (mid - delta * i) / 1024;
+ }
+
+ int tailcount = maxPriority - midrange;
+ delta = (tmpmax - mid) / tailcount;
+
+ for(int i = 0; i < tailcount; i++) {
+ mapping[midrange + i] = (mid + delta * i) / 1024;
+ }
+
+ mapping[maxPriority] = max;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::createNewThread(decaf_thread_t* handle, threadMainMethod threadMain, void* threadArg,
+ int priority, long long stackSize, long long* threadId) {
+
+ pthread_attr_t attributes;
+ struct sched_param schedData;
+
+ pthread_attr_init( &attributes );
+ pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_JOINABLE);
+
+ schedData.sched_priority = priority;
+ if (pthread_attr_setschedparam(&attributes, &schedData) != 0) {
+ throw RuntimeException(__FILE__, __LINE__,
+ "Failed to set new Therad priority to value: %d", schedData.sched_priority);
+ }
+
+ if (stackSize != -1) {
+
+ #ifdef LINUX
+ if (stackSize < PTHREAD_STACK_MIN) {
+ stackSize = PTHREAD_STACK_MIN;
+ }
+ #endif
+
+ if (pthread_attr_setstacksize(&attributes, (size_t)stackSize) == EINVAL) {
+ throw RuntimeException(
+ __FILE__, __LINE__,
+ "Failed to create new Thread due to invalid stack size setting: %d.",
+ stackSize );
+ }
+ }
+
+ int result = pthread_create(handle, &attributes, threadMain, threadArg);
+
+ *threadId = (long long)(&handle);
+
+ pthread_attr_destroy( &attributes );
+
+ if( result != 0 ) {
+ throw RuntimeException(
+ __FILE__, __LINE__, "Failed to create new Thread." );
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::detachThread(decaf_thread_t handle) {
+ pthread_detach(handle);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::joinThread(decaf_thread_t handle) {
+ void* theReturn = 0;
+ pthread_join(handle, &theReturn);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::exitThread() {
+ int result = 0;
+ pthread_exit(&result);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+decaf_thread_t PlatformThread::getCurrentThread() {
+ return pthread_self();
+}
+
+////////////////////////////////////////////////////////////////////////////////
+long long PlatformThread::getCurrentThreadId() {
+
+ // pthread_t can be an int or struct or who knows so we use the address
+ // of the value returned which seems to remain fixed.
+ return (long long)&(pthread_self);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+int PlatformThread::getPriority(decaf_thread_t thread) {
+ struct sched_param schedParam;
+ int policy = SCHED_OTHER;
+ pthread_getschedparam(thread, &policy, &schedParam);
+ return schedParam.sched_priority;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::setPriority(decaf_thread_t thread, int priority) {
+ struct sched_param schedParam;
+ schedParam.sched_priority = priority;
+ pthread_setschedparam(thread, SCHED_OTHER, &schedParam);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+long long PlatformThread::getStackSize(decaf_thread_t thread DECAF_UNUSED) {
+
+ pthread_attr_t attributes;
+ pthread_attr_init( &attributes );
+ size_t stackSize;
+
+ if (pthread_attr_getstacksize(&attributes, &stackSize) == EINVAL) {
+ pthread_attr_destroy( &attributes );
+ throw RuntimeException(
+ __FILE__, __LINE__,
+ "Failed to get stack size setting.");
+ }
+
+ pthread_attr_destroy( &attributes );
+
+ return (long long)stackSize;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::setStackSize(decaf_thread_t thread DECAF_UNUSED, long long stackSize) {
+
+ pthread_attr_t attributes;
+ pthread_attr_init( &attributes );
+
+#ifdef LINUX
+ if (stackSize < PTHREAD_STACK_MIN) {
+ stackSize = PTHREAD_STACK_MIN;
+ }
+#endif
+
+ if (pthread_attr_setstacksize(&attributes, (size_t)stackSize) == EINVAL) {
+ pthread_attr_destroy( &attributes );
+ throw RuntimeException(
+ __FILE__, __LINE__,
+ "Failed to set stack size setting: %d.", stackSize);
+ }
+
+ pthread_attr_destroy( &attributes );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::yeild() {
+
+ #ifdef HAVE_PTHREAD_YIELD
+ pthread_yield();
+ #else
+ #ifdef HAVE_SCHED_YIELD
+ sched_yield();
+ #endif
+ #endif
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::createTlsKey(decaf_tls_key* tlsKey) {
+ pthread_key_create(tlsKey, NULL);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::destroyTlsKey(decaf_tls_key tlsKey) {
+ pthread_key_delete(tlsKey);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void* PlatformThread::getTlsValue(decaf_tls_key tlsKey) {
+ return pthread_getspecific(tlsKey);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::setTlsValue(decaf_tls_key tlsKey, void* value) {
+ pthread_setspecific(tlsKey, value);
+}
Propchange: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/unix/PlatformThread.cpp
------------------------------------------------------------------------------
svn:eol-style = native
Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/PlatformDefs.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/PlatformDefs.h?rev=1100383&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/PlatformDefs.h (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/PlatformDefs.h Fri May 6 21:22:39 2011
@@ -0,0 +1,63 @@
+/*
+ * 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_WINDOWS_PLATFORMDEFS_H_
+#define _DECAF_INTERNAL_UTIL_CONCURRENT_WINDOWS_PLATFORMDEFS_H_
+
+#include <decaf/util/Config.h>
+
+#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
+#ifdef HAVE_PROCESS_H
+#include <process.h>
+#endif
+
+namespace decaf{
+namespace internal{
+namespace util{
+namespace concurrent{
+
+ typedef void* PLATFORM_THREAD_ENTRY_ARG;
+ #define PLATFORM_THREAD_RETURN() return 0;
+ #define PLATFORM_THREAD_CALLBACK_TYPE unsigned
+ #define PLATFORM_DEFAULT_STACK_SIZE 0x8000
+ #define PLATFORM_CALLING_CONV __stdcall
+
+ typedef HANDLE decaf_thread_t;
+ typedef DWORD decaf_tls_key;
+ typedef HANDLE decaf_condition_t;
+ typedef CRITICAL_SECTION decaf_mutex_t;
+
+}}}}
+
+#endif /* _DECAF_INTERNAL_UTIL_CONCURRENT_WINDOWS_PLATFORMDEFS_H_ */
Propchange: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/PlatformDefs.h
------------------------------------------------------------------------------
svn:eol-style = native
Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/PlatformThread.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/PlatformThread.cpp?rev=1100383&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/PlatformThread.cpp (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/PlatformThread.cpp Fri May 6 21:22:39 2011
@@ -0,0 +1,271 @@
+/*
+ * 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/PlatformThread.h>
+
+#include <decaf/lang/Thread.h>
+#include <decaf/lang/exceptions/RuntimeException.h>
+#include <decaf/lang/exceptions/NullPointerException.h>
+
+#include <memory>
+
+#ifdef HAVE_PROCESS_H
+#include <process.h>
+#endif
+
+using namespace decaf;
+using namespace decaf::lang;
+using namespace decaf::lang::exceptions;
+using namespace decaf::internal;
+using namespace decaf::internal::util;
+using namespace decaf::internal::util::concurrent;
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::createMutex(decaf_mutex_t* mutex) {
+ ::InitializeCriticalSection(mutex);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::lockMutex(decaf_mutex_t mutex) {
+ ::EnterCriticalSection(&mutex);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+bool PlatformThread::tryLockMutex(decaf_mutex_t mutex) {
+ return ::TryEnterCriticalSection(&mutex) > 0 ? true : false;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::unlockMutex(decaf_mutex_t mutex) {
+ ::LeaveCriticalSection(&mutex);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::destroyMutex(decaf_mutex_t mutex) {
+ ::DeleteCriticalSection(&mutex);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::createCondition(decaf_condition_t* condition) {
+
+ *condition = ::CreateEvent(NULL, TRUE, FALSE, NULL);
+
+ if (condition == NULL) {
+ throw RuntimeException(
+ __FILE__, __LINE__, "Failed to initialize OS Condition object.");
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::notify(decaf_condition_t condition) {
+ ::SetEvent(condition);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::notifyAll(decaf_condition_t condition) {
+ ::SetEvent(condition);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::waitOnCondition(decaf_condition_t condition, decaf_mutex_t mutex) {
+
+ ::ResetEvent(condition);
+ PlatformThread::unlockMutex(mutex);
+ ::WaitForSingleObject(condition, INFINITE);
+ PlatformThread::lockMutex(mutex);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+bool PlatformThread::waitOnCondition(decaf_condition_t condition, decaf_mutex_t mutex,
+ long long mills, int nanos) {
+
+ ::ResetEvent(condition);
+ PlatformThread::unlockMutex(mutex);
+ int result = ::WaitForSingleObject(condition, (DWORD)mills);
+ PlatformThread::lockMutex(mutex);
+
+ if (result == WAIT_TIMEOUT) {
+ return true;
+ }
+
+ return false;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::interruptibleWaitOnCondition(decaf_condition_t condition, decaf_mutex_t mutex,
+ CompletionCondition& complete) {
+
+ do {
+
+ ::ResetEvent(condition);
+ PlatformThread::unlockMutex(mutex);
+ ::WaitForSingleObject(condition, INFINITE);
+ PlatformThread::lockMutex(mutex);
+
+ if (complete()) {
+ break;
+ }
+
+ } while(true);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+bool PlatformThread::interruptibleWaitOnCondition(decaf_condition_t condition, decaf_mutex_t mutex,
+ long long mills, int nanos, CompletionCondition& complete) {
+
+ DWORD startTime = ::GetTickCount();
+ DWORD timeOut = (DWORD)mills;
+ DWORD initialTimeOut = (DWORD)mills;
+
+ bool result = false;
+
+ do {
+
+ ::ResetEvent(condition);
+ PlatformThread::unlockMutex(mutex);
+ int timedOut = ::WaitForSingleObject(condition, timeOut);
+ PlatformThread::lockMutex(mutex);
+
+ if (timedOut == WAIT_TIMEOUT) {
+
+ // interruption events take precedence over timeout.
+ if (complete(true)) {
+ break;
+ }
+
+ result = true;
+ break;
+ }
+
+ // check if spurious wake or intentional.
+ if (complete(false)) {
+ break;
+ }
+
+ timeOut = initialTimeOut - (::GetTickCount() - startTime);
+ if( timeOut < 0) {
+ timeOut = 0;
+ }
+
+ } while(true);
+
+ return result;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::destroyCondition(decaf_condition_t condition) {
+ ::CloseHandle(condition);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::initPriorityMapping(int maxPriority, std::vector<int>& mapping) {
+
+ mapping.clear();
+ mapping.resize(maxPriority + 1);
+
+ for (int i = 0; i <= maxPriority; ++i) {
+ mapping[i] = i;
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::createNewThread(decaf_thread_t* handle, threadMainMethod threadMain, void* threadArg,
+ int priority, long long stackSize, long long* threadId) {
+
+ unsigned int osThreadId = 0;
+
+ *handle = (HANDLE)_beginthreadex(
+ NULL, (DWORD)0, threadMain, threadArg, 0, &osThreadId );
+
+ if (*handle == 0) {
+ throw RuntimeException(
+ __FILE__, __LINE__, "Failed to create new Thread." );
+ }
+
+ *threadId = (long long)osThreadId;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::detachThread(decaf_thread_t handle) {
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::joinThread(decaf_thread_t handle) {
+ ::WaitForSingleObject(handle, INFINITE);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::exitThread() {
+ _endthreadex(0);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+decaf_thread_t PlatformThread::getCurrentThread() {
+ return ::GetCurrentThread();
+}
+
+////////////////////////////////////////////////////////////////////////////////
+long long PlatformThread::getCurrentThreadId() {
+ return ::GetCurrentThreadId();
+}
+
+////////////////////////////////////////////////////////////////////////////////
+int PlatformThread::getPriority(decaf_thread_t thread) {
+ return Thread::NORM_PRIORITY;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::setPriority(decaf_thread_t thread, int priority) {
+}
+
+////////////////////////////////////////////////////////////////////////////////
+long long PlatformThread::getStackSize(decaf_thread_t thread DECAF_UNUSED) {
+ return PLATFORM_DEFAULT_STACK_SIZE;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::setStackSize(decaf_thread_t thread DECAF_UNUSED, long long stackSize DECAF_UNUSED) {
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::yeild() {
+ SwitchToThread();
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::createTlsKey(decaf_tls_key* tlsKey) {
+ if (tlsKey == NULL) {
+ throw NullPointerException(__FILE__, __LINE__, "TLS Key pointer must not be NULL.");
+ }
+
+ *tlsKey = ::TlsAlloc();
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::destroyTlsKey(decaf_tls_key tlsKey) {
+ ::TlsFree(tlsKey);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void* PlatformThread::getTlsValue(decaf_tls_key tlsKey) {
+ return ::TlsGetValue(tlsKey);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void PlatformThread::setTlsValue(decaf_tls_key tlsKey, void* value) {
+ ::TlsSetValue(tlsKey, value);
+}
Propchange: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/windows/PlatformThread.cpp
------------------------------------------------------------------------------
svn:eol-style = native
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/System.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/System.cpp?rev=1100383&r1=1100382&r2=1100383&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/System.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/System.cpp Fri May 6 21:22:39 2011
@@ -69,6 +69,8 @@ namespace lang {
class SystemData {
public:
+ StlMap<string, string> cachedEnvValues;
+ AprPool aprPool;
Properties systemProperties;
public:
@@ -107,8 +109,7 @@ void System::shutdownSystem() {
////////////////////////////////////////////////////////////////////////////////
AprPool& System::getAprPool() {
- static AprPool aprPool;
- return aprPool;
+ return System::sys->aprPool;
}
////////////////////////////////////////////////////////////////////////////////
@@ -389,8 +390,7 @@ long long System::nanoTime() {
////////////////////////////////////////////////////////////////////////////////
const Map<string, string>& System::getenv() {
- static StlMap<string, string> values;
- values.clear();
+ System::sys->cachedEnvValues.clear();
StringTokenizer tokenizer( "" );
string key = "";
@@ -426,10 +426,10 @@ const Map<string, string>& System::geten
}
// Store the env var
- values.put( key, value );
+ System::sys->cachedEnvValues.put( key, value );
}
- return values;
+ return System::sys->cachedEnvValues;
}
#if defined(_WIN32)
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/Thread.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/Thread.cpp?rev=1100383&r1=1100382&r2=1100383&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/Thread.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/Thread.cpp Fri May 6 21:22:39 2011
@@ -22,6 +22,7 @@
#endif
#include <decaf/internal/DecafRuntime.h>
+#include <decaf/internal/util/concurrent/Threading.h>
#include <decaf/lang/Integer.h>
#include <decaf/lang/Long.h>
#include <decaf/lang/Math.h>
@@ -35,33 +36,10 @@
#include <vector>
-#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
-#ifdef HAVE_PROCESS_H
-#include <process.h>
-#endif
-
using namespace decaf;
using namespace decaf::internal;
+using namespace decaf::internal::util;
+using namespace decaf::internal::util::concurrent;
using namespace decaf::lang;
using namespace decaf::lang::exceptions;
using namespace decaf::util;
@@ -79,249 +57,60 @@ namespace lang{
public:
- decaf::util::concurrent::Mutex mutex;
Runnable* task;
-
- /**
- * The Platform Specific Thread Handle.
- */
- #ifdef HAVE_PTHREAD_H
- pthread_t handle;
- #else
- HANDLE handle;
- #endif
-
- volatile Thread::State state;
- int priority;
- bool interrupted;
- bool unparked;
- bool parked;
- std::string name;
+ ThreadHandle* handle;
Thread::UncaughtExceptionHandler* exHandler;
- Thread* parent;
static unsigned int id;
+ static Thread::UncaughtExceptionHandler* defaultHandler;
public:
- ThreadProperties( const std::string& name ) : mutex( name + "-mutex" ),
- task(NULL),
- handle(),
- state(Thread::NEW),
- priority(Thread::NORM_PRIORITY),
- interrupted(false),
- unparked(false),
- parked(false),
- name(name),
- exHandler(NULL),
- parent(NULL) {
- }
-
- ~ThreadProperties() {
- #ifndef HAVE_PTHREAD_H
- ::CloseHandle( handle );
- #endif
- }
-
- public:
+ ThreadProperties() : task(NULL), handle(NULL), exHandler(NULL) {}
- static void runCallback( ThreadProperties* properties ) {
-
- synchronized( &properties->mutex ) {
- properties->state = Thread::RUNNABLE;
- }
-
- // Invoke run on the task.
- try{
- properties->task->run();
- } catch( decaf::lang::Throwable& error ){
-
- if( properties->exHandler != NULL ) {
- properties->exHandler->uncaughtException( properties->parent, error );
- }
- } catch( std::exception& stdEx ) {
-
- RuntimeException error( __FILE__, __LINE__, stdEx.what() );
-
- if( properties->exHandler != NULL ) {
- properties->exHandler->uncaughtException( properties->parent, error );
- }
- } catch( ... ) {
-
- RuntimeException error(
- __FILE__, __LINE__,
- "Uncaught exception bubbled up to Thread::run, Thread Terminating.");
-
- if( properties->exHandler != NULL ) {
- properties->exHandler->uncaughtException( properties->parent, error );
- }
- }
-
- synchronized( &properties->mutex ) {
-
- // Indicate we are done.
- properties->state = Thread::TERMINATED;
-
- // Signal any joined threads.
- properties->mutex.notifyAll();
- }
- }
};
}}
////////////////////////////////////////////////////////////////////////////////
-namespace{
-
- Thread* mainThread = NULL;
- std::vector<Thread*> foreignThreads;
-
- #ifdef HAVE_PTHREAD_H
-
- pthread_key_t currentThreadKey;
-
- void* threadWorker( void* arg ) {
-
- ThreadProperties* properties = (ThreadProperties*)arg;
-
- pthread_setspecific( currentThreadKey, (void*)properties->parent );
-
- ThreadProperties::runCallback( properties );
-
- pthread_setspecific( currentThreadKey, NULL );
-
- //pthread_detach( properties->handle );
- pthread_exit(NULL);
-
- properties->state = Thread::TERMINATED;
-
- return NULL;
- }
- #else
-
- DWORD currentThreadKey;
-
- unsigned int __stdcall threadWorker( void* arg ) {
- ThreadProperties* properties = (ThreadProperties*)arg;
-
- ::TlsSetValue( currentThreadKey, (void*)properties->parent );
-
- ThreadProperties::runCallback( properties );
-
- ::TlsSetValue( currentThreadKey, NULL );
-
- properties->state = Thread::TERMINATED;
-
- #ifndef _WIN32_WCE
- _endthreadex( 0 );
- #else
- ExitThread( 0 );
- #endif
-
- return 0;
- }
-
- #endif
-}
-
-////////////////////////////////////////////////////////////////////////////////
unsigned int ThreadProperties::id = 0;
+Thread::UncaughtExceptionHandler* ThreadProperties::defaultHandler = NULL;
////////////////////////////////////////////////////////////////////////////////
-void Thread::initThreading() {
+Thread::Thread(ThreadHandle* handle) : Runnable(), properties(NULL) {
- // We mark the thread where Decaf's Init routine is called from as our Main Thread.
- mainThread = Thread::createForeignThreadInstance( "Main Thread" );
-
- // Initialize the Executors static data for use in ExecutorService classes.
- Executors::initialize();
+ this->properties = new ThreadProperties();
+ this->properties->handle = handle;
}
////////////////////////////////////////////////////////////////////////////////
-void Thread::shutdownThreading() {
-
- // First shutdown the Executors static data to remove dependencies on Threading.
- Executors::shutdown();
-
- // Clear the Main Thread instance pointer, this indicates we are Shutdown.
- mainThread = NULL;
-
- // Destroy any Foreign Thread Facades that were created during runtime.
- std::vector<Thread*>::iterator iter = foreignThreads.begin();
- for( ; iter != foreignThreads.end(); ++iter ) {
- delete *iter;
- }
- foreignThreads.clear();
-
- #ifdef HAVE_PTHREAD_H
-
- // Destroy the current Thread key now, no longer needed.
- pthread_key_delete( currentThreadKey );
-
- #else
-
- // Destroy our TLS resources before we shutdown.
- ::TlsFree( currentThreadKey );
-
- #endif
+Thread::Thread() : Runnable(), properties(NULL) {
+ this->initializeSelf( NULL, "", -1 );
}
////////////////////////////////////////////////////////////////////////////////
-Thread* Thread::createForeignThreadInstance( const std::string& name ) {
-
- Thread* foreignThread = new Thread( name );
-
- foreignThread->properties->state = Thread::RUNNABLE;
- foreignThread->properties->priority = Thread::NORM_PRIORITY;
- foreignThread->properties->parent = NULL;
-
- #ifdef HAVE_PTHREAD_H
-
- foreignThread->properties->handle = pthread_self();
-
- // Create the Key used to store the Current Thread data
- pthread_key_create( ¤tThreadKey, NULL );
- pthread_setspecific( currentThreadKey, foreignThread );
-
- #else
-
- foreignThread->properties->handle = ::GetCurrentThread();
-
- // Create the key used to store the Current Thread data
- currentThreadKey = ::TlsAlloc();
- ::TlsSetValue( currentThreadKey, foreignThread );
-
- #endif
-
- // Store the Facade for later deletion
- foreignThreads.push_back( foreignThread );
-
- return foreignThread;
-}
-
-////////////////////////////////////////////////////////////////////////////////
-Thread::Thread() : Runnable(), properties( NULL ) {
- this->initialize( this, "" );
+Thread::Thread( Runnable* task ) : Runnable(), properties(NULL) {
+ this->initializeSelf( task, "", -1 );
}
////////////////////////////////////////////////////////////////////////////////
-Thread::Thread( Runnable* task ): Runnable(), properties( NULL ) {
- this->initialize( task, "" );
+Thread::Thread( const std::string& name ) : Runnable(), properties(NULL) {
+ this->initializeSelf( NULL, name, -1 );
}
////////////////////////////////////////////////////////////////////////////////
-Thread::Thread( const std::string& name ): Runnable(), properties( NULL ) {
- this->initialize( this, name );
+Thread::Thread( Runnable* task, const std::string& name ) : Runnable(), properties( NULL ) {
+ this->initializeSelf( task, name, -1 );
}
////////////////////////////////////////////////////////////////////////////////
-Thread::Thread( Runnable* task, const std::string& name ): Runnable(), properties( NULL ) {
- this->initialize( task, name );
+Thread::Thread( Runnable* task, const std::string& name, long long stackSize ) : Runnable(), properties( NULL ) {
+ this->initializeSelf( task, name, stackSize );
}
////////////////////////////////////////////////////////////////////////////////
-void Thread::initialize( Runnable* task, const std::string& name ) {
+void Thread::initializeSelf( Runnable* task, const std::string& name, long long stackSize ) {
std::string threadName = name;
@@ -331,29 +120,16 @@ void Thread::initialize( Runnable* task,
threadName = name;
}
- this->properties = new ThreadProperties( threadName );
-
- this->properties->state = Thread::NEW;
- this->properties->priority = Thread::NORM_PRIORITY;
+ this->properties = new ThreadProperties();
+ this->properties->handle =
+ Threading::createNewThread(this, threadName.c_str(), stackSize);
this->properties->task = task;
- this->properties->exHandler = NULL;
- this->properties->parent = this;
}
////////////////////////////////////////////////////////////////////////////////
Thread::~Thread() {
try{
-
- // Don't join foreign threads on destruction, we don't control them.
- if( properties->parent != NULL ) {
- #ifdef HAVE_PTHREAD_H
- void* theReturn = 0;
- pthread_join( properties->handle, &theReturn );
- #else
- ::WaitForSingleObject( properties->handle, INFINITE );
- #endif
- }
-
+ Threading::destroyThread(this->properties->handle);
delete this->properties;
}
DECAF_CATCH_NOTHROW( Exception )
@@ -362,7 +138,10 @@ Thread::~Thread() {
////////////////////////////////////////////////////////////////////////////////
void Thread::run() {
- // No work to do as yet.
+
+ if (this->properties->task != NULL) {
+ this->properties->task->run();
+ }
}
////////////////////////////////////////////////////////////////////////////////
@@ -370,64 +149,13 @@ void Thread::start() {
try {
- if( this->properties->state > Thread::NEW ) {
+ if( Threading::getThreadState(this->properties->handle) > Thread::NEW ) {
throw IllegalThreadStateException(
__FILE__, __LINE__,
"Thread::start - Thread already started");
}
- // The lock here acts as a gate to the newly created thread, it won't begin execution
- // until this method completes which ensures that if the main thread calls start and
- // then join but the child thread has already exited there won't be a deadlock on the
- // wait handle. Also the new thread won't be able to update any internal data until
- // creation is complete which ensure that there is no state corruption.
- synchronized( &this->properties->mutex ) {
-
- #ifdef HAVE_PTHREAD_H
-
- pthread_attr_t attributes;
-
- pthread_attr_init( &attributes );
- pthread_attr_setdetachstate( &attributes, PTHREAD_CREATE_JOINABLE );
-
- int result = pthread_create( &( properties->handle ), &attributes, threadWorker, properties );
-
- pthread_attr_destroy( &attributes );
-
- if( result != 0 ) {
- throw RuntimeException(
- __FILE__, __LINE__, "Failed to create new Thread." );
- }
-
- #else
-
- unsigned int threadId = 0;
-
- #ifndef _WIN32_WCE
-
- properties->handle = (HANDLE)_beginthreadex(
- NULL, (DWORD)0, threadWorker, properties, 0, &threadId );
-
- #else
-
- properties->hanlde = CreateThread( NULL, 0, threadWorker, properties.get(), 0, &threadId ) );
-
- if( properties->handle == 0 ) {
- throw RuntimeException(
- __FILE__, __LINE__, "Failed to create new Thread." );
- }
-
- #endif
-
- #endif
-
- // Only try and set this if its not the default value.
- if( properties->priority != Thread::NORM_PRIORITY ) {
- setPriority( properties->priority );
- }
-
- properties->state = Thread::RUNNABLE;
- }
+ Threading::start(this->properties->handle);
}
DECAF_CATCH_RETHROW( IllegalThreadStateException )
DECAF_CATCH_RETHROW( RuntimeException )
@@ -437,24 +165,11 @@ void Thread::start() {
////////////////////////////////////////////////////////////////////////////////
void Thread::join() {
-
- if( this->properties->state == Thread::TERMINATED ) {
- return;
- }
-
- synchronized( &this->properties->mutex ) {
-
- if( this->properties->state < Thread::RUNNABLE ||
- this->properties->state == Thread::TERMINATED ) {
- return;
- }
-
- this->properties->mutex.wait();
- }
+ Threading::join(this->properties->handle, 0, 0);
}
////////////////////////////////////////////////////////////////////////////////
-void Thread::join( long long millisecs ) {
+void Thread::join(long long millisecs) {
if( millisecs < 0 ) {
throw IllegalArgumentException(
@@ -462,11 +177,11 @@ void Thread::join( long long millisecs )
"Thread::join( millisecs ) - Value given {%d} is less than 0", millisecs );
}
- this->join( millisecs, 0 );
+ Threading::join(this->properties->handle, millisecs, 0);
}
////////////////////////////////////////////////////////////////////////////////
-void Thread::join( long long millisecs, unsigned int nanos ) {
+void Thread::join(long long millisecs, int nanos) {
if( millisecs < 0 ) {
throw IllegalArgumentException(
@@ -474,35 +189,22 @@ void Thread::join( long long millisecs,
"Thread::join( millisecs, nanos ) - Value given {%d} is less than 0", millisecs );
}
- if( nanos > 999999 ) {
+ if( nanos < 0 || nanos > 999999 ) {
throw IllegalArgumentException(
__FILE__, __LINE__,
"Thread::join( millisecs, nanos ) - Nanoseconds must be in range [0...999999]" );
}
- if( this->properties->state == Thread::TERMINATED ) {
- return;
- }
-
- synchronized( &this->properties->mutex ) {
-
- if( this->properties->state < Thread::RUNNABLE ||
- this->properties->state == Thread::TERMINATED ) {
- return;
- }
-
- this->properties->mutex.wait( millisecs, nanos );
- }
+ Threading::join(this->properties->handle, millisecs, nanos);
}
////////////////////////////////////////////////////////////////////////////////
-void Thread::sleep( long long millisecs ) {
-
- Thread::sleep( millisecs, 0 );
+void Thread::sleep(long long millisecs) {
+ Threading::sleep(millisecs, 0);
}
////////////////////////////////////////////////////////////////////////////////
-void Thread::sleep( long long millisecs, unsigned int nanos ) {
+void Thread::sleep(long long millisecs, int nanos) {
if( millisecs < 0 ) {
throw IllegalArgumentException(
@@ -510,61 +212,34 @@ void Thread::sleep( long long millisecs,
"Thread::sleep( millisecs, nanos ) - Value given {%d} is less than 0", millisecs );
}
- if( nanos > 999999 ) {
+ if( nanos < 0 || nanos > 999999 ) {
throw IllegalArgumentException(
__FILE__, __LINE__,
"Thread::sleep( millisecs, nanos ) - Nanoseconds must be in range [0...999999]" );
}
- #ifdef HAVE_PTHREAD_H
- long long usecs = TimeUnit::MILLISECONDS.toMicros( millisecs ) +
- TimeUnit::NANOSECONDS.toMicros( nanos );
-
- struct timeval tv;
- tv.tv_usec = usecs % 1000000;
- tv.tv_sec = usecs / 1000000;
- select( 0, NULL, NULL, NULL, &tv );
- #else
- ::Sleep( (DWORD)millisecs );
- #endif
+
+ Threading::sleep(millisecs, nanos);
}
////////////////////////////////////////////////////////////////////////////////
void Thread::yield() {
-
- #ifdef HAVE_PTHREAD_H
- #ifdef HAVE_PTHREAD_YIELD
- pthread_yield();
- #else
- #ifdef HAVE_SCHED_YIELD
- sched_yield();
- #endif
- #endif
- #else
- #ifndef _WIN32_WCE
- SwitchToThread();
- #endif
- #endif
+ Threading::yeild();
}
////////////////////////////////////////////////////////////////////////////////
-long long Thread::getId() {
-
- #ifdef HAVE_PTHREAD_H
- return (long long)pthread_self();
- #else
- return (long long)::GetCurrentThreadId();
- #endif
+long long Thread::getId() const {
+ return Threading::getThreadId(this->properties->handle);
}
////////////////////////////////////////////////////////////////////////////////
void Thread::setName( const std::string& name ) {
- this->properties->name = name;
+ Threading::setThreadName(this->properties->handle, name.c_str());
}
////////////////////////////////////////////////////////////////////////////////
std::string Thread::getName() const {
- return this->properties->name;
+ return Threading::getThreadName(this->properties->handle);
}
////////////////////////////////////////////////////////////////////////////////
@@ -576,150 +251,73 @@ void Thread::setPriority( int value ) {
"Thread::setPriority - Specified value {%d} is out of range", value );
}
- this->properties->priority = value;
-
- //ThreadImpl::setPriority( this->properties->threadHandle.get(), value );
+ Threading::setThreadPriority(this->properties->handle, value);
}
////////////////////////////////////////////////////////////////////////////////
int Thread::getPriority() const {
- return this->properties->priority;
+ return Threading::getThreadPriority(this->properties->handle);
}
////////////////////////////////////////////////////////////////////////////////
-bool Thread::isDaemon() const {
- return false;
+void Thread::setUncaughtExceptionHandler( UncaughtExceptionHandler* handler ) {
+ this->properties->exHandler = handler;
}
////////////////////////////////////////////////////////////////////////////////
-void Thread::setDaemon(bool value DECAF_UNUSED) {
-
- if(this->properties->state > Thread::NEW) {
- throw IllegalThreadStateException(__FILE__, __LINE__, "Thread is already active.");
- }
-
- // TODO - Set thread to detached or joinable as indicated by the value arg.
+Thread::UncaughtExceptionHandler* Thread::getUncaughtExceptionHandler() const {
+ return this->properties->exHandler;
}
////////////////////////////////////////////////////////////////////////////////
-void Thread::setUncaughtExceptionHandler( UncaughtExceptionHandler* handler ) {
- this->properties->exHandler = handler;
+Thread::UncaughtExceptionHandler* Thread::getDefaultUncaughtExceptionHandler() {
+ return ThreadProperties::defaultHandler;
}
////////////////////////////////////////////////////////////////////////////////
-const Thread::UncaughtExceptionHandler* Thread::getUncaughtExceptionHandler() const {
- return this->properties->exHandler;
+void Thread::setDefaultUncaughtExceptionHandler( Thread::UncaughtExceptionHandler* handler ) {
+ ThreadProperties::defaultHandler = handler;
}
////////////////////////////////////////////////////////////////////////////////
std::string Thread::toString() const {
- return this->properties->name + ": Priority=" +
- Integer::toString( this->properties->priority ) + ", ThreadID=" +
- Long::toString( Thread::getId() );
+ return std::string(Threading::getThreadName(this->properties->handle)) + ": Priority=" +
+ Integer::toString(Threading::getThreadPriority(this->properties->handle)) +
+ ", ThreadID=" + Long::toString( Threading::getThreadId(this->properties->handle));
}
////////////////////////////////////////////////////////////////////////////////
bool Thread::isAlive() const {
- return this->properties->state != Thread::NEW &&
- this->properties->state != Thread::TERMINATED;
+ return Threading::isThreadAlive(this->properties->handle);
}
////////////////////////////////////////////////////////////////////////////////
Thread::State Thread::getState() const {
- return this->properties->state;
+ return Threading::getThreadState(this->properties->handle);
}
////////////////////////////////////////////////////////////////////////////////
Thread* Thread::currentThread() {
-
- if( mainThread == NULL ) {
- throw RuntimeException(
- __FILE__, __LINE__, "The Decaf Threading API is in a Shutdown State." );
- }
-
- void* result = NULL;
-
- #ifdef HAVE_PTHREAD_H
-
- result = pthread_getspecific( currentThreadKey );
-
- #else
-
- result = ::TlsGetValue( currentThreadKey );
-
- #endif
-
- // This is a foreign Thread (Not Created By Decaf) lets create a stand in Thread
- // instance so that other threads in Decaf can join it and wait on it.
- if( result == NULL ) {
- result = Thread::createForeignThreadInstance(
- std::string( "ForeignThread-" ) + Long::toString( Thread::getId() ) );
- }
-
- return (Thread*)result;
+ return Threading::getCurrentThread();
}
////////////////////////////////////////////////////////////////////////////////
-void Thread::park( Thread* thread ) {
-
- if( thread == NULL ) {
- throw NullPointerException(
- __FILE__, __LINE__, "Null Thread Pointer Passed." );
- }
-
- Thread::park( thread, 0LL, 0LL );
+void Thread::interrupt() {
+ Threading::interrupt(this->properties->handle);
}
////////////////////////////////////////////////////////////////////////////////
-void Thread::park( Thread* thread, long long mills, long long nanos ) {
-
- if( thread == NULL ) {
- throw NullPointerException(
- __FILE__, __LINE__, "Null Thread Pointer Passed." );
- }
-
- synchronized( &thread->properties->mutex ) {
-
- if( thread->properties->unparked ) {
- thread->properties->unparked = false;
- return;
- }
-
- thread->properties->parked = true;
-
- if( mills == 0 && nanos == 0 ) {
- thread->properties->mutex.wait();
- } else {
- thread->properties->mutex.wait( mills, (int)nanos );
- }
-
- thread->properties->parked = false;
-
- // consume the unparked token.
- thread->properties->unparked = false;
- }
+bool Thread::interrupted() {
+ return Threading::interrupted();
}
////////////////////////////////////////////////////////////////////////////////
-void Thread::unpark( Thread* thread ) {
-
- if( thread == NULL ) {
- throw NullPointerException(
- __FILE__, __LINE__, "Null Thread Pointer Passed." );
- }
-
- synchronized( &thread->properties->mutex ) {
+bool Thread::isInterrupted() const {
+ return Threading::isInterrupted(this->properties->handle, false);
+}
- // Set the unparked token, if the thread is parked it will consume
- // it when it resumes, otherwise the next call to park will consume
- // it without needing to actually wait.
- thread->properties->unparked = true;
-
- // If the thread is actually parked then we send it a signal so
- // that it will resume.
- if( thread->properties->parked ) {
- thread->properties->mutex.notifyAll();
- }
- }
+////////////////////////////////////////////////////////////////////////////////
+ThreadHandle* Thread::getHandle() const {
+ return this->properties->handle;
}
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/Thread.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/Thread.h?rev=1100383&r1=1100382&r2=1100383&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/Thread.h (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/Thread.h Fri May 6 21:22:39 2011
@@ -26,14 +26,14 @@
#include <decaf/util/Config.h>
namespace decaf{
+namespace internal{
namespace util{
namespace concurrent{
-namespace locks{
- class LockSupport;
+ class Threading;
+ struct ThreadHandle;
}}}
-namespace lang{
-
- class Runtime;
+namespace lang
+{
class ThreadGroup;
class ThreadProperties;
@@ -55,9 +55,7 @@ namespace lang{
*
* Each Thread has an integer priority that basically determines the amount
* of CPU time the Thread gets. It can be set using the {@link #setPriority(int)}
- * method. A Thread can also be made a daemon, which makes it run in the
- * background. The latter also affects VM termination behavior: the VM does not
- * terminate automatically as long as there are non-daemon threads running.
+ * method.
*
* @see decaf.lang.ThreadGroup
*
@@ -180,6 +178,27 @@ namespace lang{
*/
Thread( Runnable* task, const std::string& name );
+ /**
+ * Constructs a new Thread with the given target Runnable task and name. This constructor
+ * has the same effect as Thread( NULL, task, GIVEN_NAME ), where GIVEN_NAME is a
+ * newly generated name. When no name is given the name is automatically generated
+ * and are of the form "Thread-"+n, where n is an integer.
+ *
+ * The stack size option is platform independent and may have no effect on the newly
+ * created thread on some systems. If the value given is invalid on the system a
+ * RuntimeException is thrown, the stack size can be invalid if it is outside the
+ * allowed range or doesn't match the size of the system page size on some system.
+ *
+ * @param task
+ * The Runnable that this thread manages, if the task is NULL the Thread's
+ * run method is used instead.
+ * @param name
+ * The name to assign to this Thread.
+ * @param stackSize
+ * The size of the newly allocated thread's stack.
+ */
+ Thread( Runnable* task, const std::string& name, long long stackSize );
+
virtual ~Thread();
/**
@@ -225,7 +244,7 @@ namespace lang{
* The interrupted status of the current thread is cleared when this
* exception is thrown.
*/
- virtual void join( long long millisecs, unsigned int nanos );
+ virtual void join( long long millisecs, int nanos );
/**
* Default implementation of the run method - does nothing.
@@ -233,6 +252,14 @@ namespace lang{
virtual void run();
/**
+ * Obtains the Thread Id of the current thread, this value is OS specific but is
+ * guaranteed not to change for the lifetime of this thread.
+ *
+ * @return Thread Id of this Thread instance.
+ */
+ long long getId() const;
+
+ /**
* Returns the Thread's assigned name.
* @returns the Name of the Thread.
*/
@@ -254,7 +281,7 @@ namespace lang{
/**
* Sets the current Thread's priority to the newly specified value. The given value
- * must be within the rane Thread::MIN_PRIORITY and Thread::MAX_PRIORITY.
+ * must be within the range Thread::MIN_PRIORITY and Thread::MAX_PRIORITY.
*
* @param value the new priority value to assign to this Thread.
*
@@ -263,30 +290,11 @@ namespace lang{
void setPriority( int value );
/**
- * Sets if the given Thread is a Daemon Thread or not. Daemon threads cannot be
- * joined and its resource are automatically reclaimed when it terminates.
- *
- * @param value
- * Boolean indicating if this thread should be a daemon thread or not.
- *
- * @throws IllegalThreadStateException if the thread is already active.
- */
- void setDaemon(bool value);
-
- /**
- * Returns whether this thread is a daemon thread or not, if true this thread cannot
- * be joined.
- *
- * @return true if the thread is a daemon thread.
- */
- bool isDaemon() const;
-
- /**
* Set the handler invoked when this thread abruptly terminates due to an uncaught exception.
*
* @returns a pointer to the set UncaughtExceptionHandler.
*/
- const UncaughtExceptionHandler* getUncaughtExceptionHandler() const;
+ UncaughtExceptionHandler* getUncaughtExceptionHandler() const;
/**
* Set the handler invoked when this thread abruptly terminates due to an uncaught exception.
@@ -318,6 +326,27 @@ namespace lang{
*/
Thread::State getState() const;
+ /**
+ * Interrupts the Thread if it is blocked and in an interruptible state.
+ *
+ * When the thread is in one of its own join or sleep methods or blocked by a call to
+ * a monitor or mutex wait call it will clear its interrupted flag and and an
+ * InterruptedException will be thrown.
+ *
+ * In other cases the thread's interrupted status will be set and an instance of
+ * an InterruptedException may be thrown.
+ *
+ * If the thread is not alive when this method is called there is no affect.
+ */
+ void interrupt();
+
+ /**
+ * Returns but does not clear the state of this Thread's interrupted flag.
+ *
+ * @returns true if the thread was interrupted, false otherwise.
+ */
+ bool isInterrupted() const;
+
public:
/**
@@ -349,7 +378,7 @@ namespace lang{
* or the milliseconds paramter is negative.
* @throws InterruptedException if the Thread was interrupted while sleeping.
*/
- static void sleep( long long millisecs, unsigned int nanos );
+ static void sleep( long long millisecs, int nanos );
/**
* Causes the currently executing thread object to temporarily pause
@@ -358,48 +387,58 @@ namespace lang{
static void yield();
/**
- * Obtains the Thread Id of the current thread
- * @return Thread Id
- */
- static long long getId();
-
- /**
* Returns a pointer to the currently executing thread object.
*
* @return Pointer to the Thread object representing the currently running Thread.
*/
static Thread* currentThread();
- private:
+ /**
+ * Returns whether the thread has been interrupted and clears the interrupted state
+ * such that a subsequent call will return false unless an interrupt occurs between
+ * the two calls.
+ *
+ * @returns true if the thread was interrupted, false otherwise.
+ */
+ static bool interrupted();
- // Initialize the Threads internal state
- void initialize( Runnable* task, const std::string& name );
+ /**
+ * Set the default handler invoked when a thread abruptly terminates due to an uncaught
+ * exception, this handler is used only if there is no other handler defined for the
+ * Thread. This method will return NULL if no handler has ever been set, or the handler
+ * is cleared via a call to the setDefaultUncaughtExceptionHandler method will NULL as
+ * the value of the handler argument.
+ *
+ * @returns a pointer to the default UncaughtExceptionHandler for all Threads.
+ */
+ static UncaughtExceptionHandler* getDefaultUncaughtExceptionHandler();
- // Create a Thread Facade for threads not created by the Decaf Library.
- static Thread* createForeignThreadInstance( const std::string& name );
+ /**
+ * Set the default handler invoked when a thread abruptly terminates due to an uncaught
+ * exception,
+ *
+ * @param handler
+ * The UncaightExceptionHandler to invoke when a Thread terminates due
+ * to an uncaught exception, passing NULL clears this value.
+ */
+ static void setDefaultUncaughtExceptionHandler( UncaughtExceptionHandler* handler );
- // Called by the Decaf Runtime at startup to allow the Platform Threading
- // code to initialize any necessary Threading constructs needed to support
- // the features of this class.
- static void initThreading();
+ private:
- // Called by the Decf Runtime at Shutdown to allow the Platform Threading
- // code to return any resources that were allocated at startup for the
- // Threading library.
- static void shutdownThreading();
+ // Initialize the Threads internal state
+ void initializeSelf( Runnable* task, const std::string& name, long long stackSize );
- // Called from LockSupport to Park a Thread (suspend it from execution)
- static void park( Thread* thread );
+ // Creates a Thread instance for a ThreadProperties pointer, used for
+ // wrapping OS threads
+ Thread(decaf::internal::util::concurrent::ThreadHandle* osThread);
- // Called from LockSupport to Park a Thread (suspend it from execution)
- static void park( Thread* thread, long long mills, long long nanos );
+ private:
- // Called from LockSupport to UnPark a Thread (resume its execution status).
- static void unpark( Thread* thread );
+ // Allows the Threading class to get this thread objects handle.
+ decaf::internal::util::concurrent::ThreadHandle* getHandle() const;
- // Allow some Decaf Classes greater access to the Threading model.
- friend class decaf::util::concurrent::locks::LockSupport;
- friend class decaf::lang::Runtime;
+ // Allow some Decaf Classes greater access to the Thread class.
+ friend class decaf::internal::util::concurrent::Threading;
};
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/CountDownLatch.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/CountDownLatch.cpp?rev=1100383&r1=1100382&r2=1100383&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/CountDownLatch.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/CountDownLatch.cpp Fri May 6 21:22:39 2011
@@ -73,7 +73,9 @@ bool CountDownLatch::await( long long ti
return true;
}
- mutex.wait( timeOut );
+ if (timeOut > 0) {
+ mutex.wait( timeOut );
+ }
return count == 0;
}
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/Executors.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/Executors.cpp?rev=1100383&r1=1100382&r2=1100383&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/Executors.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/Executors.cpp Fri May 6 21:22:39 2011
@@ -64,10 +64,6 @@ namespace {
Thread* newThread(Runnable* task) {
Thread* thread = new Thread(task, namePrefix + Integer::toString(threadNumber.getAndIncrement()));
- if (thread->isDaemon()) {
- thread->setDaemon(false);
- }
-
if (thread->getPriority() != Thread::NORM_PRIORITY) {
thread->setPriority(Thread::NORM_PRIORITY);
}
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/Executors.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/Executors.h?rev=1100383&r1=1100382&r2=1100383&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/Executors.h (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/Executors.h Fri May 6 21:22:39 2011
@@ -103,7 +103,7 @@ namespace concurrent {
static void initialize();
static void shutdown();
- friend class decaf::lang::Thread;
+ friend class decaf::internal::util::concurrent::Threading;
};
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=1100383&r1=1100382&r2=1100383&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 Fri May 6 21:22:39 2011
@@ -17,8 +17,7 @@
#include <decaf/util/concurrent/Mutex.h>
-#include <decaf/internal/util/concurrent/MutexImpl.h>
-#include <decaf/internal/util/concurrent/ConditionImpl.h>
+#include <decaf/internal/util/concurrent/Threading.h>
#include <decaf/lang/Integer.h>
#include <list>
@@ -45,53 +44,45 @@ namespace concurrent{
public:
- MutexProperties( const std::string& name ) : mutex( NULL ), condition( NULL ), name( name ) {
+ MutexProperties() : monitor(NULL), name() {
+ this->name = DEFAULT_NAME_PREFIX + Integer::toString( ++id );
+ }
+
+ MutexProperties(const std::string& name) : monitor(NULL), name(name) {
if( this->name.empty() ) {
- this->name = std::string( "Mutex-" ) + Integer::toString( ++id );
+ this->name = DEFAULT_NAME_PREFIX + Integer::toString( ++id );
}
}
- // The Platform Mutex object and an associated Condition Object
- // for use in the wait / notify pattern.
- MutexHandle* mutex;
- ConditionHandle* condition;
+ MonitorHandle* monitor;
std::string name;
static unsigned int id;
+ static std::string DEFAULT_NAME_PREFIX;
};
unsigned int MutexProperties::id = 0;
+ std::string MutexProperties::DEFAULT_NAME_PREFIX = "Mutex-";
}}}
////////////////////////////////////////////////////////////////////////////////
-Mutex::Mutex() : Synchronizable(), properties( NULL ) {
-
- this->properties = new MutexProperties("");
-
- // Allocate the OS Mutex Implementation.
- this->properties->mutex = MutexImpl::create();
- this->properties->condition = ConditionImpl::create( this->properties->mutex );
+Mutex::Mutex() : Synchronizable(), properties(NULL) {
+ this->properties = new MutexProperties();
}
////////////////////////////////////////////////////////////////////////////////
-Mutex::Mutex( const std::string& name ) : Synchronizable(), properties( NULL ) {
-
+Mutex::Mutex( const std::string& name ) : Synchronizable(), properties(NULL) {
this->properties = new MutexProperties( name );
-
- // Allocate the OS Mutex Implementation.
- this->properties->mutex = MutexImpl::create();
- this->properties->condition = ConditionImpl::create( this->properties->mutex );
}
////////////////////////////////////////////////////////////////////////////////
Mutex::~Mutex() {
- unlock();
-
- ConditionImpl::destroy( this->properties->condition );
- MutexImpl::destroy( this->properties->mutex );
+ if (this->properties->monitor != NULL) {
+ Threading::returnMonitor(this->properties->monitor);
+ }
delete this->properties;
}
@@ -108,51 +99,96 @@ std::string Mutex::toString() const {
////////////////////////////////////////////////////////////////////////////////
void Mutex::lock() {
- MutexImpl::lock( this->properties->mutex );
+
+ if(this->properties->monitor == NULL) {
+
+ Threading::lockThreadsLib();
+
+ if (this->properties->monitor == NULL) {
+ this->properties->monitor = Threading::takeMonitor(true);
+ }
+
+ Threading::unlockThreadsLib();
+ }
+
+ Threading::enterMonitor(this->properties->monitor);
}
////////////////////////////////////////////////////////////////////////////////
bool Mutex::tryLock() {
- return MutexImpl::trylock( this->properties->mutex );
+
+ if(this->properties->monitor == NULL) {
+
+ Threading::lockThreadsLib();
+
+ if (this->properties->monitor == NULL) {
+ this->properties->monitor = Threading::takeMonitor(true);
+ }
+
+ Threading::unlockThreadsLib();
+ }
+
+ return Threading::tryEnterMonitor(this->properties->monitor);
}
////////////////////////////////////////////////////////////////////////////////
void Mutex::unlock() {
- MutexImpl::unlock( this->properties->mutex );
+
+ if (this->properties->monitor == NULL) {
+ throw IllegalMonitorStateException(__FILE__, __LINE__,
+ "Call to unlock without prior call to lock or tryLock");
+ }
+
+ Threading::exitMonitor(this->properties->monitor);
}
////////////////////////////////////////////////////////////////////////////////
void Mutex::wait() {
- ConditionImpl::wait( this->properties->condition );
+ wait(0, 0);
}
////////////////////////////////////////////////////////////////////////////////
-void Mutex::wait( long long millisecs ) {
- wait( millisecs, 0 );
+void Mutex::wait(long long millisecs) {
+ wait(millisecs, 0);
}
////////////////////////////////////////////////////////////////////////////////
void Mutex::wait( long long millisecs, int nanos ) {
- if( millisecs < 0 ) {
- throw IllegalArgumentException(
- __FILE__, __LINE__, "Milliseconds value cannot be negative." );
+ if (millisecs < 0) {
+ throw IllegalArgumentException(__FILE__, __LINE__, "Milliseconds value cannot be negative.");
}
- if( nanos < 0 || nanos > 999999 ) {
- throw IllegalArgumentException(
- __FILE__, __LINE__, "Nanoseconds value must be in the range [0..999999]." );
+ if (nanos < 0 || nanos > 999999) {
+ throw IllegalArgumentException(__FILE__, __LINE__, "Nanoseconds value must be in the range [0..999999].");
}
- ConditionImpl::wait( this->properties->condition, millisecs, nanos );
+ if (this->properties->monitor == NULL) {
+ throw IllegalMonitorStateException(__FILE__, __LINE__,
+ "Call to wait without prior call to lock or tryLock");
+ }
+
+ Threading::waitOnMonitor(this->properties->monitor, millisecs, nanos);
}
////////////////////////////////////////////////////////////////////////////////
void Mutex::notify() {
- ConditionImpl::notify( this->properties->condition );
+
+ if (this->properties->monitor == NULL) {
+ throw IllegalMonitorStateException(__FILE__, __LINE__,
+ "Call to notify without prior call to lock or tryLock");
+ }
+
+ Threading::notifyWaiter(this->properties->monitor);
}
////////////////////////////////////////////////////////////////////////////////
void Mutex::notifyAll() {
- ConditionImpl::notifyAll( this->properties->condition );
+
+ if (this->properties->monitor == NULL) {
+ throw IllegalMonitorStateException(__FILE__, __LINE__,
+ "Call to notifyAll without prior call to lock or tryLock");
+ }
+
+ Threading::notifyAllWaiters(this->properties->monitor);
}
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/locks/LockSupport.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/locks/LockSupport.cpp?rev=1100383&r1=1100382&r2=1100383&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/locks/LockSupport.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/locks/LockSupport.cpp Fri May 6 21:22:39 2011
@@ -21,11 +21,14 @@
#include <decaf/lang/Thread.h>
#include <decaf/lang/System.h>
+#include <decaf/internal/util/concurrent/Threading.h>
+
using namespace decaf;
using namespace decaf::lang;
using namespace decaf::util;
using namespace decaf::util::concurrent;
using namespace decaf::util::concurrent::locks;
+using namespace decaf::internal::util::concurrent;
////////////////////////////////////////////////////////////////////////////////
LockSupport::LockSupport() {
@@ -39,7 +42,7 @@ LockSupport::~LockSupport() {
void LockSupport::unpark( decaf::lang::Thread* thread ) throw() {
try{
- Thread::unpark( thread );
+ Threading::unpark( thread );
} DECAF_CATCHALL_NOTHROW()
}
@@ -47,7 +50,7 @@ void LockSupport::unpark( decaf::lang::T
void LockSupport::park() throw() {
try{
- Thread::park( Thread::currentThread() );
+ Threading::park( Thread::currentThread() );
} DECAF_CATCHALL_NOTHROW()
}
@@ -63,7 +66,7 @@ void LockSupport::parkNanos( long long n
nanos = nanos % 1000000;
}
- Thread::park( Thread::currentThread(), mills, nanos );
+ Threading::park(Thread::currentThread(), mills, (int)nanos);
} DECAF_CATCHALL_NOTHROW()
}
@@ -79,7 +82,7 @@ void LockSupport::parkUntil( long long d
return;
}
- Thread::park( Thread::currentThread(), ( deadline - now ), 0 );
+ Threading::park(Thread::currentThread(), ( deadline - now ), 0);
} DECAF_CATCHALL_NOTHROW()
}
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/locks/ReentrantLock.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/locks/ReentrantLock.cpp?rev=1100383&r1=1100382&r2=1100383&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/locks/ReentrantLock.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/locks/ReentrantLock.cpp Fri May 6 21:22:39 2011
@@ -217,7 +217,7 @@ ReentrantLock::~ReentrantLock() {
////////////////////////////////////////////////////////////////////////////////
void ReentrantLock::lock() {
- long long threadId = Thread::getId();
+ long long threadId = Thread::currentThread()->getId();
if( threadId == handle->lock_owner_tid ) {
handle->lock_count++;
@@ -256,7 +256,7 @@ void ReentrantLock::lockInterruptibly()
////////////////////////////////////////////////////////////////////////////////
bool ReentrantLock::tryLock() {
- long long threadId = Thread::getId();
+ long long threadId = Thread::currentThread()->getId();
if( threadId == handle->lock_owner_tid ) {
handle->lock_count++;
@@ -317,7 +317,7 @@ void ReentrantLock::unlock() {
return;
}
- if( handle->lock_owner_tid != Thread::getId() ) {
+ if( handle->lock_owner_tid != Thread::currentThread()->getId() ) {
throw IllegalMonitorStateException(
__FILE__, __LINE__,
"Unlock Failed, this thread is not the Lock Owner!" );
@@ -351,7 +351,7 @@ Condition* ReentrantLock::newCondition()
////////////////////////////////////////////////////////////////////////////////
int ReentrantLock::getHoldCount() const {
- long long threadId = Thread::getId();
+ long long threadId = Thread::currentThread()->getId();
if( threadId == handle->lock_owner_tid ) {
return (int)handle->lock_count;
@@ -362,7 +362,8 @@ int ReentrantLock::getHoldCount() const
////////////////////////////////////////////////////////////////////////////////
bool ReentrantLock::isHeldByCurrentThread() const {
- long long threadId = Thread::getId();
+
+ long long threadId = Thread::currentThread()->getId();
if( threadId == handle->lock_owner_tid ) {
return true;
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/logging/LogManager.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/logging/LogManager.cpp?rev=1100383&r1=1100382&r2=1100383&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/logging/LogManager.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/logging/LogManager.cpp Fri May 6 21:22:39 2011
@@ -63,6 +63,7 @@ LogManager::LogManager() : listeners(),
////////////////////////////////////////////////////////////////////////////////
LogManager::~LogManager() {
+ delete internal;
// TODO - Delete all the loggers.
}
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/logging/LogManager.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/logging/LogManager.h?rev=1100383&r1=1100382&r2=1100383&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/logging/LogManager.h (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/logging/LogManager.h Fri May 6 21:22:39 2011
@@ -154,7 +154,7 @@ namespace logging{
util::Properties properties;
// Data structure for LogManager Internal data.
- Pointer<LogManagerInternals> internal;
+ LogManagerInternals* internal;
public: