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/test/ThreadFactoryTests.h b/lib/cpp/src/concurrency/test/ThreadFactoryTests.h
index d1ec0df..c019159 100644
--- a/lib/cpp/src/concurrency/test/ThreadFactoryTests.h
+++ b/lib/cpp/src/concurrency/test/ThreadFactoryTests.h
@@ -1,7 +1,7 @@
-#include <Thread.h>
-#include <PosixThreadFactory.h>
-#include <Monitor.h>
-#include <Util.h>
+#include <concurrency/Thread.h>
+#include <concurrency/PosixThreadFactory.h>
+#include <concurrency/Monitor.h>
+#include <concurrency/Util.h>
#include <assert.h>
#include <iostream>
@@ -18,10 +18,15 @@
class ThreadFactoryTests {
- class Task: public Runnable {
+public:
+
+ static const double ERROR;
+
+ class Task: public Runnable {
public:
+
Task() {}
void run() {
@@ -29,26 +34,20 @@
}
};
-public:
-
/** Hello world test */
bool helloWorldTest() {
PosixThreadFactory threadFactory = PosixThreadFactory();
- Task* task = new ThreadFactoryTests::Task();
+ shared_ptr<Task> task = shared_ptr<Task>(new ThreadFactoryTests::Task());
- Thread* thread = threadFactory.newThread(task);
+ shared_ptr<Thread> thread = threadFactory.newThread(task);
thread->start();
thread->join();
- delete thread;
-
- delete task;
-
std::cout << "\t\t\tSuccess!" << std::endl;
return true;
@@ -92,13 +91,13 @@
PosixThreadFactory threadFactory = PosixThreadFactory();
- std::set<Thread*> threads;
+ std::set<shared_ptr<Thread> > threads;
for(int ix = 0; ix < count; ix++) {
- threads.insert(threadFactory.newThread(new ReapNTask(*monitor, *activeCount)));
+ threads.insert(threadFactory.newThread(shared_ptr<Runnable>(new ReapNTask(*monitor, *activeCount))));
}
- for(std::set<Thread*>::const_iterator thread = threads.begin(); thread != threads.end(); thread++) {
+ for(std::set<shared_ptr<Thread> >::const_iterator thread = threads.begin(); thread != threads.end(); thread++) {
(*thread)->start();
}
@@ -111,11 +110,9 @@
}
}
- for(std::set<Thread*>::const_iterator thread = threads.begin(); thread != threads.end(); thread++) {
+ for(std::set<shared_ptr<Thread> >::const_iterator thread = threads.begin(); thread != threads.end(); thread++) {
- delete (*thread)->runnable();
-
- delete *thread;
+ threads.erase(*thread);
}
std::cout << "\t\t\tSuccess!" << std::endl;
@@ -177,11 +174,11 @@
SynchStartTask::STATE state = SynchStartTask::UNINITIALIZED;
- SynchStartTask* task = new SynchStartTask(monitor, state);
+ shared_ptr<SynchStartTask> task = shared_ptr<SynchStartTask>(new SynchStartTask(monitor, state));
PosixThreadFactory threadFactory = PosixThreadFactory();
- Thread* thread = threadFactory.newThread(task);
+ shared_ptr<Thread> thread = threadFactory.newThread(task);
if(state == SynchStartTask::UNINITIALIZED) {
@@ -247,16 +244,15 @@
error *= 1.0;
}
- bool success = error < .10;
+ bool success = error < ThreadFactoryTests::ERROR;
std::cout << "\t\t\t" << (success ? "Success" : "Failure") << "! expected time: " << count * timeout << "ms elapsed time: "<< endTime - startTime << "ms error%: " << error * 100.0 << std::endl;
return success;
}
};
-
-}}}} // facebook::thrift::concurrency
+const double ThreadFactoryTests::ERROR = .20;
-using namespace facebook::thrift::concurrency::test;
+}}}} // facebook::thrift::concurrency::test
diff --git a/lib/cpp/src/concurrency/test/ThreadManagerTests.h b/lib/cpp/src/concurrency/test/ThreadManagerTests.h
index 8b2dda8..7e74aac 100644
--- a/lib/cpp/src/concurrency/test/ThreadManagerTests.h
+++ b/lib/cpp/src/concurrency/test/ThreadManagerTests.h
@@ -1,8 +1,8 @@
#include <config.h>
-#include <ThreadManager.h>
-#include <PosixThreadFactory.h>
-#include <Monitor.h>
-#include <Util.h>
+#include <concurrency/ThreadManager.h>
+#include <concurrency/PosixThreadFactory.h>
+#include <concurrency/Monitor.h>
+#include <concurrency/Util.h>
#include <assert.h>
#include <set>
@@ -23,6 +23,8 @@
public:
+ static const double ERROR;
+
class Task: public Runnable {
public:
@@ -78,9 +80,9 @@
size_t activeCount = count;
- ThreadManager* threadManager = ThreadManager::newSimpleThreadManager(workerCount);
+ shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workerCount);
- PosixThreadFactory* threadFactory = new PosixThreadFactory();
+ shared_ptr<PosixThreadFactory> threadFactory = shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
threadFactory->priority(PosixThreadFactory::HIGHEST);
@@ -88,16 +90,16 @@
threadManager->start();
- std::set<ThreadManagerTests::Task*> tasks;
+ std::set<shared_ptr<ThreadManagerTests::Task> > tasks;
for(size_t ix = 0; ix < count; ix++) {
- tasks.insert(new ThreadManagerTests::Task(monitor, activeCount, timeout));
+ tasks.insert(shared_ptr<ThreadManagerTests::Task>(new ThreadManagerTests::Task(monitor, activeCount, timeout)));
}
long long time00 = Util::currentTime();
- for(std::set<ThreadManagerTests::Task*>::iterator ix = tasks.begin(); ix != tasks.end(); ix++) {
+ for(std::set<shared_ptr<ThreadManagerTests::Task> >::iterator ix = tasks.begin(); ix != tasks.end(); ix++) {
threadManager->add(*ix);
}
@@ -119,9 +121,9 @@
long long minTime = 9223372036854775807LL;
long long maxTime = 0;
- for(std::set<ThreadManagerTests::Task*>::iterator ix = tasks.begin(); ix != tasks.end(); ix++) {
+ for(std::set<shared_ptr<ThreadManagerTests::Task> >::iterator ix = tasks.begin(); ix != tasks.end(); ix++) {
- ThreadManagerTests::Task* task = *ix;
+ shared_ptr<ThreadManagerTests::Task> task = *ix;
long long delta = task->_endTime - task->_startTime;
@@ -144,8 +146,6 @@
}
averageTime+= delta;
-
- delete *ix;
}
averageTime /= count;
@@ -160,18 +160,16 @@
error*= -1.0;
}
- bool success = error < .10;
-
- delete threadManager;
-
- delete threadFactory;
+ bool success = error < ERROR;
std::cout << "\t\t\t" << (success ? "Success" : "Failure") << "! expected time: " << expectedTime << "ms elapsed time: "<< time01 - time00 << "ms error%: " << error * 100.0 << std::endl;
return success;
}
};
-
+
+const double ThreadManagerTests::ERROR = .20;
+
}}}} // facebook::thrift::concurrency
using namespace facebook::thrift::concurrency::test;
diff --git a/lib/cpp/src/concurrency/test/TimerManagerTests.h b/lib/cpp/src/concurrency/test/TimerManagerTests.h
index 3c7fc0b..fe56d31 100644
--- a/lib/cpp/src/concurrency/test/TimerManagerTests.h
+++ b/lib/cpp/src/concurrency/test/TimerManagerTests.h
@@ -1,7 +1,7 @@
-#include <TimerManager.h>
-#include <PosixThreadFactory.h>
-#include <Monitor.h>
-#include <Util.h>
+#include <concurrency/TimerManager.h>
+#include <concurrency/PosixThreadFactory.h>
+#include <concurrency/Monitor.h>
+#include <concurrency/Util.h>
#include <assert.h>
#include <iostream>
@@ -17,17 +17,23 @@
class TimerManagerTests {
+ public:
+
+ static const double ERROR;
+
class Task: public Runnable {
public:
- Task(Monitor& monitor, long long timeout) :
- _timeout(timeout),
- _startTime(Util::currentTime()),
- _monitor(monitor),
- _success(false),
+ Task(Monitor& monitor, long long timeout) :
+ _timeout(timeout),
+ _startTime(Util::currentTime()),
+ _monitor(monitor),
+ _success(false),
_done(false) {}
+ ~Task() {std::cerr << this << std::endl;}
+
void run() {
_endTime = Util::currentTime();
@@ -41,19 +47,20 @@
float error = delta / _timeout;
- if(error < .10) {
+ if(error < ERROR) {
_success = true;
}
- std::cout << "\t\t\tHello World" << std::endl;
-
_done = true;
-
+
+ std::cout << "\t\t\tTimerManagerTests::Task[" << this << "] done" << std::endl; //debug
+
{Synchronized s(_monitor);
_monitor.notifyAll();
}
}
+
long long _timeout;
long long _startTime;
@@ -63,27 +70,25 @@
bool _done;
};
-public:
-
/** This test creates two tasks and waits for the first to expire within 10% of the expected expiration time. It then verifies that
the timer manager properly clean up itself and the remaining orphaned timeout task when the manager goes out of scope and its
destructor is called. */
bool test00(long long timeout=1000LL) {
- TimerManagerTests::Task* orphanTask = new TimerManagerTests::Task(_monitor, 10 * timeout);
+ shared_ptr<TimerManagerTests::Task> orphanTask = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, 10 * timeout));
{
TimerManager timerManager;
- timerManager.threadFactory(new PosixThreadFactory());
+ timerManager.threadFactory(shared_ptr<PosixThreadFactory>(new PosixThreadFactory()));
timerManager.start();
assert(timerManager.state() == TimerManager::STARTED);
- TimerManagerTests::Task* task = new TimerManagerTests::Task(_monitor, timeout);
+ shared_ptr<TimerManagerTests::Task> task = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, timeout));
{Synchronized s(_monitor);
@@ -98,16 +103,12 @@
std::cout << "\t\t\t" << (task->_success ? "Success" : "Failure") << "!" << std::endl;
-
- delete task;
}
// timerManager.stop(); This is where it happens via destructor
assert(!orphanTask->_done);
- delete orphanTask;
-
return true;
}
@@ -115,7 +116,8 @@
Monitor _monitor;
};
-
+const double TimerManagerTests::ERROR = .20;
+
}}}} // facebook::thrift::concurrency