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 | #include "barrier.h" |
6 | |
7 | QT_BEGIN_NAMESPACE |
8 | |
9 | namespace Tasking { |
10 | |
11 | // That's cut down qtcassert.{c,h} to avoid the dependency. |
12 | #define QT_STRING(cond) qDebug("SOFT ASSERT: \"%s\" in %s: %s", cond, __FILE__, QT_STRINGIFY(__LINE__)) |
13 | #define QT_ASSERT(cond, action) if (Q_LIKELY(cond)) {} else { QT_STRING(#cond); action; } do {} while (0) |
14 | |
15 | void Barrier::setLimit(int value) |
16 | { |
17 | QT_ASSERT(!isRunning(), return); |
18 | QT_ASSERT(value > 0, return); |
19 | |
20 | m_limit = value; |
21 | } |
22 | |
23 | void Barrier::start() |
24 | { |
25 | QT_ASSERT(!isRunning(), return); |
26 | m_current = 0; |
27 | m_result.reset(); |
28 | } |
29 | |
30 | void Barrier::advance() |
31 | { |
32 | // Calling advance on finished is OK |
33 | QT_ASSERT(isRunning() || m_result, return); |
34 | if (!isRunning()) // no-op |
35 | return; |
36 | ++m_current; |
37 | if (m_current == m_limit) |
38 | stopWithResult(result: DoneResult::Success); |
39 | } |
40 | |
41 | void Barrier::stopWithResult(DoneResult result) |
42 | { |
43 | // Calling stopWithResult on finished is OK when the same success is passed |
44 | QT_ASSERT(isRunning() || (m_result && *m_result == result), return); |
45 | if (!isRunning()) // no-op |
46 | return; |
47 | m_current = -1; |
48 | m_result = result; |
49 | emit done(success: result); |
50 | } |
51 | |
52 | } // namespace Tasking |
53 | |
54 | QT_END_NAMESPACE |
55 | |