qsemaphore.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/corelib/thread/qsemaphore.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
1/****************************************************************************-
2**-
3** Copyright (C) 2015 The Qt Company Ltd.-
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:LGPL21$-
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 http://www.qt.io/terms-conditions. For further-
15** information use the contact form at http://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 2.1 or version 3 as published by the Free-
20** Software Foundation and appearing in the file LICENSE.LGPLv21 and-
21** LICENSE.LGPLv3 included in the packaging of this file. Please review the-
22** following information to ensure the GNU Lesser General Public License-
23** requirements will be met: https://www.gnu.org/licenses/lgpl.html and-
24** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.-
25**-
26** As a special exception, The Qt Company gives you certain additional-
27** rights. These rights are described in The Qt Company LGPL Exception-
28** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.-
29**-
30** $QT_END_LICENSE$-
31**-
32****************************************************************************/-
33-
34#include "qsemaphore.h"-
35-
36#ifndef QT_NO_THREAD-
37#include "qmutex.h"-
38#include "qwaitcondition.h"-
39#include "qelapsedtimer.h"-
40#include "qdatetime.h"-
41-
42QT_BEGIN_NAMESPACE-
43-
44/*!-
45 \class QSemaphore-
46 \inmodule QtCore-
47 \brief The QSemaphore class provides a general counting semaphore.-
48-
49 \threadsafe-
50-
51 \ingroup thread-
52-
53 A semaphore is a generalization of a mutex. While a mutex can-
54 only be locked once, it's possible to acquire a semaphore-
55 multiple times. Semaphores are typically used to protect a-
56 certain number of identical resources.-
57-
58 Semaphores support two fundamental operations, acquire() and-
59 release():-
60-
61 \list-
62 \li acquire(\e{n}) tries to acquire \e n resources. If there aren't-
63 that many resources available, the call will block until this-
64 is the case.-
65 \li release(\e{n}) releases \e n resources.-
66 \endlist-
67-
68 There's also a tryAcquire() function that returns immediately if-
69 it cannot acquire the resources, and an available() function that-
70 returns the number of available resources at any time.-
71-
72 Example:-
73-
74 \snippet code/src_corelib_thread_qsemaphore.cpp 0-
75-
76 A typical application of semaphores is for controlling access to-
77 a circular buffer shared by a producer thread and a consumer-
78 thread. The \l{Semaphores Example} shows how-
79 to use QSemaphore to solve that problem.-
80-
81 A non-computing example of a semaphore would be dining at a-
82 restaurant. A semaphore is initialized with the number of chairs-
83 in the restaurant. As people arrive, they want a seat. As seats-
84 are filled, available() is decremented. As people leave, the-
85 available() is incremented, allowing more people to enter. If a-
86 party of 10 people want to be seated, but there are only 9 seats,-
87 those 10 people will wait, but a party of 4 people would be-
88 seated (taking the available seats to 5, making the party of 10-
89 people wait longer).-
90-
91 \sa QMutex, QWaitCondition, QThread, {Semaphores Example}-
92*/-
93-
94class QSemaphorePrivate {-
95public:-
96 inline QSemaphorePrivate(int n) : avail(n) { }
executed 1309342 times by 333 tests: end of block
Executed by:
  • tst_Gestures
  • tst_ModelTest
  • tst_NetworkSelfTest
  • tst_QAbstractButton
  • tst_QAbstractItemView
  • tst_QAbstractNetworkCache
  • tst_QAbstractScrollArea
  • tst_QAbstractSlider
  • tst_QAbstractSpinBox
  • tst_QAccessibility
  • tst_QAction
  • tst_QActionGroup
  • tst_QApplication
  • tst_QBackingStore
  • tst_QBoxLayout
  • tst_QButtonGroup
  • tst_QCalendarWidget
  • tst_QCheckBox
  • tst_QColorDialog
  • tst_QColumnView
  • tst_QComboBox
  • tst_QCommandLinkButton
  • tst_QDBusAbstractAdaptor
  • tst_QDBusAbstractInterface
  • tst_QDBusConnection
  • ...
1309342
97-
98 QMutex mutex;-
99 QWaitCondition cond;-
100-
101 int avail;-
102};-
103-
104/*!-
105 Creates a new semaphore and initializes the number of resources-
106 it guards to \a n (by default, 0).-
107-
108 \sa release(), available()-
109*/-
110QSemaphore::QSemaphore(int n)-
111{-
112 Q_ASSERT_X(n >= 0, "QSemaphore", "parameter 'n' must be non-negative");-
113 d = new QSemaphorePrivate(n);-
114}
executed 1309342 times by 333 tests: end of block
Executed by:
  • tst_Gestures
  • tst_ModelTest
  • tst_NetworkSelfTest
  • tst_QAbstractButton
  • tst_QAbstractItemView
  • tst_QAbstractNetworkCache
  • tst_QAbstractScrollArea
  • tst_QAbstractSlider
  • tst_QAbstractSpinBox
  • tst_QAccessibility
  • tst_QAction
  • tst_QActionGroup
  • tst_QApplication
  • tst_QBackingStore
  • tst_QBoxLayout
  • tst_QButtonGroup
  • tst_QCalendarWidget
  • tst_QCheckBox
  • tst_QColorDialog
  • tst_QColumnView
  • tst_QComboBox
  • tst_QCommandLinkButton
  • tst_QDBusAbstractAdaptor
  • tst_QDBusAbstractInterface
  • tst_QDBusConnection
  • ...
