version 4.0.0_alpha

in almost all methods removed timeouts in milliseconds, replaced to PISystemTime
PITimer rewrite, remove internal impl, now only thread implementation, API similar to PIThread
PITimer API no longer pass void*
PIPeer, PIConnection improved stability on reinit and exit
PISystemTime new methods
pisd now exit without hanging
This commit is contained in:
2024-07-30 14:18:02 +03:00
parent f07c9cbce8
commit 1c7fc39b6c
58 changed files with 677 additions and 1191 deletions

View File

@@ -535,33 +535,33 @@ PRIVATE_DEFINITION_START(PIThread)
PRIVATE_DEFINITION_END(PIThread)
PIThread::PIThread(void * data, ThreadFunc func, bool startNow, int loop_delay): PIObject() {
PIThread::PIThread(void * data, ThreadFunc func, bool startNow, PISystemTime loop_delay): PIObject() {
PIINTROSPECTION_THREAD_NEW(this);
data_ = data;
ret_func = func;
terminating = running_ = lockRun = false;
priority_ = piNormal;
delay_ = loop_delay;
if (startNow) start(loop_delay);
if (startNow) start();
}
PIThread::PIThread(std::function<void()> func, bool startNow, int loop_delay) {
PIThread::PIThread(std::function<void()> func, bool startNow, PISystemTime loop_delay) {
PIINTROSPECTION_THREAD_NEW(this);
ret_func = [func](void *) { func(); };
terminating = running_ = lockRun = false;
priority_ = piNormal;
delay_ = loop_delay;
if (startNow) start(loop_delay);
if (startNow) start();
}
PIThread::PIThread(bool startNow, int loop_delay): PIObject() {
PIThread::PIThread(bool startNow, PISystemTime loop_delay): PIObject() {
PIINTROSPECTION_THREAD_NEW(this);
terminating = running_ = lockRun = false;
priority_ = piNormal;
delay_ = loop_delay;
if (startNow) start(loop_delay);
if (startNow) start();
}
@@ -573,7 +573,7 @@ PIThread::~PIThread() {
// PICout(PICoutManipulators::DefaultControls) << "~PIThread" << PRIVATE->thread;
// PICout(PICoutManipulators::DefaultControls) << pthread_join(PRIVATE->thread, 0);
PICout(PICoutManipulators::DefaultControls) << "FreeRTOS can't terminate pthreads! waiting for stop";
stop(true);
stopAndWait();
// PICout(PICoutManipulators::DefaultControls) << "stopped!";
#else
# ifndef WINDOWS
@@ -591,15 +591,47 @@ PIThread::~PIThread() {
}
bool PIThread::start(ThreadFunc func, int loop_delay) {
ret_func = func;
return start(loop_delay);
bool PIThread::start() {
if (running_) return false;
return _startThread((void *)thread_function);
}
bool PIThread::start(std::function<void()> func, int loop_delay) {
bool PIThread::start(PISystemTime loop_delay) {
delay_ = loop_delay;
return start();
}
bool PIThread::start(ThreadFunc func) {
ret_func = func;
return start();
}
bool PIThread::start(ThreadFunc func, PISystemTime loop_delay) {
ret_func = func;
delay_ = loop_delay;
return start();
}
bool PIThread::start(std::function<void()> func) {
ret_func = [func](void *) { func(); };
return start(loop_delay);
return start();
}
bool PIThread::start(std::function<void()> func, PISystemTime loop_delay) {
ret_func = [func](void *) { func(); };
delay_ = loop_delay;
return start();
}
bool PIThread::startOnce() {
if (running_) return false;
return _startThread((void *)thread_function_once);
}
@@ -609,9 +641,15 @@ bool PIThread::startOnce(ThreadFunc func) {
}
void PIThread::stopAndWait(int timeout_ms) {
bool PIThread::startOnce(std::function<void()> func) {
ret_func = [func](void *) { func(); };
return startOnce();
}
void PIThread::stopAndWait(PISystemTime timeout) {
stop();
waitForFinish(timeout_ms);
waitForFinish(timeout);
}
@@ -621,19 +659,6 @@ void PIThread::stop() {
}
bool PIThread::start(int loop_delay) {
if (running_) return false;
delay_ = loop_delay;
return _startThread((void *)thread_function);
}
bool PIThread::startOnce() {
if (running_) return false;
return _startThread((void *)thread_function_once);
}
void PIThread::terminate() {
piCoutObj << "Warning, terminate!";
// PICout(PICoutManipulators::DefaultControls) << "thread" << this << "terminate ..." << running_;
@@ -766,7 +791,7 @@ void PIThread::setPriority(PIThread::Priority prior) {
#else
# ifndef WINDOWS
// PICout(PICoutManipulators::DefaultControls) << "setPriority" << PRIVATE->thread;
policy_ = 0;
int policy_ = 0;
memset(&(PRIVATE->sparam), 0, sizeof(PRIVATE->sparam));
pthread_getschedparam(PRIVATE->thread, &policy_, &(PRIVATE->sparam));
PRIVATE->sparam.
@@ -785,16 +810,6 @@ void PIThread::setPriority(PIThread::Priority prior) {
}
bool PIThread::waitForStart(PISystemTime timeout) {
return waitForStart(timeout.toMilliseconds());
}
bool PIThread::waitForFinish(PISystemTime timeout) {
return waitForFinish(timeout.toMilliseconds());
}
#ifdef WINDOWS
bool isExists(HANDLE hThread) {
// errorClear();
@@ -808,11 +823,12 @@ bool isExists(HANDLE hThread) {
}
#endif
bool PIThread::waitForFinish(int timeout_msecs) {
bool PIThread::waitForFinish(PISystemTime timeout) {
// PICout(PICoutManipulators::DefaultControls) << "thread" << this << "PIThread::waitForFinish" << running_ << terminating <<
// timeout_msecs;
if (!running_) return true;
if (timeout_msecs < 0) {
if (timeout.isNull()) {
while (running_) {
piMinSleep();
#ifdef WINDOWS
@@ -825,7 +841,7 @@ bool PIThread::waitForFinish(int timeout_msecs) {
return true;
}
tmf_.reset();
while (running_ && tmf_.elapsed_m() < timeout_msecs) {
while (running_ && tmf_.elapsed() < timeout) {
piMinSleep();
#ifdef WINDOWS
if (!isExists(PRIVATE->thread)) {
@@ -834,21 +850,21 @@ bool PIThread::waitForFinish(int timeout_msecs) {
}
#endif
}
return tmf_.elapsed_m() < timeout_msecs;
return tmf_.elapsed() < timeout;
}
bool PIThread::waitForStart(int timeout_msecs) {
bool PIThread::waitForStart(PISystemTime timeout) {
if (running_) return true;
if (timeout_msecs < 0) {
if (timeout.isNull()) {
while (!running_)
piMinSleep();
return true;
}
tms_.reset();
while (!running_ && tms_.elapsed_m() < timeout_msecs)
while (!running_ && tms_.elapsed() < timeout)
piMinSleep();
return tms_.elapsed_m() < timeout_msecs;
return tms_.elapsed() < timeout;
}
@@ -940,11 +956,11 @@ void PIThread::__thread_func__() {
_runThread();
// PICout(PICoutManipulators::DefaultControls) << "thread" << this << "wait" << "...";
PIINTROSPECTION_THREAD_WAIT(this);
if (delay_ > 0) {
if (delay_.isNotNull()) {
tmr_.reset();
double sl(0.);
while (1) {
sl = piMind(delay_ - tmr_.elapsed_m(), PIP_MIN_MSLEEP);
sl = piMind(delay_.toMilliseconds() - tmr_.elapsed_m(), PIP_MIN_MSLEEP);
if (terminating) break;
if (sl <= 0.) break;
piMSleep(sl);