locking.h revision d4a3aaf4
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#define xmalloc(s) Xmalloc(s)
38#define xfree(s) Xfree(s)
39#include <X11/Xlib.h>
40#include <X11/Xlibint.h>
41#include <X11/Xthreads.h>
42
43struct _XCVList {
44    xcondition_t cv;
45    xReply *buf;
46    struct _XCVList *next;
47};
48
49extern xthread_t (*_Xthread_self_fn)( /* in XlibInt.c */
50    void
51);
52
53/* Display->lock is a pointer to one of these */
54
55struct _XLockInfo {
56	xmutex_t mutex;		/* mutex for critical sections */
57	int reply_bytes_left;	/* nbytes of the reply still to read */
58	Bool reply_was_read;	/* _XReadEvents read a reply for _XReply */
59	struct _XCVList *reply_awaiters; /* list of CVs for _XReply */
60	struct _XCVList **reply_awaiters_tail;
61	struct _XCVList *event_awaiters; /* list of CVs for _XReadEvents */
62	struct _XCVList **event_awaiters_tail;
63	Bool reply_first;	/* who may read, reply queue or event queue */
64	/* for XLockDisplay */
65	int locking_level;	/* how many times into XLockDisplay we are */
66	xthread_t locking_thread; /* thread that did XLockDisplay */
67	xcondition_t cv;	/* wait if another thread has XLockDisplay */
68	xthread_t reading_thread; /* cache */
69	xthread_t conni_thread;	/* thread in XProcessInternalConnection */
70	xcondition_t writers;	/* wait for writable */
71	int num_free_cvls;
72	struct _XCVList *free_cvls;
73	/* used only in XlibInt.c */
74	void (*pop_reader)(
75			   Display* /* dpy */,
76			   struct _XCVList** /* list */,
77			   struct _XCVList*** /* tail */
78			   );
79	struct _XCVList *(*push_reader)(
80					Display *	   /* dpy */,
81					struct _XCVList*** /* tail */
82					);
83	void (*condition_wait)(
84			       xcondition_t /* cv */,
85			       xmutex_t /* mutex */
86#if defined(XTHREADS_WARN) || defined(XTHREADS_FILE_LINE)
87			       , char* /* file */,
88			       int /* line */
89#endif
90			       );
91	void (*internal_lock_display)(
92				      Display* /* dpy */,
93				      Bool /* wskip */
94#if defined(XTHREADS_WARN) || defined(XTHREADS_FILE_LINE)
95				      , char* /* file */,
96				      int /* line */
97#endif
98				      );
99	/* used in XlibInt.c and locking.c */
100	void (*condition_signal)(
101				 xcondition_t /* cv */
102#if defined(XTHREADS_WARN) || defined(XTHREADS_FILE_LINE)
103				 , char* /* file */,
104				 int /* line */
105#endif
106				 );
107	void (*condition_broadcast)(
108				 xcondition_t /* cv */
109#if defined(XTHREADS_WARN) || defined(XTHREADS_FILE_LINE)
110				 , char* /* file */,
111				 int /* line */
112#endif
113				    );
114	/* used in XlibInt.c and XLockDis.c */
115	void (*lock_wait)(
116			  Display* /* dpy */
117			  );
118	void (*user_lock_display)(
119				  Display* /* dpy */
120				  );
121	void (*user_unlock_display)(
122				    Display* /* dpy */
123				    );
124	struct _XCVList *(*create_cvl)(
125				       Display * /* dpy */
126				       );
127};
128
129#define UnlockNextEventReader(d) if ((d)->lock) \
130    (*(d)->lock->pop_reader)((d),&(d)->lock->event_awaiters,&(d)->lock->event_awaiters_tail)
131
132#if defined(XTHREADS_WARN) || defined(XTHREADS_FILE_LINE)
133#define ConditionWait(d,c) if ((d)->lock) \
134	(*(d)->lock->condition_wait)(c, (d)->lock->mutex,__FILE__,__LINE__)
135#define ConditionSignal(d,c) if ((d)->lock) \
136	(*(d)->lock->condition_signal)(c,__FILE__,__LINE__)
137#define ConditionBroadcast(d,c) if ((d)->lock) \
138	(*(d)->lock->condition_broadcast)(c,__FILE__,__LINE__)
139#else
140#define ConditionWait(d,c) if ((d)->lock) \
141	(*(d)->lock->condition_wait)(c, (d)->lock->mutex)
142#define ConditionSignal(d,c) if ((d)->lock) \
143	(*(d)->lock->condition_signal)(c)
144#define ConditionBroadcast(d,c) if ((d)->lock) \
145	(*(d)->lock->condition_broadcast)(c)
146#endif
147
148typedef struct _LockInfoRec {
149	xmutex_t	lock;
150} LockInfoRec;
151
152/* A list of threads currently invoking error handlers on this display
153 * LockDisplay operates differently for these threads, avoiding
154 * generating any requests or reading any events as that can cause
155 * recursion into the error handling code, which will deadlock the
156 * thread.
157 */
158struct _XErrorThreadInfo
159{
160    struct _XErrorThreadInfo *next;
161    xthread_t error_thread;
162};
163
164/* XOpenDis.c */
165extern int (*_XInitDisplayLock_fn)(Display *dpy);
166extern void (*_XFreeDisplayLock_fn)(Display *dpy);
167
168#endif /* _X_locking_H_ */
169