Qt 4.8
qwaitcondition_win.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 "qwaitcondition.h"
43 #include "qnamespace.h"
44 #include "qmutex.h"
45 #include "qreadwritelock.h"
46 #include "qlist.h"
47 #include "qalgorithms.h"
48 #include "qt_windows.h"
49 
50 #ifndef QT_NO_THREAD
51 
52 #define Q_MUTEX_T void*
53 #include <private/qmutex_p.h>
54 #include <private/qreadwritelock_p.h>
55 
57 
58 //***********************************************************************
59 // QWaitConditionPrivate
60 // **********************************************************************
61 
63 {
64 public:
65  inline QWaitConditionEvent() : priority(0), wokenUp(false)
66  {
67  event = CreateEvent(NULL, TRUE, FALSE, NULL);
68  }
69  inline ~QWaitConditionEvent() { CloseHandle(event); }
70  int priority;
71  bool wokenUp;
73 };
74 
76 
78 {
79 public:
83 
84  QWaitConditionEvent *pre();
85  bool wait(QWaitConditionEvent *wce, unsigned long time);
86  void post(QWaitConditionEvent *wce, bool ret);
87 };
88 
90 {
91  mtx.lock();
92  QWaitConditionEvent *wce =
93  freeQueue.isEmpty() ? new QWaitConditionEvent : freeQueue.takeFirst();
94  wce->priority = GetThreadPriority(GetCurrentThread());
95  wce->wokenUp = false;
96 
97  // insert 'wce' into the queue (sorted by priority)
98  int index = 0;
99  for (; index < queue.size(); ++index) {
100  QWaitConditionEvent *current = queue.at(index);
101  if (current->priority < wce->priority)
102  break;
103  }
104  queue.insert(index, wce);
105  mtx.unlock();
106 
107  return wce;
108 }
109 
110 bool QWaitConditionPrivate::wait(QWaitConditionEvent *wce, unsigned long time)
111 {
112  // wait for the event
113  bool ret = false;
114  switch (WaitForSingleObject(wce->event, time)) {
115  default: break;
116 
117  case WAIT_OBJECT_0:
118  ret = true;
119  break;
120  }
121  return ret;
122 }
123 
125 {
126  mtx.lock();
127 
128  // remove 'wce' from the queue
129  queue.removeAll(wce);
130  ResetEvent(wce->event);
131  freeQueue.append(wce);
132 
133  // wakeups delivered after the timeout should be forwarded to the next waiter
134  if (!ret && wce->wokenUp && !queue.isEmpty()) {
135  QWaitConditionEvent *other = queue.first();
136  SetEvent(other->event);
137  other->wokenUp = true;
138  }
139 
140  mtx.unlock();
141 }
142 
143 //***********************************************************************
144 // QWaitCondition implementation
145 //***********************************************************************
146 
148 {
149  d = new QWaitConditionPrivate;
150 }
151 
153 {
154  if (!d->queue.isEmpty()) {
155  qWarning("QWaitCondition: Destroyed while threads are still waiting");
156  qDeleteAll(d->queue);
157  }
158 
159  qDeleteAll(d->freeQueue);
160  delete d;
161 }
162 
163 bool QWaitCondition::wait(QMutex *mutex, unsigned long time)
164 {
165  if (!mutex)
166  return false;
167  if (mutex->d->recursive) {
168  qWarning("QWaitCondition::wait: Cannot wait on recursive mutexes");
169  return false;
170  }
171 
172  QWaitConditionEvent *wce = d->pre();
173  mutex->unlock();
174 
175  bool returnValue = d->wait(wce, time);
176 
177  mutex->lock();
178  d->post(wce, returnValue);
179 
180  return returnValue;
181 }
182 
183 bool QWaitCondition::wait(QReadWriteLock *readWriteLock, unsigned long time)
184 {
185  if (!readWriteLock || readWriteLock->d->accessCount == 0)
186  return false;
187  if (readWriteLock->d->accessCount < -1) {
188  qWarning("QWaitCondition: cannot wait on QReadWriteLocks with recursive lockForWrite()");
189  return false;
190  }
191 
192  QWaitConditionEvent *wce = d->pre();
193  int previousAccessCount = readWriteLock->d->accessCount;
194  readWriteLock->unlock();
195 
196  bool returnValue = d->wait(wce, time);
197 
198  if (previousAccessCount < 0)
199  readWriteLock->lockForWrite();
200  else
201  readWriteLock->lockForRead();
202  d->post(wce, returnValue);
203 
204  return returnValue;
205 }
206 
208 {
209  // wake up the first waiting thread in the queue
210  QMutexLocker locker(&d->mtx);
211  for (int i = 0; i < d->queue.size(); ++i) {
212  QWaitConditionEvent *current = d->queue.at(i);
213  if (current->wokenUp)
214  continue;
215  SetEvent(current->event);
216  current->wokenUp = true;
217  break;
218  }
219 }
220 
222 {
223  // wake up the all threads in the queue
224  QMutexLocker locker(&d->mtx);
225  for (int i = 0; i < d->queue.size(); ++i) {
226  QWaitConditionEvent *current = d->queue.at(i);
227  SetEvent(current->event);
228  current->wokenUp = true;
229  }
230 }
231 
233 #endif // QT_NO_THREAD
double d
Definition: qnumeric_p.h:62
QMutexData * d
Definition: qmutex.h:98
#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 post(QWaitConditionEvent *wce, bool ret)
void unlock()
Unlocks the lock.
void lockForWrite()
Locks the lock for writing.
bool wait(unsigned long time)
#define WAIT_OBJECT_0
QReadWriteLockPrivate * d
QWaitConditionEvent * pre()
#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.
#define FALSE
Synonym for false.
Definition: qglobal.h:1019
void * HANDLE
Definition: qnamespace.h:1671
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 TRUE
Synonym for true.
Definition: qglobal.h:1018
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
The QReadWriteLock class provides read-write locking.
quint16 index
QList< QWaitConditionEvent * > EventQueue
Q_OUTOFLINE_TEMPLATE void qDeleteAll(ForwardIterator begin, ForwardIterator end)
Definition: qalgorithms.h:319