Converted concurrency classes to use boost::shared_ptr and boost::weak_ptr:
Wrapped all thrift code in facebook::thrift:: namespace
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664735 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/cpp/src/concurrency/PosixThreadFactory.cc b/lib/cpp/src/concurrency/PosixThreadFactory.cc
index 00b2613..f07db86 100644
--- a/lib/cpp/src/concurrency/PosixThreadFactory.cc
+++ b/lib/cpp/src/concurrency/PosixThreadFactory.cc
@@ -3,8 +3,14 @@
#include <assert.h>
#include <pthread.h>
+#include <iostream>
+
+#include <boost/weak_ptr.hpp>
+
namespace facebook { namespace thrift { namespace concurrency {
+using namespace boost;
+
/** The POSIX thread class.
@author marc
@@ -36,18 +42,23 @@
int _stackSize;
+ weak_ptr<PthreadThread> _self;
+
public:
- PthreadThread(int policy, int priority, int stackSize, Runnable* runnable) :
+ PthreadThread(int policy, int priority, int stackSize, shared_ptr<Runnable> runnable) :
_pthread(0),
_state(uninitialized),
_policy(policy),
_priority(priority),
- _stackSize(stackSize) {
+ _stackSize(stackSize) {
this->Thread::runnable(runnable);
}
+ ~PthreadThread() {
+ }
+
void start() {
if(_state != uninitialized) {
@@ -75,9 +86,13 @@
// Set thread priority
- // assert(pthread_attr_setschedparam(&thread_attr, &sched_param) == 0);
+ assert(pthread_attr_setschedparam(&thread_attr, &sched_param) == 0);
- assert(pthread_create(&_pthread, &thread_attr, threadMain, (void*)this) == 0);
+ shared_ptr<PthreadThread>* selfRef = new shared_ptr<PthreadThread>();
+
+ *selfRef = _self.lock();
+
+ assert(pthread_create(&_pthread, &thread_attr, threadMain, (void*)selfRef) == 0);
}
void join() {
@@ -90,17 +105,27 @@
}
}
- Runnable* runnable() const {return Thread::runnable();}
+ shared_ptr<Runnable> runnable() const {return Thread::runnable();}
- void runnable(Runnable* value) {Thread::runnable(value);}
+ void runnable(shared_ptr<Runnable> value) {Thread::runnable(value);}
+ void weakRef(shared_ptr<PthreadThread> self) {
+ assert(self.get() == this);
+ _self = weak_ptr<PthreadThread>(self);
+ }
};
void* PthreadThread::threadMain(void* arg) {
// XXX need a lock here when testing thread state
- PthreadThread* thread = (PthreadThread*)arg;
-
+ shared_ptr<PthreadThread> thread = *(shared_ptr<PthreadThread>*)arg;
+
+ delete reinterpret_cast<shared_ptr<PthreadThread>*>(arg);
+
+ if(thread == NULL) {
+ return (void*)0;
+ }
+
if(thread->_state != starting) {
return (void*)0;
}
@@ -184,9 +209,12 @@
@param runnable A runnable object */
- Thread* newThread(Runnable* runnable) const {
+ shared_ptr<Thread> newThread(shared_ptr<Runnable> runnable) const {
- return new PthreadThread(toPthreadPolicy(_policy), toPthreadPriority(_policy, _priority), _stackSize, runnable);
+ shared_ptr<PthreadThread> result = shared_ptr<PthreadThread>(new PthreadThread(toPthreadPolicy(_policy), toPthreadPriority(_policy, _priority), _stackSize, runnable));
+ result->weakRef(result);
+ runnable->thread(result);
+ return result;
}
int stackSize() const { return _stackSize;}
@@ -198,7 +226,7 @@
/** Sets priority.
XXX
- Need to handle incremental priorities properl. */
+ Need to handle incremental priorities properly. */
void priority(PRIORITY value) { _priority = value;}
@@ -207,7 +235,7 @@
PosixThreadFactory::PosixThreadFactory(POLICY policy, PRIORITY priority, int stackSize, bool detached) :
_impl(new PosixThreadFactory::Impl(policy, priority, stackSize, detached)) {}
-Thread* PosixThreadFactory::newThread(Runnable* runnable) const {return _impl->newThread(runnable);}
+shared_ptr<Thread> PosixThreadFactory::newThread(shared_ptr<Runnable> runnable) const {return _impl->newThread(runnable);}
int PosixThreadFactory::stackSize() const {return _impl->stackSize();}