Files
pip/pithread.h

104 lines
3.5 KiB
C++
Executable File

/*
PIP - Platform Independent Primitives
Thread
Copyright (C) 2012 Ivan Pelipenko peri4ko@gmail.com
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 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PITHREAD_H
#define PITHREAD_H
#include <signal.h>
#include "pimutex.h"
#include "piobject.h"
#ifdef WINDOWS
inline void msleep(int msecs) {Sleep(msecs);}
#else
inline void msleep(int msecs) {usleep(msecs * 1000);}
#endif
typedef void (*ThreadFunc)(void * );
class PIThread: public PIObject {
public:
PIThread(void * data, ThreadFunc func, bool startNow = false, int timer_delay = -1);
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);
EVENT_HANDLER0(PIThread, bool, start) {return start(-1);}
EVENT_HANDLER1(PIThread, bool, start, int, timer_delay);
EVENT_HANDLER1(PIThread, bool, start, ThreadFunc, func) {ret_func = func; return start(-1);}
EVENT_HANDLER2(PIThread, bool, start, ThreadFunc, func, int, timer_delay) {ret_func = func; return start(timer_delay);}
EVENT_HANDLER0(PIThread, bool, startOnce);
EVENT_HANDLER1(PIThread, bool, startOnce, ThreadFunc, func) {ret_func = func; return startOnce();}
EVENT_HANDLER0(PIThread, void, stop) {stop(false);}
EVENT_HANDLER1(PIThread, void, stop, bool, wait) {terminating = true; if (wait) waitForFinish();}
EVENT_HANDLER0(PIThread, void, terminate) {terminate(false);}
EVENT_HANDLER1(PIThread, void, terminate, bool, hard);
void setData(void * d) {data_ = d;}
void setSlot(ThreadFunc func) {ret_func = func;}
void setPriority(PIThread::Priority prior);
PIThread::Priority priority() const {return priority_;}
bool isRunning() const {return running;}
EVENT_HANDLER0(PIThread, bool, waitForFinish) {return waitForFinish(-1);}
EVENT_HANDLER1(PIThread, bool, waitForFinish, int, timeout_msecs);
EVENT_HANDLER0(PIThread, bool, waitForStart) {return waitForStart(-1);}
EVENT_HANDLER1(PIThread, bool, waitForStart, int, timeout_msecs);
void needLockRun(bool need) {lockRun = need;}
EVENT_HANDLER0(PIThread, void, lock) {mutex_.lock();}
EVENT_HANDLER0(PIThread, void, unlock) {mutex_.unlock();}
PIMutex & mutex() {return mutex_;}
protected:
static void * thread_function(void * t);
static void * thread_function_once(void * t);
virtual void begin() {;} // executed at start
virtual void run() {;} // main loop executed with "timer_delay" timeout
virtual void end() {;} // executed at finish
volatile bool terminating, running, lockRun;
int timer, policy;
void * data_;
PIMutex mutex_;
PIThread::Priority priority_;
ThreadFunc ret_func;
#ifndef WINDOWS
pthread_t thread;
sched_param sparam;
#else
void * thread;
#endif
};
#endif // PITHREAD_H