QtBase  v6.3.1
qsemaphore.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QSEMAPHORE_H
41 #define QSEMAPHORE_H
42 
43 #include <QtCore/qglobal.h>
44 #include <QtCore/qmutex.h> // for convertToMilliseconds()
45 
46 #include <chrono>
47 
49 
51 
52 class QSemaphorePrivate;
53 
54 class Q_CORE_EXPORT QSemaphore
55 {
56 public:
57  explicit QSemaphore(int n = 0);
58  ~QSemaphore();
59 
60  void acquire(int n = 1);
61  bool tryAcquire(int n = 1);
62  bool tryAcquire(int n, int timeout);
63  template <typename Rep, typename Period>
64  bool tryAcquire(int n, std::chrono::duration<Rep, Period> timeout)
65  { return tryAcquire(n, QtPrivate::convertToMilliseconds(timeout)); }
66 
67  void release(int n = 1);
68 
69  int available() const;
70 
71  // std::counting_semaphore compatibility:
72  bool try_acquire() noexcept { return tryAcquire(); }
73  template <typename Rep, typename Period>
74  bool try_acquire_for(const std::chrono::duration<Rep, Period> &timeout)
75  { return tryAcquire(1, timeout); }
76  template <typename Clock, typename Duration>
77  bool try_acquire_until(const std::chrono::time_point<Clock, Duration> &tp)
78  {
79  return try_acquire_for(tp - Clock::now());
80  }
81 private:
83 
84  union {
89  };
90 };
91 
93 {
94 public:
95  QSemaphoreReleaser() = default;
96  explicit QSemaphoreReleaser(QSemaphore &sem, int n = 1) noexcept
97  : m_sem(&sem), m_n(n) {}
98  explicit QSemaphoreReleaser(QSemaphore *sem, int n = 1) noexcept
99  : m_sem(sem), m_n(n) {}
101  : m_sem(other.cancel()), m_n(other.m_n) {}
103 
105  {
106  if (m_sem)
107  m_sem->release(m_n);
108  }
109 
110  void swap(QSemaphoreReleaser &other) noexcept
111  {
112  qt_ptr_swap(m_sem, other.m_sem);
113  std::swap(m_n, other.m_n);
114  }
115 
117  { return m_sem; }
118 
119  QSemaphore *cancel() noexcept
120  {
121  return qExchange(m_sem, nullptr);
122  }
123 
124 private:
125  QSemaphore *m_sem = nullptr;
126  int m_n;
127 };
128 
130 
131 #endif // QSEMAPHORE_H
The QSemaphore class provides a general counting semaphore.
Definition: qsemaphore.h:55
bool try_acquire() noexcept
Definition: qsemaphore.h:72
QSemaphorePrivate * d
Definition: qsemaphore.h:85
bool try_acquire_for(const std::chrono::duration< Rep, Period > &timeout)
Definition: qsemaphore.h:74
QBasicAtomicInteger< quintptr > u
Definition: qsemaphore.h:86
QBasicAtomicInteger< quint64 > u64
Definition: qsemaphore.h:88
void release(int n=1)
Definition: qsemaphore.cpp:351
bool try_acquire_until(const std::chrono::time_point< Clock, Duration > &tp)
Definition: qsemaphore.h:77
bool tryAcquire(int n, std::chrono::duration< Rep, Period > timeout)
Definition: qsemaphore.h:64
The QSemaphoreReleaser class provides exception-safe deferral of a QSemaphore::release() call.
Definition: qsemaphore.h:93
QSemaphoreReleaser()=default
QSemaphoreReleaser(QSemaphore *sem, int n=1) noexcept
Definition: qsemaphore.h:98
QSemaphoreReleaser(QSemaphoreReleaser &&other) noexcept
Definition: qsemaphore.h:100
QSemaphore * cancel() noexcept
Definition: qsemaphore.h:119
QSemaphoreReleaser(QSemaphore &sem, int n=1) noexcept
Definition: qsemaphore.h:96
void swap(QSemaphoreReleaser &other) noexcept
Definition: qsemaphore.h:110
QSemaphore * semaphore() const noexcept
Definition: qsemaphore.h:116
auto it unsigned count const
Definition: hb-iter.hh:848
void swap(SimpleVector< T > &v1, SimpleVector< T > &v2)
Definition: simplevector.h:331
#define Q_DISABLE_COPY(Class)
Definition: qglobal.h:515
#define QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(Class)
Definition: qglobal.h:556
GLbitfield GLuint64 timeout
[4]
GLfloat n
QT_REQUIRE_CONFIG(thread)
UINT32_TYPE u32
Definition: sqlite3.c:13904
sem acquire()
sem release()
Definition: main.cpp:48
QSystemSemaphore sem("market", 3, QSystemSemaphore::Create)
[0]
sem tryAcquire(1)
QSharedPointer< T > other(t)
[5]