Heart
1.4.918.d927d9c
Heart is base back end library for your c++ Qt projects.
Toggle main menu visibility
Loading...
Searching...
No Matches
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 "
async.h
"
9
10
#include <QCoreApplication>
11
#include <QDateTime>
12
#include <QThread>
13
#include <QDebug>
14
#include <quasarapp.h>
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
36
namespace
QH
{
37
38
Async::Async
(QThread *thread):
39
QObject() {
40
41
threadAnalize(thread);
42
moveToThread(thread);
43
}
44
45
Async::~Async
() {
46
47
}
48
49
void
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
63
void
Async::threadAnalize(QThread *thread) {
64
debug_assert(thread,
"Async object must be initialized with a valid work thread"
);
65
}
66
67
bool
Async::waitFor
(
bool
*condition,
int
timeout,
bool
freaze)
const
{
68
if
(!condition)
69
return
false
;
70
71
if
(freaze) {
72
freazePrivate
(*condition, timeout)
73
}
else
{
74
waitPrivate
(*condition, timeout)
75
}
76
}
77
78
bool
Async::asyncLauncher
(
const
Async::Job
&job,
bool
await,
bool
freaze)
const
{
79
80
if
(QThread::currentThread() == thread()) {
81
return
job();
82
}
83
84
if
(!thread()->isRunning()) {
85
86
qCritical() <<
"The work threand of the async object is not running"
;
87
88
return
false
;
89
}
90
91
if
(!await) {
92
return
QMetaObject::invokeMethod(
const_cast<
Async
*
>
(
this
),
93
"asyncHandler"
,
94
Qt::QueuedConnection,
95
Q_ARG(
QH::Async::Job
, std::move(job)));
96
}
97
98
bool
workOfEnd =
false
, workResult =
false
;
99
100
bool
invockeResult = QMetaObject::invokeMethod(
const_cast<
Async
*
>
(
this
),
101
"asyncHandler"
,
102
Qt::QueuedConnection,
103
Q_ARG(
QH::Async::Job
, std::move(job)),
104
Q_ARG(
bool
*, &workOfEnd),
105
Q_ARG(
bool
*, &workResult));
106
107
108
if
(!invockeResult)
109
return
false
;
110
111
if
(!
waitFor
(&workOfEnd,
WAIT_TIME
, freaze)) {
112
return
false
;
113
}
114
115
return
workResult;
116
}
117
118
bool
Async::waitFor
(
const
std::function<
bool
()> &condition,
int
timeout,
bool
freaze)
const
{
119
if
(freaze) {
120
freazePrivate
(condition(), timeout)
121
}
else
{
122
waitPrivate
(condition(), timeout)
123
}
124
125
}
126
127
}
freazePrivate
#define freazePrivate(CONDITION, TIMEOUT)
Definition
async.cpp:29
waitPrivate
#define waitPrivate(CONDITION, TIMEOUT)
Definition
async.cpp:20
async.h
QH::Async::Job
std::function< bool()> Job
Definition
async.h:34
QH::Async::Async
Async(QThread *thread)
Async This is default constructor of the async object.
Definition
async.cpp:38
QH::Async::waitFor
bool waitFor(bool *condition, int timeout=WAIT_TIME, bool freaze=true) const
waitFor This is base wait function.
Definition
async.cpp:67
QH::Async::~Async
~Async()
Definition
async.cpp:45
QH::Async::asyncLauncher
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:78
WAIT_TIME
#define WAIT_TIME
Definition
config.h:13
QH
The QH namespace - QuasarApp Heart namespace. This namespace contains all classes of the Heart librar...
Definition
heart.cpp:13
src
public
async.cpp
Generated by
1.18.0