more shared_pointer everywhere
This commit is contained in:
@@ -10,109 +10,55 @@
|
||||
#include <string>
|
||||
#include <cstdio>
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <pv/linkedList.h>
|
||||
#include <pv/lock.h>
|
||||
#include <pv/thread.h>
|
||||
#include <pv/event.h>
|
||||
#include <pv/executor.h>
|
||||
#include <pv/CDRMonitor.h>
|
||||
|
||||
namespace epics { namespace pvData {
|
||||
|
||||
// special instance to stop the executor thread
|
||||
static
|
||||
class ExecutorShutdown : public Command {
|
||||
virtual void command(){};
|
||||
} executorShutdown;
|
||||
virtual void command();
|
||||
};
|
||||
|
||||
void ExecutorShutdown::command()
|
||||
{
|
||||
}
|
||||
|
||||
static
|
||||
Command *shutdown=&executorShutdown;
|
||||
std::tr1::shared_ptr<Command> shutdown(new ExecutorShutdown());
|
||||
|
||||
PVDATA_REFCOUNT_MONITOR_DEFINE(executor);
|
||||
|
||||
typedef LinkedListNode<ExecutorNode> ExecutorListNode;
|
||||
typedef LinkedList<ExecutorNode> ExecutorList;
|
||||
|
||||
class ExecutorNode {
|
||||
public:
|
||||
ExecutorNode(Command *command);
|
||||
|
||||
Command *command;
|
||||
ExecutorListNode node;
|
||||
ExecutorListNode runNode;
|
||||
};
|
||||
|
||||
ExecutorNode::ExecutorNode(Command *command)
|
||||
: command(command),
|
||||
node(*this),
|
||||
runNode(*this)
|
||||
{}
|
||||
|
||||
class ExecutorPvt : public Runnable{
|
||||
public:
|
||||
ExecutorPvt(String threadName,ThreadPriority priority);
|
||||
~ExecutorPvt();
|
||||
ExecutorNode * createNode(Command *command);
|
||||
void execute(ExecutorNode *node);
|
||||
virtual void run();
|
||||
private:
|
||||
ExecutorList executorList;
|
||||
ExecutorList runList;
|
||||
Event moreWork;
|
||||
Event stopped;
|
||||
Mutex mutex;
|
||||
Thread thread;
|
||||
};
|
||||
|
||||
ExecutorPvt::ExecutorPvt(String threadName,ThreadPriority priority)
|
||||
: executorList(),
|
||||
runList(),
|
||||
moreWork(),
|
||||
stopped(),
|
||||
mutex(),
|
||||
thread(threadName,priority,this)
|
||||
{}
|
||||
|
||||
ExecutorPvt::~ExecutorPvt()
|
||||
Executor::Executor(String threadName,ThreadPriority priority)
|
||||
: thread(threadName,priority,this)
|
||||
{
|
||||
ExecutorNode shutdownNode(shutdown);
|
||||
}
|
||||
|
||||
execute(&shutdownNode);
|
||||
Executor::~Executor()
|
||||
{
|
||||
execute(shutdown);
|
||||
stopped.wait();
|
||||
|
||||
// The thread signals 'stopped' while still holding
|
||||
// the lock. By taking it we wait for the run() function
|
||||
// to actually return
|
||||
Lock xx(mutex);
|
||||
|
||||
ExecutorListNode *node;
|
||||
while((node=executorList.removeHead())!=0) {
|
||||
delete &node->getObject();
|
||||
}
|
||||
head.reset();
|
||||
tail.reset();
|
||||
}
|
||||
|
||||
void ExecutorPvt::run()
|
||||
void Executor::run()
|
||||
{
|
||||
Lock xx(mutex);
|
||||
while(true) {
|
||||
ExecutorListNode * executorListNode = 0;
|
||||
while(runList.isEmpty()) {
|
||||
while(head.get()==NULL) {
|
||||
xx.unlock();
|
||||
moreWork.wait();
|
||||
xx.lock();
|
||||
}
|
||||
executorListNode = runList.removeHead();
|
||||
|
||||
if(!executorListNode) continue;
|
||||
Command *cmd=executorListNode->getObject().command;
|
||||
|
||||
if(cmd==shutdown) break;
|
||||
|
||||
CommandPtr command = head;
|
||||
if(command.get()==NULL) continue;
|
||||
if(command.get()==shutdown.get()) break;
|
||||
xx.unlock();
|
||||
try {
|
||||
executorListNode->getObject().command->command();
|
||||
command->command();
|
||||
}catch(std::exception& e){
|
||||
//TODO: feed into logging mechanism
|
||||
fprintf(stderr, "Executor: Unhandled exception: %s",e.what());
|
||||
@@ -122,41 +68,20 @@ void ExecutorPvt::run()
|
||||
|
||||
xx.lock();
|
||||
}
|
||||
|
||||
stopped.signal();
|
||||
}
|
||||
|
||||
ExecutorNode * ExecutorPvt::createNode(Command *command)
|
||||
void Executor::execute(CommandPtr const & command)
|
||||
{
|
||||
Lock xx(mutex);
|
||||
ExecutorNode *executorNode = new ExecutorNode(command);
|
||||
executorList.addTail(executorNode->node);
|
||||
return executorNode;
|
||||
command->next.reset();
|
||||
if(head.get()==NULL) {
|
||||
head = command;
|
||||
moreWork.signal();
|
||||
return;
|
||||
}
|
||||
if(tail.get()==NULL) return;
|
||||
tail->next = command;
|
||||
}
|
||||
|
||||
void ExecutorPvt::execute(ExecutorNode *node)
|
||||
{
|
||||
Lock xx(mutex);
|
||||
if(node->runNode.isOnList()) return;
|
||||
bool isEmpty = runList.isEmpty();
|
||||
runList.addTail(node->runNode);
|
||||
if(isEmpty) moreWork.signal();
|
||||
}
|
||||
|
||||
Executor::Executor(String threadName,ThreadPriority priority)
|
||||
: pImpl(new ExecutorPvt(threadName,priority))
|
||||
{
|
||||
PVDATA_REFCOUNT_MONITOR_CONSTRUCT(executor);
|
||||
}
|
||||
|
||||
Executor::~Executor() {
|
||||
delete pImpl;
|
||||
PVDATA_REFCOUNT_MONITOR_DESTRUCT(executor);
|
||||
}
|
||||
|
||||
ExecutorNode * Executor::createNode(Command*command)
|
||||
{return pImpl->createNode(command);}
|
||||
|
||||
void Executor::execute(ExecutorNode *node) {pImpl->execute(node);}
|
||||
|
||||
}}
|
||||
|
||||
Reference in New Issue
Block a user