Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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
28QT_BEGIN_NAMESPACE
29
30namespace QtOhos {
31
32template<typename SlaveContext>
33class QOhosMtBlockingCallsGateway : public std::enable_shared_from_this<QOhosMtBlockingCallsGateway<SlaveContext>>
34{
35public:
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
52 std::function<void(SlaveContext &, QOhosTaskPromise<>)> &&task,
53 const std::string &callerContextName = std::string());
55 QOhosConsumer<std::function<void()>> &&task,
57
58private:
59 struct MasterThreadTaskState
60 {
61 bool started = false;
62 std::optional<MasterThreadTaskResult> result;
63 std::condition_variable resultSetCondVar;
64 };
65
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
77template<typename SlaveContext>
86
87template<typename SlaveContext>
88QOhosMtBlockingCallsGateway<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
96template<typename SlaveContext>
97void QOhosMtBlockingCallsGateway<SlaveContext>::invokeInMasterThread(std::function<void()> &&task)
98{
99 m_masterThreadTasksExecutor(std::move(task));
100}
101
102template<typename SlaveContext>
103void QOhosMtBlockingCallsGateway<SlaveContext>::invokeInSlaveThread(std::function<void(SlaveContext &)> &&task)
104{
105 m_slaveThreadTasksExecutor(std::move(task));
106}
107
108template<typename SlaveContext>
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
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
151template<typename SlaveContext>
153QOhosMtBlockingCallsGateway<SlaveContext>::tryInvokeInMasterThreadAndTryWaitForContinue(
154 QOhosConsumer<std::function<void()>> &&task,
156{
158
159 {
161 if (m_masterWaiting)
164 }
165
167
168 auto continueFunc = [weakSelf, taskState]() {
169 auto self = weakSelf.lock();
170 if (self) {
176 }
177 }
178 };
179
182 auto self = weakSelf.lock();
183 if (self) {
184 bool upToDate;
185 {
190 }
191 if (upToDate)
193 }
194 });
195
197 {
201 [&]() {
202 return taskState->result.has_value();
203 });
208 }
209
210 return result;
211}
212
213}
214
215QT_END_NAMESPACE
216
217#endif
JsState & jsState() const
~JsState() override
void invokeInSlaveThread(std::function< void(SlaveContext &)> &&task)
void runInSlaveThreadAndWaitForContinue(std::function< void(SlaveContext &, QOhosTaskPromise<>)> &&task, const std::string &callerContextName=std::string())
static std::shared_ptr< QOhosMtBlockingCallsGateway< SlaveContext > > makeInstance(QOhosConsumer< std::function< void()> > masterThreadTasksExecutor, QOhosConsumer< std::function< void(SlaveContext &)> > slaveThreadTasksExecutor)
void invokeInMasterThread(std::function< void()> &&task)
void * tryCastWithTypeIdObject(const void *matchTypeIdObject) final
static std::shared_ptr< QUiAbilityPeer > tryCastFromQAbilityPeerOrNull(std::shared_ptr< QAbilityPeer > qAbilityPeer)
~QUiAbilityPeer() override
std::string const char * mapBoolToTrueFalseStr(bool value)
void invokeInJsThread(std::function< void(JsState &)> task)
void dispatchNewWant(QNapi::Object want, QNapi::Object launchParam)
void removeMatchingJsQAbilityPeer(QNapi::Object qAbility)
void runInJsThreadAndWait(const std::function< void(JsState &)> &task, std::string callerContextName={})
void initJsThreadState(napi_env env, std::map< std::string, QNapi::Reference< QNapi::Function > > &&jsModulesFactories, std::shared_ptr< AppFunctions > appFunctions, QtRunMode qtRunMode)
Q_REQUIRED_RESULT bool tryInvokeInQtThreadAndTryWaitForContinue(std::function< void(std::function< void()>)> &&task, std::chrono::nanoseconds timeout)
void invokeInJsThreadAndWaitForContinue(std::function< void(JsState &, QOhosTaskPromise<>)> &&task, std::string callerContextName={})
enums::ohos::app::ability::AbilityConstant::OnContinueResult QOhosAbilityOnContinueResult
void addJsQAbilityPeer(std::shared_ptr< QAbilityPeer > qAbilityPeer)