Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qohosmtblockingcallsgateway_p.h
Go to the documentation of this file.
1
// Copyright (C) 2025 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4
#
ifndef
QOHOSMTBLOCKINGCALLSGATEWAY_H
5
#
define
QOHOSMTBLOCKINGCALLSGATEWAY_H
6
7
//
8
// W A R N I N G
9
// -------------
10
//
11
// This file is not part of the Qt API. It exists purely as an
12
// implementation detail. This header file may change from version to
13
// version without notice, or even be removed.
14
//
15
// We mean it.
16
//
17
18
#
include
<
QtCore
/
private
/
qohoscommon_p
.
h
>
19
#
include
<
QtCore
/
qglobal
.
h
>
20
#
include
<
chrono
>
21
#
include
<
condition_variable
>
22
#
include
<
functional
>
23
#
include
<
future
>
24
#
include
<
memory
>
25
#
include
<
mutex
>
26
#
include
<
optional
>
27
28
QT_BEGIN_NAMESPACE
29
30
namespace
QtOhos
{
31
32
template
<
typename
SlaveContext>
33
class
QOhosMtBlockingCallsGateway
:
public
std
::enable_shared_from_this<
QOhosMtBlockingCallsGateway
<SlaveContext>>
34
{
35
public
:
36
enum
class
MasterThreadTaskResult
37
{
38
CancelledByDeadlock
,
39
TimeoutedBeforeStart
,
40
TimeoutedAfterStart
,
41
Finished
,
42
};
43
44
static
std
::shared_ptr<
QOhosMtBlockingCallsGateway
<SlaveContext>>
makeInstance
(
45
QOhosConsumer<std::function<
void
()>> masterThreadTasksExecutor,
46
QOhosConsumer<std::function<
void
(SlaveContext &)>> slaveThreadTasksExecutor);
47
48
void
invokeInMasterThread
(
std
::function<
void
()> &&task);
49
void
invokeInSlaveThread
(
std
::function<
void
(SlaveContext &)> &&task);
50
51
void
runInSlaveThreadAndWaitForContinue
(
52
std
::function<
void
(SlaveContext &, QOhosTaskPromise<>)> &&task,
53
const
std
::string &callerContextName =
std
::string());
54
Q_REQUIRED_RESULT
MasterThreadTaskResult
tryInvokeInMasterThreadAndTryWaitForContinue
(
55
QOhosConsumer
<
std
::
function
<
void
()>> &&
task
,
56
std
::
chrono
::
nanoseconds
timeout
);
57
58
private
:
59
struct
MasterThreadTaskState
60
{
61
bool
started =
false
;
62
std
::optional<
MasterThreadTaskResult
> result;
63
std
::condition_variable resultSetCondVar;
64
};
65
66
QOhosMtBlockingCallsGateway
(
67
QOhosConsumer<std::function<
void
()>> masterThreadTasksExecutor,
68
QOhosConsumer<std::function<
void
(SlaveContext &)>> slaveThreadTasksExecutor);
69
70
QOhosConsumer<std::function<
void
()>> m_masterThreadTasksExecutor;
71
QOhosConsumer<std::function<
void
(SlaveContext &)>> m_slaveThreadTasksExecutor;
72
std
::mutex m_waitStateMutex;
73
bool
m_masterWaiting =
false
;
74
std
::shared_ptr<MasterThreadTaskState> m_masterThreadTaskState;
75
};
76
77
template
<
typename
SlaveContext>
78
std
::shared_ptr<
QOhosMtBlockingCallsGateway
<SlaveContext>>
QOhosMtBlockingCallsGateway
<SlaveContext>::makeInstance(
79
QOhosConsumer
<
std
::
function
<
void
()>>
masterThreadTasksExecutor
,
80
QOhosConsumer
<
std
::
function
<
void
(
SlaveContext
&)>>
slaveThreadTasksExecutor
)
81
{
82
return
std
::
shared_ptr
<
QOhosMtBlockingCallsGateway
<
SlaveContext
>>(
83
new
QOhosMtBlockingCallsGateway
<
SlaveContext
>(
84
std
::
move
(
masterThreadTasksExecutor
),
std
::
move
(
slaveThreadTasksExecutor
)));
85
}
86
87
template
<
typename
SlaveContext>
88
QOhosMtBlockingCallsGateway
<SlaveContext>::QOhosMtBlockingCallsGateway(
89
QOhosConsumer<std::function<
void
()>> masterThreadTasksExecutor,
90
QOhosConsumer<std::function<
void
(SlaveContext &)>> slaveThreadTasksExecutor)
91
: m_masterThreadTasksExecutor(std::move(masterThreadTasksExecutor))
92
, m_slaveThreadTasksExecutor(std::move(slaveThreadTasksExecutor))
93
{
94
}
95
96
template
<
typename
SlaveContext>
97
void
QOhosMtBlockingCallsGateway
<SlaveContext>::
invokeInMasterThread
(
std
::function<
void
()> &&task)
98
{
99
m_masterThreadTasksExecutor(std::move(task));
100
}
101
102
template
<
typename
SlaveContext>
103
void
QOhosMtBlockingCallsGateway
<SlaveContext>::
invokeInSlaveThread
(
std
::function<
void
(SlaveContext &)> &&task)
104
{
105
m_slaveThreadTasksExecutor(std::move(task));
106
}
107
108
template
<
typename
SlaveContext>
109
void
QOhosMtBlockingCallsGateway
<SlaveContext>::
runInSlaveThreadAndWaitForContinue
(
110
std
::function<
void
(SlaveContext &, QOhosTaskPromise<>)> &&task,
111
const
std
::string &callerContextName)
112
{
113
const
auto
funcInfo = Q_FUNC_INFO;
114
115
{
116
std
::lock_guard<
std
::mutex> waitStateLock(m_waitStateMutex);
117
if
(m_masterThreadTaskState) {
118
auto
taskState = std::exchange(m_masterThreadTaskState,
nullptr
);
119
taskState->result =
MasterThreadTaskResult
::CancelledByDeadlock;
120
taskState->resultSetCondVar.notify_all();
121
}
122
m_masterWaiting =
true
;
123
}
124
125
auto
taskFinishedPromise =
std
::make_shared<
std
::promise<
void
>>();
126
auto
taskFinishedFuture = taskFinishedPromise->get_future();
127
128
auto
sharedTaskPromise = std::make_shared<QOhosTaskPromise<>>(
129
[taskFinishedPromise]() {
130
taskFinishedPromise->set_value();
131
},
132
[funcInfo, callerContextName]() {
133
qOhosReportFatalErrorAndAbort(
134
"%s: promise destroyed without notifying the caller: %s"
,
135
funcInfo, callerContextName.c_str());
136
},
137
callerContextName);
138
139
invokeInSlaveThread
(
140
[task =
std
::move(task), sharedTaskPromise](SlaveContext &context) {
141
task(context,
std
::move(*sharedTaskPromise));
142
}
)
;
143
taskFinishedFuture.wait();
144
145
{
146
std
::lock_guard<
std
::mutex> waitStateLock(m_waitStateMutex);
147
m_masterWaiting =
false
;
148
}
149
}
150
151
template
<
typename
SlaveContext>
152
typename
QOhosMtBlockingCallsGateway
<SlaveContext>::
MasterThreadTaskResult
153
QOhosMtBlockingCallsGateway
<SlaveContext>::tryInvokeInMasterThreadAndTryWaitForContinue(
154
QOhosConsumer
<
std
::
function
<
void
()>> &&
task
,
155
std
::
chrono
::
nanoseconds
timeout
)
156
{
157
auto
taskState
=
std
::
make_shared
<
MasterThreadTaskState
>();
158
159
{
160
std
::
lock_guard
<
std
::
mutex
>
waitStateLock
(
m_waitStateMutex
);
161
if
(
m_masterWaiting
)
162
return
MasterThreadTaskResult
::
CancelledByDeadlock
;
163
m_masterThreadTaskState
=
taskState
;
164
}
165
166
auto
weakSelf
=
std
::
weak_ptr
<
QOhosMtBlockingCallsGateway
<
SlaveContext
>>(
this
->
shared_from_this
());
167
168
auto
continueFunc
= [
weakSelf
,
taskState
]() {
169
auto
self
=
weakSelf
.
lock
();
170
if
(
self
) {
171
std
::
lock_guard
<
std
::
mutex
>
waitStateLock
(
self
->
m_waitStateMutex
);
172
if
(
self
->
m_masterThreadTaskState
==
taskState
) {
173
self
->
m_masterThreadTaskState
.
reset
();
174
taskState
->
result
=
MasterThreadTaskResult
::
Finished
;
175
taskState
->
resultSetCondVar
.
notify_all
();
176
}
177
}
178
};
179
180
invokeInMasterThread
(
181
[
weakSelf
,
taskState
,
task
=
std
::
move
(
task
),
continueFunc
=
std
::
move
(
continueFunc
)]()
mutable
{
182
auto
self
=
weakSelf
.
lock
();
183
if
(
self
) {
184
bool
upToDate
;
185
{
186
std
::
lock_guard
<
std
::
mutex
>
waitStateLock
(
self
->
m_waitStateMutex
);
187
if
(
self
->
m_masterThreadTaskState
==
taskState
)
188
self
->
m_masterThreadTaskState
->
started
=
true
;
189
upToDate
=
self
->
m_masterThreadTaskState
==
taskState
;
190
}
191
if
(
upToDate
)
192
task
(
std
::
move
(
continueFunc
));
193
}
194
});
195
196
MasterThreadTaskResult
result
;
197
{
198
std
::
unique_lock
<
std
::
mutex
>
waitStateLock
(
m_waitStateMutex
);
199
taskState
->
resultSetCondVar
.
wait_for
(
200
waitStateLock
,
timeout
,
201
[&]() {
202
return
taskState
->
result
.
has_value
();
203
});
204
result
=
taskState
->
result
.
value_or
(
205
taskState
->
started
206
?
MasterThreadTaskResult
::
TimeoutedAfterStart
207
:
MasterThreadTaskResult
::
TimeoutedBeforeStart
);
208
}
209
210
return
result
;
211
}
212
213
}
214
215
QT_END_NAMESPACE
216
217
#
endif
QtOhos::AppFunctions
Definition
qohosplugincore.h:61
QtOhos::AppFunctions::~AppFunctions
virtual ~AppFunctions()
QtOhos::CallbackInfo
Definition
qohosplugincore.h:127
QtOhos::CallbackInfo::jsState
JsState & jsState() const
Definition
qohosplugincore.cpp:881
QtOhos::JsState
Definition
qohosplugincore.h:79
QtOhos::JsState::~JsState
~JsState() override
QtOhos::JsState::JsState
JsState()
QtOhos::JsWindowsTracker
Definition
qohosplugincore.h:28
QtOhos::QAbilityPeer
Definition
qohosplugincore.h:37
QtOhos::QAbilityPeer::QAbilityPeer
QAbilityPeer()
QtOhos::QAbilityPeer::~QAbilityPeer
virtual ~QAbilityPeer()
QtOhos::QOhosMtBlockingCallsGateway
Definition
qohosmtblockingcallsgateway_p.h:34
QtOhos::QOhosMtBlockingCallsGateway::invokeInSlaveThread
void invokeInSlaveThread(std::function< void(SlaveContext &)> &&task)
Definition
qohosmtblockingcallsgateway_p.h:103
QtOhos::QOhosMtBlockingCallsGateway::runInSlaveThreadAndWaitForContinue
void runInSlaveThreadAndWaitForContinue(std::function< void(SlaveContext &, QOhosTaskPromise<>)> &&task, const std::string &callerContextName=std::string())
Definition
qohosmtblockingcallsgateway_p.h:109
QtOhos::QOhosMtBlockingCallsGateway::makeInstance
static std::shared_ptr< QOhosMtBlockingCallsGateway< SlaveContext > > makeInstance(QOhosConsumer< std::function< void()> > masterThreadTasksExecutor, QOhosConsumer< std::function< void(SlaveContext &)> > slaveThreadTasksExecutor)
Definition
qohosmtblockingcallsgateway_p.h:78
QtOhos::QOhosMtBlockingCallsGateway::invokeInMasterThread
void invokeInMasterThread(std::function< void()> &&task)
Definition
qohosmtblockingcallsgateway_p.h:97
QtOhos::QOhosMtBlockingCallsGateway::MasterThreadTaskResult
MasterThreadTaskResult
Definition
qohosmtblockingcallsgateway_p.h:37
QtOhos::QOhosMtBlockingCallsGateway::MasterThreadTaskResult::CancelledByDeadlock
@ CancelledByDeadlock
Definition
qohosmtblockingcallsgateway_p.h:38
QtOhos::QOhosMtBlockingCallsGateway::MasterThreadTaskResult::Finished
@ Finished
Definition
qohosmtblockingcallsgateway_p.h:41
QtOhos::QOhosMtBlockingCallsGateway::MasterThreadTaskResult::TimeoutedBeforeStart
@ TimeoutedBeforeStart
Definition
qohosmtblockingcallsgateway_p.h:39
QtOhos::QOhosMtBlockingCallsGateway::MasterThreadTaskResult::TimeoutedAfterStart
@ TimeoutedAfterStart
Definition
qohosmtblockingcallsgateway_p.h:40
QtOhos::QUiAbilityPeer
Definition
qohosplugincore.h:103
QtOhos::QUiAbilityPeer::QUiAbilityPeer
QUiAbilityPeer()
QtOhos::QUiAbilityPeer::tryCastWithTypeIdObject
void * tryCastWithTypeIdObject(const void *matchTypeIdObject) final
Definition
qohosplugincore.cpp:850
QtOhos::QUiAbilityPeer::tryCastFromQAbilityPeerOrNull
static std::shared_ptr< QUiAbilityPeer > tryCastFromQAbilityPeerOrNull(std::shared_ptr< QAbilityPeer > qAbilityPeer)
Definition
qohosplugincore.cpp:855
QtOhos::QUiAbilityPeer::~QUiAbilityPeer
~QUiAbilityPeer() override
QtOhosQpa::enums::ohos::app::ability::AbilityConstant::OnContinueResult::REJECT
@ REJECT
Definition
qohosqpaenums.h:89
QtOhos
Definition
qcore_ohos.cpp:59
QtOhos::mapBoolToTrueFalseStr
std::string const char * mapBoolToTrueFalseStr(bool value)
Definition
qohosutils.cpp:55
QtOhos::predefinedJsModuleNames
std::vector< std::string > predefinedJsModuleNames()
Definition
qohosplugincore.cpp:905
QtOhos::initJsThreadState
void initJsThreadState(napi_env env, std::map< std::string, QNapi::Reference< QNapi::Function > > &&jsModulesFactories, std::shared_ptr< AppFunctions > appFunctions, QtRunMode qtRunMode)
Definition
qohosplugincore.cpp:910
QtOhos::tryInvokeInQtThreadAndTryWaitForContinue
Q_REQUIRED_RESULT bool tryInvokeInQtThreadAndTryWaitForContinue(std::function< void(std::function< void()>)> &&task, std::chrono::nanoseconds timeout)
Definition
qohosplugincore.cpp:932
QtOhos::isJsThread
bool isJsThread()
Definition
qohosplugincore.cpp:922
QtOhos::QOhosAbilityOnContinueResult
QtOhosQpa::enums::ohos::app::ability::AbilityConstant::OnContinueResult QOhosAbilityOnContinueResult
Definition
qohosplugincore.h:100
QtOhos::dispatchNewWant
void dispatchNewWant(QNapi::Object qAbility, QNapi::Object want, QNapi::Object launchParam)
Definition
qohosplugincore.cpp:927
QtOhos::QtRunMode
QtRunMode
Definition
qohosplugincore.h:73
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
QtOhos::QAbilityInstancesManager
Definition
qohosqabilityinstancesmanager.h:56
QtOhos::QAbilityInstancesManager::defaultQAbilityPeer
virtual std::shared_ptr< QAbilityPeer > defaultQAbilityPeer()=0
QtOhos::QAbilityInstancesManager::tryGetQAbilityPeerByInstanceId
virtual std::shared_ptr< QAbilityPeer > tryGetQAbilityPeerByInstanceId(const std::string &instanceId)=0
qtbase
src
plugins
platforms
ohos
qohosmtblockingcallsgateway_p.h
Generated on
for Qt by
1.18.0