version 3.21.0
add PISystemTime overload for thread/timer/io classes
This commit is contained in:
@@ -116,7 +116,7 @@ __THREAD_FUNC_RET__ thread_function_once(void * t) {
|
||||
//! while (isRunning()) { // while not stop()
|
||||
//! virtual run()
|
||||
//! ThreadFunc() // Slot or lambda
|
||||
//! piMSleep(timer_delay) // if timer_delay > 0
|
||||
//! piMSleep(loop_delay) // if loop_delay > 0
|
||||
//! }
|
||||
//! event stopped()
|
||||
//! virtual end()
|
||||
@@ -216,7 +216,7 @@ __THREAD_FUNC_RET__ thread_function_once(void * t) {
|
||||
//! \~russian \section PIThread_sec1 Использование без наследования
|
||||
//! \~english
|
||||
//! You can use %PIThread without subclassing by using "ThreadFunc" pointer
|
||||
//! that can be set from constructor or by overloaded function \a start(ThreadFunc func, int timer_delay).
|
||||
//! that can be set from constructor or by overloaded function \a start(ThreadFunc func, int loop_delay).
|
||||
//! If "func" if not null this function will be executed after \a run().
|
||||
//!
|
||||
//! ThreadFunc is any static function with format "void func(void * data)", or
|
||||
@@ -354,18 +354,18 @@ __THREAD_FUNC_RET__ thread_function_once(void * t) {
|
||||
//!
|
||||
|
||||
|
||||
//! \fn bool PIThread::start(int timer_delay = -1)
|
||||
//! \fn bool PIThread::start(int loop_delay = -1)
|
||||
//! \~\details
|
||||
//! \~english
|
||||
//! Start execution of \a run() in internal loop with
|
||||
//! "timer_delay" delay in milliseconds. If "timer_delay" <= 0
|
||||
//! "loop_delay" delay in milliseconds. If "loop_delay" <= 0
|
||||
//! this 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
|
||||
//!
|
||||
//! \~russian
|
||||
//! Начинает выполнение \a run() во внутреннем цикле
|
||||
//! с задержкой "timer_delay" миллисекунд. Если "timer_delay" <= 0
|
||||
//! с задержкой "loop_delay" миллисекунд. Если "loop_delay" <= 0
|
||||
//! то задержки не будет. Также поток вызывает внешний метод,
|
||||
//! заданный через \a setSlot(), если он существует.
|
||||
//! \return \c false если поток уже запущен или не может запуститься
|
||||
@@ -525,51 +525,43 @@ __PIThreadCollection_Initializer__::~__PIThreadCollection_Initializer__() {
|
||||
|
||||
PRIVATE_DEFINITION_START(PIThread)
|
||||
#if defined(WINDOWS)
|
||||
void * thread;
|
||||
void * thread = nullptr;
|
||||
#elif defined(FREERTOS)
|
||||
TaskHandle_t thread;
|
||||
#else
|
||||
pthread_t thread;
|
||||
pthread_t thread = 0;
|
||||
sched_param sparam;
|
||||
#endif
|
||||
PRIVATE_DEFINITION_END(PIThread)
|
||||
|
||||
|
||||
PIThread::PIThread(void * data, ThreadFunc func, bool startNow, int timer_delay): PIObject() {
|
||||
PIThread::PIThread(void * data, ThreadFunc func, bool startNow, int loop_delay): PIObject() {
|
||||
PIINTROSPECTION_THREAD_NEW(this);
|
||||
tid_ = -1;
|
||||
PRIVATE->thread = 0;
|
||||
data_ = data;
|
||||
ret_func = func;
|
||||
data_ = data;
|
||||
ret_func = func;
|
||||
terminating = running_ = lockRun = false;
|
||||
priority_ = piNormal;
|
||||
delay_ = timer_delay;
|
||||
if (startNow) start(timer_delay);
|
||||
delay_ = loop_delay;
|
||||
if (startNow) start(loop_delay);
|
||||
}
|
||||
|
||||
|
||||
PIThread::PIThread(std::function<void()> func, bool startNow, int timer_delay) {
|
||||
PIThread::PIThread(std::function<void()> func, bool startNow, int loop_delay) {
|
||||
PIINTROSPECTION_THREAD_NEW(this);
|
||||
tid_ = -1;
|
||||
PRIVATE->thread = 0;
|
||||
data_ = 0;
|
||||
ret_func = [func](void *) { func(); };
|
||||
ret_func = [func](void *) { func(); };
|
||||
terminating = running_ = lockRun = false;
|
||||
priority_ = piNormal;
|
||||
delay_ = timer_delay;
|
||||
if (startNow) start(timer_delay);
|
||||
delay_ = loop_delay;
|
||||
if (startNow) start(loop_delay);
|
||||
}
|
||||
|
||||
|
||||
PIThread::PIThread(bool startNow, int timer_delay): PIObject() {
|
||||
PIThread::PIThread(bool startNow, int loop_delay): PIObject() {
|
||||
PIINTROSPECTION_THREAD_NEW(this);
|
||||
tid_ = -1;
|
||||
PRIVATE->thread = 0;
|
||||
ret_func = 0;
|
||||
terminating = running_ = lockRun = false;
|
||||
priority_ = piNormal;
|
||||
delay_ = timer_delay;
|
||||
if (startNow) start(timer_delay);
|
||||
delay_ = loop_delay;
|
||||
if (startNow) start(loop_delay);
|
||||
}
|
||||
|
||||
|
||||
@@ -599,6 +591,24 @@ PIThread::~PIThread() {
|
||||
}
|
||||
|
||||
|
||||
bool PIThread::start(ThreadFunc func, int loop_delay) {
|
||||
ret_func = func;
|
||||
return start(loop_delay);
|
||||
}
|
||||
|
||||
|
||||
bool PIThread::start(std::function<void()> func, int loop_delay) {
|
||||
ret_func = [func](void *) { func(); };
|
||||
return start(loop_delay);
|
||||
}
|
||||
|
||||
|
||||
bool PIThread::startOnce(ThreadFunc func) {
|
||||
ret_func = func;
|
||||
return startOnce();
|
||||
}
|
||||
|
||||
|
||||
void PIThread::stopAndWait(int timeout_ms) {
|
||||
stop();
|
||||
waitForFinish(timeout_ms);
|
||||
@@ -611,9 +621,9 @@ void PIThread::stop() {
|
||||
}
|
||||
|
||||
|
||||
bool PIThread::start(int timer_delay) {
|
||||
bool PIThread::start(int loop_delay) {
|
||||
if (running_) return false;
|
||||
delay_ = timer_delay;
|
||||
delay_ = loop_delay;
|
||||
return _startThread((void *)thread_function);
|
||||
}
|
||||
|
||||
|
||||
@@ -104,22 +104,16 @@ public:
|
||||
};
|
||||
|
||||
EVENT_HANDLER0(bool, start) { return start(-1); }
|
||||
EVENT_HANDLER1(bool, start, int, timer_delay);
|
||||
EVENT_HANDLER1(bool, start, int, loop_delay);
|
||||
bool start(PISystemTime loop_delay) { return start(loop_delay.toMilliseconds()); }
|
||||
bool start(ThreadFunc func) { return start(func, -1); }
|
||||
bool start(ThreadFunc func, int timer_delay) {
|
||||
ret_func = func;
|
||||
return start(timer_delay);
|
||||
}
|
||||
bool start(ThreadFunc func, int loop_delay);
|
||||
bool start(ThreadFunc func, PISystemTime loop_delay) { return start(func, loop_delay.toMilliseconds()); }
|
||||
bool start(std::function<void()> func) { return start(func, -1); }
|
||||
bool start(std::function<void()> func, int timer_delay) {
|
||||
ret_func = [func](void *) { func(); };
|
||||
return start(timer_delay);
|
||||
}
|
||||
bool start(std::function<void()> func, int loop_delay);
|
||||
bool start(std::function<void()> func, PISystemTime loop_delay) { return start(func, loop_delay.toMilliseconds()); }
|
||||
EVENT_HANDLER0(bool, startOnce);
|
||||
EVENT_HANDLER1(bool, startOnce, ThreadFunc, func) {
|
||||
ret_func = func;
|
||||
return startOnce();
|
||||
}
|
||||
EVENT_HANDLER1(bool, startOnce, ThreadFunc, func);
|
||||
EVENT_HANDLER0(void, stop);
|
||||
EVENT_HANDLER0(void, terminate);
|
||||
|
||||
@@ -127,6 +121,10 @@ public:
|
||||
//! \~russian Останавливает поток и ожидает завершения.
|
||||
void stopAndWait(int timeout_ms = -1);
|
||||
|
||||
//! \~english Stop thread and wait for finish.
|
||||
//! \~russian Останавливает поток и ожидает завершения.
|
||||
void stopAndWait(PISystemTime timeout) { stopAndWait(timeout.toMilliseconds()); }
|
||||
|
||||
//! \~english Set common data passed to external function
|
||||
//! \~russian Устанавливает данные, передаваемые в функцию потока
|
||||
void setData(void * d) { data_ = d; }
|
||||
@@ -200,7 +198,7 @@ public:
|
||||
//! \handlers
|
||||
//! \{
|
||||
|
||||
//! \fn bool start(int timer_delay = -1)
|
||||
//! \fn bool start(int loop_delay = -1)
|
||||
//! \brief
|
||||
//! \~english Start thread
|
||||
//! \~russian Запускает поток
|
||||
@@ -268,8 +266,8 @@ protected:
|
||||
//! \~russian Метод выполняется один раз при старте потока
|
||||
virtual void begin() { ; }
|
||||
|
||||
//! \~english Function executed at every "timer_delay" msecs until thread was stopped
|
||||
//! \~russian Метод выполняется каждые "timer_delay" миллисекунд
|
||||
//! \~english Function executed at every "loop_delay" msecs until thread was stopped
|
||||
//! \~russian Метод выполняется каждые "loop_delay" миллисекунд
|
||||
virtual void run() { ; }
|
||||
|
||||
//! \~english Function executed once at the end of thread
|
||||
@@ -278,12 +276,12 @@ protected:
|
||||
|
||||
std::atomic_bool terminating, running_, lockRun;
|
||||
int delay_, policy_;
|
||||
llong tid_;
|
||||
void * data_;
|
||||
llong tid_ = -1;
|
||||
void * data_ = nullptr;
|
||||
mutable PIMutex thread_mutex;
|
||||
PITimeMeasurer tmf_, tms_, tmr_;
|
||||
PIThread::Priority priority_;
|
||||
ThreadFunc ret_func;
|
||||
PIThread::Priority priority_ = piNormal;
|
||||
ThreadFunc ret_func = nullptr;
|
||||
PRIVATE_DECLARATION(PIP_EXPORT)
|
||||
|
||||
private:
|
||||
|
||||
@@ -120,11 +120,7 @@
|
||||
|
||||
|
||||
_PITimerBase::_PITimerBase() {
|
||||
interval_ = 1000;
|
||||
deferred_delay = 0.;
|
||||
running_ = deferred_ = deferred_mode = false;
|
||||
tfunc = 0;
|
||||
parent = 0;
|
||||
running_ = false;
|
||||
}
|
||||
|
||||
|
||||
@@ -593,9 +589,6 @@ bool PITimer::isStopped() const {
|
||||
void PITimer::initFirst() {
|
||||
lockRun = false;
|
||||
callEvents = true;
|
||||
data_t = 0;
|
||||
ret_func = 0;
|
||||
imp = 0;
|
||||
setProperty("interval", 0.);
|
||||
}
|
||||
|
||||
@@ -714,6 +707,20 @@ void PITimer::startDeferred(double interval_ms, PIDateTime start_datetime) {
|
||||
}
|
||||
|
||||
|
||||
void PITimer::addDelimiter(int delim, std::function<void(void *)> slot) {
|
||||
delims << Delimiter([slot](void * d, int) { slot(d); }, delim);
|
||||
}
|
||||
|
||||
|
||||
void PITimer::removeDelimiter(int delim) {
|
||||
for (int i = 0; i < delims.size_s(); ++i)
|
||||
if (delims[i].delim == delim) {
|
||||
delims.remove(i);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool PITimer::restart() {
|
||||
init();
|
||||
imp->stop();
|
||||
|
||||
@@ -57,15 +57,15 @@ public:
|
||||
bool stop();
|
||||
|
||||
typedef void (*TickFunc)(PITimer *);
|
||||
TickFunc tfunc;
|
||||
PITimer * parent;
|
||||
TickFunc tfunc = nullptr;
|
||||
PITimer * parent = nullptr;
|
||||
|
||||
protected:
|
||||
virtual bool startTimer(double interval_ms) = 0;
|
||||
virtual bool stopTimer() = 0;
|
||||
|
||||
double interval_, deferred_delay;
|
||||
bool deferred_, deferred_mode; // mode: true - date, false - delay
|
||||
double interval_ = 1000., deferred_delay = 0.;
|
||||
bool deferred_ = false, deferred_mode = false; // mode: true - date, false - delay
|
||||
std::atomic_bool running_;
|
||||
PIDateTime deferred_datetime;
|
||||
};
|
||||
@@ -131,6 +131,7 @@ public:
|
||||
double interval() const;
|
||||
|
||||
EVENT_HANDLER1(void, setInterval, double, ms);
|
||||
void setInterval(PISystemTime interval) { setInterval(interval.toMilliseconds()); }
|
||||
|
||||
//! \~english Returns if timer is started
|
||||
//! \~russian Возвращает работает ли таймер
|
||||
@@ -143,6 +144,7 @@ public:
|
||||
EVENT_HANDLER0(bool, start);
|
||||
EVENT_HANDLER1(bool, start, double, interval_ms_d);
|
||||
bool start(int interval_ms_i);
|
||||
bool start(PISystemTime interval) { return start(interval.toMilliseconds()); }
|
||||
EVENT_HANDLER0(bool, restart);
|
||||
|
||||
|
||||
@@ -212,19 +214,11 @@ public:
|
||||
|
||||
//! \~english Add frequency delimiter "delim" with optional delimiter slot "slot"
|
||||
//! \~russian Добавляет делитель частоты "delim" с необязательным методом "slot"
|
||||
void addDelimiter(int delim, std::function<void(void *)> slot) {
|
||||
delims << Delimiter([slot](void * d, int) { slot(d); }, delim);
|
||||
}
|
||||
void addDelimiter(int delim, std::function<void(void *)> slot);
|
||||
|
||||
//! \~english Remove all frequency delimiters "delim"
|
||||
//! \~russian Удаляет все делители частоты "delim"
|
||||
void removeDelimiter(int delim) {
|
||||
for (int i = 0; i < delims.size_s(); ++i)
|
||||
if (delims[i].delim == delim) {
|
||||
delims.remove(i);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
void removeDelimiter(int delim);
|
||||
|
||||
EVENT_HANDLER0(void, clearDelimiters) { delims.clear(); }
|
||||
|
||||
@@ -301,11 +295,10 @@ protected:
|
||||
Delimiter(TimerEvent slot_ = 0, int delim_ = 1) {
|
||||
slot = slot_;
|
||||
delim = delim_;
|
||||
tick = 0;
|
||||
}
|
||||
TimerEvent slot;
|
||||
int delim;
|
||||
int tick;
|
||||
int delim = 0;
|
||||
int tick = 0;
|
||||
};
|
||||
|
||||
void initFirst();
|
||||
@@ -318,14 +311,14 @@ protected:
|
||||
//! Timer execution function, similar to "slot" or event \a timeout(). By default does nothing
|
||||
virtual void tick(void * data_, int delimiter) {}
|
||||
|
||||
void * data_t;
|
||||
void * data_t = nullptr;
|
||||
std::atomic_bool lockRun, callEvents;
|
||||
PIMutex mutex_;
|
||||
TimerEvent ret_func;
|
||||
TimerImplementation imp_mode;
|
||||
TimerEvent ret_func = nullptr;
|
||||
TimerImplementation imp_mode = Thread;
|
||||
PIVector<Delimiter> delims;
|
||||
|
||||
mutable _PITimerBase * imp;
|
||||
mutable _PITimerBase * imp = nullptr;
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user