1309342
115-
116/*!-
117 Destroys the semaphore.-
118-
119 \warning Destroying a semaphore that is in use may result in-
120 undefined behavior.-
121*/-
122QSemaphore::~QSemaphore()-
123{
executed 1309353 times by 341 tests: end of block
Executed by:
  • tst_Gestures
  • tst_ModelTest
  • tst_NetworkSelfTest
  • tst_QAbstractButton
  • tst_QAbstractItemView
  • tst_QAbstractNetworkCache
  • tst_QAbstractScrollArea
  • tst_QAbstractSlider
  • tst_QAbstractSpinBox
  • tst_QAccessibility
  • tst_QAction
  • tst_QActionGroup
  • tst_QApplication
  • tst_QBackingStore
  • tst_QBoxLayout
  • tst_QButtonGroup
  • tst_QCalendarWidget
  • tst_QCheckBox
  • tst_QColorDialog
  • tst_QColumnView
  • tst_QComboBox
  • tst_QCommandLinkButton
  • tst_QDBusAbstractAdaptor
  • tst_QDBusAbstractInterface
  • tst_QDBusConnection
  • ...
delete d; }
executed 1309353 times by 341 tests: end of block
Executed by:
  • tst_Gestures
  • tst_ModelTest
  • tst_NetworkSelfTest
  • tst_QAbstractButton
  • tst_QAbstractItemView
  • tst_QAbstractNetworkCache
  • tst_QAbstractScrollArea
  • tst_QAbstractSlider
  • tst_QAbstractSpinBox
  • tst_QAccessibility
  • tst_QAction
  • tst_QActionGroup
  • tst_QApplication
  • tst_QBackingStore
  • tst_QBoxLayout
  • tst_QButtonGroup
  • tst_QCalendarWidget
  • tst_QCheckBox
  • tst_QColorDialog
  • tst_QColumnView
  • tst_QComboBox
  • tst_QCommandLinkButton
  • tst_QDBusAbstractAdaptor
  • tst_QDBusAbstractInterface
  • tst_QDBusConnection
  • ...
