QtBase  v6.3.1
qwaitcondition_p.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2019 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com>
4 ** Contact: http://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 #ifndef QWAITCONDITION_P_H
40 #define QWAITCONDITION_P_H
41 
42 //
43 // W A R N I N G
44 // -------------
45 //
46 // This file is not part of the Qt API. It exists for the convenience
47 // of qmutex.cpp, qmutex_unix.cpp, and qmutex_win.cpp. This header
48 // file may change from version to version without notice, or even be
49 // removed.
50 //
51 // We mean it.
52 //
53 
54 #include <QtCore/QWaitCondition>
55 #include <QtCore/QMutex>
56 #include <QtCore/QDeadlineTimer>
57 
58 #include <condition_variable>
59 #include <mutex>
60 
61 // There's no feature macro for C++11 std::mutex, so we use the C++14 one
62 // for shared_mutex to detect it.
63 // Needed for: MinGW without gthreads, Integrity
64 #if __has_include(<shared_mutex>)
65 # include <shared_mutex>
66 #endif
67 
69 
70 namespace QtPrivate {
71 
72 #if !defined(__cpp_lib_shared_timed_mutex)
73 
74 enum class cv_status { no_timeout, timeout };
75 class condition_variable;
76 
77 class mutex : private QMutex
78 {
80 
81 public:
82  // all special member functions are ok!
83  // do not expose the (QMutex::Recursive) ctor
84  // don't use 'using QMutex::lock;' etc as those have the wrong noexcept
85 
86  void lock() { return QMutex::lock(); }
87  void unlock() { return QMutex::unlock(); }
88  bool try_lock() { return QMutex::tryLock(); }
89 };
90 
92 {
93 public:
94  // all special member functions are ok!
95 
98 
99  void wait(std::unique_lock<QtPrivate::mutex> &lock) { QWaitCondition::wait(lock.mutex()); }
100  template <class Predicate>
101  void wait(std::unique_lock<QtPrivate::mutex> &lock, Predicate p)
102  {
103  while (!p())
104  wait(lock);
105  }
106 
107  template <typename Rep, typename Period>
108  cv_status wait_for(std::unique_lock<QtPrivate::mutex> &lock,
109  const std::chrono::duration<Rep, Period> &d)
110  {
111  return QWaitCondition::wait(lock.mutex(), QDeadlineTimer{d})
114  }
115  template <typename Rep, typename Period, typename Predicate>
116  bool wait_for(std::unique_lock<QtPrivate::mutex> &lock,
117  const std::chrono::duration<Rep, Period> &d, Predicate p)
118  {
119  const auto timer = QDeadlineTimer{d};
120  while (!p()) {
121  if (!QWaitCondition::wait(lock.mutex(), timer))
122  return p();
123  }
124  return true;
125  }
126 
127  template <typename Clock, typename Duration>
128  cv_status wait_until(std::unique_lock<QtPrivate::mutex> &lock,
129  const std::chrono::time_point<Clock, Duration> &t)
130  {
131  return QWaitCondition::wait(lock.mutex(), QDeadlineTimer{t})
134  }
135 
136  template <typename Clock, typename Duration, typename Predicate>
137  bool wait_until(std::unique_lock<QtPrivate::mutex> &lock,
138  const std::chrono::time_point<Clock, Duration> &t, Predicate p)
139  {
140  const auto timer = QDeadlineTimer{t};
141  while (!p()) {
142  if (!QWaitCondition::wait(lock.mutex(), timer))
143  return p();
144  }
145  return true;
146  }
147 
148 };
149 
150 #else // C++11 threads
151 
152 using mutex = std::mutex;
153 using condition_variable = std::condition_variable;
154 
155 #endif // C++11 threads
156 
157 } // namespace QtPrivate
158 
160 
161 #endif /* QWAITCONDITION_P_H */
The QDeadlineTimer class marks a deadline in the future.
The QMutex class provides access serialization between threads.
Definition: qmutex.h:285
bool tryLock(int timeout=0) noexcept
Definition: qmutex.h:291
void unlock() noexcept
Definition: qmutex.h:293
void lock() noexcept
Definition: qmutex.h:290
bool wait(QMutex *, QDeadlineTimer=QDeadlineTimer(QDeadlineTimer::Forever))
bool wait_until(std::unique_lock< QtPrivate::mutex > &lock, const std::chrono::time_point< Clock, Duration > &t, Predicate p)
cv_status wait_for(std::unique_lock< QtPrivate::mutex > &lock, const std::chrono::duration< Rep, Period > &d)
bool wait_for(std::unique_lock< QtPrivate::mutex > &lock, const std::chrono::duration< Rep, Period > &d, Predicate p)
cv_status wait_until(std::unique_lock< QtPrivate::mutex > &lock, const std::chrono::time_point< Clock, Duration > &t)
void wait(std::unique_lock< QtPrivate::mutex > &lock, Predicate p)
void wait(std::unique_lock< QtPrivate::mutex > &lock)
Generic::PredicateMatcher< T > Predicate(std::function< bool(T const &)> const &predicate, std::string const &description="")
Definition: catch_p_p.h:3521
GLbitfield GLuint64 timeout
[4]
GLdouble GLdouble t
[9]
Definition: qopenglext.h:243
GLfloat GLfloat p
[1]
Definition: qopenglext.h:12698
QTimer * timer
[3]
QReadWriteLock lock
[0]
QMutex mutex