LockDis.c revision 61b2299d
1/* $Xorg: LockDis.c,v 1.4 2001/02/09 02:03:34 xorgcvs Exp $ */
2
3/*
4
5Copyright 1993, 1998  The Open Group
6
7Permission to use, copy, modify, distribute, and sell this software and its
8documentation for any purpose is hereby granted without fee, provided that
9the above copyright notice appear in all copies and that both that
10copyright notice and this permission notice appear in supporting
11documentation.
12
13The above copyright notice and this permission notice shall be included in
14all copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
19OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23Except as contained in this notice, the name of The Open Group shall not be
24used in advertising or otherwise to promote the sale, use or other dealings
25in this Software without prior written authorization from The Open Group.
26
27*/
28/* $XFree86$ */
29
30/*
31 * Author: Stephen Gildea, MIT X Consortium
32 *
33 * XLockDis.c - multi-thread application-level locking routines
34 */
35
36#ifdef HAVE_CONFIG_H
37#include <config.h>
38#endif
39#include "Xlibint.h"
40#ifdef XTHREADS
41#include "locking.h"
42#endif
43
44void
45XLockDisplay(
46    register Display* dpy)
47{
48#ifdef XTHREADS
49    LockDisplay(dpy);
50    if (dpy->lock)
51	(*dpy->lock->user_lock_display)(dpy);
52    /*
53     * We want the threads in the reply queue to all get out before
54     * XLockDisplay returns, in case they have any side effects the
55     * caller of XLockDisplay was trying to protect against.
56     * XLockDisplay puts itself at the head of the event waiters queue
57     * to wait for all the replies to come in.
58     */
59    if (dpy->lock && dpy->lock->reply_awaiters) {
60	struct _XCVList *cvl;
61
62	cvl = (*dpy->lock->create_cvl)(dpy);
63
64	/* stuff ourselves on the head of the queue */
65	cvl->next = dpy->lock->event_awaiters;
66	dpy->lock->event_awaiters = cvl;
67
68	while (dpy->lock->reply_awaiters)
69	    ConditionWait(dpy, cvl->cv);
70	UnlockNextEventReader(dpy); /* pass the signal on */
71    }
72    UnlockDisplay(dpy);
73#endif
74}
75
76void
77XUnlockDisplay(
78    register Display* dpy)
79{
80#ifdef XTHREADS
81    LockDisplay(dpy);
82    if (dpy->lock)
83	(*dpy->lock->user_unlock_display)(dpy);
84    UnlockDisplay(dpy);
85#endif
86}
87