Honeycomb  0.1
Component-Model Framework
DepTask.h
Go to the documentation of this file.
1 // Honeycomb, Copyright (C) 2015 NewGamePlus Inc. Distributed under the Boost Software License v1.0.
2 #pragma once
3 
5 #include "Honey/Graph/Dep.h"
6 
7 namespace honey
8 {
9 
10 class DepTask;
11 class DepSched;
13 //for weak ptr member var, is_base_of needs a defined class
14 namespace mt { template<> struct is_base_of<honey::priv::SharedObj_tag, DepTask> : std::true_type {}; }
17 class DepTask : public SharedObj<DepTask>, thread::Pool::Task
19 {
20  friend class DepSched;
21  friend struct mt::Funcptr<void ()>;
22 
23 public:
25  typedef function<void ()> Func;
28 
29  virtual ~DepTask() {}
30 
32  static DepTask& current();
33 
35  bool active() const { return _state != State::idle; }
36 
38  void interrupt(const Exception::ConstPtr& e = new thread::Interrupted) { Mutex::Scoped _(_lock); if (_thread) _thread->interrupt(e); }
41 
43  void setPriority(int priority) { Mutex::Scoped _(_lock); _priority = priority; if (_thread) _thread->setPriority(_priority); }
45  int getPriority() const { return _priority; }
46 
48  void setId(const Id& id) { assert(!_regCount, "Must unregister prior to modifying"); _depNode.setKey(id); }
49  const Id& getId() const { return _depNode.getKey(); }
50 
52 
56  DepNode& deps() { assert(!_regCount, "Must unregister prior to modifying"); return _depNode; }
57 
59  operator const Id&() const { return getId(); }
60 
62  String info() const;
63 
64 protected:
65  enum class State
66  {
67  idle,
68  queued,
69  depUpWait,
70  exec,
72  };
73 
74  DepTask(const Id& id);
75 
76  virtual void exec() = 0;
77  virtual void resetFunctor() = 0;
78 
79  void bindDirty();
80  void operator()();
82  void finalize_();
83 
84  virtual void trace(const String& file, int line, const String& msg) const;
85  virtual bool traceEnabled() const;
86 
87  DepNode _depNode;
90  int _regCount;
93  int _bindId;
94  bool _bindDirty;
100  bool _onStack;
103 };
104 
106 template<class Result>
107 class DepTask_ : public DepTask
108 {
109 public:
111 
116  template<class Func>
117  DepTask_(Func&& f, const Id& id = idnull) : DepTask(id), _func(forward<Func>(f)) {}
118 
120 
123  Future<Result> future() { return _func.future(); }
124 
126  static DepTask_& current() { return static_cast<DepTask_&>(DepTask::current()); }
127 
128 private:
129  virtual void exec() { _func.invoke_delayedReady(); }
130  virtual void resetFunctor() { _func.setReady(true); }
131 
132  PackagedTask<Result ()> _func;
133 };
134 
136 
139 class DepSched
140 {
141  friend class DepTask;
142 
143 public:
145  static mt_global(DepSched, inst, (future::AsyncSched::inst()));
146 
150  DepSched(thread::Pool& pool);
151 
153 
157  bool reg(DepTask& task);
159  bool unreg(DepTask& task);
160 
162 
175  bool enqueue(DepTask& task);
176 
178  static bool trace;
179 
180 private:
181  void bind(DepTask& root);
182  bool enqueue_priv(DepTask& task);
183 
185  Mutex _lock;
186  vector<DepTask*> _taskStack;
187  DepTask::DepGraph _depGraph;
188  int _bindId;
189 };
190 
191 inline bool DepTask::traceEnabled() const { return DepSched::trace; }
192 
193 }
A scoped lock that references any lockable. Locks on construction and unlocks on destruction.
Definition: Mutex.h:10
DepTask(const Id &id)
Definition: DepTask.cpp:15
static mt_global(DepSched, inst,(future::AsyncSched::inst()))
Get singleton, uses global future::AsyncSched pool.
Atomic< State > _state
Definition: DepTask.h:89
Unique future, guarantees sole access to a future function result.
Definition: Future.h:53
Thread class.
Definition: Thread.h:128
DepGraph::Vertex * _vertex
Definition: DepTask.h:99
const Id & getId() const
Definition: DepTask.h:49
Combined intrusive/non-intrusive smart pointer. Can reference and share any object automatically...
Definition: SharedPtr.h:175
SharedPtr< DepTask_ > Ptr
Definition: DepTask.h:110
Thread * _thread
Definition: DepTask.h:101
String info() const
Get task info for prepending to a log record.
Definition: DepTask.cpp:134
SharedPtr< DepTask > Ptr
Definition: DepTask.h:24
int _priority
Definition: DepTask.h:102
static bool trace
Whether to log task execution flow.
Definition: DepTask.h:178
Definition: Meta.h:233
Waiting for upstream tasks (dependency subgraph) to complete.
WeakPtr< DepTask > _root
Definition: DepTask.h:92
static AsyncSched & inst()
Definition: Util.h:153
int _depDownWaitInit
Definition: DepTask.h:97
void setPriority(int priority)
Set thread execution scheduling priority.
Definition: Thread.h:185
Base class of DepTask_, can be added to scheduler. Instances must be created through class DepTask_...
Definition: DepTask.h:18
static DepTask & current()
Get the current task object. Must be called from a task functor.
Definition: DepTask.cpp:32
int _bindId
Definition: DepTask.h:93
void interrupt(const Exception::ConstPtr &e=new thread::Interrupted)
Request an interrupt in the executing task's thread.
Definition: DepTask.h:38
Atomic< int > _depDownWait
Definition: DepTask.h:98
static auto _
Definition: Module.cpp:8
bool enqueue(DepTask &task)
Schedule a task for execution. Returns false if task is already active.
Definition: DepTask.cpp:275
DepGraph< DepTask::DepNode > DepGraph
Definition: DepTask.h:27
DepSched * _sched
Definition: DepTask.h:91
Future< Result > future()
Get future from which delayed result can be retrieved.
Definition: DepTask.h:123
Mutex _lock
Definition: DepTask.h:88
void bindDirty()
Definition: DepTask.cpp:39
virtual bool traceEnabled() const
Definition: DepTask.h:191
DepNode & deps()
Get dependency node. Upstream and downstream tasks can be specified through the node.
Definition: DepTask.h:56
int _depUpWaitInit
Definition: DepTask.h:95
virtual void resetFunctor()=0
virtual void exec()=0
bool _bindDirty
Definition: DepTask.h:94
virtual void trace(const String &file, int line, const String &msg) const
Definition: DepTask.cpp:139
State
Definition: DepTask.h:65
Atomic< int > _depUpWait
Definition: DepTask.h:96
virtual ~DepTask()
Definition: DepTask.h:29
bool unreg(DepTask &task)
Unregister a task. Returns false if not registered.
Definition: DepTask.cpp:172
#define assert(...)
Forwards to assert_#args. See assert_1(), assert_2().
Definition: Debug.h:24
#define idnull
Null id.
Definition: Id.h:124
Unicode UTF-16 string class, wrapper around std::u16string.
Definition: String.h:23
bool reg(DepTask &task)
Register a task, linking it into this scheduler's dependency graph.
Definition: DepTask.cpp:152
int _regCount
Definition: DepTask.h:90
Interrupted exception.
Definition: Thread.h:101
void setPriority(int priority)
Set task's thread execution scheduling priority.
Definition: DepTask.h:43
Executing functor.
A vertex is initially associated with one key and acts like a multi-map, storing all nodes and graph ...
Definition: Dep.h:107
bool active() const
Check if task is in queue or executing.
Definition: DepTask.h:35
function< void()> Func
Definition: DepTask.h:25
bool interruptRequested()
Check whether an interrupt has been requested for the executing task's thread.
Definition: DepTask.h:40
void setId(const Id &id)
Set id used for dependency graph and debug output.
Definition: DepTask.h:48
Waiting for downsteam tasks (immediate dependees) to complete.
bool _onStack
Definition: DepTask.h:100
void interrupt(const Exception::ConstPtr &e=new thread::Interrupted)
Request an interrupt in the thread.
Definition: Thread.cpp:156
DepSched(thread::Pool &pool)
Definition: DepTask.cpp:148
A thread lock where the lock is acquired by suspending thread execution until it becomes available...
Definition: Mutex.h:17
const Key & getKey() const
Definition: Dep.h:46
void operator()()
Definition: DepTask.cpp:47
Scheduler that serializes and parallelizes task execution given a dependency graph of tasks and a poo...
Definition: DepTask.h:139
Holds a name string and its hashed value for fast comparison ops. See String Identifier.
Definition: Id.h:25
static DepTask_ & current()
Wrapper for DepTask::current()
Definition: DepTask.h:126
void finalize_()
Clean up task after execution.
Definition: DepTask.cpp:123
Point to a shared object without holding a reference. The object is accessible through a lock...
Definition: SharedPtr.h:165
DepNode< DepTask * > DepNode
Definition: DepTask.h:26
bool interruptRequested() const
Check whether an interrupt has been requested for the thread.
Definition: Thread.cpp:167
DepTask_(Func &&f, const Id &id=idnull)
Definition: DepTask.h:117
void setKey(const Key &key)
Set the key used to identify this node.
Definition: Dep.h:45
Global Honeycomb namespace.
DepNode _depNode
Definition: DepTask.h:87
int getPriority() const
Get task's thread execution scheduling priority.
Definition: DepTask.h:45
Queued for execution.
Holds a functor and dependency information, enqueue in a scheduler to run the task.
Definition: DepTask.h:107
Spreads task execution across a pool of re-usable threads. Uses a lock-free work-stealing queue to en...
Definition: Pool.h:12