Qt 4.8
qwaitcondition_unix.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 "qplatformdefs.h"
43 #include "qwaitcondition.h"
44 #include "qmutex.h"
45 #include "qreadwritelock.h"
46 #include "qatomic.h"
47 #include "qstring.h"
48 
49 #include "qmutex_p.h"
50 #include "qreadwritelock_p.h"
51 
52 #include <errno.h>
53 
54 #ifndef QT_NO_THREAD
55 
57 
58 static void report_error(int code, const char *where, const char *what)
59 {
60  if (code != 0)
61  qWarning("%s: %s failure: %s", where, what, qPrintable(qt_error_string(code)));
62 }
63 
65 public:
66  pthread_mutex_t mutex;
67  pthread_cond_t cond;
68  int waiters;
69  int wakeups;
70 
71  bool wait(unsigned long time)
72  {
73  int code;
74  forever {
75  if (time != ULONG_MAX) {
76  struct timeval tv;
77  gettimeofday(&tv, 0);
78 
79  timespec ti;
80  ti.tv_nsec = (tv.tv_usec + (time % 1000) * 1000) * 1000;
81  ti.tv_sec = tv.tv_sec + (time / 1000) + (ti.tv_nsec / 1000000000);
82  ti.tv_nsec %= 1000000000;
83 
84  code = pthread_cond_timedwait(&cond, &mutex, &ti);
85  } else {
86  code = pthread_cond_wait(&cond, &mutex);
87  }
88  if (code == 0 && wakeups == 0) {
89  // many vendors warn of spurios wakeups from
90  // pthread_cond_wait(), especially after signal delivery,
91  // even though POSIX doesn't allow for it... sigh
92  continue;
93  }
94  break;
95  }
96 
97  Q_ASSERT_X(waiters > 0, "QWaitCondition::wait", "internal error (waiters)");
98  --waiters;
99  if (code == 0) {
100  Q_ASSERT_X(wakeups > 0, "QWaitCondition::wait", "internal error (wakeups)");
101  --wakeups;
102  }
103  report_error(pthread_mutex_unlock(&mutex), "QWaitCondition::wait()", "mutex unlock");
104 
105  if (code && code != ETIMEDOUT)
106  report_error(code, "QWaitCondition::wait()", "cv wait");
107 
108  return (code == 0);
109  }
110 };
111 
112 
114 {
115  d = new QWaitConditionPrivate;
116  report_error(pthread_mutex_init(&d->mutex, NULL), "QWaitCondition", "mutex init");
117  report_error(pthread_cond_init(&d->cond, NULL), "QWaitCondition", "cv init");
118  d->waiters = d->wakeups = 0;
119 }
120 
121 
123 {
124  report_error(pthread_cond_destroy(&d->cond), "QWaitCondition", "cv destroy");
125  report_error(pthread_mutex_destroy(&d->mutex), "QWaitCondition", "mutex destroy");
126  delete d;
127 }
128 
130 {
131  report_error(pthread_mutex_lock(&d->mutex), "QWaitCondition::wakeOne()", "mutex lock");
132  d->wakeups = qMin(d->wakeups + 1, d->waiters);
133  report_error(pthread_cond_signal(&d->cond), "QWaitCondition::wakeOne()", "cv signal");
134  report_error(pthread_mutex_unlock(&d->mutex), "QWaitCondition::wakeOne()", "mutex unlock");
135 }
136 
138 {
139  report_error(pthread_mutex_lock(&d->mutex), "QWaitCondition::wakeAll()", "mutex lock");
140  d->wakeups = d->waiters;
141  report_error(pthread_cond_broadcast(&d->cond), "QWaitCondition::wakeAll()", "cv broadcast");
142  report_error(pthread_mutex_unlock(&d->mutex), "QWaitCondition::wakeAll()", "mutex unlock");
143 }
144 
145 bool QWaitCondition::wait(QMutex *mutex, unsigned long time)
146 {
147  if (! mutex)
148  return false;
149  if (mutex->d->recursive) {
150  qWarning("QWaitCondition: cannot wait on recursive mutexes");
151  return false;
152  }
153 
154  report_error(pthread_mutex_lock(&d->mutex), "QWaitCondition::wait()", "mutex lock");
155  ++d->waiters;
156  mutex->unlock();
157 
158  bool returnValue = d->wait(time);
159 
160  mutex->lock();
161 
162  return returnValue;
163 }
164 
165 bool QWaitCondition::wait(QReadWriteLock *readWriteLock, unsigned long time)
166 {
167  if (!readWriteLock || readWriteLock->d->accessCount == 0)
168  return false;
169  if (readWriteLock->d->accessCount < -1) {
170  qWarning("QWaitCondition: cannot wait on QReadWriteLocks with recursive lockForWrite()");
171  return false;
172  }
173 
174  report_error(pthread_mutex_lock(&d->mutex), "QWaitCondition::wait()", "mutex lock");
175  ++d->waiters;
176 
177  int previousAccessCount = readWriteLock->d->accessCount;
178  readWriteLock->unlock();
179 
180  bool returnValue = d->wait(time);
181 
182  if (previousAccessCount < 0)
183  readWriteLock->lockForWrite();
184  else
185  readWriteLock->lockForRead();
186 
187  return returnValue;
188 }
189 
191 
192 #endif // QT_NO_THREAD
double d
Definition: qnumeric_p.h:62
QString qt_error_string(int errorCode)
Definition: qglobal.cpp:2600
QMutexData * d
Definition: qmutex.h:98
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void lock()
Locks the mutex.
Definition: qmutex.cpp:151
The QMutex class provides access serialization between threads.
Definition: qmutex.h:60
void unlock()
Unlocks the lock.
void lockForWrite()
Locks the lock for writing.
bool wait(unsigned long time)
QReadWriteLockPrivate * d
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
Q_CORE_EXPORT void qWarning(const char *,...)
void lockForRead()
Locks the lock for reading.
const uint recursive
Definition: qmutex.h:159
void unlock()
Unlocks the mutex.
Definition: qmutex.cpp:296
bool wait(QMutex *mutex, unsigned long time=ULONG_MAX)
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
The QReadWriteLock class provides read-write locking.
static void report_error(int code, const char *where, const char *what)
#define qPrintable(string)
Definition: qglobal.h:1750
#define forever
This macro is provided for convenience for writing infinite loops.
Definition: qglobal.h:2452