30.11.2010 - initial commit
This commit is contained in:
61
pithread.h
Normal file
61
pithread.h
Normal file
@@ -0,0 +1,61 @@
|
||||
#ifndef PITHREAD_H
|
||||
#define PITHREAD_H
|
||||
|
||||
#include <pthread.h>
|
||||
#include "piincludes.h"
|
||||
#include "pimutex.h"
|
||||
|
||||
#ifdef WINDOWS
|
||||
inline void msleep(int msecs) {Sleep(msecs);}
|
||||
#else
|
||||
inline void msleep(int msecs) {usleep(msecs * 1000);}
|
||||
#endif
|
||||
|
||||
class PIThread {
|
||||
public:
|
||||
PIThread(bool startNow = false, int timer_delay = -1);
|
||||
~PIThread();
|
||||
|
||||
#ifdef QNX
|
||||
enum Priority {piHighest = 12,
|
||||
piHigh = 11,
|
||||
piNormal = 10,
|
||||
piLow = 9,
|
||||
piLowerst = 8 };
|
||||
#else
|
||||
enum Priority {piHighest = -2,
|
||||
piHigh = -1,
|
||||
piNormal = 0,
|
||||
piLow = 1,
|
||||
piLowerst = 2 };
|
||||
#endif
|
||||
|
||||
bool start(int timer_delay = -1);
|
||||
void stop() {terminating = true;}
|
||||
void setPriority(PIThread::Priority prior);
|
||||
PIThread::Priority priority() const {return priority_;}
|
||||
bool isRunning() const {return running;}
|
||||
void waitForFinish() {while (running) msleep(1);}
|
||||
void needLockRun(bool need) {lockRun = need;}
|
||||
void lock() {mutex_.lock();;}
|
||||
void unlock() {mutex_.unlock();}
|
||||
PIMutex & mutex() {return mutex_;}
|
||||
|
||||
private:
|
||||
virtual void begin() {;} // executed at start
|
||||
virtual void run() {;} // main loop
|
||||
virtual void end() {;} // executed at finish
|
||||
|
||||
protected:
|
||||
static void * thread_function(void * t);
|
||||
|
||||
volatile bool terminating, running, lockRun;
|
||||
int timer, policy;
|
||||
pthread_t thread;
|
||||
PIMutex mutex_;
|
||||
sched_param sparam;
|
||||
PIThread::Priority priority_;
|
||||
|
||||
};
|
||||
|
||||
#endif // PITHREAD_H
|
||||
Reference in New Issue
Block a user