Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
JobScheduler.h
1 /*
2  * JobScheduler class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2015, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef ELM_SYS_JOBSCHEDULER_H_
22 #define ELM_SYS_JOBSCHEDULER_H_
23 
24 #include <elm/sys/Thread.h>
25 
26 namespace elm { namespace sys {
27 
28 class Job: public Runnable {
29 };
30 
31 class JobProducer {
32 public:
33  virtual ~JobProducer(void) { }
34  virtual Job *next(void) = 0;
35  virtual void harvest(Job *job) { }
36 };
37 
38 template <class T, class I>
40 public:
41  IteratorProducer(const I& iter): i(iter) { }
42  virtual Job *next(void) { if(i.ended()) return 0; else { T *r = i.item(); i.next(); return r; } }
43  virtual void harvest(Job *job) { harvest(static_cast<T *>(job)); }
44  virtual void harvest(T *job) { }
45 private:
46  I i;
47 };
48 
49 class JobScheduler: private Runnable {
50 public:
51  JobScheduler(void);
53  ~JobScheduler(void);
54 
55  // accessors
56  inline JobProducer& producer(void) const { return *prod; }
58  inline int threadCount(void) const { return cnt; }
59  void setThreadCount(int count);
60 
61  // commands
62  void start(void);
63  void stop(void);
64 
65 private:
66  void init(void);
67  virtual void run(void);
68  JobProducer *prod;
69  Mutex *mutex;
70  int cnt;
71  Thread **thds;
72  enum {
73  WAIT = 0,
74  RUN = 1,
75  STOP = 2,
76  EXN = 3
77  } state;
78  string err;
79 };
80 
81 } } // elm::sys
82 
83 #endif /* ELM_SYS_JOBSCHEDULER_H_ */
elm::sys::JobScheduler::setThreadCount
void setThreadCount(int count)
Definition: sys_JobScheduler.cpp:148
elm::sys::JobProducer
Definition: JobScheduler.h:31
elm::sys::JobScheduler::start
void start(void)
Definition: sys_JobScheduler.cpp:158
elm::sys::JobScheduler::threadCount
int threadCount(void) const
Definition: JobScheduler.h:58
elm::iter
Definition: util_WAHVector.cpp:157
elm::sys::IteratorProducer::IteratorProducer
IteratorProducer(const I &iter)
Definition: JobScheduler.h:41
elm::sys::JobScheduler::stop
void stop(void)
Definition: sys_JobScheduler.cpp:194
elm::sys::JobProducer::harvest
virtual void harvest(Job *job)
Definition: JobScheduler.h:35
elm::sys::JobScheduler::JobScheduler
JobScheduler(void)
Definition: sys_JobScheduler.cpp:95
elm::sys::JobScheduler
Definition: JobScheduler.h:49
elm::sys::JobProducer::next
virtual Job * next(void)=0
elm::sys::IteratorProducer::harvest
virtual void harvest(T *job)
Definition: JobScheduler.h:44
elm
Definition: adapter.h:26
elm::sys::JobScheduler::~JobScheduler
~JobScheduler(void)
Definition: sys_JobScheduler.cpp:112
elm::sys::IteratorProducer::next
virtual Job * next(void)
Definition: JobScheduler.h:42
elm::sys::Thread
Definition: Thread.h:60
elm::sys::Runnable
Definition: Thread.h:40
elm::sys::Mutex
Definition: Thread.h:85
elm::sys::IteratorProducer
Definition: JobScheduler.h:39
elm::count
int count(const C &c, const P &p)
Definition: util.h:65
elm::sys::Job
Definition: JobScheduler.h:28
elm::sys::JobScheduler::setProducer
void setProducer(JobProducer &producer)
Definition: sys_JobScheduler.cpp:132
elm::sys::IteratorProducer::harvest
virtual void harvest(Job *job)
Definition: JobScheduler.h:43
elm::sys::JobScheduler::producer
JobProducer & producer(void) const
Definition: JobScheduler.h:56
elm::sys::JobProducer::~JobProducer
virtual ~JobProducer(void)
Definition: JobScheduler.h:33