Elm  1.0
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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) throw(SystemException);
53  ~JobScheduler(void);
54 
55  // accessors
56  inline JobProducer& producer(void) const { return *prod; }
57  void setProducer(JobProducer& producer);
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) throw(SystemException);
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_ */
Definition: Thread.h:70
Definition: JobScheduler.h:28
Definition: JobScheduler.h:31
virtual void harvest(T *job)
Definition: JobScheduler.h:44
virtual void harvest(Job *job)
Definition: JobScheduler.h:35
int threadCount(void) const
Definition: JobScheduler.h:58
void stop(void)
Definition: sys_JobScheduler.cpp:193
virtual ~JobProducer(void)
Definition: JobScheduler.h:33
JobProducer & producer(void) const
Definition: JobScheduler.h:56
Definition: Thread.h:53
Definition: SystemException.h:29
~JobScheduler(void)
Definition: sys_JobScheduler.cpp:111
void start(void)
Definition: sys_JobScheduler.cpp:157
Definition: JobScheduler.h:39
void setThreadCount(int count)
Definition: sys_JobScheduler.cpp:147
virtual void harvest(Job *job)
Definition: JobScheduler.h:43
JobScheduler(void)
Definition: sys_JobScheduler.cpp:94
virtual Job * next(void)=0
Definition: JobScheduler.h:49
IteratorProducer(const I &iter)
Definition: JobScheduler.h:41
virtual Job * next(void)
Definition: JobScheduler.h:42
void setProducer(JobProducer &producer)
Definition: sys_JobScheduler.cpp:131
Definition: Thread.h:41