locking.h revision 0b3dccf2
1/*
2
3Copyright 1992, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21Except as contained in this notice, the name of The Open Group shall not be
22used in advertising or otherwise to promote the sale, use or other dealings
23in this Software without prior written authorization from The Open Group.
24
25*/
26
27/*
28 * Author: Stephen Gildea, MIT X Consortium
29 *
30 * locking.h - data types for C Threads locking.
31 * Used by XlibInt.c, locking.c, LockDis.c
32 */
33
34#ifndef _X_locking_H_
35#define _X_locking_H_
36
37#ifndef xmalloc
38#define xmalloc(s) Xmalloc(s)
39#endif
40#ifndef xfree
41#define xfree(s) Xfree(s)
42#endif
43#include <X11/Xlib.h>
44#include <X11/Xlibint.h>
45#include <X11/Xthreads.h>
46
47struct _XCVList {
48    xcondition_t cv;
49    xReply *buf;
50    struct _XCVList *next;
51};
52
53extern xthread_t (*_Xthread_self_fn)( /* in XlibInt.c */
54    void
55);
56
57/* Display->lock is a pointer to one of these */
58
59struct _XLockInfo {
60	xmutex_t mutex;		/* mutex for critical sections */
61	int reply_bytes_left;	/* nbytes of the reply still to read */
62	Bool reply_was_read;	/* _XReadEvents read a reply for _XReply */
63	struct _XCVList *reply_awaiters; /* list of CVs for _XReply */
64	struct _XCVList **reply_awaiters_tail;
65	struct _XCVList *event_awaiters; /* list of CVs for _XReadEvents */
66	struct _XCVList **event_awaiters_tail;
67	Bool reply_first;	/* who may read, reply queue or event queue */
68	/* for XLockDisplay */
69	int locking_level;	/* how many times into XLockDisplay we are */
70	xthread_t locking_thread; /* thread that did XLockDisplay */
71	xcondition_t cv;	/* wait if another thread has XLockDisplay */
72	xthread_t reading_thread; /* cache */
73	xthread_t conni_thread;	/* thread in XProcessInternalConnection */
74	xcondition_t writers;	/* wait for writable */
75	int num_free_cvls;
76	struct _XCVList *free_cvls;
77	/* used only in XlibInt.c */
78	void (*pop_reader)(
79			   Display* /* dpy */,
80			   struct _XCVList** /* list */,
81			   struct _XCVList*** /* tail */
82			   );
83	struct _XCVList *(*push_reader)(
84					Display *	   /* dpy */,
85					struct _XCVList*** /* tail */
86					);
87	void (*condition_wait)(
88			       xcondition_t /* cv */,
89			       xmutex_t /* mutex */
90#if defined(XTHREADS_WARN) || defined(XTHREADS_FILE_LINE)
91			       , char* /* file */,
92			       int /* line */
93#endif
94			       );
95	void (*internal_lock_display)(
96				      Display* /* dpy */,
97				      Bool /* wskip */
98#if defined(XTHREADS_WARN) || defined(XTHREADS_FILE_LINE)
99				      , char* /* file */,
100				      int /* line */
101#endif
102				      );
103	/* used in XlibInt.c and locking.c */
104	void (*condition_signal)(
105				 xcondition_t /* cv */
106#if defined(XTHREADS_WARN) || defined(XTHREADS_FILE_LINE)
107				 , char* /* file */,
108				 int /* line */
109#endif
110				 );
111	void (*condition_broadcast)(
112				 xcondition_t /* cv */
113#if defined(XTHREADS_WARN) || defined(XTHREADS_FILE_LINE)
114				 , char* /* file */,
115				 int /* line */
116#endif
117				    );
118	/* used in XlibInt.c and XLockDis.c */
119	void (*lock_wait)(
120			  Display* /* dpy */
121			  );
122	void (*user_lock_display)(
123				  Display* /* dpy */
124				  );
125	void (*user_unlock_display)(
126				    Display* /* dpy */
127				    );
128	struct _XCVList *(*create_cvl)(
129				       Display * /* dpy */
130				       );
131};
132
133#define UnlockNextEventReader(d) if ((d)->lock) \
134    (*(d)->lock->pop_reader)((d),&(d)->lock->event_awaiters,&(d)->lock->event_awaiters_tail)
135
136#if defined(XTHREADS_WARN) || defined(XTHREADS_FILE_LINE)
137#define ConditionWait(d,c) if ((d)->lock) \
138	(*(d)->lock->condition_wait)(c, (d)->lock->mutex,__FILE__,__LINE__)
139#define ConditionSignal(d,c) if ((d)->lock) \
140	(*(d)->lock->condition_signal)(c,__FILE__,__LINE__)
141#define ConditionBroadcast(d,c) if ((d)->lock) \
142	(*(d)->lock->condition_broadcast)(c,__FILE__,__LINE__)
143#else
144#define ConditionWait(d,c) if ((d)->lock) \
145	(*(d)->lock->condition_wait)(c, (d)->lock->mutex)
146#define ConditionSignal(d,c) if ((d)->lock) \
147	(*(d)->lock->condition_signal)(c)
148#define ConditionBroadcast(d,c) if ((d)->lock) \
149	(*(d)->lock->condition_broadcast)(c)
150#endif
151
152typedef struct _LockInfoRec {
153	xmutex_t	lock;
154} LockInfoRec;
155
156/* A list of threads currently invoking error handlers on this display
157 * LockDisplay operates differently for these threads, avoiding
158 * generating any requests or reading any events as that can cause
159 * recursion into the error handling code, which will deadlock the
160 * thread.
161 */
162struct _XErrorThreadInfo
163{
164    struct _XErrorThreadInfo *next;
165    xthread_t error_thread;
166};
167
168/* XOpenDis.c */
169extern int (*_XInitDisplayLock_fn)(Display *dpy);
170extern void (*_XFreeDisplayLock_fn)(Display *dpy);
171
172#endif /* _X_locking_H_ */
173