Qt 4.8
qsemaphore.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qsemaphore.h"
43 
44 #ifndef QT_NO_THREAD
45 #include "qmutex.h"
46 #include "qwaitcondition.h"
47 #include "qelapsedtimer.h"
48 #include "qdatetime.h"
49 
51 
105 public:
106  inline QSemaphorePrivate(int n) : avail(n) { }
107 
110 
111  int avail;
112 };
113 
121 {
122  Q_ASSERT_X(n >= 0, "QSemaphore", "parameter 'n' must be non-negative");
123  d = new QSemaphorePrivate(n);
124 }
125 
133 { delete d; }
134 
143 {
144  Q_ASSERT_X(n >= 0, "QSemaphore::acquire", "parameter 'n' must be non-negative");
145  QMutexLocker locker(&d->mutex);
146  while (n > d->avail)
147  d->cond.wait(locker.mutex());
148  d->avail -= n;
149 }
150 
162 {
163  Q_ASSERT_X(n >= 0, "QSemaphore::release", "parameter 'n' must be non-negative");
164  QMutexLocker locker(&d->mutex);
165  d->avail += n;
166  d->cond.wakeAll();
167 }
168 
176 {
177  QMutexLocker locker(&d->mutex);
178  return d->avail;
179 }
180 
193 {
194  Q_ASSERT_X(n >= 0, "QSemaphore::tryAcquire", "parameter 'n' must be non-negative");
195  QMutexLocker locker(&d->mutex);
196  if (n > d->avail)
197  return false;
198  d->avail -= n;
199  return true;
200 }
201 
218 bool QSemaphore::tryAcquire(int n, int timeout)
219 {
220  Q_ASSERT_X(n >= 0, "QSemaphore::tryAcquire", "parameter 'n' must be non-negative");
221  QMutexLocker locker(&d->mutex);
222  if (timeout < 0) {
223  while (n > d->avail)
224  d->cond.wait(locker.mutex());
225  } else {
227  timer.start();
228  while (n > d->avail) {
229  const qint64 elapsed = timer.elapsed();
230  if (timeout - elapsed <= 0
231  || !d->cond.wait(locker.mutex(), timeout - elapsed))
232  return false;
233  }
234  }
235  d->avail -= n;
236  return true;
237 
238 
239 }
240 
242 
243 #endif // QT_NO_THREAD
double d
Definition: qnumeric_p.h:62
static double elapsed(qint64 after, qint64 before)
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
The QMutex class provides access serialization between threads.
Definition: qmutex.h:60
QWaitCondition cond
Definition: qsemaphore.cpp:109
void release(int n=1)
Releases n resources guarded by the semaphore.
Definition: qsemaphore.cpp:161
bool tryAcquire(int n=1)
Tries to acquire n resources guarded by the semaphore and returns true on success.
Definition: qsemaphore.cpp:192
int available() const
Returns the number of resources currently available to the semaphore.
Definition: qsemaphore.cpp:175
QSemaphorePrivate(int n)
Definition: qsemaphore.cpp:106
EventLoopTimerRef timer
The QElapsedTimer class provides a fast way to calculate elapsed times.
Definition: qelapsedtimer.h:53
qint64 elapsed() const
Returns the number of milliseconds since this QElapsedTimer was last started.
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QSemaphore(int n=0)
Creates a new semaphore and initializes the number of resources it guards to n (by default...
Definition: qsemaphore.cpp:120
__int64 qint64
Definition: qglobal.h:942
void acquire(int n=1)
Tries to acquire n resources guarded by the semaphore.
Definition: qsemaphore.cpp:142
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
void start()
Starts this timer.
~QSemaphore()
Destroys the semaphore.
Definition: qsemaphore.cpp:132
QMutex * mutex() const
Returns a pointer to the mutex that was locked in the constructor.
Definition: qmutex.h:140