| 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 | QT_BEGIN_NAMESPACE |
| 62 | |
| 63 | namespace QtPrivate { |
| 64 | |
| 65 | #if defined(Q_OS_INTEGRITY) |
| 66 | |
| 67 | class condition_variable; |
| 68 | |
| 69 | class mutex : private QMutex |
| 70 | { |
| 71 | friend class QtPrivate::condition_variable; |
| 72 | |
| 73 | public: |
| 74 | // all special member functions are ok! |
| 75 | // do not expose the (QMutex::Recursive) ctor |
| 76 | // don't use 'using QMutex::lock;' etc as those have the wrong noexcept |
| 77 | |
| 78 | void lock() { return QMutex::lock(); } |
| 79 | void unlock() { return QMutex::unlock(); } |
| 80 | bool try_lock() { return QMutex::tryLock(); } |
| 81 | }; |
| 82 | |
| 83 | class condition_variable : private QWaitCondition |
| 84 | { |
| 85 | public: |
| 86 | // all special member functions are ok! |
| 87 | |
| 88 | void notify_one() { QWaitCondition::wakeOne(); } |
| 89 | void notify_all() { QWaitCondition::wakeAll(); } |
| 90 | |
| 91 | void wait(std::unique_lock<QtPrivate::mutex> &lock) { QWaitCondition::wait(lock.mutex()); } |
| 92 | template <class Predicate> |
| 93 | void wait(std::unique_lock<QtPrivate::mutex> &lock, Predicate p) |
| 94 | { |
| 95 | while (!p()) |
| 96 | wait(lock); |
| 97 | } |
| 98 | |
| 99 | template <typename Rep, typename Period> |
| 100 | std::cv_status wait_for(std::unique_lock<QtPrivate::mutex> &lock, |
| 101 | const std::chrono::duration<Rep, Period> &d) |
| 102 | { |
| 103 | return QWaitCondition::wait(lock.mutex(), QDeadlineTimer{d}) |
| 104 | ? std::cv_status::no_timeout |
| 105 | : std::cv_status::timeout; |
| 106 | } |
| 107 | template <typename Rep, typename Period, typename Predicate> |
| 108 | bool wait_for(std::unique_lock<QtPrivate::mutex> &lock, |
| 109 | const std::chrono::duration<Rep, Period> &d, Predicate p) |
| 110 | { |
| 111 | const auto timer = QDeadlineTimer{d}; |
| 112 | while (!p()) { |
| 113 | if (!QWaitCondition::wait(lock.mutex(), timer)) |
| 114 | return p(); |
| 115 | } |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | template <typename Clock, typename Duration> |
| 120 | std::cv_status wait_until(std::unique_lock<QtPrivate::mutex> &lock, |
| 121 | const std::chrono::time_point<Clock, Duration> &t) |
| 122 | { |
| 123 | return QWaitCondition::wait(lock.mutex(), QDeadlineTimer{t}) |
| 124 | ? std::cv_status::no_timeout |
| 125 | : std::cv_status::timeout; |
| 126 | } |
| 127 | |
| 128 | template <typename Clock, typename Duration, typename Predicate> |
| 129 | bool wait_until(std::unique_lock<QtPrivate::mutex> &lock, |
| 130 | const std::chrono::time_point<Clock, Duration> &t, Predicate p) |
| 131 | { |
| 132 | const auto timer = QDeadlineTimer{t}; |
| 133 | while (!p()) { |
| 134 | if (!QWaitCondition::wait(lock.mutex(), timer)) |
| 135 | return p(); |
| 136 | } |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | }; |
| 141 | |
| 142 | #else // Integrity |
| 143 | |
| 144 | using mutex = std::mutex; |
| 145 | using condition_variable = std::condition_variable; |
| 146 | |
| 147 | #endif // Integrity |
| 148 | |
| 149 | } // namespace QtPrivate |
| 150 | |
| 151 | QT_END_NAMESPACE |
| 152 | |
| 153 | #endif /* QWAITCONDITION_P_H */ |
| 154 | |