Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
barrier.h
Go to the documentation of this file.
1// Copyright (C) 2024 Jarek Kobus
2// Copyright (C) 2024 The Qt Company Ltd.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#ifndef TASKING_BARRIER_H
6#define TASKING_BARRIER_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include "tasking_global.h"
20
21#include "tasktree.h"
22
24
25namespace Tasking {
26
27class TASKING_EXPORT Barrier final : public QObject
28{
30
31public:
32 void setLimit(int value);
33 int limit() const { return m_limit; }
34
35 void start();
36 void advance(); // If limit reached, stops with true
37 void stopWithResult(DoneResult result); // Ignores limit
38
39 bool isRunning() const { return m_current >= 0; }
40 int current() const { return m_current; }
41 std::optional<DoneResult> result() const { return m_result; }
42
44 void done(DoneResult success);
45
46private:
47 std::optional<DoneResult> m_result = {};
48 int m_limit = 1;
49 int m_current = -1;
50};
51
53{
54public:
55 BarrierTaskAdapter() { connect(task(), &Barrier::done, this, &TaskInterface::done); }
56 void start() final { task()->start(); }
57};
58
59using BarrierTask = CustomTask<BarrierTaskAdapter>;
60
61template <int Limit = 1>
63{
64public:
65 static_assert(Limit > 0, "SharedBarrier's limit should be 1 or more.");
66 SharedBarrier() : m_barrier(new Barrier) {
67 m_barrier->setLimit(Limit);
68 m_barrier->start();
69 }
70 Barrier *barrier() const { return m_barrier.get(); }
71
72private:
73 std::shared_ptr<Barrier> m_barrier;
74};
75
76template <int Limit = 1>
77using MultiBarrier = Storage<SharedBarrier<Limit>>;
78
79// Can't write: "MultiBarrier barrier;". Only "MultiBarrier<> barrier;" would work.
80// Can't have one alias with default type in C++17, getting the following error:
81// alias template deduction only available with C++20.
82using SingleBarrier = MultiBarrier<1>;
83
84template <int Limit>
85GroupItem waitForBarrierTask(const MultiBarrier<Limit> &sharedBarrier)
86{
87 return BarrierTask([sharedBarrier](Barrier &barrier) {
88 SharedBarrier<Limit> *activeBarrier = sharedBarrier.activeStorage();
89 if (!activeBarrier) {
90 qWarning("The barrier referenced from WaitForBarrier element "
91 "is not reachable in the running tree. "
92 "It is possible that no barrier was added to the tree, "
93 "or the storage is not reachable from where it is referenced. "
94 "The WaitForBarrier task finishes with an error. ");
96 }
97 Barrier *activeSharedBarrier = activeBarrier->barrier();
98 const std::optional<DoneResult> result = activeSharedBarrier->result();
99 if (result.has_value()) {
102 }
103 QObject::connect(activeSharedBarrier, &Barrier::done, &barrier, &Barrier::stopWithResult);
105 });
106}
107
108} // namespace Tasking
109
111
112#endif // TASKING_BARRIER_H
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
void start() final
This method is called by the running TaskTree for starting the Task instance.
Definition barrier.h:56
int limit() const
Definition barrier.h:33
bool isRunning() const
Definition barrier.h:39
std::optional< DoneResult > result() const
Definition barrier.h:41
void stopWithResult(DoneResult result)
Definition barrier.cpp:41
int current() const
Definition barrier.h:40
void done(DoneResult success)
\inheaderfile solutions/tasking/tasktree.h \inmodule TaskingSolution
Definition tasktree.h:209
Barrier * barrier() const
Definition barrier.h:70
\inheaderfile solutions/tasking/tasktree.h \inmodule TaskingSolution
Definition tasktree.h:453
Combined button and popup list for selecting options.
\inmodule TaskingSolution
Definition barrier.cpp:9
GroupItem waitForBarrierTask(const MultiBarrier< Limit > &sharedBarrier)
Definition barrier.h:85
CustomTask< BarrierTaskAdapter > BarrierTask
Definition barrier.h:59
MultiBarrier< 1 > SingleBarrier
Definition barrier.h:82
Storage< SharedBarrier< Limit > > MultiBarrier
Definition barrier.h:77
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define qWarning
Definition qlogging.h:167
GLuint start
GLuint64EXT * result
[6]
#define Q_OBJECT
#define Q_SIGNALS
connect(quitButton, &QPushButton::clicked, &app, &QCoreApplication::quit, Qt::QueuedConnection)
#define TASKING_EXPORT