1309353
124-
125/*!-
126 Tries to acquire \c n resources guarded by the semaphore. If \a n-
127 > available(), this call will block until enough resources are-
128 available.-
129-
130 \sa release(), available(), tryAcquire()-
131*/-
132void QSemaphore::acquire(int n)-
133{-
134 Q_ASSERT_X(n >= 0, "QSemaphore::acquire", "parameter 'n' must be non-negative");-
135 QMutexLocker locker(&d->mutex);-
136 while (n > d->avail)
n > d->availDescription
TRUEevaluated 732887 times by 198 tests
Evaluated by:
  • tst_Gestures
  • tst_ModelTest
  • tst_NetworkSelfTest
  • tst_QAbstractButton
  • tst_QAbstractItemView
  • tst_QAbstractNetworkCache
  • tst_QAbstractScrollArea
  • tst_QAbstractSlider
  • tst_QAbstractSpinBox
  • tst_QAccessibility
  • tst_QAction
  • tst_QActionGroup
  • tst_QApplication
  • tst_QBackingStore
  • tst_QBoxLayout
  • tst_QButtonGroup
  • tst_QCalendarWidget
  • tst_QCheckBox
  • tst_QColorDialog
  • tst_QColumnView
  • tst_QComboBox
  • tst_QCommandLinkButton
  • tst_QDBusAbstractAdaptor
  • tst_QDBusAbstractInterface
  • tst_QDBusConnection
  • ...
FALSEevaluated 3361823 times by 333 tests
Evaluated by:
  • tst_Gestures
  • tst_ModelTest
  • tst_NetworkSelfTest
  • tst_QAbstractButton
  • tst_QAbstractItemView
  • tst_QAbstractNetworkCache
  • tst_QAbstractScrollArea
  • tst_QAbstractSlider
  • tst_QAbstractSpinBox
  • tst_QAccessibility
  • tst_QAction
  • tst_QActionGroup
  • tst_QApplication
  • tst_QBackingStore
  • tst_QBoxLayout
  • tst_QButtonGroup
  • tst_QCalendarWidget
  • tst_QCheckBox
  • tst_QColorDialog
  • tst_QColumnView
  • tst_QComboBox
  • tst_QCommandLinkButton
  • tst_QDBusAbstractAdaptor
  • tst_QDBusAbstractInterface
  • tst_QDBusConnection
  • ...
732887-3361823
137 d->cond.wait(locker.mutex());
executed 732887 times by 198 tests: d->cond.wait(locker.mutex());
Executed by:
  • tst_Gestures
  • tst_ModelTest
  • tst_NetworkSelfTest
  • tst_QAbstractButton
  • tst_QAbstractItemView
  • tst_QAbstractNetworkCache
  • tst_QAbstractScrollArea
  • tst_QAbstractSlider
  • tst_QAbstractSpinBox
  • tst_QAccessibility
  • tst_QAction
  • tst_QActionGroup
  • tst_QApplication
  • tst_QBackingStore
  • tst_QBoxLayout
  • tst_QButtonGroup
  • tst_QCalendarWidget
  • tst_QCheckBox
  • tst_QColorDialog
  • tst_QColumnView
  • tst_QComboBox
  • tst_QCommandLinkButton
  • tst_QDBusAbstractAdaptor
  • tst_QDBusAbstractInterface
  • tst_QDBusConnection
  • ...
732887
138 d->avail -= n;-
139}
executed 3361823 times by 333 tests: end of block
Executed by:
  • tst_Gestures
  • tst_ModelTest
  • tst_NetworkSelfTest
  • tst_QAbstractButton
  • tst_QAbstractItemView
  • tst_QAbstractNetworkCache
  • tst_QAbstractScrollArea
  • tst_QAbstractSlider
  • tst_QAbstractSpinBox
  • tst_QAccessibility
  • tst_QAction
  • tst_QActionGroup
  • tst_QApplication
  • tst_QBackingStore
  • tst_QBoxLayout
  • tst_QButtonGroup
  • tst_QCalendarWidget
  • tst_QCheckBox
  • tst_QColorDialog
  • tst_QColumnView
  • tst_QComboBox
  • tst_QCommandLinkButton
  • tst_QDBusAbstractAdaptor
  • tst_QDBusAbstractInterface
  • tst_QDBusConnection
  • ...
3361823
140-
141/*!-
142 Releases \a n resources guarded by the semaphore.-
143-
144 This function can be used to "create" resources as well. For-
145 example:-
146-
147 \snippet code/src_corelib_thread_qsemaphore.cpp 1-
148-
149 \sa acquire(), available()-
150*/-
151void QSemaphore::release(int n)-
152{-
153 Q_ASSERT_X(n >= 0, "QSemaphore::release", "parameter 'n' must be non-negative");-
154 QMutexLocker locker(&d->mutex);-
155 d->avail += n;-
156 d->cond.wakeAll();-
157}
executed 5078073 times by 334 tests: end of block
Executed by:
  • tst_Gestures
  • tst_ModelTest
  • tst_NetworkSelfTest
  • tst_QAbstractButton
  • tst_QAbstractItemView
  • tst_QAbstractNetworkCache
  • tst_QAbstractScrollArea
  • tst_QAbstractSlider
  • tst_QAbstractSpinBox
  • tst_QAccessibility
  • tst_QAction
  • tst_QActionGroup
  • tst_QApplication
  • tst_QBackingStore
  • tst_QBoxLayout
  • tst_QButtonGroup
  • tst_QCalendarWidget
  • tst_QCheckBox
  • tst_QColorDialog
  • tst_QColumnView
  • tst_QComboBox
  • tst_QCommandLinkButton
  • tst_QDBusAbstractAdaptor
  • tst_QDBusAbstractInterface
  • tst_QDBusConnection
  • ...
