yat  0.14.5pre
Scheduler.h
1 #ifndef theplu_yat_utility_scheduler
2 #define theplu_yat_utility_scheduler
3 
4 // $Id: Scheduler.h 3550 2017-01-03 05:41:02Z peter $
5 
6 /*
7  Copyright (C) 2014, 2015, 2016 Peter Johansson
8 
9  This file is part of the yat library, http://dev.thep.lu.se/yat
10 
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 3 of the
14  License, or (at your option) any later version.
15 
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with yat. If not, see <http://www.gnu.org/licenses/>.
23 */
24 
25 #include "PriorityQueue.h"
26 #include "Queue.h"
27 
28 #include <boost/exception_ptr.hpp>
29 #include <boost/thread.hpp>
30 #include <boost/shared_ptr.hpp>
31 
32 #include <set>
33 #include <deque>
34 
35 namespace theplu {
36 namespace yat {
37 namespace utility {
38 
83  class Scheduler
84  {
85  public:
89  class Job
90  {
91  public:
95  explicit Job(unsigned int prio=0);
96 
100  virtual ~Job(void);
101 
105  unsigned int priority(void) const;
106 
110  virtual void operator()(void)=0;
111  private:
112  friend class Scheduler;
113  // set of jobs that have to finish before this can run
114  std::set<boost::shared_ptr<Job> > prerequisite_;
115  // jobs that have *this as prerequisite
116  std::vector<boost::shared_ptr<Job> > observers_;
117  enum status { pristine, prepared, running, completed};
118  status status_;
119  unsigned priority_;
120  unsigned id_;
121  boost::exception_ptr error_;
122  }; // end class Job
123 
129  Scheduler(unsigned int threads);
130 
137  void add_dependency(boost::shared_ptr<Job> job,
138  boost::shared_ptr<Job> prerequisite);
142  void interrupt(void);
143 
153  void submit(boost::shared_ptr<Job> job);
154 
158  void wait(void);
159 
160  private:
161  typedef boost::shared_ptr<Scheduler::Job> JobPtr;
162 
163  struct LowerPriority
164  {
165  bool operator()(const JobPtr& lhs, const JobPtr& rhs) const;
166  };
167 
168  // some typedefs for convenience
169  typedef PriorityQueue<JobPtr, LowerPriority> JobQueue;
170 
171  // \internal class that does the job
172  //
173  // It processes any job that is pushed to the \a queue until a
174  // NULL Job shows up which signals that the work is done. When
175  // NULL is observed the NULL Job is pushed to the queue so
176  // co-workers are notified too.
177  class Worker
178  {
179  public:
180  Worker(JobQueue& queue, Queue<JobPtr>& completed);
181  void operator()(void);
182  private:
183  JobQueue& queue_;
184  Queue<JobPtr>& completed_;
185  }; // end class Worker
186 
187  // function called when job has finished and returned from
188  // worker. If there are any jobs that depend on \a job, those jobs
189  // are notified and if it makes them ready to be processed they
190  // are sent to queue.
191  void post_process(boost::shared_ptr<Job> job);
192 
193  // If \a job has parent jobs, which need to finish first, update
194  // map children_ to reflect that. If all parents have finished,
195  // send job to queue.
196  void process(boost::shared_ptr<Job> job);
197 
198  // send job to queue
199  void queue(boost::shared_ptr<Job> job);
200 
201  int running_jobs_;
202 
203  JobQueue queue_;
204  Queue<JobPtr> completed_;
205  boost::thread_group workers_;
206  unsigned long int job_counter_;
207  }; // end class Scheduler
208 
209 }}}
210 #endif
Handle a number of jobs and send them to threads.
Definition: Scheduler.h:83
Job(unsigned int prio=0)
constructor
void submit(boost::shared_ptr< Job > job)
submit a job to Scheduler
virtual ~Job(void)
destructor
Definition: Scheduler.h:89
void interrupt(void)
interrrupt all jobs
void add_dependency(boost::shared_ptr< Job > job, boost::shared_ptr< Job > prerequisite)
add a dependency rule
Scheduler(unsigned int threads)
constructor
void wait(void)
wait for all jobs to finish
unsigned int priority(void) const

Generated on Tue Sep 26 2017 02:33:29 for yat by  doxygen 1.8.5