File indexing completed on 2025-01-18 10:07:35
0001
0002
0003
0004 #ifndef QSEMAPHORE_H
0005 #define QSEMAPHORE_H
0006
0007 #include <QtCore/qglobal.h>
0008 #include <QtCore/qdeadlinetimer.h>
0009
0010 #include <chrono>
0011
0012 QT_REQUIRE_CONFIG(thread);
0013
0014 QT_BEGIN_NAMESPACE
0015
0016 class QSemaphorePrivate;
0017 class Q_CORE_EXPORT QSemaphore
0018 {
0019 public:
0020 explicit QSemaphore(int n = 0);
0021 ~QSemaphore();
0022
0023 void acquire(int n = 1);
0024 bool tryAcquire(int n = 1);
0025 QT_CORE_INLINE_SINCE(6, 6)
0026 bool tryAcquire(int n, int timeout);
0027 bool tryAcquire(int n, QDeadlineTimer timeout);
0028 #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
0029 template <typename Rep, typename Period>
0030 bool tryAcquire(int n, std::chrono::duration<Rep, Period> timeout)
0031 { return tryAcquire(n, QDeadlineTimer(timeout)); }
0032 #endif
0033
0034 void release(int n = 1);
0035
0036 int available() const;
0037
0038
0039 bool try_acquire() noexcept { return tryAcquire(); }
0040 template <typename Rep, typename Period>
0041 bool try_acquire_for(const std::chrono::duration<Rep, Period> &timeout)
0042 { return tryAcquire(1, timeout); }
0043 template <typename Clock, typename Duration>
0044 bool try_acquire_until(const std::chrono::time_point<Clock, Duration> &tp)
0045 {
0046 return try_acquire_for(tp - Clock::now());
0047 }
0048 private:
0049 Q_DISABLE_COPY(QSemaphore)
0050
0051 union {
0052 QSemaphorePrivate *d;
0053 QBasicAtomicInteger<quintptr> u;
0054 QBasicAtomicInteger<quint32> u32[2];
0055 QBasicAtomicInteger<quint64> u64;
0056 };
0057 };
0058
0059 #if QT_CORE_INLINE_IMPL_SINCE(6, 6)
0060 bool QSemaphore::tryAcquire(int n, int timeout)
0061 {
0062 return tryAcquire(n, QDeadlineTimer(timeout));
0063 }
0064 #endif
0065
0066 class QSemaphoreReleaser
0067 {
0068 public:
0069 Q_NODISCARD_CTOR
0070 QSemaphoreReleaser() = default;
0071 Q_NODISCARD_CTOR
0072 explicit QSemaphoreReleaser(QSemaphore &sem, int n = 1) noexcept
0073 : m_sem(&sem), m_n(n) {}
0074 Q_NODISCARD_CTOR
0075 explicit QSemaphoreReleaser(QSemaphore *sem, int n = 1) noexcept
0076 : m_sem(sem), m_n(n) {}
0077 Q_NODISCARD_CTOR
0078 QSemaphoreReleaser(QSemaphoreReleaser &&other) noexcept
0079 : m_sem(other.cancel()), m_n(other.m_n) {}
0080 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QSemaphoreReleaser)
0081
0082 ~QSemaphoreReleaser()
0083 {
0084 if (m_sem)
0085 m_sem->release(m_n);
0086 }
0087
0088 void swap(QSemaphoreReleaser &other) noexcept
0089 {
0090 qt_ptr_swap(m_sem, other.m_sem);
0091 std::swap(m_n, other.m_n);
0092 }
0093
0094 QSemaphore *semaphore() const noexcept
0095 { return m_sem; }
0096
0097 QSemaphore *cancel() noexcept
0098 {
0099 return std::exchange(m_sem, nullptr);
0100 }
0101
0102 private:
0103 QSemaphore *m_sem = nullptr;
0104 int m_n;
0105 };
0106
0107 QT_END_NAMESPACE
0108
0109 #endif