SyB3R - Synthetic Benchmark for 3D Reconstruction
TaskScheduler.h
Go to the documentation of this file.
1 
5 #ifndef TASKSCHEDULER_H
6 #define TASKSCHEDULER_H
7 
8 #include <boost/thread.hpp>
9 #include <boost/thread/condition.hpp>
10 #include <boost/function.hpp>
11 #include <list>
12 #include <stdexcept>
13 
14 
15 namespace syb3r {
16 namespace tools {
17 
18 
19 class TaskScheduler;
20 
21 class TaskGroup;
22 
23 class Task
24 {
25  public:
26  enum State {
27  STATE_IDLE,
28  STATE_SCHEDULED,
29  STATE_INPROGRESS,
30  STATE_FINISHED
31  };
32 
33  typedef boost::function<void(void)> TaskProc;
34  Task();
35  ~Task();
36  void schedule(const TaskProc &proc, TaskScheduler &scheduler, TaskGroup *taskGroup = NULL);
37  inline State getState() const { return m_state; }
38  private:
39  TaskGroup *m_taskGroup;
40  volatile State m_state;
41  TaskProc m_proc;
42  friend class TaskScheduler;
43 };
44 
45 class TaskGroup
46 {
47  public:
48  TaskGroup();
49  ~TaskGroup();
50  void clear();
51  void add(const Task::TaskProc &proc, TaskScheduler &scheduler);
52  uint32_t getRemainingUnfinishedTasks() const { return m_remainingUnfinishedTasks; }
53  protected:
54  volatile uint32_t m_remainingUnfinishedTasks;
55  std::vector<Task*> m_tasks;
56  friend class TaskScheduler;
57 };
58 
59 class LockedTaskGroup : public TaskGroup
60 {
61  public:
62  void clear();
63  void add(const Task::TaskProc &proc, TaskScheduler &scheduler);
64  private:
65  boost::mutex m_mutex;
66  friend class TaskScheduler;
67 };
68 
70 {
71  public:
72  static void Init(unsigned helperThreads);
73  static void Shutdown();
74  TaskScheduler(unsigned helperThreads);
75  ~TaskScheduler();
76 
77  void waitFor(Task *task);
78  void waitFor(TaskGroup *taskGroup);
79 
80  static TaskScheduler &get() { if (m_mainInstance != NULL) return *m_mainInstance; throw std::runtime_error("TaskScheduler not initialized yet!"); }
81  private:
82  void scheduleTask(Task *task);
83  friend class Task;
84 
85  static TaskScheduler *m_mainInstance;
86 
87  volatile bool m_shutdown;
88  typedef std::list<Task*> TaskList;
89  TaskList m_scheduledTasks;
90 
91  #define TASK_SCHEDULER_USE_KERNEL
92 
93  #ifdef TASK_SCHEDULER_USE_KERNEL
94  boost::condition m_wakeupCondition;
95  boost::mutex m_wakeupMutex;
96 
97  boost::condition m_waitForCondition;
98  boost::condition m_groupFinishedCondition;
99  boost::mutex m_waitForMutex;
100  #else
101  volatile uint32_t m_taskListLock;
102  volatile uint32_t m_numTasksEnqueued;
103  #endif
104 
105 
106  std::vector<boost::thread*> m_helperThreads;
107 
108  void helperThreadOperate();
109 };
110 
111 }
112 }
113 
114 #endif // TASKSCHEDULER_H
Definition: CameraPathEvaluation.cpp:10
Definition: TaskScheduler.h:69
Definition: TaskScheduler.h:59
Definition: TaskScheduler.h:23
Definition: TaskScheduler.h:45