/usr/include/tjutils/tjthread.h is in libodin-dev 1.8.8-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | /***************************************************************************
tjthread.h - description
-------------------
begin : Mon Dec 12 2005
copyright : (C) 2000-2014 by Thies H. Jochimsen
email : thies@jochimsen.de
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef TJTHREAD_H
#define TJTHREAD_H
#include <tjutils/tjutils.h>
/**
* @addtogroup tjutils
* @{
*/
////////////////////////////////////////////////////////////////////
// for debugging
class ThreadComponent {
public:
static const char* get_compName();
};
////////////////////////////////////////////////////////////////////
/**
* Lock for mutually exclusive (mutex) access in threads. The mutex is recursive.
*/
class Mutex {
public:
/**
* Constructs unlocked mutex object.
*/
Mutex();
~Mutex();
/**
* Locks the mutex.
*/
void lock();
/**
* Unlocks the mutex.
*/
void unlock();
private:
friend class Event;
// disable copying
Mutex(const Mutex&) {}
Mutex& operator = (const Mutex&) {return *this;}
void* id;
};
////////////////////////////////////////////////////////////////////
/**
* Lock object using a mutex.
*/
class MutexLock {
public:
/**
* Constructs a lock using 'mutex'. The mutex is locked for the lifetime of the object.
*/
MutexLock(Mutex& mutex) : m(mutex) {m.lock();}
~MutexLock() {m.unlock();}
private:
// disable copying
MutexLock(const MutexLock& ml) : m(ml.m) {} // bogus
MutexLock& operator = (const MutexLock&) {return *this;}
Mutex& m;
};
////////////////////////////////////////////////////////////////////
/**
* Manual-reset event to synchronize threads.
*/
class Event {
public:
/**
* Constructs an event object.
*/
Event();
~Event();
/**
* Waits until any other thread calls signal().
* Returns immediately if this has already happended.
*/
void wait();
/**
* Signal event to all waiting threads.
*/
void signal();
/**
* Manually reset event.
*/
void reset();
private:
// disable copying
Event(const Event&) {}
Event& operator = (const Event&) {return *this;}
void* id;
// Extra stuff for pthread
Mutex mutex;
bool active;
};
////////////////////////////////////////////////////////////////////
class ThreadIndex; // forward declaration
/**
* Base class for threads. Derive from this and implement
* the run() function to use it.
*/
class Thread {
public:
/**
* Constructs thread object with the given label.
*/
Thread();
virtual ~Thread();
/**
* Starts the thread.
* 'stack_size' is used as the threads stack size if not zero (in which case the
* systems default stack size is used).
* Returns 'true' only on sucess.
*/
bool start(unsigned int stack_size=0);
/**
* Waits for the thread to finish. Returns 'true' only on sucess.
*/
bool wait();
/**
* Implement to run threads execution path.
*/
virtual void run() = 0;
/**
* Returns thread-unique ID of the current thread.
*/
static int self();
private:
// disable copying
Thread(const Thread&) {}
Thread& operator = (const Thread&) {return *this;}
void clear_id();
void* id;
ThreadIndex* index; // give each thread a unique index
};
////////////////////////////////////////////////////////////////////
/**
* Class to parallelize loops.
*/
template<typename In, typename Out, typename Local>
class ThreadedLoop {
public:
/**
* Default constructor.
*/
ThreadedLoop() : mainbegin(0), mainend(0) {}
virtual ~ThreadedLoop() {destroy();}
/**
* Initialize to use 'numof_threads' executing 'loopsize' jobs in parallel.
* Starts the worker threads.
*/
bool init(unsigned int numof_threads, unsigned int loopsize);
/**
* Stops the worker threads.
*/
void destroy();
/**
* Execute loop using input 'in' and storing result from each thread in 'outvec'.
*/
bool execute(const In& in, STD_vector<Out>& outvec);
private:
/**
* Overload this function to do the work of one thread using input 'in',
* generating output 'out' with thread-local storage 'local' for the the loop inidices (begin <= i < end).
*/
virtual bool kernel(const In& in, Out& out, Local& local, unsigned int begin, unsigned int end) = 0;
class WorkThread : public Thread {
friend class ThreadedLoop<In,Out,Local>;
WorkThread(ThreadedLoop<In,Out,Local>* tl) : tloop(tl) {}
void run();
ThreadedLoop<In,Out,Local>* tloop;
unsigned int begin;
unsigned int end;
Event process;
Event finished;
volatile bool status;
Out* out_cache;
Local local;
};
unsigned int mainbegin;
unsigned int mainend;
Local mainlocal;
friend class WorkThread;
STD_vector<WorkThread*> threads;
const In* in_cache;
volatile bool cont;
};
/** @}
*/
#endif
|