disable dangerous asyn bahavior

This commit is contained in:
Andrei Yankovich 2021-11-01 13:39:01 +03:00
parent 3427a68592
commit a284cd7d66
2 changed files with 31 additions and 9 deletions

View File

@ -13,6 +13,8 @@
#include <QThread>
#include <QDebug>
#include <quasarapp.h>
#include <thread>
#include <chrono>
// Private implementation of waitFor functions.
#define waitPrivate(CONDITION, TIMEOUT) \
@ -23,6 +25,14 @@
QCoreApplication::processEvents(); \
return CONDITION; \
#define freazePrivate(CONDITION, TIMEOUT) \
auto curmsec = QDateTime::currentMSecsSinceEpoch() + TIMEOUT; \
while (curmsec > QDateTime::currentMSecsSinceEpoch() && !CONDITION) { \
std::this_thread::sleep_for(std::chrono::microseconds(1)); \
} \
return CONDITION; \
namespace QH {
Async::Async(QThread *thread, QObject *ptr):
QObject(ptr) {
@ -55,14 +65,18 @@ void Async::threadAnalize(QThread *thread) {
debug_assert((mainThread != thread) && thread, "You try create async object into main thread");
}
bool Async::waitFor(bool *condition, int timeout) const {
bool Async::waitFor(bool *condition, int timeout, bool freaze) const {
if (!condition)
return false;
waitPrivate(*condition, timeout)
if (freaze) {
freazePrivate(*condition, timeout)
} else {
waitPrivate(*condition, timeout)
}
}
bool Async::asyncLauncher(const Async::Job &job, bool await) const {
bool Async::asyncLauncher(const Async::Job &job, bool await, bool freaze) const {
if (QThread::currentThread() == thread()) {
return job();
@ -96,15 +110,20 @@ bool Async::asyncLauncher(const Async::Job &job, bool await) const {
if (!invockeResult)
return false;
if (!waitFor(&workOfEnd)) {
if (!waitFor(&workOfEnd, WAIT_TIME, freaze)) {
return false;
}
return workResult;
}
bool Async::waitFor(const std::function<bool ()> &condition, int timeout) const {
waitPrivate(condition(), timeout)
bool Async::waitFor(const std::function<bool ()> &condition, int timeout, bool freaze) const {
if (freaze) {
freazePrivate(condition(), timeout)
} else {
waitPrivate(condition(), timeout)
}
}
}

View File

@ -38,10 +38,11 @@ public:
* @brief asyncLauncher This method invoke a job on the thread (using the asyncHandler method) of this object.
* @param job This is function with needed job.
* @param await This is boolean option for enable or disable wait for finish of the job function.
* @param freaze This option disaable process event of waiting of results.
* @return true if the job function started correctly. If the await option is true then
* this method return result of job function.
*/
bool asyncLauncher(const Job &job, bool await = false) const;
bool asyncLauncher(const Job &job, bool await = false, bool freaze = true) const;
protected:
/**
@ -62,17 +63,19 @@ protected:
* @brief waitFor This is base wait function.
* @param condition This is pointer to awaiting boolean variable.
* @param timeout This is maximum time for wait. By default this value equals WAIT_TIME it is 30000 msec.
* @param freaze This frease current thread for waiting results of another thread. If you set this option to false then will be invoked process event method.
* @return true if condition is true.
*/
bool waitFor(bool* condition, int timeout = WAIT_TIME) const;
bool waitFor(bool* condition, int timeout = WAIT_TIME, bool freaze = true) const;
/**
* @brief waitFor This is base wait function.
* @param condition This is lambda method with condition results.
* @param timeout This is maximum time for wait. By default this value equals WAIT_TIME it is 30000 msec.
* @param freaze This frease current thread for waiting results of another thread. If you set this option to false then will be invoked process event method.
* @return true if condition is true.
*/
bool waitFor(const Job &condition, int timeout = WAIT_TIME) const;
bool waitFor(const Job &condition, int timeout = WAIT_TIME, bool freaze = true) const;
private slots: