forked from cawka/ndn.cxx
diff --git a/executor/executor.cc b/executor/executor.cc
new file mode 100644
index 0000000..87b5760
--- /dev/null
+++ b/executor/executor.cc
@@ -0,0 +1,129 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ * Zhenkai Zhu
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#include "executor.h"
+#include "logging.h"
+
+INIT_LOGGER("Executor")
+
+using namespace std;
+using namespace boost;
+
+Executor::Executor (int poolSize)
+ : m_needStop (true)
+ , m_poolSize (poolSize)
+{
+}
+
+Executor::~Executor()
+{
+ _LOG_DEBUG ("Enter destructor");
+ shutdown ();
+ _LOG_DEBUG ("Exit destructor");
+}
+
+void
+Executor::start ()
+{
+ if (m_needStop)
+ {
+ m_needStop = false;
+ for (int i = 0; i < m_poolSize; i++)
+ {
+ m_group.create_thread (bind(&Executor::run, this));
+ }
+ }
+}
+
+void
+Executor::shutdown ()
+{
+ if (!m_needStop)
+ {
+ m_needStop = true;
+ _LOG_DEBUG ("Iterrupting all");
+ m_group.interrupt_all ();
+ _LOG_DEBUG ("Join all");
+ m_group.join_all ();
+ }
+}
+
+
+void
+Executor::execute(const Job &job)
+{
+ _LOG_DEBUG ("Add to job queue");
+
+ Lock lock(m_mutex);
+ bool queueWasEmpty = m_queue.empty ();
+ m_queue.push_back(job);
+
+ // notify working threads if the queue was empty
+ if (queueWasEmpty)
+ {
+ m_cond.notify_one ();
+ }
+}
+
+int
+Executor::poolSize()
+{
+ return m_group.size();
+}
+
+int
+Executor::jobQueueSize()
+{
+ Lock lock(m_mutex);
+ return m_queue.size();
+}
+
+void
+Executor::run ()
+{
+ _LOG_DEBUG ("Start thread");
+
+ while(!m_needStop)
+ {
+ Job job = waitForJob();
+
+ _LOG_DEBUG (">>> enter job");
+ job (); // even if job is "null", nothing bad will happen
+ _LOG_DEBUG ("<<< exit job");
+ }
+
+ _LOG_DEBUG ("Executor thread finished");
+}
+
+Executor::Job
+Executor::waitForJob()
+{
+ Lock lock(m_mutex);
+
+ // wait until job queue is not empty
+ while (m_queue.empty())
+ {
+ _LOG_DEBUG ("Unlocking mutex for wait");
+ m_cond.wait(lock);
+ _LOG_DEBUG ("Re-locking mutex after wait");
+ }
+
+ _LOG_DEBUG ("Got signal on condition");
+
+ Job job;
+ if (!m_queue.empty ()) // this is not always guaranteed, especially after interruption from destructor
+ {
+ job = m_queue.front();
+ m_queue.pop_front();
+ }
+ return job;
+}
diff --git a/executor/executor.h b/executor/executor.h
new file mode 100644
index 0000000..dd9eac3
--- /dev/null
+++ b/executor/executor.h
@@ -0,0 +1,80 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Alexander Afanasyev
+ * Zhenkai Zhu
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef EXECUTOR_H
+#define EXECUTOR_H
+
+#include <boost/function.hpp>
+#include <boost/shared_ptr.hpp>
+#include <boost/thread/condition_variable.hpp>
+#include <boost/thread/mutex.hpp>
+#include <boost/thread/locks.hpp>
+#include <boost/thread/thread.hpp>
+#include <deque>
+
+/* A very simple executor to execute submitted tasks immediately or
+ * in the future (depending on whether there is idle thread)
+ * A fixed number of threads are created for executing tasks;
+ * The policy is FIFO
+ * No cancellation of submitted tasks
+ */
+
+class Executor
+{
+public:
+ typedef boost::function<void ()> Job;
+
+ Executor(int poolSize);
+ ~Executor();
+
+ // execute the job immediately or sometime in the future
+ void
+ execute(const Job &job);
+
+ int
+ poolSize();
+
+// only for test
+ int
+ jobQueueSize();
+
+ void
+ start ();
+
+ void
+ shutdown ();
+
+private:
+ void
+ run();
+
+ Job
+ waitForJob();
+
+private:
+ typedef std::deque<Job> JobQueue;
+ typedef boost::mutex Mutex;
+ typedef boost::unique_lock<Mutex> Lock;
+ typedef boost::condition_variable Cond;
+ typedef boost::thread Thread;
+ typedef boost::thread_group ThreadGroup;
+ JobQueue m_queue;
+ Mutex m_mutex;
+ Cond m_cond;
+ ThreadGroup m_group;
+
+ volatile bool m_needStop;
+ int m_poolSize;
+};
+
+typedef boost::shared_ptr<Executor> ExecutorPtr;
+#endif // EXECUTOR_H