1 /************************************************************
2 Copyright (c) 1993, Oracle and/or its affiliates. All rights reserved.
4 Permission is hereby granted, free of charge, to any person obtaining a
5 copy of this software and associated documentation files (the "Software"),
6 to deal in the Software without restriction, including without limitation
7 the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 and/or sell copies of the Software, and to permit persons to whom the
9 Software is furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice (including the next
12 paragraph) shall be included in all copies or substantial portions of the
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 DEALINGS IN THE SOFTWARE.
23 ********************************************************/
27 Copyright 1994, 1998 The Open Group
29 Permission to use, copy, modify, distribute, and sell this software and its
30 documentation for any purpose is hereby granted without fee, provided that
31 the above copyright notice appear in all copies and that both that
32 copyright notice and this permission notice appear in supporting
35 The above copyright notice and this permission notice shall be included in
36 all copies or substantial portions of the Software.
38 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
39 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
40 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
41 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
42 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
43 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
45 Except as contained in this notice, the name of The Open Group shall not be
46 used in advertising or otherwise to promote the sale, use or other dealings
47 in this Software without prior written authorization from The Open Group.
54 #include "IntrinsicI.h"
58 #define xmalloc __XtMalloc
60 #include <X11/Xthreads.h>
68 typedef struct _ThreadStack {
77 typedef struct _LockRec {
90 static LockPtr process_lock = NULL;
96 process_lock = XtNew(LockRec);
97 process_lock->mutex = xmutex_malloc();
98 xmutex_init(process_lock->mutex);
99 process_lock->level = 0;
100 #ifndef _XMUTEX_NESTS
101 process_lock->cond = xcondition_malloc();
102 xcondition_init(process_lock->cond);
103 xthread_clear_id(process_lock->holder);
112 xmutex_lock(process_lock->mutex);
113 process_lock->level++;
115 xthread_t this_thread = xthread_self();
117 xmutex_lock(process_lock->mutex);
119 if (!xthread_have_id(process_lock->holder)) {
120 process_lock->holder = this_thread;
121 xmutex_unlock(process_lock->mutex);
125 if (xthread_equal(process_lock->holder,this_thread)) {
126 process_lock->level++;
127 xmutex_unlock(process_lock->mutex);
131 while(xthread_have_id(process_lock->holder))
132 xcondition_wait(process_lock->cond, process_lock->mutex);
134 process_lock->holder = this_thread;
135 assert(xthread_equal(process_lock->holder, this_thread));
136 xmutex_unlock(process_lock->mutex);
144 process_lock->level--;
145 xmutex_unlock(process_lock->mutex);
147 xmutex_lock(process_lock->mutex);
148 assert(xthread_equal(process_lock->holder, xthread_self()));
149 if (process_lock->level != 0) {
150 process_lock->level--;
151 xmutex_unlock(process_lock->mutex);
155 xthread_clear_id(process_lock->holder);
156 xcondition_signal(process_lock->cond);
158 xmutex_unlock(process_lock->mutex);
164 AppLock(XtAppContext app)
166 LockPtr app_lock = app->lock_info;
168 xmutex_lock(app_lock->mutex);
171 xthread_t self = xthread_self();
172 xmutex_lock(app_lock->mutex);
173 if (!xthread_have_id(app_lock->holder)) {
174 app_lock->holder = self;
175 assert(xthread_equal(app_lock->holder, self));
176 xmutex_unlock(app_lock->mutex);
179 if (xthread_equal(app_lock->holder, self)) {
181 xmutex_unlock(app_lock->mutex);
184 while(xthread_have_id(app_lock->holder)) {
185 xcondition_wait(app_lock->cond, app_lock->mutex);
187 app_lock->holder = self;
188 assert(xthread_equal(app_lock->holder, self));
189 xmutex_unlock(app_lock->mutex);
194 AppUnlock(XtAppContext app)
196 LockPtr app_lock = app->lock_info;
199 xmutex_unlock(app_lock->mutex);
203 self = xthread_self();
204 xmutex_lock(app_lock->mutex);
205 assert(xthread_equal(app_lock->holder, self));
206 if (app_lock->level != 0) {
208 xmutex_unlock(app_lock->mutex);
211 xthread_clear_id(app_lock->holder);
212 xcondition_signal(app_lock->cond);
213 xmutex_unlock(app_lock->mutex);
220 Boolean* push_thread,
221 Boolean* pushed_thread,
224 LockPtr app_lock = app->lock_info;
225 xthread_t self = xthread_self();
226 #ifndef _XMUTEX_NESTS
227 xmutex_lock(app_lock->mutex);
228 assert(xthread_equal(app_lock->holder, self));
230 *level = app_lock->level;
232 *push_thread = FALSE;
233 *pushed_thread = TRUE;
235 if(app_lock->stack.sp == (int)app_lock->stack.size - 1) {
237 app_lock->stack.st = (struct _Tstack *)
238 XtRealloc ((char *)app_lock->stack.st,
239 (app_lock->stack.size + STACK_INCR) * sizeof (struct _Tstack));
240 ii = app_lock->stack.size;
241 app_lock->stack.size += STACK_INCR;
242 for ( ; ii < app_lock->stack.size; ii++) {
243 app_lock->stack.st[ii].c = xcondition_malloc();
244 xcondition_init(app_lock->stack.st[ii].c);
247 app_lock->stack.st[++(app_lock->stack.sp)].t = self;
250 while (app_lock->level > 0) {
252 xmutex_unlock(app_lock->mutex);
255 xcondition_signal(app_lock->cond);
257 xthread_clear_id(app_lock->holder);
258 xmutex_unlock(app_lock->mutex);
266 Boolean* pushed_thread)
268 LockPtr app_lock = app->lock_info;
269 xthread_t self = xthread_self();
270 xmutex_lock(app_lock->mutex);
274 while(xthread_have_id(app_lock->holder)) {
275 xcondition_wait(app_lock->cond, app_lock->mutex);
278 if (!xthread_equal(app_lock->stack.st[app_lock->stack.sp].t, self)) {
280 for (ii = app_lock->stack.sp - 1; ii >= 0; ii--) {
281 if (xthread_equal(app_lock->stack.st[ii].t, self)) {
282 xcondition_wait(app_lock->stack.st[ii].c, app_lock->mutex);
286 #ifndef _XMUTEX_NESTS
287 while(xthread_have_id(app_lock->holder)) {
288 xcondition_wait(app_lock->cond, app_lock->mutex);
293 while (app_lock->level < level) {
294 xmutex_lock(app_lock->mutex);
298 app_lock->holder = self;
299 app_lock->level = level;
300 assert(xthread_equal(app_lock->holder, self));
302 if (*pushed_thread) {
303 *pushed_thread = FALSE;
304 (app_lock->stack.sp)--;
305 if (app_lock->stack.sp >= 0) {
306 xcondition_signal (app_lock->stack.st[app_lock->stack.sp].c);
309 #ifndef _XMUTEX_NESTS
310 xmutex_unlock(app_lock->mutex);
315 FreeAppLock(XtAppContext app)
318 LockPtr app_lock = app->lock_info;
321 xmutex_clear(app_lock->mutex);
322 xmutex_free(app_lock->mutex);
323 #ifndef _XMUTEX_NESTS
324 xcondition_clear(app_lock->cond);
325 xcondition_free(app_lock->cond);
327 if(app_lock->stack.st != (struct _Tstack *)NULL) {
328 for (ii = 0; ii < app_lock->stack.size; ii++) {
329 xcondition_clear(app_lock->stack.st[ii].c);
330 xcondition_free(app_lock->stack.st[ii].c);
332 XtFree((char *)app_lock->stack.st);
334 XtFree((char *)app_lock);
335 app->lock_info = NULL;
340 InitAppLock(XtAppContext app)
346 app->unlock = AppUnlock;
347 app->yield_lock = YieldAppLock;
348 app->restore_lock = RestoreAppLock;
349 app->free_lock = FreeAppLock;
351 app_lock = app->lock_info = XtNew(LockRec);
352 app_lock->mutex = xmutex_malloc();
353 xmutex_init(app_lock->mutex);
355 #ifndef _XMUTEX_NESTS
356 app_lock->cond = xcondition_malloc();
357 xcondition_init(app_lock->cond);
358 xthread_clear_id(app_lock->holder);
360 app_lock->stack.size = STACK_INCR;
361 app_lock->stack.sp = -1;
363 (struct _Tstack *)__XtMalloc(sizeof(struct _Tstack)*STACK_INCR);
364 for (ii = 0; ii < STACK_INCR; ii++) {
365 app_lock->stack.st[ii].c = xcondition_malloc();
366 xcondition_init(app_lock->stack.st[ii].c);
370 #endif /* defined(XTHREADS) */
372 void XtAppLock(XtAppContext app)
380 void XtAppUnlock(XtAppContext app)
388 void XtProcessLock(void)
396 void XtProcessUnlock(void)
400 (*_XtProcessUnlock)();
404 Boolean XtToolkitThreadInitialize(void)
407 if (_XtProcessLock == NULL) {
412 _XtProcessLock = ProcessLock;
413 _XtProcessUnlock = ProcessUnlock;
414 _XtInitAppLock = InitAppLock;