Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qsemaphore.h
Go to the documentation of this file.
1// Copyright (C) 2016 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 QSEMAPHORE_H
5#define QSEMAPHORE_H
6
7#include <QtCore/qglobal.h>
8#include <QtCore/qdeadlinetimer.h>
9
11
13
15class Q_CORE_EXPORT QSemaphore
16{
17public:
18 explicit QSemaphore(int n = 0);
20
21 void acquire(int n = 1);
22 bool tryAcquire(int n = 1);
23 QT_CORE_INLINE_SINCE(6, 6)
24 bool tryAcquire(int n, int timeout);
26#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
27 template <typename Rep, typename Period>
28 bool tryAcquire(int n, std::chrono::duration<Rep, Period> timeout)
29 { return tryAcquire(n, QDeadlineTimer(timeout)); }
30#endif
31
32 void release(int n = 1);
33
34 int available() const;
35
36 // std::counting_semaphore compatibility:
37 bool try_acquire() noexcept { return tryAcquire(); }
38 template <typename Rep, typename Period>
39 bool try_acquire_for(const std::chrono::duration<Rep, Period> &timeout)
40 { return tryAcquire(1, timeout); }
41 template <typename Clock, typename Duration>
42 bool try_acquire_until(const std::chrono::time_point<Clock, Duration> &tp)
43 {
44 return try_acquire_for(tp - Clock::now());
45 }
46private:
47 Q_DISABLE_COPY(QSemaphore)
48
49 union {
54 };
55};
56
57#if QT_CORE_INLINE_IMPL_SINCE(6, 6)
58bool QSemaphore::tryAcquire(int n, int timeout)
59{
61}
62#endif
63
65{
66public:
68 QSemaphoreReleaser() = default;
70 explicit QSemaphoreReleaser(QSemaphore &sem, int n = 1) noexcept
71 : m_sem(&sem), m_n(n) {}
73 explicit QSemaphoreReleaser(QSemaphore *sem, int n = 1) noexcept
74 : m_sem(sem), m_n(n) {}
77 : m_sem(other.cancel()), m_n(other.m_n) {}
78 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QSemaphoreReleaser)
79
81 {
82 if (m_sem)
83 m_sem->release(m_n);
84 }
85
87 {
88 qt_ptr_swap(m_sem, other.m_sem);
89 std::swap(m_n, other.m_n);
90 }
91
92 QSemaphore *semaphore() const noexcept
93 { return m_sem; }
94
95 QSemaphore *cancel() noexcept
96 {
97 return std::exchange(m_sem, nullptr);
98 }
99
100private:
101 QSemaphore *m_sem = nullptr;
102 int m_n;
103};
104
106
107#endif // QSEMAPHORE_H
\inmodule QtCore
The QSemaphoreReleaser class provides exception-safe deferral of a QSemaphore::release() call.
Definition qsemaphore.h:65
Q_NODISCARD_CTOR QSemaphoreReleaser()=default
Default constructor.
Q_NODISCARD_CTOR QSemaphoreReleaser(QSemaphore *sem, int n=1) noexcept
Constructor.
Definition qsemaphore.h:73
~QSemaphoreReleaser()
Unless canceled, calls QSemaphore::release() with the arguments provided to the constructor,...
Definition qsemaphore.h:80
Q_NODISCARD_CTOR QSemaphoreReleaser(QSemaphoreReleaser &&other) noexcept
Move constructor.
Definition qsemaphore.h:76
QSemaphore * cancel() noexcept
Cancels this QSemaphoreReleaser such that the destructor will no longer call {semaphore()->release()}...
Definition qsemaphore.h:95
Q_NODISCARD_CTOR QSemaphoreReleaser(QSemaphore &sem, int n=1) noexcept
Constructor.
Definition qsemaphore.h:70
void swap(QSemaphoreReleaser &other) noexcept
Exchanges the responsibilities of {*this} and other.
Definition qsemaphore.h:86
QSemaphore * semaphore() const noexcept
Returns a pointer to the QSemaphore object provided to the constructor, or by the last move assignmen...
Definition qsemaphore.h:92
\inmodule QtCore
Definition qsemaphore.h:16
bool try_acquire() noexcept
Definition qsemaphore.h:37
QSemaphorePrivate * d
Definition qsemaphore.h:50
bool try_acquire_for(const std::chrono::duration< Rep, Period > &timeout)
Definition qsemaphore.h:39
QBasicAtomicInteger< quintptr > u
Definition qsemaphore.h:51
bool tryAcquire(int n=1)
Tries to acquire n resources guarded by the semaphore and returns true on success.
QBasicAtomicInteger< quint64 > u64
Definition qsemaphore.h:53
void release(int n=1)
Releases n resources guarded by the semaphore.
bool try_acquire_until(const std::chrono::time_point< Clock, Duration > &tp)
Definition qsemaphore.h:42
Combined button and popup list for selecting options.
#define Q_NODISCARD_CTOR
GLbitfield GLuint64 timeout
[4]
GLfloat n
constexpr void qt_ptr_swap(T *&lhs, T *&rhs) noexcept
Definition qswap.h:43
#define QT_REQUIRE_CONFIG(feature)
sem acquire()
sem release()
QSemaphore sem(5)
[0]
sem tryAcquire(1)
QSharedPointer< T > other(t)
[5]