Honeycomb  0.1
Component-Model Framework
Pool.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 
4 #include "Honey/Thread/Thread.h"
7 
8 namespace honey { namespace thread
9 {
10 
12 class Pool : public SharedObj<Pool>
13 {
14 public:
17  {
18  friend class Pool;
19  friend class Worker;
20 
21  protected:
22  virtual void trace(const String& file, int line, const String& msg) const;
23  virtual bool traceEnabled() const { return false; }
24  };
25 
30  Pool(szt workerCount, szt workerTaskMax);
31  ~Pool();
32 
34  template<class Task>
35  void enqueue(Task&& task) { enqueue_(TaskPtr(forward<Task>(task))); }
36 
38  static Task* current() { auto& ptr = Worker::current()._task; return ptr ? &(*ptr) : nullptr; }
39 
40 private:
41  struct TaskPtr : mt::Funcptr<void ()>
42  {
43  TaskPtr() = default;
44  TaskPtr(nullptr_t) : TaskPtr() {}
45  template<class Task>
46  TaskPtr(Task&& task) : mt::Funcptr<void ()>(forward<Task>(task)) {}
47 
48  Task& operator*() const { assert(base); return static_cast<Task&>(*base); }
49  Task* operator->() const { assert(base); return static_cast<Task*>(base); }
50  };
51 
52  class Worker
53  {
54  friend class Pool;
55  public:
56  Worker(Pool& pool);
57 
59  static Worker& current() { assert(*_current); return **_current; }
60 
61  private:
62  void start();
63  void join();
64  void run();
65 
67  TaskPtr next();
68 
69  Pool& _pool;
70  Thread _thread;
71  bool _active;
72  ConditionLock _cond;
73  bool _condWait;
74  SpinLock _condOne;
75  lockfree::Queue<TaskPtr> _tasks;
76  TaskPtr _task;
77  static thread::Local<Worker*> _current;
78  };
79 
80  void enqueue_(TaskPtr task);
81 
82  const szt _workerTaskMax;
83  vector<UniquePtr<Worker>> _workers;
84  lockfree::Queue<TaskPtr> _tasks;
85 };
86 
87 } }
friend class Worker
Definition: Pool.h:19
static Task * current()
Get the current task object of the calling thread. Must be called from inside a task, returns null otherwise.
Definition: Pool.h:38
Pool(szt workerCount, szt workerTaskMax)
Definition: Pool.cpp:24
Definition: Meta.h:233
Reference-counted object for intrusive shared pointers.
Definition: SharedPtr.h:93
~Pool()
Definition: Pool.cpp:31
#define assert(...)
Forwards to assert_#args. See assert_1(), assert_2().
Definition: Debug.h:24
Unicode UTF-16 string class, wrapper around std::u16string.
Definition: String.h:23
All tasks must inherit from this class. std::function is not used here to avoid the operator() virtua...
Definition: Pool.h:16
size_t szt
Size type, shorthand for size_t.
Definition: Core.h:90
void enqueue(Task &&task)
Schedule a task for execution.
Definition: Pool.h:35
Inherit to enable non-virtual functor calling.
Definition: Meta.h:231
Global Honeycomb namespace.
virtual void trace(const String &file, int line, const String &msg) const
Definition: Pool.cpp:16
virtual bool traceEnabled() const
Definition: Pool.h:23
Spreads task execution across a pool of re-usable threads. Uses a lock-free work-stealing queue to en...
Definition: Pool.h:12