1 | /****************************************************************************
|
---|
2 | ** $Id: qmutex_pm.cpp 8 2005-11-16 19:36:46Z dmik $
|
---|
3 | **
|
---|
4 | ** Implementation of QMutex 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 "qt_os2.h"
|
---|
41 | #include "qmutex.h"
|
---|
42 | #include "qnamespace.h"
|
---|
43 |
|
---|
44 | #define Q_MUTEX_T HMTX
|
---|
45 | #include <private/qmutex_p.h>
|
---|
46 |
|
---|
47 | QMutexPrivate::~QMutexPrivate()
|
---|
48 | {
|
---|
49 | APIRET rc = DosCloseMutexSem( handle );
|
---|
50 | if ( rc ) {
|
---|
51 | #ifdef QT_CHECK_RANGE
|
---|
52 | qSystemWarning( "Mutex destroy failure", rc );
|
---|
53 | #endif
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | /*
|
---|
58 | QRecursiveMutexPrivate - implements a recursive mutex
|
---|
59 | */
|
---|
60 |
|
---|
61 | class QRecursiveMutexPrivate : public QMutexPrivate
|
---|
62 | {
|
---|
63 | public:
|
---|
64 | QRecursiveMutexPrivate();
|
---|
65 |
|
---|
66 | void lock();
|
---|
67 | void unlock();
|
---|
68 | bool locked();
|
---|
69 | bool trylock();
|
---|
70 | int type() const { return Q_MUTEX_RECURSIVE; }
|
---|
71 | };
|
---|
72 |
|
---|
73 | QRecursiveMutexPrivate::QRecursiveMutexPrivate()
|
---|
74 | {
|
---|
75 | APIRET rc = DosCreateMutexSem( NULL, &handle, 0, FALSE );
|
---|
76 | if ( rc ) {
|
---|
77 | handle = 0;
|
---|
78 | #ifdef QT_CHECK_RANGE
|
---|
79 | qSystemWarning( "Mutex init failure", rc );
|
---|
80 | #endif
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | void QRecursiveMutexPrivate::lock()
|
---|
85 | {
|
---|
86 | APIRET rc = DosRequestMutexSem( handle, SEM_INDEFINITE_WAIT );
|
---|
87 | switch ( rc ) {
|
---|
88 | case NO_ERROR:
|
---|
89 | break;
|
---|
90 | case ERROR_SEM_OWNER_DIED:
|
---|
91 | #ifdef QT_CHECK_RANGE
|
---|
92 | qWarning( "Thread terminated while locking mutex!" );
|
---|
93 | #endif
|
---|
94 | break;
|
---|
95 | default:
|
---|
96 | #ifdef QT_CHECK_RANGE
|
---|
97 | qSystemWarning( "Mutex lock failure", rc );
|
---|
98 | #endif
|
---|
99 | break;
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | void QRecursiveMutexPrivate::unlock()
|
---|
104 | {
|
---|
105 | APIRET rc = DosReleaseMutexSem( handle );
|
---|
106 | if ( rc ) {
|
---|
107 | #ifdef QT_CHECK_RANGE
|
---|
108 | qSystemWarning( "Mutex unlock failure", rc );
|
---|
109 | #endif
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | bool QRecursiveMutexPrivate::locked()
|
---|
114 | {
|
---|
115 | PID pid = 0;
|
---|
116 | TID tid = 0;
|
---|
117 | ULONG count = 0;
|
---|
118 | APIRET rc = DosQueryMutexSem( handle, &pid, &tid, &count );
|
---|
119 | if ( rc ) {
|
---|
120 | #ifdef QT_CHECK_RANGE
|
---|
121 | if ( rc == ERROR_SEM_OWNER_DIED )
|
---|
122 | qWarning( "Thread terminated while locking mutex!" );
|
---|
123 | else
|
---|
124 | qSystemWarning( "Mutex locktest failure", rc );
|
---|
125 | #endif
|
---|
126 | return FALSE;
|
---|
127 | }
|
---|
128 | return count != 0;
|
---|
129 | }
|
---|
130 |
|
---|
131 | bool QRecursiveMutexPrivate::trylock()
|
---|
132 | {
|
---|
133 | APIRET rc = DosRequestMutexSem( handle, SEM_IMMEDIATE_RETURN );
|
---|
134 | switch ( rc ) {
|
---|
135 | case NO_ERROR:
|
---|
136 | return TRUE;
|
---|
137 | case ERROR_TIMEOUT:
|
---|
138 | break;
|
---|
139 | case ERROR_SEM_OWNER_DIED:
|
---|
140 | #ifdef QT_CHECK_RANGE
|
---|
141 | qWarning( "Thread terminated while locking mutex!" );
|
---|
142 | #endif
|
---|
143 | break;
|
---|
144 | default:
|
---|
145 | #ifdef QT_CHECK_RANGE
|
---|
146 | qSystemWarning( "Mutex lock failure", rc );
|
---|
147 | #endif
|
---|
148 | break;
|
---|
149 | }
|
---|
150 | return FALSE;
|
---|
151 | }
|
---|
152 |
|
---|
153 | /*
|
---|
154 | QNonRecursiveMutexPrivate - implements a non-recursive mutex
|
---|
155 | */
|
---|
156 |
|
---|
157 | class QNonRecursiveMutexPrivate : public QRecursiveMutexPrivate
|
---|
158 | {
|
---|
159 | public:
|
---|
160 | QNonRecursiveMutexPrivate();
|
---|
161 |
|
---|
162 | void lock();
|
---|
163 | bool trylock();
|
---|
164 | int type() const { return Q_MUTEX_NORMAL; };
|
---|
165 | };
|
---|
166 |
|
---|
167 | QNonRecursiveMutexPrivate::QNonRecursiveMutexPrivate()
|
---|
168 | : QRecursiveMutexPrivate()
|
---|
169 | {
|
---|
170 | }
|
---|
171 |
|
---|
172 | void QNonRecursiveMutexPrivate::lock()
|
---|
173 | {
|
---|
174 | PTIB ptib;
|
---|
175 | DosGetInfoBlocks( &ptib, NULL );
|
---|
176 |
|
---|
177 | PID pid = 0;
|
---|
178 | TID tid = 0;
|
---|
179 | ULONG count = 0;
|
---|
180 | APIRET rc = DosQueryMutexSem( handle, &pid, &tid, &count );
|
---|
181 | if ( rc ) {
|
---|
182 | #ifdef QT_CHECK_RANGE
|
---|
183 | if ( rc == ERROR_SEM_OWNER_DIED )
|
---|
184 | qWarning( "Thread terminated while locking mutex!" );
|
---|
185 | else
|
---|
186 | qSystemWarning( "Mutex locktest failure", rc );
|
---|
187 | #endif
|
---|
188 | return;
|
---|
189 | }
|
---|
190 |
|
---|
191 | if ( count != 0 && tid == ptib->tib_ptib2->tib2_ultid ) {
|
---|
192 | #ifdef QT_CHECK_RANGE
|
---|
193 | qWarning( "Non-recursive mutex already locked by this thread" );
|
---|
194 | #endif
|
---|
195 | } else {
|
---|
196 | QRecursiveMutexPrivate::lock();
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | bool QNonRecursiveMutexPrivate::trylock()
|
---|
201 | {
|
---|
202 | PTIB ptib;
|
---|
203 | DosGetInfoBlocks( &ptib, NULL );
|
---|
204 |
|
---|
205 | PID pid = 0;
|
---|
206 | TID tid = 0;
|
---|
207 | ULONG count = 0;
|
---|
208 | APIRET rc = DosQueryMutexSem( handle, &pid, &tid, &count );
|
---|
209 | if ( rc ) {
|
---|
210 | #ifdef QT_CHECK_RANGE
|
---|
211 | if ( rc == ERROR_SEM_OWNER_DIED )
|
---|
212 | qWarning( "Thread terminated while locking mutex!" );
|
---|
213 | else
|
---|
214 | qSystemWarning( "Mutex locktest failure", rc );
|
---|
215 | #endif
|
---|
216 | return FALSE;
|
---|
217 | }
|
---|
218 |
|
---|
219 | if ( count != 0 && tid == ptib->tib_ptib2->tib2_ultid ) {
|
---|
220 | // locked by this thread already, return FALSE
|
---|
221 | return FALSE;
|
---|
222 | }
|
---|
223 | return QRecursiveMutexPrivate::trylock();
|
---|
224 | }
|
---|
225 |
|
---|
226 | /*
|
---|
227 | QMutex implementation
|
---|
228 | */
|
---|
229 |
|
---|
230 | QMutex::QMutex(bool recursive)
|
---|
231 | {
|
---|
232 | if ( recursive )
|
---|
233 | d = new QRecursiveMutexPrivate();
|
---|
234 | else
|
---|
235 | d = new QNonRecursiveMutexPrivate();
|
---|
236 | }
|
---|
237 |
|
---|
238 | QMutex::~QMutex()
|
---|
239 | {
|
---|
240 | delete d;
|
---|
241 | }
|
---|
242 |
|
---|
243 | void QMutex::lock()
|
---|
244 | {
|
---|
245 | d->lock();
|
---|
246 | }
|
---|
247 |
|
---|
248 | void QMutex::unlock()
|
---|
249 | {
|
---|
250 | d->unlock();
|
---|
251 | }
|
---|
252 |
|
---|
253 | bool QMutex::locked()
|
---|
254 | {
|
---|
255 | return d->locked();
|
---|
256 | }
|
---|
257 |
|
---|
258 | bool QMutex::tryLock()
|
---|
259 | {
|
---|
260 | return d->trylock();
|
---|
261 | }
|
---|
262 |
|
---|
263 | #endif
|
---|