4.06.2013 - Version 0.3.4 - PIOBJECT() macro, ethernet improvement, documentation based on Doxygen
This commit is contained in:
191
pithread.h
191
pithread.h
@@ -1,3 +1,8 @@
|
||||
/*! \file pithread.h
|
||||
* \brief Thread
|
||||
*
|
||||
* This file declare thread class and some wait functions
|
||||
*/
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Thread
|
||||
@@ -24,86 +29,188 @@
|
||||
#include "pimutex.h"
|
||||
#include "piobject.h"
|
||||
|
||||
#ifdef WINDOWS
|
||||
inline void msleep(int msecs) {Sleep(msecs);}
|
||||
#ifdef DOXYGEN
|
||||
//! \brief Sleep for "msecs" milliseconds
|
||||
void msleep(int msecs);
|
||||
#else
|
||||
# ifdef WINDOWS
|
||||
inline void msleep(int msecs) {Sleep(msecs);}
|
||||
# else
|
||||
inline void msleep(int msecs) {usleep(msecs * 1000);}
|
||||
# endif
|
||||
#endif
|
||||
|
||||
/*! \brief Precise sleep for "usecs" microseconds
|
||||
* \details This function consider \c "usleep" offset
|
||||
* on QNX/Linux/Mac, which is calculated with
|
||||
* \a pip_sys_test program. If there is correct
|
||||
* offset value in system config, this function
|
||||
* wait \b exactly "usecs" microseconds. */
|
||||
void piUSleep(int usecs); // on !Windows consider constant "usleep" offset
|
||||
|
||||
/*! \brief Precise sleep for "msecs" milliseconds
|
||||
* \details This function exec \a piUSleep (msecs * 1000). */
|
||||
inline void piMSleep(int msecs) {piUSleep(msecs * 1000);} // on !Windows consider constant "usleep" offset
|
||||
|
||||
typedef void (*ThreadFunc)(void * );
|
||||
|
||||
/// events:
|
||||
/// void started()
|
||||
/// void stopped()
|
||||
///
|
||||
/// handlers:
|
||||
/// bool start(int timer_delay = -1)
|
||||
/// bool start(ThreadFunc func, int timer_delay = -1)
|
||||
/// bool startOnce()
|
||||
/// bool startOnce(ThreadFunc func)
|
||||
/// void stop(bool wait = false)
|
||||
/// void terminate(bool hard = false)
|
||||
/// bool waitForStart(int timeout_msecs = -1)
|
||||
/// bool waitForFinish(int timeout_msecs = -1)
|
||||
/// void lock()
|
||||
/// void unlock()
|
||||
class PIThread: public PIObject {
|
||||
|
||||
class PIP_EXPORT PIThread: public PIObject
|
||||
{
|
||||
PIOBJECT(PIThread)
|
||||
public:
|
||||
PIThread(void * data, ThreadFunc func, bool startNow = false, int timer_delay = -1);
|
||||
PIThread(bool startNow = false, int timer_delay = -1);
|
||||
virtual ~PIThread();
|
||||
|
||||
#ifdef QNX
|
||||
#ifdef DOXYGEN
|
||||
//! Priority of thread
|
||||
enum Priority {piHighest /** Highest */,
|
||||
piHigh /** High */,
|
||||
piNormal /** Normal, default */,
|
||||
piLow /** Low */,
|
||||
piLowerst /** Lowest */
|
||||
};
|
||||
#else
|
||||
# ifdef QNX
|
||||
enum Priority {piHighest = 12,
|
||||
piHigh = 11,
|
||||
piNormal = 10,
|
||||
piLow = 9,
|
||||
piLowerst = 8 };
|
||||
#else
|
||||
# else
|
||||
enum Priority {piHighest = -2,
|
||||
piHigh = -1,
|
||||
piNormal = 0,
|
||||
piLow = 1,
|
||||
piLowerst = 2 };
|
||||
# endif
|
||||
#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);
|
||||
EVENT_HANDLER0(bool, start) {return start(-1);}
|
||||
EVENT_HANDLER1(bool, start, int, timer_delay);
|
||||
EVENT_HANDLER1(bool, start, ThreadFunc, func) {ret_func = func; return start(-1);}
|
||||
EVENT_HANDLER2(bool, start, ThreadFunc, func, int, timer_delay) {ret_func = func; return start(timer_delay);}
|
||||
EVENT_HANDLER0(bool, startOnce);
|
||||
EVENT_HANDLER1(bool, startOnce, ThreadFunc, func) {ret_func = func; return startOnce();}
|
||||
EVENT_HANDLER0(void, stop) {stop(false);}
|
||||
EVENT_HANDLER1(void, stop, bool, wait) {terminating = true; if (wait) waitForFinish();}
|
||||
EVENT_HANDLER0(void, terminate);
|
||||
|
||||
//! \brief Set common data passed to external function
|
||||
void setData(void * d) {data_ = d;}
|
||||
|
||||
//! \brief Set external function that will be executed after every \a run()
|
||||
void setSlot(ThreadFunc func) {ret_func = func;}
|
||||
|
||||
//! \brief Set priority of thread
|
||||
void setPriority(PIThread::Priority prior);
|
||||
|
||||
//! \brief Return priority of thread
|
||||
PIThread::Priority priority() const {return priority_;}
|
||||
|
||||
//! \brief Return \c true if thread is running
|
||||
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);
|
||||
|
||||
bool isStopping() const {return running && terminating;}
|
||||
|
||||
EVENT_HANDLER0(bool, waitForStart) {return waitForStart(-1);}
|
||||
EVENT_HANDLER1(bool, waitForStart, int, timeout_msecs);
|
||||
EVENT_HANDLER0(bool, waitForFinish) {return waitForFinish(-1);}
|
||||
EVENT_HANDLER1(bool, waitForFinish, int, timeout_msecs);
|
||||
|
||||
//! \brief Set necessity of lock every \a run with internal mutex
|
||||
void needLockRun(bool need) {lockRun = need;}
|
||||
EVENT_HANDLER0(PIThread, void, lock) {mutex_.lock();}
|
||||
EVENT_HANDLER0(PIThread, void, unlock) {mutex_.unlock();}
|
||||
EVENT_HANDLER0(void, lock) {mutex_.lock();}
|
||||
EVENT_HANDLER0(void, unlock) {mutex_.unlock();}
|
||||
|
||||
//! \brief Return internal mutex
|
||||
PIMutex & mutex() {return mutex_;}
|
||||
|
||||
EVENT(PIThread, started)
|
||||
EVENT(PIThread, stopped)
|
||||
EVENT(started)
|
||||
EVENT(stopped)
|
||||
|
||||
//! \handlers
|
||||
//! \{
|
||||
|
||||
/** \fn bool start(int timer_delay = -1)
|
||||
* \brief Start thread
|
||||
* \details Start execution of \a run() in internal loop with
|
||||
* "timer_delay" delay in milliseconds. If "timer_delay" <= 0
|
||||
* there is no delay in loop. Thread also exec external function
|
||||
* set by \a setSlot() if it`s not null
|
||||
*
|
||||
* \return \c false if thread already started or can`t start thread */
|
||||
|
||||
/** \fn bool start(ThreadFunc func, int timer_delay = -1)
|
||||
* \brief Start thread
|
||||
* \details Overloaded function. Set external function "func" before start
|
||||
*
|
||||
* \return \c false if thread already started or can`t start thread */
|
||||
|
||||
/** \fn bool startOnce()
|
||||
* \brief Start thread without internal loop
|
||||
* \details Start execution of \a run() once. Thread also exec
|
||||
* external function set by \a setSlot() if it`s not null
|
||||
*
|
||||
* \return \c false if thread already started or can`t start thread */
|
||||
|
||||
/** \fn bool startOnce(ThreadFunc func)
|
||||
* \brief Start thread without internal loop
|
||||
* \details Overloaded function. Set external function "func" before start
|
||||
*
|
||||
* \return \c false if thread already started or can`t start thread */
|
||||
|
||||
/** \fn void stop(bool wait = false)
|
||||
* \brief Stop thread
|
||||
* \details Stop execution of thread and wait for it finish
|
||||
* if "wait" is \c true. This function can block for infinite
|
||||
* time if "wait" is \c true and any of thread function is
|
||||
* busy forever */
|
||||
|
||||
/** \fn void terminate()
|
||||
* \brief Strongly stop thread
|
||||
* \details Stop execution of thread immediately */
|
||||
|
||||
/** \fn bool waitForStart(int timeout_msecs = -1)
|
||||
* \brief Wait for thread start
|
||||
* \details This function block until thread finish for "timeout_msecs"
|
||||
* or forever if "timeout_msecs" < 0
|
||||
*
|
||||
* \return \c false if timeout is exceeded */
|
||||
|
||||
/** \fn bool waitForFinish(int timeout_msecs = -1)
|
||||
* \brief Wait for thread finish
|
||||
* \details This function block until thread start for "timeout_msecs"
|
||||
* or forever if "timeout_msecs" < 0
|
||||
*
|
||||
* \return \c false if timeout is exceeded */
|
||||
|
||||
//! \fn void lock()
|
||||
//! \brief Lock internal mutex
|
||||
|
||||
//! \fn void unlock()
|
||||
//! \brief Unlock internal mutex
|
||||
|
||||
//! \}
|
||||
//! \events
|
||||
//! \{
|
||||
|
||||
//! \fn void started()
|
||||
//! \brief Raise on thread start
|
||||
|
||||
//! \fn void stopped()
|
||||
//! \brief Raise on thread stop
|
||||
|
||||
//! \}
|
||||
|
||||
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
|
||||
virtual void begin() {;}
|
||||
virtual void run() {;}
|
||||
virtual void end() {;}
|
||||
|
||||
volatile bool terminating, running, lockRun;
|
||||
int timer, policy;
|
||||
|
||||
Reference in New Issue
Block a user