0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the QtCore module of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
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 |
|
|
56 |
QT_BEGIN_NAMESPACE
|
|
57 |
|
|
58 |
//***********************************************************************
|
|
59 |
// QWaitConditionPrivate
|
|
60 |
// **********************************************************************
|
|
61 |
|
|
62 |
class QWaitConditionEvent
|
|
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;
|
|
72 |
HANDLE event;
|
|
73 |
};
|
|
74 |
|
|
75 |
typedef QList<QWaitConditionEvent *> EventQueue;
|
|
76 |
|
|
77 |
class QWaitConditionPrivate
|
|
78 |
{
|
|
79 |
public:
|
|
80 |
QMutex mtx;
|
|
81 |
EventQueue queue;
|
|
82 |
EventQueue freeQueue;
|
|
83 |
|
|
84 |
QWaitConditionEvent *pre();
|
|
85 |
bool wait(QWaitConditionEvent *wce, unsigned long time);
|
|
86 |
void post(QWaitConditionEvent *wce, bool ret);
|
|
87 |
};
|
|
88 |
|
|
89 |
QWaitConditionEvent *QWaitConditionPrivate::pre()
|
|
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 |
|
|
124 |
void QWaitConditionPrivate::post(QWaitConditionEvent *wce, bool ret)
|
|
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 |
|
|
147 |
QWaitCondition::QWaitCondition()
|
|
148 |
{
|
|
149 |
d = new QWaitConditionPrivate;
|
|
150 |
}
|
|
151 |
|
|
152 |
QWaitCondition::~QWaitCondition()
|
|
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 |
|
|
207 |
void QWaitCondition::wakeOne()
|
|
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 |
|
|
221 |
void QWaitCondition::wakeAll()
|
|
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 |
|
|
232 |
QT_END_NAMESPACE
|
|
233 |
#endif // QT_NO_THREAD
|