5078073
158-
159/*!-
160 Returns the number of resources currently available to the-
161 semaphore. This number can never be negative.-
162-
163 \sa acquire(), release()-
164*/-
165int QSemaphore::available() const-
166{-
167 QMutexLocker locker(&d->mutex);-
168 return d->avail;
executed 82 times by 2 tests: return d->avail;
Executed by:
  • tst_QDBusThreading
  • tst_QSemaphore
82
169}-
170-
171/*!-
172 Tries to acquire \c n resources guarded by the semaphore and-
173 returns \c true on success. If available() < \a n, this call-
174 immediately returns \c false without acquiring any resources.-
175-
176 Example:-
177-
178 \snippet code/src_corelib_thread_qsemaphore.cpp 2-
179-
180 \sa acquire()-
181*/-
182bool QSemaphore::tryAcquire(int n)-
183{-
184 Q_ASSERT_X(n >= 0, "QSemaphore::tryAcquire", "parameter 'n' must be non-negative");-
185 QMutexLocker locker(&d->mutex);-
186 if (n > d->avail)
n > d->availDescription
TRUEevaluated 8 times by 1 test
Evaluated by:
  • tst_QSemaphore
FALSEevaluated 4 times by 1 test
Evaluated by:
  • tst_QSemaphore
4-8
187 return false;
executed 8 times by 1 test: return false;
Executed by:
  • tst_QSemaphore
8
188 d->avail -= n;-
189 return true;
executed 4 times by 1 test: return true;
Executed by:
  • tst_QSemaphore
4
190}-
191-
192/*!-
193 Tries to acquire \c n resources guarded by the semaphore and-
194 returns \c true on success. If available() < \a n, this call will-
195 wait for at most \a timeout milliseconds for resources to become-
196 available.-
197-
198 Note: Passing a negative number as the \a timeout is equivalent to-
199 calling acquire(), i.e. this function will wait forever for-
200 resources to become available if \a timeout is negative.-
201-
202 Example:-
203-
204 \snippet code/src_corelib_thread_qsemaphore.cpp 3-
205-
206 \sa acquire()-
207*/-
208bool QSemaphore::tryAcquire(int n, int timeout)-
209{-
210 Q_ASSERT_X(n >= 0, "QSemaphore::tryAcquire", "parameter 'n' must be non-negative");-
211 QMutexLocker locker(&d->mutex);-
212 if (timeout < 0) {
timeout < 0Description
TRUEnever evaluated
FALSEevaluated 1716267 times by 4 tests
Evaluated by:
  • tst_QReadWriteLock
  • tst_QSemaphore
  • tst_QSslSocket
  • tst_QThreadPool
0-1716267
213 while (n > d->avail)
n > d->availDescription
TRUEnever evaluated
FALSEnever evaluated
0
214 d->cond.wait(locker.mutex());
never executed: d->cond.wait(locker.mutex());
0
215 } else {
never executed: end of block
0
216 QElapsedTimer timer;-
217 timer.start();-
218 while (n > d->avail) {
n > d->availDescription
TRUEevaluated 188205 times by 4 tests
Evaluated by:
  • tst_QReadWriteLock
  • tst_QSemaphore
  • tst_QSslSocket
  • tst_QThreadPool
FALSEevaluated 1716244 times by 3 tests
Evaluated by:
  • tst_QSemaphore
  • tst_QSslSocket
  • tst_QThreadPool
188205-1716244
219 const qint64 elapsed = timer.elapsed();-
220 if (timeout - elapsed <= 0
timeout - elapsed <= 0Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QSemaphore
FALSEevaluated 188204 times by 4 tests
Evaluated by:
  • tst_QReadWriteLock
  • tst_QSemaphore
  • tst_QSslSocket
  • tst_QThreadPool
1-188204
221 || !d->cond.wait(locker.mutex(), timeout - elapsed))
!d->cond.wait(...out - elapsed)Description
TRUEevaluated 22 times by 3 tests
Evaluated by:
  • tst_QReadWriteLock
  • tst_QSemaphore
  • tst_QThreadPool
FALSEevaluated 188182 times by 3 tests
Evaluated by:
  • tst_QSemaphore
  • tst_QSslSocket
  • tst_QThreadPool
22-188182
222 return false;
executed 23 times by 3 tests: return false;
Executed by:
  • tst_QReadWriteLock
  • tst_QSemaphore
  • tst_QThreadPool
23
223 }
executed 188182 times by 3 tests: end of block
Executed by:
  • tst_QSemaphore
  • tst_QSslSocket
  • tst_QThreadPool
188182
224 }
executed 1716244 times by 3 tests: end of block
Executed by:
  • tst_QSemaphore
  • tst_QSslSocket
  • tst_QThreadPool
1716244
225 d->avail -= n;-
226 return true;
executed 1716244 times by 3 tests: return true;
Executed by:
  • tst_QSemaphore
  • tst_QSslSocket
  • tst_QThreadPool
1716244
227-
228-
229}-
230-
231QT_END_NAMESPACE-
232-
233#endif // QT_NO_THREAD-
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9