ODIN
tjthread.h
1 /***************************************************************************
2  tjthread.h - description
3  -------------------
4  begin : Mon Dec 12 2005
5  copyright : (C) 2000-2021 by Thies H. Jochimsen
6  email : thies@jochimsen.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef TJTHREAD_H
19 #define TJTHREAD_H
20 
21 
22 #include <tjutils/tjutils.h>
23 
32 
33 // for debugging
34 class ThreadComponent {
35  public:
36  static const char* get_compName();
37 };
38 
39 
41 
42 
46 class Mutex {
47 
48  public:
49 
53  Mutex();
54 
55  ~Mutex();
56 
60  void lock();
61 
65  void unlock();
66 
67 
68  private:
69  friend class Event;
70 
71  // disable copying
72  Mutex(const Mutex&) {}
73  Mutex& operator = (const Mutex&) {return *this;}
74 
75  void* id;
76 
77 };
78 
80 
84 class MutexLock {
85 
86  public:
87 
91  MutexLock(Mutex& mutex) : m(mutex) {m.lock();}
92 
93  ~MutexLock() {m.unlock();}
94 
95  private:
96 
97  // disable copying
98  MutexLock(const MutexLock& ml) : m(ml.m) {} // bogus
99  MutexLock& operator = (const MutexLock&) {return *this;}
100 
101  Mutex& m;
102 };
103 
104 
106 
107 
111 class Event {
112 
113  public:
114 
118  Event();
119 
120  ~Event();
121 
126  void wait();
127 
131  void signal();
132 
136  void reset();
137 
138 
139  private:
140 
141  // disable copying
142  Event(const Event&) {}
143  Event& operator = (const Event&) {return *this;}
144 
145  void* id;
146 
147  // Extra stuff for pthread
148  Mutex mutex;
149  bool active;
150 
151 };
152 
153 
155 
156 
157 class ThreadIndex; // forward declaration
158 
163 class Thread {
164 
165  public:
166 
171 
172  virtual ~Thread();
173 
180  bool start(unsigned int stack_size=0);
181 
185  bool wait();
186 
187 
191  virtual void run() = 0;
192 
193 
197  static int self();
198 
199 
200  private:
201 
202  // disable copying
203  Thread(const Thread&) {}
204  Thread& operator = (const Thread&) {return *this;}
205 
206 
207  void clear_id();
208 
209  void* id;
210 
211  ThreadIndex* index; // give each thread a unique index
212 
213 };
214 
215 
217 
218 
222 template<typename In, typename Out, typename Local>
224 
225  public:
226 
230  ThreadedLoop() : mainbegin(0), mainend(0) {}
231 
232  virtual ~ThreadedLoop() {destroy();}
233 
238  bool init(unsigned int numof_threads, unsigned int loopsize);
239 
243  void destroy();
244 
248  bool execute(const In& in, STD_vector<Out>& outvec);
249 
250  private:
251 
256  virtual bool kernel(const In& in, Out& out, Local& local, unsigned int begin, unsigned int end) = 0;
257 
258 
259  class WorkThread : public Thread {
260 
261  friend class ThreadedLoop<In,Out,Local>;
262 
263  WorkThread(ThreadedLoop<In,Out,Local>* tl) : tloop(tl) {}
264 
265  void run();
266 
268  unsigned int begin;
269  unsigned int end;
270 
271  Event process;
272  Event finished;
273  volatile bool status;
274 
275  Out* out_cache;
276 
277  Local local;
278 
279  };
280 
281 
282  unsigned int mainbegin;
283  unsigned int mainend;
284  Local mainlocal;
285  friend class WorkThread;
286  STD_vector<WorkThread*> threads;
287  const In* in_cache;
288  volatile bool cont;
289 
290 };
291 
292 
293 
294 
297 #endif
void wait()
void reset()
void signal()
MutexLock(Mutex &mutex)
Definition: tjthread.h:91
Definition: tjthread.h:46
void unlock()
void lock()
bool start(unsigned int stack_size=0)
bool wait()
virtual void run()=0
bool execute(const In &in, STD_vector< Out > &outvec)
Definition: tjthread_code.h:74
bool init(unsigned int numof_threads, unsigned int loopsize)
Definition: tjthread_code.h:30
void destroy()
Definition: tjthread_code.h:60