Heart 1.4.984.b4712811
Heart is base back end library for your c++ Qt projects.
Loading...
Searching...
No Matches
async.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2018-2027 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 <thread>
16#include <chrono>
17#include <qaglobalutils.h>
18
19// Private implementation of waitFor functions.
20#define waitPrivate(CONDITION, TIMEOUT) \
21 auto curmsec = QDateTime::currentMSecsSinceEpoch() + TIMEOUT; \
22 while (curmsec > QDateTime::currentMSecsSinceEpoch() && !CONDITION) { \
23 QCoreApplication::processEvents(); \
24 } \
25 QCoreApplication::processEvents(); \
26 return CONDITION; \
27
28
29#define freazePrivate(CONDITION, TIMEOUT) \
30 auto curmsec = QDateTime::currentMSecsSinceEpoch() + TIMEOUT; \
31 while (curmsec > QDateTime::currentMSecsSinceEpoch() && !CONDITION) { \
32 std::this_thread::sleep_for(std::chrono::microseconds(1)); \
33 } \
34 return CONDITION; \
35
36namespace QH {
37
38Async::Async(QThread *thread):
39 QObject() {
40
41 threadAnalize(thread);
42 if (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), "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:29
#define waitPrivate(CONDITION, TIMEOUT)
Definition async.cpp:20
static QThread * mainThreadID()
mainThreadID This method return the pointer to main thread
std::function< bool()> Job
Definition async.h:34
Async(QThread *thread=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: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