You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by js...@apache.org on 2007/01/09 02:50:39 UTC
svn commit: r494293 - in /incubator/tuscany/cpp/sca:
VSExpress/tuscany_sca/tuscany_sca/ runtime/core/src/tuscany/sca/util/
Author: jsdelfino
Date: Mon Jan 8 17:50:38 2007
New Revision: 494293
URL: http://svn.apache.org/viewvc?view=rev&rev=494293
Log:
Added a Queue and Thread util classes to help work in a multithreaded environment.
Added:
incubator/tuscany/cpp/sca/runtime/core/src/tuscany/sca/util/Queue.cpp
incubator/tuscany/cpp/sca/runtime/core/src/tuscany/sca/util/Queue.h
incubator/tuscany/cpp/sca/runtime/core/src/tuscany/sca/util/Thread.cpp
incubator/tuscany/cpp/sca/runtime/core/src/tuscany/sca/util/Thread.h
Modified:
incubator/tuscany/cpp/sca/VSExpress/tuscany_sca/tuscany_sca/tuscany_sca.vcproj
Modified: incubator/tuscany/cpp/sca/VSExpress/tuscany_sca/tuscany_sca/tuscany_sca.vcproj
URL: http://svn.apache.org/viewvc/incubator/tuscany/cpp/sca/VSExpress/tuscany_sca/tuscany_sca/tuscany_sca.vcproj?view=diff&rev=494293&r1=494292&r2=494293
==============================================================================
--- incubator/tuscany/cpp/sca/VSExpress/tuscany_sca/tuscany_sca/tuscany_sca.vcproj (original)
+++ incubator/tuscany/cpp/sca/VSExpress/tuscany_sca/tuscany_sca/tuscany_sca.vcproj Mon Jan 8 17:50:38 2007
@@ -1014,6 +1014,22 @@
>
</File>
<File
+ RelativePath="..\..\..\runtime\core\src\tuscany\sca\util\Queue.cpp"
+ >
+ </File>
+ <File
+ RelativePath="..\..\..\runtime\core\src\tuscany\sca\util\Queue.h"
+ >
+ </File>
+ <File
+ RelativePath="..\..\..\runtime\core\src\tuscany\sca\util\Thread.cpp"
+ >
+ </File>
+ <File
+ RelativePath="..\..\..\runtime\core\src\tuscany\sca\util\Thread.h"
+ >
+ </File>
+ <File
RelativePath="..\..\..\runtime\core\src\tuscany\sca\util\ThreadLocal.cpp"
>
</File>
Added: incubator/tuscany/cpp/sca/runtime/core/src/tuscany/sca/util/Queue.cpp
URL: http://svn.apache.org/viewvc/incubator/tuscany/cpp/sca/runtime/core/src/tuscany/sca/util/Queue.cpp?view=auto&rev=494293
==============================================================================
--- incubator/tuscany/cpp/sca/runtime/core/src/tuscany/sca/util/Queue.cpp (added)
+++ incubator/tuscany/cpp/sca/runtime/core/src/tuscany/sca/util/Queue.cpp Mon Jan 8 17:50:38 2007
@@ -0,0 +1,224 @@
+/*
+ * 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.
+ */
+
+/* $Rev: 491752 $ $Date: 2007-01-01 22:22:23 -0800 (Mon, 01 Jan 2007) $ */
+
+#if defined(WIN32) || defined (_WINDOWS)
+#pragma warning(disable: 4786)
+#pragma warning(disable: 4251)
+#else
+#include "tuscany_sca_config.h"
+#endif
+
+#include <errno.h>
+
+#include <sstream>
+
+#include "tuscany/sca/util/Queue.h"
+#include "tuscany/sca/util/Utils.h"
+#include "tuscany/sca/util/Logging.h"
+#include "tuscany/sca/core/Exceptions.h"
+
+using namespace std;
+
+
+namespace tuscany
+{
+ namespace sca
+ {
+ namespace util
+ {
+ Queue::Queue()
+ {
+ logentry();
+#if defined(WIN32) || defined (_WINDOWS)
+ InitializeCriticalSection(§ion);
+ hevent = CreateEvent (NULL, TRUE, FALSE, NULL);
+#else
+ int rc = pthread_mutex_init(&mutex, NULL);
+ if (rc)
+ {
+ ostringstream msg;
+ msg << "Failed to create mutex, errno: " << errno;
+ throwException(TuscanyRuntimeException, msg.str().c_str());
+ }
+
+ rc = pthread_cond_init(&cond, NULL);
+ if (rc)
+ {
+ ostringstream msg;
+ msg << "Failed to create condition variable, errno: " << errno;
+ throwException(TuscanyRuntimeException, msg.str().c_str());
+ }
+#endif
+ }
+
+ Queue::~Queue()
+ {
+ logentry();
+#if defined(WIN32) || defined (_WINDOWS)
+ DeleteCriticalSection(§ion);
+ CloseHandle(hevent);
+#else
+ int rc = pthread_mutex_destroy(&mutex);
+ if (rc) {
+ ostringstream msg;
+ msg << "Failed to destroy mutex, errno: " << errno;
+ throwException(TuscanyRuntimeException, msg.str().c_str());
+ }
+
+ rc = pthread_cond_destroy(&cond);
+ if (rc) {
+ ostringstream msg;
+ msg << "Failed to destroy condition variable, errno: " << errno;
+ throwException(TuscanyRuntimeException, msg.str().c_str());
+ }
+#endif
+ }
+
+ void Queue::enqueue(void* element)
+ {
+ logentry();
+#if defined(WIN32) || defined (_WINDOWS)
+ EnterCriticalSection(§ion);
+
+ try
+ {
+ queue.push(element);
+
+ if (queue.size() == 1)
+ {
+ SetEvent(hevent);
+ }
+ }
+ catch (...)
+ {
+ LeaveCriticalSection(§ion);
+ throw;
+ }
+ LeaveCriticalSection(§ion);
+#else
+ int rc = pthread_mutex_lock(&mutex);
+ if (rc) {
+ ostringstream msg;
+ msg << "Failed to lock mutex, errno: " << errno;
+ throwException(TuscanyRuntimeException, msg.str().c_str());
+ }
+ try
+ {
+ queue.push(element);
+
+ if (queue.size() == 1)
+ {
+ rc = pthread_cond_signal(&cond);
+ if (rc) {
+ ostringstream msg;
+ msg << "Failed to broadcast condition variable, errno: " << errno;
+ throwException(TuscanyRuntimeException, msg.str().c_str());
+ }
+ }
+
+ }
+ catch(...)
+ {
+ pthread_mutex_unlock(&mutex);
+ throw;
+ }
+ rc = pthread_mutex_unlock(&mutex);
+ if (rc) {
+ ostringstream msg;
+ msg << "Failed to unlock mutex, errno: " << errno;
+ throwException(TuscanyRuntimeException, msg.str().c_str());
+ }
+#endif
+ }
+
+ void* Queue::dequeue()
+ {
+ logentry();
+
+ void* element = NULL;
+
+#if defined(WIN32) || defined (_WINDOWS)
+
+ EnterCriticalSection(§ion);
+ try
+ {
+ while (queue.size() == 0) {
+ LeaveCriticalSection(§ion);
+ WaitForSingleObject(hevent, INFINITE);
+ EnterCriticalSection(§ion);
+ }
+
+ element = queue.front();
+ queue.pop();
+
+ if (queue.size() == 0)
+ {
+ ResetEvent(hevent);
+ }
+ }
+ catch(...)
+ {
+ LeaveCriticalSection(§ion);
+ throw;
+ }
+ LeaveCriticalSection(§ion);
+
+#else
+ int rc = pthread_mutex_lock(&mutex);
+ if (rc) {
+ ostringstream msg;
+ msg << "Failed to lock mutex, errno: " << errno;
+ throwException(TuscanyRuntimeException, msg.str().c_str());
+ }
+ try
+ {
+ while (queue.size() == 0) {
+ rc = pthread_cond_wait(&cond, &mutex);
+ if (rc) {
+ ostringstream msg;
+ msg << "Failed to wait for condition variable, errno: " << errno;
+ throwException(TuscanyRuntimeException, msg.str().c_str());
+ }
+ }
+
+ element = queue.front();
+ queue.pop();
+ }
+ catch(...)
+ {
+ pthread_mutex_unlock(&mutex);
+ throw;
+ }
+ rc = pthread_mutex_unlock(&mutex);
+ if (rc) {
+ ostringstream msg;
+ msg << "Failed to unlock mutex, errno: " << errno;
+ throwException(TuscanyRuntimeException, msg.str().c_str());
+ }
+
+#endif
+
+ return element;
+ }
+
+ } // End namespace util
+ } // End namespace sca
+} // End namespace tuscany
Added: incubator/tuscany/cpp/sca/runtime/core/src/tuscany/sca/util/Queue.h
URL: http://svn.apache.org/viewvc/incubator/tuscany/cpp/sca/runtime/core/src/tuscany/sca/util/Queue.h?view=auto&rev=494293
==============================================================================
--- incubator/tuscany/cpp/sca/runtime/core/src/tuscany/sca/util/Queue.h (added)
+++ incubator/tuscany/cpp/sca/runtime/core/src/tuscany/sca/util/Queue.h Mon Jan 8 17:50:38 2007
@@ -0,0 +1,93 @@
+/*
+ * 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.
+ */
+
+/* $Rev: 492011 $ $Date: 2007-01-02 18:15:11 -0800 (Tue, 02 Jan 2007) $ */
+
+#ifndef tuscany_sca_util_queue_h
+#define tuscany_sca_util_queue_h
+
+#if defined(WIN32) || defined (_WINDOWS)
+#include <windows.h>
+#else
+#include <pthread.h>
+#endif
+
+#include <queue>
+
+#include "tuscany/sca/export.h"
+
+
+namespace tuscany
+{
+ namespace sca
+ {
+ namespace util
+ {
+ /**
+ * A thread safe FIFO queue.
+ */
+ class SCA_API Queue
+ {
+ public:
+ /**
+ * Constructor.
+ */
+ Queue();
+
+ /**
+ * Destructor.
+ */
+ virtual ~Queue();
+
+ /**
+ * Dequeue an element
+ */
+ void* dequeue();
+
+ /**
+ * Enqueue an element
+ */
+ void enqueue(void* element);
+
+ private:
+
+ /**
+ * The STL queue used to hold elements.
+ */
+ std::queue<void*> queue;
+
+ /**
+ * Handles to the mutex and condition variable
+ * used to synchronize access to the queue.
+ */
+#if defined(WIN32) || defined (_WINDOWS)
+ CRITICAL_SECTION section;
+ HANDLE hevent;
+#else
+ pthread_mutex_t mutex;
+ pthread_cond_t cond;
+#endif
+
+ };
+
+ } // End namespace util
+ } // End namespace sca
+} // End namespace tuscany
+
+#endif // tuscany_sca_util_queue_h
Added: incubator/tuscany/cpp/sca/runtime/core/src/tuscany/sca/util/Thread.cpp
URL: http://svn.apache.org/viewvc/incubator/tuscany/cpp/sca/runtime/core/src/tuscany/sca/util/Thread.cpp?view=auto&rev=494293
==============================================================================
--- incubator/tuscany/cpp/sca/runtime/core/src/tuscany/sca/util/Thread.cpp (added)
+++ incubator/tuscany/cpp/sca/runtime/core/src/tuscany/sca/util/Thread.cpp Mon Jan 8 17:50:38 2007
@@ -0,0 +1,126 @@
+/*
+ * 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.
+ */
+
+/* $Rev: 491752 $ $Date: 2007-01-01 22:22:23 -0800 (Mon, 01 Jan 2007) $ */
+
+#if defined(WIN32) || defined (_WINDOWS)
+#pragma warning(disable: 4786)
+#include <process.h>
+#else
+#include "tuscany_sca_config.h"
+#endif
+
+#include <errno.h>
+
+#include <sstream>
+
+#include "tuscany/sca/util/Thread.h"
+#include "tuscany/sca/util/Utils.h"
+#include "tuscany/sca/util/Logging.h"
+#include "tuscany/sca/core/Exceptions.h"
+
+using namespace std;
+
+
+namespace tuscany
+{
+ namespace sca
+ {
+ namespace util
+ {
+
+#if defined(WIN32) || defined (_WINDOWS)
+ unsigned int __stdcall runThread(void *args)
+ {
+ Thread* thread = (Thread*)args;
+ thread->run();
+ return 0;
+ }
+#else
+ void* runThread(void* args)
+ {
+ Thread* thread = (Thread*)args;
+ thread->run();
+ return NULL;
+ }
+#endif
+
+ Thread::Thread()
+#if defined(WIN32) || defined (_WINDOWS)
+ : hthread(0)
+#else
+#endif
+ {
+ logentry();
+ }
+
+ Thread::~Thread()
+ {
+ logentry();
+#if defined(WIN32) || defined (_WINDOWS)
+ if (hthread != 0)
+ {
+ CloseHandle(hthread);
+ }
+#else
+#endif
+ }
+
+ void Thread::start()
+ {
+ logentry();
+#if defined(WIN32) || defined (_WINDOWS)
+ hthread = (HANDLE)_beginthreadex(NULL, 0, runThread, this, 0, NULL);
+ if (hthread == 0)
+ {
+ ostringstream msg;
+ msg << "Failed to create thread, errno: " << __doserrno;
+ throwException(TuscanyRuntimeException, msg.str().c_str());
+ }
+#else
+ int rc =pthread_create(&thread, NULL, runThread, this);
+ if (rc)
+ {
+ ostringstream msg;
+ msg << "Failed to create thread, errno: " << errno;
+ throwException(TuscanyRuntimeException, msg.str().c_str());
+ }
+#endif
+ }
+
+ void Thread::join()
+ {
+ logentry();
+
+#if defined(WIN32) || defined (_WINDOWS)
+ WaitForSingleObject(hthread, INFINITE);
+#else
+ int rc =pthread_join(thread, NULL);
+ if (rc)
+ {
+ ostringstream msg;
+ msg << "Failed to join thread, errno: " << errno;
+ throwException(TuscanyRuntimeException, msg.str().c_str());
+ }
+#endif
+ }
+
+ } // End namespace util
+ } // End namespace sca
+} // End namespace tuscany
Added: incubator/tuscany/cpp/sca/runtime/core/src/tuscany/sca/util/Thread.h
URL: http://svn.apache.org/viewvc/incubator/tuscany/cpp/sca/runtime/core/src/tuscany/sca/util/Thread.h?view=auto&rev=494293
==============================================================================
--- incubator/tuscany/cpp/sca/runtime/core/src/tuscany/sca/util/Thread.h (added)
+++ incubator/tuscany/cpp/sca/runtime/core/src/tuscany/sca/util/Thread.h Mon Jan 8 17:50:38 2007
@@ -0,0 +1,90 @@
+/*
+ * 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.
+ */
+
+/* $Rev: 492011 $ $Date: 2007-01-02 18:15:11 -0800 (Tue, 02 Jan 2007) $ */
+
+#ifndef tuscany_sca_util_thread_h
+#define tuscany_sca_util_thread_h
+
+#if defined(WIN32) || defined (_WINDOWS)
+#include <windows.h>
+#else
+#include <pthread.h>
+#endif
+
+#include <queue>
+
+#include "tuscany/sca/export.h"
+
+
+namespace tuscany
+{
+ namespace sca
+ {
+ namespace util
+ {
+ /**
+ * A portable wrapper for native threads.
+ */
+ class SCA_API Thread
+ {
+ public:
+ /**
+ * Constructor.
+ */
+ Thread();
+
+ /**
+ * Destructor.
+ */
+ virtual ~Thread();
+
+ /**
+ * The method that will be run in the thread.
+ */
+ virtual void run() = 0;
+
+ /**
+ * Start the thread.
+ */
+ void start();
+
+ /**
+ * Join the thread.
+ */
+ void join();
+
+ private:
+
+ /**
+ * Native thread handle.
+ */
+#if defined(WIN32) || defined (_WINDOWS)
+ HANDLE hthread;
+#else
+ pthread_t thread;
+#endif
+
+ };
+
+ } // End namespace util
+ } // End namespace sca
+} // End namespace tuscany
+
+#endif // tuscany_sca_util_thread_h
---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org