1 | /****************************************************************************
|
---|
2 | ** $Id: qwaitcondition_pm.cpp 8 2005-11-16 19:36:46Z dmik $
|
---|
3 | **
|
---|
4 | ** Implementation of QWaitCondition class for OS/2
|
---|
5 | **
|
---|
6 | ** Copyright (C) 2000-2001 Trolltech AS. All rights reserved.
|
---|
7 | ** Copyright (C) 2004 Norman ASA. Initial OS/2 Port.
|
---|
8 | ** Copyright (C) 2005 netlabs.org. Further OS/2 Development.
|
---|
9 | **
|
---|
10 | ** This file is part of the tools module of the Qt GUI Toolkit.
|
---|
11 | **
|
---|
12 | ** This file may be distributed under the terms of the Q Public License
|
---|
13 | ** as defined by Trolltech AS of Norway and appearing in the file
|
---|
14 | ** LICENSE.QPL included in the packaging of this file.
|
---|
15 | **
|
---|
16 | ** This file may be distributed and/or modified under the terms of the
|
---|
17 | ** GNU General Public License version 2 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
19 | ** packaging of this file.
|
---|
20 | **
|
---|
21 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
|
---|
22 | ** licenses may use this file in accordance with the Qt Commercial License
|
---|
23 | ** Agreement provided with the Software.
|
---|
24 | **
|
---|
25 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
---|
26 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
---|
27 | **
|
---|
28 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
---|
29 | ** information about Qt Commercial License Agreements.
|
---|
30 | ** See http://www.trolltech.com/qpl/ for QPL licensing information.
|
---|
31 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
---|
32 | **
|
---|
33 | ** Contact info@trolltech.com if any conditions of this licensing are
|
---|
34 | ** not clear to you.
|
---|
35 | **
|
---|
36 | **********************************************************************/
|
---|
37 |
|
---|
38 | #if defined(QT_THREAD_SUPPORT)
|
---|
39 |
|
---|
40 | #include "qwaitcondition.h"
|
---|
41 | #include "qnamespace.h"
|
---|
42 | #include "qmutex.h"
|
---|
43 | #include "qptrlist.h"
|
---|
44 | #include "qt_os2.h"
|
---|
45 |
|
---|
46 | #define Q_MUTEX_T HMTX
|
---|
47 | #include <private/qmutex_p.h>
|
---|
48 | #include <private/qcriticalsection_p.h>
|
---|
49 |
|
---|
50 | //***********************************************************************
|
---|
51 | // QWaitConditionPrivate
|
---|
52 | // **********************************************************************
|
---|
53 |
|
---|
54 | class QWaitConditionEvent
|
---|
55 | {
|
---|
56 | public:
|
---|
57 | inline QWaitConditionEvent() : priority(0)
|
---|
58 | {
|
---|
59 | event = 0;
|
---|
60 | DosCreateEventSem( NULL, &event, 0, FALSE );
|
---|
61 | }
|
---|
62 | inline ~QWaitConditionEvent() { DosCloseEventSem( event ); }
|
---|
63 | int priority;
|
---|
64 | HEV event;
|
---|
65 | };
|
---|
66 |
|
---|
67 | typedef QPtrList<QWaitConditionEvent> EventQueue;
|
---|
68 |
|
---|
69 | class QWaitConditionPrivate
|
---|
70 | {
|
---|
71 | public:
|
---|
72 | QCriticalSection cs;
|
---|
73 | EventQueue queue;
|
---|
74 | EventQueue freeQueue;
|
---|
75 |
|
---|
76 | bool wait(QMutex *mutex, unsigned long time);
|
---|
77 | };
|
---|
78 |
|
---|
79 | bool QWaitConditionPrivate::wait(QMutex *mutex, unsigned long time)
|
---|
80 | {
|
---|
81 | bool ret = FALSE;
|
---|
82 |
|
---|
83 | cs.enter();
|
---|
84 | QWaitConditionEvent *wce = freeQueue.take();
|
---|
85 | if (!wce)
|
---|
86 | wce = new QWaitConditionEvent;
|
---|
87 |
|
---|
88 | PTIB ptib;
|
---|
89 | DosGetInfoBlocks( &ptib, NULL );
|
---|
90 | wce->priority = ptib->tib_ptib2->tib2_ulpri;
|
---|
91 |
|
---|
92 | // insert 'wce' into the queue (sorted by priority)
|
---|
93 | QWaitConditionEvent *current = queue.first();
|
---|
94 | int index = 0;
|
---|
95 | while (current && current->priority >= wce->priority) {
|
---|
96 | current = queue.next();
|
---|
97 | ++index;
|
---|
98 | }
|
---|
99 | queue.insert(index, wce);
|
---|
100 | cs.leave();
|
---|
101 |
|
---|
102 | if (mutex) mutex->unlock();
|
---|
103 |
|
---|
104 | // wait for the event
|
---|
105 | APIRET rc = DosWaitEventSem( wce->event, time );
|
---|
106 | if ( !rc ) ret = TRUE;
|
---|
107 |
|
---|
108 | if (mutex) mutex->lock();
|
---|
109 |
|
---|
110 | cs.enter();
|
---|
111 | // remove 'wce' from the queue
|
---|
112 | queue.removeRef(wce);
|
---|
113 | ULONG posts;
|
---|
114 | DosResetEventSem( wce->event, &posts );
|
---|
115 | freeQueue.append(wce);
|
---|
116 | cs.leave();
|
---|
117 |
|
---|
118 | return ret;
|
---|
119 | }
|
---|
120 |
|
---|
121 | //***********************************************************************
|
---|
122 | // QWaitCondition implementation
|
---|
123 | //***********************************************************************
|
---|
124 |
|
---|
125 | QWaitCondition::QWaitCondition()
|
---|
126 | {
|
---|
127 | d = new QWaitConditionPrivate;
|
---|
128 | d->freeQueue.setAutoDelete(TRUE);
|
---|
129 | }
|
---|
130 |
|
---|
131 | QWaitCondition::~QWaitCondition()
|
---|
132 | {
|
---|
133 | Q_ASSERT(d->queue.isEmpty());
|
---|
134 | delete d;
|
---|
135 | }
|
---|
136 |
|
---|
137 | bool QWaitCondition::wait( unsigned long time )
|
---|
138 | {
|
---|
139 | return d->wait(0, time);
|
---|
140 | }
|
---|
141 |
|
---|
142 | bool QWaitCondition::wait( QMutex *mutex, unsigned long time)
|
---|
143 | {
|
---|
144 | if ( !mutex )
|
---|
145 | return FALSE;
|
---|
146 |
|
---|
147 | if ( mutex->d->type() == Q_MUTEX_RECURSIVE ) {
|
---|
148 | #ifdef QT_CHECK_RANGE
|
---|
149 | qWarning("QWaitCondition::wait: Cannot wait on recursive mutexes.");
|
---|
150 | #endif
|
---|
151 | return FALSE;
|
---|
152 | }
|
---|
153 | return d->wait(mutex, time);
|
---|
154 | }
|
---|
155 |
|
---|
156 | void QWaitCondition::wakeOne()
|
---|
157 | {
|
---|
158 | // wake up the first thread in the queue
|
---|
159 | d->cs.enter();
|
---|
160 | QWaitConditionEvent *first = d->queue.first();
|
---|
161 | if (first)
|
---|
162 | DosPostEventSem( first->event );
|
---|
163 | d->cs.leave();
|
---|
164 | }
|
---|
165 |
|
---|
166 | void QWaitCondition::wakeAll()
|
---|
167 | {
|
---|
168 | // wake up the all threads in the queue
|
---|
169 | d->cs.enter();
|
---|
170 | QWaitConditionEvent *current = d->queue.first();
|
---|
171 | while (current) {
|
---|
172 | DosPostEventSem( current->event );
|
---|
173 | current = d->queue.next();
|
---|
174 | }
|
---|
175 | d->cs.leave();
|
---|
176 | }
|
---|
177 |
|
---|
178 | #endif // QT_THREAD_SUPPORT
|
---|