Threads.c revision a3bd7f05
1444c061aSmrg/************************************************************
2249c3046SmrgCopyright (c) 1993, Oracle and/or its affiliates. All rights reserved.
31477040fSmrg
41477040fSmrgPermission is hereby granted, free of charge, to any person obtaining a
51477040fSmrgcopy of this software and associated documentation files (the "Software"),
61477040fSmrgto deal in the Software without restriction, including without limitation
71477040fSmrgthe rights to use, copy, modify, merge, publish, distribute, sublicense,
81477040fSmrgand/or sell copies of the Software, and to permit persons to whom the
91477040fSmrgSoftware is furnished to do so, subject to the following conditions:
101477040fSmrg
111477040fSmrgThe above copyright notice and this permission notice (including the next
121477040fSmrgparagraph) shall be included in all copies or substantial portions of the
131477040fSmrgSoftware.
141477040fSmrg
151477040fSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
161477040fSmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
171477040fSmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
181477040fSmrgTHE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
191477040fSmrgLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
201477040fSmrgFROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
211477040fSmrgDEALINGS IN THE SOFTWARE.
22444c061aSmrg
23444c061aSmrg********************************************************/
24444c061aSmrg
25444c061aSmrg/*
26444c061aSmrg
27444c061aSmrgCopyright 1994, 1998  The Open Group
28444c061aSmrg
29444c061aSmrgPermission to use, copy, modify, distribute, and sell this software and its
30444c061aSmrgdocumentation for any purpose is hereby granted without fee, provided that
31444c061aSmrgthe above copyright notice appear in all copies and that both that
32444c061aSmrgcopyright notice and this permission notice appear in supporting
33444c061aSmrgdocumentation.
34444c061aSmrg
35444c061aSmrgThe above copyright notice and this permission notice shall be included in
36444c061aSmrgall copies or substantial portions of the Software.
37444c061aSmrg
38444c061aSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
39444c061aSmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
40444c061aSmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
41444c061aSmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
42444c061aSmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
43444c061aSmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
44444c061aSmrg
45444c061aSmrgExcept as contained in this notice, the name of The Open Group shall not be
46444c061aSmrgused in advertising or otherwise to promote the sale, use or other dealings
47444c061aSmrgin this Software without prior written authorization from The Open Group.
48444c061aSmrg
49444c061aSmrg*/
50444c061aSmrg
51444c061aSmrg#ifdef HAVE_CONFIG_H
52444c061aSmrg#include <config.h>
53444c061aSmrg#endif
54444c061aSmrg#include "IntrinsicI.h"
55444c061aSmrg
56444c061aSmrg#ifdef XTHREADS
57444c061aSmrg
58444c061aSmrg#define xmalloc __XtMalloc
59444c061aSmrg#define xfree XtFree
60444c061aSmrg#include <X11/Xthreads.h>
61444c061aSmrg
62444c061aSmrg#ifndef NDEBUG
63444c061aSmrg#define NDEBUG
64444c061aSmrg#endif
65444c061aSmrg#include <assert.h>
66444c061aSmrg#include <stdio.h>
67444c061aSmrg
68444c061aSmrgtypedef struct _ThreadStack {
69444c061aSmrg    unsigned int size;
70444c061aSmrg    int sp;
71444c061aSmrg    struct _Tstack {
72a3bd7f05Smrg        xthread_t t;
73a3bd7f05Smrg        xcondition_t c;
74444c061aSmrg    } *st;
75444c061aSmrg} ThreadStack;
76444c061aSmrg
77444c061aSmrgtypedef struct _LockRec {
78444c061aSmrg    xmutex_t mutex;
79444c061aSmrg    int level;
80444c061aSmrg    ThreadStack stack;
81444c061aSmrg#ifndef _XMUTEX_NESTS
82444c061aSmrg    xthread_t holder;
83444c061aSmrg    xcondition_t cond;
84444c061aSmrg#endif
85444c061aSmrg} LockRec;
86444c061aSmrg
87444c061aSmrg#define STACK_INCR 16
88444c061aSmrg
89444c061aSmrgstatic LockPtr process_lock = NULL;
90444c061aSmrg
91444c061aSmrgstatic void
92444c061aSmrgInitProcessLock(void)
93444c061aSmrg{
94a3bd7f05Smrg    if (!process_lock) {
95a3bd7f05Smrg        process_lock = XtNew(LockRec);
96a3bd7f05Smrg        process_lock->mutex = xmutex_malloc();
97a3bd7f05Smrg        xmutex_init(process_lock->mutex);
98a3bd7f05Smrg        process_lock->level = 0;
99444c061aSmrg#ifndef _XMUTEX_NESTS
100a3bd7f05Smrg        process_lock->cond = xcondition_malloc();
101a3bd7f05Smrg        xcondition_init(process_lock->cond);
102a3bd7f05Smrg        xthread_clear_id(process_lock->holder);
103444c061aSmrg#endif
104444c061aSmrg    }
105444c061aSmrg}
106444c061aSmrg
107444c061aSmrgstatic void
108444c061aSmrgProcessLock(void)
109444c061aSmrg{
110444c061aSmrg#ifdef _XMUTEX_NESTS
111444c061aSmrg    xmutex_lock(process_lock->mutex);
112444c061aSmrg    process_lock->level++;
113444c061aSmrg#else
114444c061aSmrg    xthread_t this_thread = xthread_self();
115444c061aSmrg
116444c061aSmrg    xmutex_lock(process_lock->mutex);
117444c061aSmrg
118444c061aSmrg    if (!xthread_have_id(process_lock->holder)) {
119a3bd7f05Smrg        process_lock->holder = this_thread;
120a3bd7f05Smrg        xmutex_unlock(process_lock->mutex);
121a3bd7f05Smrg        return;
122444c061aSmrg    }
123444c061aSmrg
124a3bd7f05Smrg    if (xthread_equal(process_lock->holder, this_thread)) {
125a3bd7f05Smrg        process_lock->level++;
126a3bd7f05Smrg        xmutex_unlock(process_lock->mutex);
127a3bd7f05Smrg        return;
128444c061aSmrg    }
129444c061aSmrg
130a3bd7f05Smrg    while (xthread_have_id(process_lock->holder))
131a3bd7f05Smrg        xcondition_wait(process_lock->cond, process_lock->mutex);
132444c061aSmrg
133444c061aSmrg    process_lock->holder = this_thread;
134444c061aSmrg    assert(xthread_equal(process_lock->holder, this_thread));
135444c061aSmrg    xmutex_unlock(process_lock->mutex);
136444c061aSmrg#endif
137444c061aSmrg}
138444c061aSmrg
139444c061aSmrgstatic void
140444c061aSmrgProcessUnlock(void)
141444c061aSmrg{
142444c061aSmrg#ifdef _XMUTEX_NESTS
143444c061aSmrg    process_lock->level--;
144444c061aSmrg    xmutex_unlock(process_lock->mutex);
145444c061aSmrg#else
146444c061aSmrg    xmutex_lock(process_lock->mutex);
147444c061aSmrg    assert(xthread_equal(process_lock->holder, xthread_self()));
148444c061aSmrg    if (process_lock->level != 0) {
149a3bd7f05Smrg        process_lock->level--;
150a3bd7f05Smrg        xmutex_unlock(process_lock->mutex);
151a3bd7f05Smrg        return;
152444c061aSmrg    }
153444c061aSmrg
154444c061aSmrg    xthread_clear_id(process_lock->holder);
155444c061aSmrg    xcondition_signal(process_lock->cond);
156444c061aSmrg
157444c061aSmrg    xmutex_unlock(process_lock->mutex);
158444c061aSmrg#endif
159444c061aSmrg}
160444c061aSmrg
161444c061aSmrgstatic void
162444c061aSmrgAppLock(XtAppContext app)
163444c061aSmrg{
164444c061aSmrg    LockPtr app_lock = app->lock_info;
165a3bd7f05Smrg
166444c061aSmrg#ifdef _XMUTEX_NESTS
167444c061aSmrg    xmutex_lock(app_lock->mutex);
168444c061aSmrg    app_lock->level++;
169444c061aSmrg#else
170444c061aSmrg    xthread_t self = xthread_self();
171a3bd7f05Smrg
172444c061aSmrg    xmutex_lock(app_lock->mutex);
173444c061aSmrg    if (!xthread_have_id(app_lock->holder)) {
174a3bd7f05Smrg        app_lock->holder = self;
175a3bd7f05Smrg        assert(xthread_equal(app_lock->holder, self));
176a3bd7f05Smrg        xmutex_unlock(app_lock->mutex);
177a3bd7f05Smrg        return;
178444c061aSmrg    }
179444c061aSmrg    if (xthread_equal(app_lock->holder, self)) {
180a3bd7f05Smrg        app_lock->level++;
181a3bd7f05Smrg        xmutex_unlock(app_lock->mutex);
182a3bd7f05Smrg        return;
183444c061aSmrg    }
184a3bd7f05Smrg    while (xthread_have_id(app_lock->holder)) {
185a3bd7f05Smrg        xcondition_wait(app_lock->cond, app_lock->mutex);
186444c061aSmrg    }
187444c061aSmrg    app_lock->holder = self;
188444c061aSmrg    assert(xthread_equal(app_lock->holder, self));
189444c061aSmrg    xmutex_unlock(app_lock->mutex);
190444c061aSmrg#endif
191444c061aSmrg}
192444c061aSmrg
193444c061aSmrgstatic void
194444c061aSmrgAppUnlock(XtAppContext app)
195444c061aSmrg{
196444c061aSmrg    LockPtr app_lock = app->lock_info;
197a3bd7f05Smrg
198444c061aSmrg#ifdef _XMUTEX_NESTS
199444c061aSmrg    app_lock->level--;
200444c061aSmrg    xmutex_unlock(app_lock->mutex);
201444c061aSmrg#else
202444c061aSmrg    xthread_t self;
203a3bd7f05Smrg
204444c061aSmrg    self = xthread_self();
205a3bd7f05Smrg    (void) self;
2060568f49bSmrg
207444c061aSmrg    xmutex_lock(app_lock->mutex);
208444c061aSmrg    assert(xthread_equal(app_lock->holder, self));
209444c061aSmrg    if (app_lock->level != 0) {
210a3bd7f05Smrg        app_lock->level--;
211a3bd7f05Smrg        xmutex_unlock(app_lock->mutex);
212a3bd7f05Smrg        return;
213444c061aSmrg    }
214444c061aSmrg    xthread_clear_id(app_lock->holder);
215444c061aSmrg    xcondition_signal(app_lock->cond);
216444c061aSmrg    xmutex_unlock(app_lock->mutex);
217444c061aSmrg#endif
218444c061aSmrg}
219444c061aSmrg
220444c061aSmrgstatic void
221a3bd7f05SmrgYieldAppLock(XtAppContext app,
222a3bd7f05Smrg             Boolean *push_thread,
223a3bd7f05Smrg             Boolean *pushed_thread,
224a3bd7f05Smrg             int *level)
225444c061aSmrg{
226444c061aSmrg    LockPtr app_lock = app->lock_info;
227444c061aSmrg    xthread_t self = xthread_self();
228a3bd7f05Smrg
229444c061aSmrg#ifndef _XMUTEX_NESTS
230444c061aSmrg    xmutex_lock(app_lock->mutex);
231444c061aSmrg    assert(xthread_equal(app_lock->holder, self));
232444c061aSmrg#endif
233444c061aSmrg    *level = app_lock->level;
234444c061aSmrg    if (*push_thread) {
235a3bd7f05Smrg        *push_thread = FALSE;
236a3bd7f05Smrg        *pushed_thread = TRUE;
237a3bd7f05Smrg
238a3bd7f05Smrg        if (app_lock->stack.sp == (int) app_lock->stack.size - 1) {
239a3bd7f05Smrg            unsigned ii;
240a3bd7f05Smrg
241a3bd7f05Smrg            app_lock->stack.st = (struct _Tstack *)
242a3bd7f05Smrg                XtRealloc((char *) app_lock->stack.st,
243a3bd7f05Smrg                          (Cardinal) ((app_lock->stack.size +
244a3bd7f05Smrg                                       STACK_INCR) * sizeof(struct _Tstack)));
245a3bd7f05Smrg            ii = app_lock->stack.size;
246a3bd7f05Smrg            app_lock->stack.size += STACK_INCR;
247a3bd7f05Smrg            for (; ii < app_lock->stack.size; ii++) {
248a3bd7f05Smrg                app_lock->stack.st[ii].c = xcondition_malloc();
249a3bd7f05Smrg                xcondition_init(app_lock->stack.st[ii].c);
250a3bd7f05Smrg            }
251a3bd7f05Smrg        }
252a3bd7f05Smrg        app_lock->stack.st[++(app_lock->stack.sp)].t = self;
253444c061aSmrg    }
254444c061aSmrg#ifdef _XMUTEX_NESTS
255444c061aSmrg    while (app_lock->level > 0) {
256a3bd7f05Smrg        app_lock->level--;
257a3bd7f05Smrg        xmutex_unlock(app_lock->mutex);
258444c061aSmrg    }
259444c061aSmrg#else
260444c061aSmrg    xcondition_signal(app_lock->cond);
261444c061aSmrg    app_lock->level = 0;
262444c061aSmrg    xthread_clear_id(app_lock->holder);
263444c061aSmrg    xmutex_unlock(app_lock->mutex);
264444c061aSmrg#endif
265444c061aSmrg}
266444c061aSmrg
267444c061aSmrgstatic void
268a3bd7f05SmrgRestoreAppLock(XtAppContext app, int level, Boolean *pushed_thread)
269444c061aSmrg{
270444c061aSmrg    LockPtr app_lock = app->lock_info;
271444c061aSmrg    xthread_t self = xthread_self();
272a3bd7f05Smrg
273444c061aSmrg    xmutex_lock(app_lock->mutex);
274444c061aSmrg#ifdef _XMUTEX_NESTS
275444c061aSmrg    app_lock->level++;
276444c061aSmrg#else
277a3bd7f05Smrg    while (xthread_have_id(app_lock->holder)) {
278a3bd7f05Smrg        xcondition_wait(app_lock->cond, app_lock->mutex);
279444c061aSmrg    }
280444c061aSmrg#endif
281444c061aSmrg    if (!xthread_equal(app_lock->stack.st[app_lock->stack.sp].t, self)) {
282a3bd7f05Smrg        int ii;
283a3bd7f05Smrg
284a3bd7f05Smrg        for (ii = app_lock->stack.sp - 1; ii >= 0; ii--) {
285a3bd7f05Smrg            if (xthread_equal(app_lock->stack.st[ii].t, self)) {
286a3bd7f05Smrg                xcondition_wait(app_lock->stack.st[ii].c, app_lock->mutex);
287a3bd7f05Smrg                break;
288a3bd7f05Smrg            }
289a3bd7f05Smrg        }
290444c061aSmrg#ifndef _XMUTEX_NESTS
291a3bd7f05Smrg        while (xthread_have_id(app_lock->holder)) {
292a3bd7f05Smrg            xcondition_wait(app_lock->cond, app_lock->mutex);
293a3bd7f05Smrg        }
294444c061aSmrg#endif
295444c061aSmrg    }
296444c061aSmrg#ifdef _XMUTEX_NESTS
297444c061aSmrg    while (app_lock->level < level) {
298a3bd7f05Smrg        xmutex_lock(app_lock->mutex);
299a3bd7f05Smrg        app_lock->level++;
300444c061aSmrg    }
301444c061aSmrg#else
302444c061aSmrg    app_lock->holder = self;
303444c061aSmrg    app_lock->level = level;
304444c061aSmrg    assert(xthread_equal(app_lock->holder, self));
305444c061aSmrg#endif
306444c061aSmrg    if (*pushed_thread) {
307a3bd7f05Smrg        *pushed_thread = FALSE;
308a3bd7f05Smrg        (app_lock->stack.sp)--;
309a3bd7f05Smrg        if (app_lock->stack.sp >= 0) {
310a3bd7f05Smrg            xcondition_signal(app_lock->stack.st[app_lock->stack.sp].c);
311a3bd7f05Smrg        }
312444c061aSmrg    }
313444c061aSmrg#ifndef _XMUTEX_NESTS
314444c061aSmrg    xmutex_unlock(app_lock->mutex);
315444c061aSmrg#endif
316444c061aSmrg}
317444c061aSmrg
318444c061aSmrgstatic void
319444c061aSmrgFreeAppLock(XtAppContext app)
320444c061aSmrg{
321444c061aSmrg    unsigned ii;
322444c061aSmrg    LockPtr app_lock = app->lock_info;
323444c061aSmrg
324a3bd7f05Smrg    if (app_lock) {
325a3bd7f05Smrg        xmutex_clear(app_lock->mutex);
326a3bd7f05Smrg        xmutex_free(app_lock->mutex);
327444c061aSmrg#ifndef _XMUTEX_NESTS
328a3bd7f05Smrg        xcondition_clear(app_lock->cond);
329a3bd7f05Smrg        xcondition_free(app_lock->cond);
330444c061aSmrg#endif
331a3bd7f05Smrg        if (app_lock->stack.st != (struct _Tstack *) NULL) {
332a3bd7f05Smrg            for (ii = 0; ii < app_lock->stack.size; ii++) {
333a3bd7f05Smrg                xcondition_clear(app_lock->stack.st[ii].c);
334a3bd7f05Smrg                xcondition_free(app_lock->stack.st[ii].c);
335a3bd7f05Smrg            }
336a3bd7f05Smrg            XtFree((char *) app_lock->stack.st);
337a3bd7f05Smrg        }
338a3bd7f05Smrg        XtFree((char *) app_lock);
339a3bd7f05Smrg        app->lock_info = NULL;
340444c061aSmrg    }
341444c061aSmrg}
342444c061aSmrg
343444c061aSmrgstatic void
344444c061aSmrgInitAppLock(XtAppContext app)
345444c061aSmrg{
346444c061aSmrg    int ii;
347444c061aSmrg    LockPtr app_lock;
348444c061aSmrg
349444c061aSmrg    app->lock = AppLock;
350444c061aSmrg    app->unlock = AppUnlock;
351444c061aSmrg    app->yield_lock = YieldAppLock;
352444c061aSmrg    app->restore_lock = RestoreAppLock;
353444c061aSmrg    app->free_lock = FreeAppLock;
354444c061aSmrg
355444c061aSmrg    app_lock = app->lock_info = XtNew(LockRec);
356444c061aSmrg    app_lock->mutex = xmutex_malloc();
357444c061aSmrg    xmutex_init(app_lock->mutex);
358444c061aSmrg    app_lock->level = 0;
359444c061aSmrg#ifndef _XMUTEX_NESTS
360444c061aSmrg    app_lock->cond = xcondition_malloc();
361444c061aSmrg    xcondition_init(app_lock->cond);
362444c061aSmrg    xthread_clear_id(app_lock->holder);
363444c061aSmrg#endif
364444c061aSmrg    app_lock->stack.size = STACK_INCR;
365444c061aSmrg    app_lock->stack.sp = -1;
366444c061aSmrg    app_lock->stack.st =
367a3bd7f05Smrg        (struct _Tstack *) __XtMalloc(sizeof(struct _Tstack) * STACK_INCR);
368444c061aSmrg    for (ii = 0; ii < STACK_INCR; ii++) {
369a3bd7f05Smrg        app_lock->stack.st[ii].c = xcondition_malloc();
370a3bd7f05Smrg        xcondition_init(app_lock->stack.st[ii].c);
371444c061aSmrg    }
372444c061aSmrg}
373444c061aSmrg
374a3bd7f05Smrg#endif                          /* defined(XTHREADS) */
375444c061aSmrg
376a3bd7f05Smrgvoid
377a3bd7f05SmrgXtAppLock(XtAppContext app)
378444c061aSmrg{
379444c061aSmrg#ifdef XTHREADS
380a3bd7f05Smrg    if (app->lock)
381a3bd7f05Smrg        (*app->lock) (app);
382444c061aSmrg#endif
383444c061aSmrg}
384444c061aSmrg
385a3bd7f05Smrgvoid
386a3bd7f05SmrgXtAppUnlock(XtAppContext app)
387444c061aSmrg{
388444c061aSmrg#ifdef XTHREADS
389a3bd7f05Smrg    if (app->unlock)
390a3bd7f05Smrg        (*app->unlock) (app);
391444c061aSmrg#endif
392444c061aSmrg}
393444c061aSmrg
394a3bd7f05Smrgvoid
395a3bd7f05SmrgXtProcessLock(void)
396444c061aSmrg{
397444c061aSmrg#ifdef XTHREADS
398a3bd7f05Smrg    if (_XtProcessLock)
399a3bd7f05Smrg        (*_XtProcessLock) ();
400444c061aSmrg#endif
401444c061aSmrg}
402444c061aSmrg
403a3bd7f05Smrgvoid
404a3bd7f05SmrgXtProcessUnlock(void)
405444c061aSmrg{
406444c061aSmrg#ifdef XTHREADS
407a3bd7f05Smrg    if (_XtProcessUnlock)
408a3bd7f05Smrg        (*_XtProcessUnlock) ();
409444c061aSmrg#endif
410444c061aSmrg}
411444c061aSmrg
412a3bd7f05SmrgBoolean
413a3bd7f05SmrgXtToolkitThreadInitialize(void)
414444c061aSmrg{
415444c061aSmrg#ifdef XTHREADS
416444c061aSmrg    if (_XtProcessLock == NULL) {
417444c061aSmrg#ifdef xthread_init
418a3bd7f05Smrg        xthread_init();
419444c061aSmrg#endif
420a3bd7f05Smrg        InitProcessLock();
421a3bd7f05Smrg        _XtProcessLock = ProcessLock;
422a3bd7f05Smrg        _XtProcessUnlock = ProcessUnlock;
423a3bd7f05Smrg        _XtInitAppLock = InitAppLock;
424444c061aSmrg    }
425444c061aSmrg    return True;
426444c061aSmrg#else
427444c061aSmrg    return False;
428444c061aSmrg#endif
429444c061aSmrg}
430