Heart 1.3.877.fa09250
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-2025 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 {
38
39Async::Async(QThread *thread, QObject *ptr):
40 QObject(ptr) {
41
42 threadAnalize(thread);
43 moveToThread(thread);
44}
45
47
48}
49
50void Async::asyncHandler(Job job,
51 bool *endOfWork,
52 bool *resultOfWork) const {
53
54 bool result = job();
55
56 if (endOfWork && resultOfWork)
57 *resultOfWork = result;
58
59 if (endOfWork) {
60 *endOfWork = true;
61 }
62}
63
64void Async::threadAnalize(QThread *thread) {
65 QThread * mainThread = AbstractNode::mainThreadID();
66
67 debug_assert((mainThread != thread) && thread, "You try create async object into main thread");
68}
69
70bool Async::waitFor(bool *condition, int timeout, bool freaze) const {
71 if (!condition)
72 return false;
73
74 if (freaze) {
75 freazePrivate(*condition, timeout)
76 } else {
77 waitPrivate(*condition, timeout)
78 }
79}
80
81bool Async::asyncLauncher(const Async::Job &job, bool await, bool freaze) const {
82
83 if (QThread::currentThread() == thread()) {
84 return job();
85 }
86
87 if (!thread()->isRunning()) {
88
89 qCritical() << "The work threand of the async object is not running";
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, std::move(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, std::move(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:25
std::function< bool()> Job
Definition async.h:34
Async(QThread *thread, QObject *ptr=nullptr)
Async This is default constructor of the async object.
Definition async.cpp:39
bool waitFor(bool *condition, int timeout=WAIT_TIME, bool freaze=true) const
waitFor This is base wait function.
Definition async.cpp:70
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:81
#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