summaryrefslogtreecommitdiffstats
path: root/src/server
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2017-10-05 19:15:57 +0200
committerDavid Robillard <d@drobilla.net>2017-12-16 13:57:39 +0100
commit4975f1e0b17cee620a58b29232867c667c24b0e3 (patch)
tree1fc112e2f672a04177fe5a629e6c880f90dd8154 /src/server
parent60c7a0b8d376bdbd0d99dbe1b74fdb7ebe2cc2d9 (diff)
downloadingen-4975f1e0b17cee620a58b29232867c667c24b0e3.tar.gz
ingen-4975f1e0b17cee620a58b29232867c667c24b0e3.tar.bz2
ingen-4975f1e0b17cee620a58b29232867c667c24b0e3.zip
Simplify Task implementation
Diffstat (limited to 'src/server')
-rw-r--r--src/server/CompiledGraph.cpp8
-rw-r--r--src/server/CompiledGraph.hpp6
-rw-r--r--src/server/Task.cpp56
-rw-r--r--src/server/Task.hpp24
4 files changed, 54 insertions, 40 deletions
diff --git a/src/server/CompiledGraph.cpp b/src/server/CompiledGraph.cpp
index 09e378b4..337baf95 100644
--- a/src/server/CompiledGraph.cpp
+++ b/src/server/CompiledGraph.cpp
@@ -55,7 +55,7 @@ has_provider_with_many_dependants(BlockImpl* n)
CompiledGraph::CompiledGraph(GraphImpl* graph)
: _log(graph->engine().log())
, _path(graph->path())
- , _master(new Task(Task::Mode::SEQUENTIAL))
+ , _master(Task::Mode::SEQUENTIAL)
{
compile_graph(graph);
}
@@ -139,7 +139,7 @@ CompiledGraph::compile_graph(GraphImpl* graph)
compile_block(b, seq, depth, predecessors);
par.push_front(std::move(seq));
}
- _master->push_front(std::move(par));
+ _master.push_front(std::move(par));
blocks = predecessors;
}
@@ -257,7 +257,7 @@ CompiledGraph::compile_block(BlockImpl* n,
void
CompiledGraph::run(RunContext& context)
{
- _master->run(context);
+ _master.run(context);
}
void
@@ -265,7 +265,7 @@ CompiledGraph::dump(std::function<void (const std::string&)> sink) const
{
sink("(compiled-graph ");
sink(_path);
- _master->dump(sink, 2, false);
+ _master.dump(sink, 2, false);
sink(")\n");
}
diff --git a/src/server/CompiledGraph.hpp b/src/server/CompiledGraph.hpp
index 0578c18a..8d6ebf52 100644
--- a/src/server/CompiledGraph.hpp
+++ b/src/server/CompiledGraph.hpp
@@ -73,9 +73,9 @@ private:
size_t max_depth,
BlockSet& k);
- Log& _log;
- const Raul::Path _path;
- std::unique_ptr<Task> _master;
+ Log& _log;
+ const Raul::Path _path;
+ Task _master;
};
} // namespace Server
diff --git a/src/server/Task.cpp b/src/server/Task.cpp
index f15c9881..7f1d4327 100644
--- a/src/server/Task.cpp
+++ b/src/server/Task.cpp
@@ -30,14 +30,14 @@ Task::run(RunContext& context)
_block->process(context);
break;
case Mode::SEQUENTIAL:
- for (const auto& task : _children) {
- task->run(context);
+ for (auto& task : _children) {
+ task.run(context);
}
break;
case Mode::PARALLEL:
// Initialize (not) done state of sub-tasks
- for (const auto& task : _children) {
- task->set_done(false);
+ for (auto& task : _children) {
+ task.set_done(false);
}
// Grab the first sub-task
@@ -66,7 +66,7 @@ Task::steal(RunContext& context)
if (_mode == Mode::PARALLEL) {
const unsigned i = _next++;
if (i < _children.size()) {
- return _children[i].get();
+ return &_children[i];
}
}
@@ -84,7 +84,7 @@ Task::get_task(RunContext& context)
while (true) {
// Push done end index as forward as possible
- while (_done_end < _children.size() && _children[_done_end]->done()) {
+ while (_done_end < _children.size() && _children[_done_end].done()) {
++_done_end;
}
@@ -105,36 +105,34 @@ Task::get_task(RunContext& context)
}
}
-std::unique_ptr<Task>
-Task::simplify(std::unique_ptr<Task>&& task)
+Task
+Task::simplify(Task task)
{
- if (task->mode() == Mode::SINGLE) {
- return std::move(task);
+ if (task.mode() == Mode::SINGLE) {
+ return task;
}
- for (auto t = task->_children.begin(); t != task->_children.end();) {
- *t = simplify(std::move(*t));
- if ((*t)->empty()) {
- // Empty task, erase
- t = task->_children.erase(t);
- } else if ((*t)->mode() == task->_mode) {
- // Subtask with the same type, fold child into parent
- std::unique_ptr<Task> child(std::move(*t));
- t = task->_children.erase(t);
- t = task->_children.insert(
- t,
- std::make_move_iterator(child->_children.begin()),
- std::make_move_iterator(child->_children.end()));
- } else {
- ++t;
+ Task ret(task.mode());
+ for (auto&& c : task._children) {
+ auto child = simplify(std::move(c));
+ if (!child.empty()) {
+ if (child.mode() == task.mode()) {
+ // Merge child into parent
+ for (auto&& grandchild : child._children) {
+ ret.append(std::move(grandchild));
+ }
+ } else {
+ // Add child task
+ ret.append(std::move(child));
+ }
}
}
- if (task->_children.size() == 1) {
- return std::move(task->_children.front());
+ if (ret._children.size() == 1) {
+ return std::move(ret._children.front());
}
- return std::move(task);
+ return ret;
}
void
@@ -152,7 +150,7 @@ Task::dump(std::function<void (const std::string&)> sink, unsigned indent, bool
} else {
sink(((_mode == Mode::SEQUENTIAL) ? "(seq " : "(par "));
for (size_t i = 0; i < _children.size(); ++i) {
- _children[i]->dump(sink, indent + 5, i == 0);
+ _children[i].dump(sink, indent + 5, i == 0);
}
sink(")");
}
diff --git a/src/server/Task.hpp b/src/server/Task.hpp
index 982a6206..203627a1 100644
--- a/src/server/Task.hpp
+++ b/src/server/Task.hpp
@@ -17,9 +17,10 @@
#ifndef INGEN_ENGINE_TASK_HPP
#define INGEN_ENGINE_TASK_HPP
+#include <atomic>
#include <cassert>
#include <deque>
-#include <memory>
+#include <functional>
#include <ostream>
namespace Ingen {
@@ -55,6 +56,17 @@ public:
, _done(task._done.load())
{}
+ Task& operator=(Task&& task)
+ {
+ _children = std::move(task._children);
+ _block = task._block;
+ _mode = task._mode;
+ _done_end = task._done_end;
+ _next = task._next.load();
+ _done = task._done.load();
+ return *this;
+ }
+
/** Run task in the given context. */
void run(RunContext& context);
@@ -65,14 +77,14 @@ public:
bool empty() const { return _mode != Mode::SINGLE && _children.empty(); }
/** Simplify task expression. */
- static std::unique_ptr<Task> simplify(std::unique_ptr<Task>&& task);
+ static Task simplify(Task task);
/** Steal a child task from this task (succeeds for PARALLEL only). */
Task* steal(RunContext& context);
/** Prepend a child to this task. */
void push_front(Task&& task) {
- _children.push_front(std::unique_ptr<Task>(new Task(std::move(task))));
+ _children.emplace_front(std::move(task));
}
Mode mode() const { return _mode; }
@@ -82,13 +94,17 @@ public:
void set_done(bool done) { _done = done; }
private:
- typedef std::deque<std::unique_ptr<Task>> Children;
+ typedef std::deque<Task> Children;
Task(const Task&) = delete;
Task& operator=(const Task&) = delete;
Task* get_task(RunContext& context);
+ void append(Task t) {
+ _children.emplace_back(std::move(t));
+ }
+
Children _children; ///< Vector of child tasks
BlockImpl* _block; ///< Used for SINGLE only
Mode _mode; ///< Execution mode