You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by as...@apache.org on 2008/08/06 22:56:19 UTC
svn commit: r683402 - in /incubator/qpid/trunk/qpid/cpp/src/qpid/sys:
Thread.h posix/Thread.cpp posix/Thread.h
Author: astitcher
Date: Wed Aug 6 13:56:19 2008
New Revision: 683402
URL: http://svn.apache.org/viewvc?rev=683402&view=rev
Log:
Refactor Thread platform code so that the implementation is completely
decoupled from its interface
Removed:
incubator/qpid/trunk/qpid/cpp/src/qpid/sys/posix/Thread.h
Modified:
incubator/qpid/trunk/qpid/cpp/src/qpid/sys/Thread.h
incubator/qpid/trunk/qpid/cpp/src/qpid/sys/posix/Thread.cpp
Modified: incubator/qpid/trunk/qpid/cpp/src/qpid/sys/Thread.h
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/sys/Thread.h?rev=683402&r1=683401&r2=683402&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/sys/Thread.h (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/sys/Thread.h Wed Aug 6 13:56:19 2008
@@ -21,11 +21,34 @@
* under the License.
*
*/
+#include <boost/shared_ptr.hpp>
-#ifdef USE_APR_PLATFORM
-#include "apr/Thread.h"
-#else
-#include "posix/Thread.h"
-#endif
+namespace qpid {
+namespace sys {
+class Runnable;
+class ThreadPrivate;
+
+class Thread
+{
+ boost::shared_ptr<ThreadPrivate> impl;
+
+ public:
+ Thread();
+ explicit Thread(qpid::sys::Runnable*);
+ explicit Thread(qpid::sys::Runnable&);
+
+ void join();
+
+ unsigned long id();
+
+ static Thread current();
+
+ /** ID of current thread for logging.
+ * Workaround for broken Thread::current() in APR
+ */
+ static unsigned long logId() { return current().id(); }
+};
+
+}}
#endif /*!_sys_Thread_h*/
Modified: incubator/qpid/trunk/qpid/cpp/src/qpid/sys/posix/Thread.cpp
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/sys/posix/Thread.cpp?rev=683402&r1=683401&r2=683402&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/sys/posix/Thread.cpp (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/sys/posix/Thread.cpp Wed Aug 6 13:56:19 2008
@@ -19,11 +19,57 @@
*
*/
-#include "Thread.h"
+#include "qpid/sys/Thread.h"
+
#include "qpid/sys/Runnable.h"
+#include "check.h"
+
+#include <pthread.h>
-void* qpid::sys::Thread::runRunnable(void* p)
+namespace qpid {
+namespace sys {
+
+namespace {
+void* runRunnable(void* p)
{
static_cast<Runnable*>(p)->run();
return 0;
}
+}
+
+struct ThreadPrivate {
+ pthread_t thread;
+
+ ThreadPrivate(Runnable* runnable) {
+ QPID_POSIX_ASSERT_THROW_IF(::pthread_create(&thread, NULL, runRunnable, runnable));
+ }
+
+ ThreadPrivate() : thread(::pthread_self()) {}
+};
+
+Thread::Thread() {}
+
+Thread::Thread(Runnable* runnable) : impl(new ThreadPrivate(runnable)) {}
+
+Thread::Thread(Runnable& runnable) : impl(new ThreadPrivate(&runnable)) {}
+
+void Thread::join(){
+ if (impl) {
+ QPID_POSIX_ASSERT_THROW_IF(::pthread_join(impl->thread, 0));
+ }
+}
+
+unsigned long Thread::id() {
+ if (impl)
+ return impl->thread;
+ else
+ return 0;
+}
+
+Thread Thread::current() {
+ Thread t;
+ t.impl.reset(new ThreadPrivate());
+ return t;
+}
+
+}}