Honeycomb  0.1
Component-Model Framework
PeriodicTask.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 
6 
7 namespace honey
8 {
9 
10 class PeriodicTask;
11 class PeriodicSched;
13 //for shared ptr member var, is_base_of needs a defined class
14 namespace mt { template<> struct is_base_of<honey::priv::SharedObj_tag, PeriodicTask> : std::true_type {}; }
17 class PeriodicTask : public SharedObj<PeriodicTask>, thread::Pool::Task
19 {
20  friend class PeriodicSched;
21  friend struct mt::Funcptr<void ()>;
22 
23 public:
25  typedef function<void ()> Func;
26 
29 
30  virtual ~PeriodicTask() {}
31 
33  static PeriodicTask& current();
34 
36  bool active() const { return _state != State::idle; }
38  MonoClock::Duration delay() const { return _due.load() - MonoClock::now(); }
39 
41  void cancel();
43  void interrupt(const Exception::ConstPtr& e = new thread::Interrupted) { Mutex::Scoped _(_lock); if (_thread) _thread->interrupt(e); }
46 
48  void setPriority(int priority) { Mutex::Scoped _(_lock); _priority = priority; if (_thread) _thread->setPriority(_priority); }
50  int getPriority() const { return _priority; }
51 
53  const Id& id() const { return _id; }
55  String info() const;
56 
57 protected:
58  enum class State
59  {
60  idle,
61  wait,
62  exec
63  };
64 
66 
67  virtual void exec() = 0;
68  virtual void readyFunctor(bool reset) = 0;
69  virtual void cancelFunctor() = 0;
70 
71  void operator()();
72 
73  virtual void trace(const String& file, int line, const String& msg) const;
74  virtual bool traceEnabled() const;
75 
81  Ptr _self;
84  bool _cancelled;
86  int _priority;
87 };
88 
90 template<class Result>
92 {
93  friend class PeriodicSched;
94 
95 public:
97 
99 
102  Future<Result> future() { return _func.future(); }
103 
105  static PeriodicTask_& current() { return static_cast<PeriodicTask_&>(PeriodicTask::current()); }
106 
107 private:
108  template<class Func>
110  : PeriodicTask(sched, period, delay, id), _func(forward<Func>(f)) {}
111 
112  virtual void exec() { _func.invoke_delayedReady(); }
113  virtual void readyFunctor(bool reset) { _func.setReady(reset); }
114  virtual void cancelFunctor() { _func.setFunc([]{ throw_ Cancelled(); }); _func(); }
115 
116  PackagedTask<Result ()> _func;
117 };
118 
121 {
122  friend class PeriodicTask;
123 
124 public:
127 
132 
133  ~PeriodicSched();
134 
136 
146  template<class Func, class PeriodicTask_ = PeriodicTask_<typename std::result_of<Func()>::type>>
148  optional<MonoClock::Duration> delay = optnull, const Id& id = idnull)
149  {
150  typename PeriodicTask_::Ptr task(new PeriodicTask_(*this, forward<Func>(f), period, delay, id));
151  add(*task);
152  return task;
153  }
154 
156  static bool trace;
157 
158 private:
159  enum class Action
160  {
161  add,
162  remove
163  };
164 
165  void add(PeriodicTask& task);
166  void remove(PeriodicTask& task);
167 
168  void run();
169 
171  Thread _thread;
172  bool _active;
173  ConditionLock _cond;
174  bool _condWait;
175  SpinLock _condOne;
176  multimap<MonoClock::TimePoint, PeriodicTask::Ptr> _tasks;
177  vector<PeriodicTask::Ptr> _due;
179 };
180 
181 inline bool PeriodicTask::traceEnabled() const { return PeriodicSched::trace; }
182 
183 }
String info() const
Get task info for prepending to a log record.
Definition: PeriodicTask.cpp:68
A scoped lock that references any lockable. Locks on construction and unlocks on destruction.
Definition: Mutex.h:10
virtual bool traceEnabled() const
Definition: PeriodicTask.h:181
virtual ~PeriodicTask()
Definition: PeriodicTask.h:30
Holds a functor and period information, returned by scheduler.
Definition: PeriodicTask.h:91
MonoClock::Duration delay() const
Returns time remaining until task is due for execution (task is due at zero time or less) ...
Definition: PeriodicTask.h:38
optional< MonoClock::Duration > _period
Definition: PeriodicTask.h:77
PeriodicSched & _sched
Definition: PeriodicTask.h:76
Ptr _self
Definition: PeriodicTask.h:81
static TimePoint now()
Get current time.
Definition: Clock.h:48
Atomic< MonoClock::TimePoint > _due
Definition: PeriodicTask.h:83
~PeriodicSched()
Definition: PeriodicTask.cpp:95
Future result when cancelled.
Definition: PeriodicTask.h:28
static optnull_t optnull
Null optional, use to reset an optional to an uninitialized state or test for initialization.
Definition: Optional.h:12
Unique future, guarantees sole access to a future function result.
Definition: Future.h:53
Thread class.
Definition: Thread.h:128
Atomic< State > _state
Definition: PeriodicTask.h:82
A thread lock where the lock is acquired through a busy wait loop.
Definition: Spin.h:17
Base class of PeriodicTask_, returned by scheduler.
Definition: PeriodicTask.h:18
int _priority
Definition: PeriodicTask.h:86
Waiting for next period.
Definition: Meta.h:233
int getPriority() const
Get task's thread execution scheduling priority.
Definition: PeriodicTask.h:50
static AsyncSched & inst()
Definition: Util.h:153
function< void()> Func
Definition: PeriodicTask.h:25
static mt_global(PeriodicSched, inst,(future::AsyncSched::inst()))
Get singleton, uses global future::AsyncSched pool.
void setPriority(int priority)
Set thread execution scheduling priority.
Definition: Thread.h:185
static PeriodicTask & current()
Get the current task object. Must be called from a task functor.
Definition: PeriodicTask.cpp:25
State
Definition: PeriodicTask.h:58
const Id & id() const
Get id used for debug output.
Definition: PeriodicTask.h:53
static PeriodicTask_ & current()
Wrapper for PeriodicTask::current()
Definition: PeriodicTask.h:105
SharedPtr< PeriodicTask > Ptr
Definition: PeriodicTask.h:24
static auto _
Definition: Module.cpp:8
Lock that is bound to a single condition. This is the common usage case of condition variables...
Definition: Lock.h:33
optional< MonoClock::Duration > _delay
Definition: PeriodicTask.h:78
static bool trace
Whether to log task execution flow.
Definition: PeriodicTask.h:156
virtual void trace(const String &file, int line, const String &msg) const
Definition: PeriodicTask.cpp:75
auto reset()
Reset bytestream manipulator state.
Definition: ByteStream.h:417
virtual void cancelFunctor()=0
Thread * _thread
Definition: PeriodicTask.h:85
void exec()
Definition: Meta.h:134
#define EXCEPTION(Class)
Declares methods required for every subclass of honey::Exception.
Definition: Exception.h:17
void interrupt(const Exception::ConstPtr &e=new thread::Interrupted)
Request an interrupt in the executing task's thread.
Definition: PeriodicTask.h:43
virtual void readyFunctor(bool reset)=0
PeriodicSched(thread::Pool &pool)
Definition: PeriodicTask.cpp:84
PeriodicTask_::Ptr schedule(Func &&f, optional< MonoClock::Duration > period=optnull, optional< MonoClock::Duration > delay=optnull, const Id &id=idnull)
Schedule a task for execution.
Definition: PeriodicTask.h:147
#define idnull
Null id.
Definition: Id.h:124
Unicode UTF-16 string class, wrapper around std::u16string.
Definition: String.h:23
virtual void exec()=0
Interrupted exception.
Definition: Thread.h:101
Base exception class. Exceptions inherited from this class provide debug info and can be thrown polym...
Definition: Exception.h:45
#define throw_
Use in place of throw keyword to throw a honey::Exception object polymorphically and provide debug in...
Definition: Exception.h:11
bool interruptRequested()
Check whether an interrupt has been requested for the executing task's thread.
Definition: PeriodicTask.h:45
Enables any type to be optional so it can exist in an uninitialized null state.
Definition: Optional.h:52
SharedPtr< PeriodicTask_ > Ptr
Definition: PeriodicTask.h:96
void cancel()
Unschedule task from further execution. If the task is awaiting execution then its future will throw ...
Definition: PeriodicTask.cpp:32
void setPriority(int priority)
Set task's thread execution scheduling priority.
Definition: PeriodicTask.h:48
Mutex _lock
Definition: PeriodicTask.h:80
PeriodicTask(PeriodicSched &sched, optional< MonoClock::Duration > period, optional< MonoClock::Duration > delay, const Id &id)
Definition: PeriodicTask.cpp:15
Lock-free FIFO queue. Uses auto-expanding freelist allocator so memory is only reclaimed upon destruc...
Definition: Queue.h:14
Future< Result > future()
Get future from which delayed result can be retrieved.
Definition: PeriodicTask.h:102
void interrupt(const Exception::ConstPtr &e=new thread::Interrupted)
Request an interrupt in the thread.
Definition: Thread.cpp:156
Id _id
Definition: PeriodicTask.h:79
A thread lock where the lock is acquired by suspending thread execution until it becomes available...
Definition: Mutex.h:17
bool _cancelled
Definition: PeriodicTask.h:84
Holds a name string and its hashed value for fast comparison ops. See String Identifier.
Definition: Id.h:25
TimePoint::Duration Duration
Definition: Clock.h:41
bool active() const
Check if task is scheduled or executing.
Definition: PeriodicTask.h:36
Scheduler that executes tasks periodically or after a delay given a pool of threads.
Definition: PeriodicTask.h:120
bool interruptRequested() const
Check whether an interrupt has been requested for the thread.
Definition: Thread.cpp:167
Global Honeycomb namespace.
void operator()()
Definition: PeriodicTask.cpp:40
Spreads task execution across a pool of re-usable threads. Uses a lock-free work-stealing queue to en...
Definition: Pool.h:12