Heart 1.3.842.34c2ab5
Heart is base back end library for your c++ Qt projects.
async.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2018-2024 QuasarApp.
3 * Distributed under the lgplv3 software license, see the accompanying
4 * Everyone is permitted to copy and distribute verbatim copies
5 * of this license document, but changing it is not allowed.
6*/
7
8#include "abstractnode.h"
9#include "async.h"
10
11#include <QCoreApplication>
12#include <QDateTime>
13#include <QThread>
14#include <QDebug>
15#include <quasarapp.h>
16#include <thread>
17#include <chrono>
18#include <qaglobalutils.h>
19
20// Private implementation of waitFor functions.
21#define waitPrivate(CONDITION, TIMEOUT) \
22 auto curmsec = QDateTime::currentMSecsSinceEpoch() + TIMEOUT; \
23 while (curmsec > QDateTime::currentMSecsSinceEpoch() && !CONDITION) { \
24 QCoreApplication::processEvents(); \
25 } \
26 QCoreApplication::processEvents(); \
27 return CONDITION; \
28
29
30#define freazePrivate(CONDITION, TIMEOUT) \
31 auto curmsec = QDateTime::currentMSecsSinceEpoch() + TIMEOUT; \
32 while (curmsec > QDateTime::currentMSecsSinceEpoch() && !CONDITION) { \
33 std::this_thread::sleep_for(std::chrono::microseconds(1)); \
34 } \
35 return CONDITION; \
36
37namespace QH {
38Async::Async(QThread *thread, QObject *ptr):
39 QObject(ptr) {
40
41 threadAnalize(thread);
42 moveToThread(thread);
43}
44
46
47}
48
49void Async::asyncHandler(Job job,
50 bool *endOfWork,
51 bool *resultOfWork) const {
52
53 bool result = job();
54
55 if (endOfWork && resultOfWork)
56 *resultOfWork = result;
57
58 if (endOfWork) {
59 *endOfWork = true;
60 }
61}
62
63void Async::threadAnalize(QThread *thread) {
64 QThread * mainThread = AbstractNode::mainThreadID();
65
66 debug_assert((mainThread != thread) && thread, "You try create async object into main thread");
67}
68
69bool Async::waitFor(bool *condition, int timeout, bool freaze) const {
70 if (!condition)
71 return false;
72
73 if (freaze) {
74 freazePrivate(*condition, timeout)
75 } else {
76 waitPrivate(*condition, timeout)
77 }
78}
79
80bool Async::asyncLauncher(const Async::Job &job, bool await, bool freaze) const {
81
82 if (QThread::currentThread() == thread()) {
83 return job();
84 }
85
86 if (!thread()->isRunning()) {
87
88 QuasarAppUtils::Params::log("The work threand of the async object is not running",
89 QuasarAppUtils::Error);
90
91 return false;
92 }
93
94 if (!await) {
95 return QMetaObject::invokeMethod(const_cast<Async*>(this),
96 "asyncHandler",
97 Qt::QueuedConnection,
98 Q_ARG(QH::Async::Job, job));
99 }
100
101 bool workOfEnd = false, workResult = false;
102
103 bool invockeResult = QMetaObject::invokeMethod(const_cast<Async*>(this),
104 "asyncHandler",
105 Qt::QueuedConnection,
106 Q_ARG(QH::Async::Job, job),
107 Q_ARG(bool*, &workOfEnd),
108 Q_ARG(bool*, &workResult));
109
110
111 if (!invockeResult)
112 return false;
113
114 if (!waitFor(&workOfEnd, WAIT_TIME, freaze)) {
115 return false;
116 }
117
118 return workResult;
119}
120
121bool Async::waitFor(const std::function<bool ()> &condition, int timeout, bool freaze) const {
122 if (freaze) {
123 freazePrivate(condition(), timeout)
124 } else {
125 waitPrivate(condition(), timeout)
126 }
127
128}
129
130}
#define freazePrivate(CONDITION, TIMEOUT)
Definition async.cpp:30
#define waitPrivate(CONDITION, TIMEOUT)
Definition async.cpp:21
static QThread * mainThreadID()
mainThreadID This method return the pointer to main thread
The Async class This is bundle of async templates and async wrappers.
Definition async.h:26
std::function< bool()> Job
Definition async.h:35
Async(QThread *thread, QObject *ptr=nullptr)
Async This is default constructor of the async object.
Definition async.cpp:38
bool waitFor(bool *condition, int timeout=WAIT_TIME, bool freaze=true) const
waitFor This is base wait function.
Definition async.cpp:69
bool asyncLauncher(const Job &job, bool await=false, bool freaze=true) const
asyncLauncher This method invoke a job on the thread (using the asyncHandler method) of this object.
Definition async.cpp:80
#define WAIT_TIME
Definition config.h:13
The QH namespace - QuasarApp Heart namespace. This namespace contains all classes of the Heart librar...
Definition heart.cpp:13