161b2299dSmrg/*
21ab64890Smrg
31ab64890SmrgCopyright 1992, 1998  The Open Group
41ab64890Smrg
51ab64890SmrgPermission to use, copy, modify, distribute, and sell this software and its
61ab64890Smrgdocumentation for any purpose is hereby granted without fee, provided that
71ab64890Smrgthe above copyright notice appear in all copies and that both that
81ab64890Smrgcopyright notice and this permission notice appear in supporting
91ab64890Smrgdocumentation.
101ab64890Smrg
111ab64890SmrgThe above copyright notice and this permission notice shall be included in
121ab64890Smrgall copies or substantial portions of the Software.
131ab64890Smrg
141ab64890SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
151ab64890SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
161ab64890SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
171ab64890SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
181ab64890SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
191ab64890SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
201ab64890Smrg
211ab64890SmrgExcept as contained in this notice, the name of The Open Group shall not be
221ab64890Smrgused in advertising or otherwise to promote the sale, use or other dealings
231ab64890Smrgin this Software without prior written authorization from The Open Group.
241ab64890Smrg
251ab64890Smrg*/
261ab64890Smrg
271ab64890Smrg/*
281ab64890Smrg * Author: Stephen Gildea, MIT X Consortium
291ab64890Smrg *
301ab64890Smrg * locking.h - data types for C Threads locking.
311ab64890Smrg * Used by XlibInt.c, locking.c, LockDis.c
321ab64890Smrg */
331ab64890Smrg
341ab64890Smrg#ifndef _X_locking_H_
351ab64890Smrg#define _X_locking_H_
361ab64890Smrg
370b3dccf2Smrg#ifndef xmalloc
381ab64890Smrg#define xmalloc(s) Xmalloc(s)
390b3dccf2Smrg#endif
400b3dccf2Smrg#ifndef xfree
411ab64890Smrg#define xfree(s) Xfree(s)
420b3dccf2Smrg#endif
43eb411b4bSmrg#include <X11/Xlib.h>
44eb411b4bSmrg#include <X11/Xlibint.h>
451ab64890Smrg#include <X11/Xthreads.h>
461ab64890Smrg
471ab64890Smrgstruct _XCVList {
481ab64890Smrg    xcondition_t cv;
491ab64890Smrg    xReply *buf;
501ab64890Smrg    struct _XCVList *next;
511ab64890Smrg};
521ab64890Smrg
531ab64890Smrgextern xthread_t (*_Xthread_self_fn)( /* in XlibInt.c */
541ab64890Smrg    void
551ab64890Smrg);
561ab64890Smrg
571ab64890Smrg/* Display->lock is a pointer to one of these */
581ab64890Smrg
591ab64890Smrgstruct _XLockInfo {
601ab64890Smrg	xmutex_t mutex;		/* mutex for critical sections */
611ab64890Smrg	int reply_bytes_left;	/* nbytes of the reply still to read */
621ab64890Smrg	Bool reply_was_read;	/* _XReadEvents read a reply for _XReply */
631ab64890Smrg	struct _XCVList *reply_awaiters; /* list of CVs for _XReply */
641ab64890Smrg	struct _XCVList **reply_awaiters_tail;
651ab64890Smrg	struct _XCVList *event_awaiters; /* list of CVs for _XReadEvents */
661ab64890Smrg	struct _XCVList **event_awaiters_tail;
671ab64890Smrg	Bool reply_first;	/* who may read, reply queue or event queue */
681ab64890Smrg	/* for XLockDisplay */
691ab64890Smrg	int locking_level;	/* how many times into XLockDisplay we are */
701ab64890Smrg	xthread_t locking_thread; /* thread that did XLockDisplay */
711ab64890Smrg	xcondition_t cv;	/* wait if another thread has XLockDisplay */
721ab64890Smrg	xthread_t reading_thread; /* cache */
731ab64890Smrg	xthread_t conni_thread;	/* thread in XProcessInternalConnection */
741ab64890Smrg	xcondition_t writers;	/* wait for writable */
751ab64890Smrg	int num_free_cvls;
761ab64890Smrg	struct _XCVList *free_cvls;
771ab64890Smrg	/* used only in XlibInt.c */
781ab64890Smrg	void (*pop_reader)(
791ab64890Smrg			   Display* /* dpy */,
801ab64890Smrg			   struct _XCVList** /* list */,
811ab64890Smrg			   struct _XCVList*** /* tail */
821ab64890Smrg			   );
831ab64890Smrg	struct _XCVList *(*push_reader)(
841ab64890Smrg					Display *	   /* dpy */,
851ab64890Smrg					struct _XCVList*** /* tail */
861ab64890Smrg					);
871ab64890Smrg	void (*condition_wait)(
881ab64890Smrg			       xcondition_t /* cv */,
891ab64890Smrg			       xmutex_t /* mutex */
901ab64890Smrg#if defined(XTHREADS_WARN) || defined(XTHREADS_FILE_LINE)
911ab64890Smrg			       , char* /* file */,
921ab64890Smrg			       int /* line */
931ab64890Smrg#endif
941ab64890Smrg			       );
951ab64890Smrg	void (*internal_lock_display)(
961ab64890Smrg				      Display* /* dpy */,
971ab64890Smrg				      Bool /* wskip */
981ab64890Smrg#if defined(XTHREADS_WARN) || defined(XTHREADS_FILE_LINE)
991ab64890Smrg				      , char* /* file */,
1001ab64890Smrg				      int /* line */
1011ab64890Smrg#endif
1021ab64890Smrg				      );
1031ab64890Smrg	/* used in XlibInt.c and locking.c */
1041ab64890Smrg	void (*condition_signal)(
1051ab64890Smrg				 xcondition_t /* cv */
1061ab64890Smrg#if defined(XTHREADS_WARN) || defined(XTHREADS_FILE_LINE)
1071ab64890Smrg				 , char* /* file */,
1081ab64890Smrg				 int /* line */
1091ab64890Smrg#endif
1101ab64890Smrg				 );
1111ab64890Smrg	void (*condition_broadcast)(
1121ab64890Smrg				 xcondition_t /* cv */
1131ab64890Smrg#if defined(XTHREADS_WARN) || defined(XTHREADS_FILE_LINE)
1141ab64890Smrg				 , char* /* file */,
1151ab64890Smrg				 int /* line */
1161ab64890Smrg#endif
1171ab64890Smrg				    );
1181ab64890Smrg	/* used in XlibInt.c and XLockDis.c */
1191ab64890Smrg	void (*lock_wait)(
1201ab64890Smrg			  Display* /* dpy */
1211ab64890Smrg			  );
1221ab64890Smrg	void (*user_lock_display)(
1231ab64890Smrg				  Display* /* dpy */
1241ab64890Smrg				  );
1251ab64890Smrg	void (*user_unlock_display)(
1261ab64890Smrg				    Display* /* dpy */
1271ab64890Smrg				    );
1281ab64890Smrg	struct _XCVList *(*create_cvl)(
1291ab64890Smrg				       Display * /* dpy */
1301ab64890Smrg				       );
1311ab64890Smrg};
1321ab64890Smrg
1331ab64890Smrg#define UnlockNextEventReader(d) if ((d)->lock) \
1341ab64890Smrg    (*(d)->lock->pop_reader)((d),&(d)->lock->event_awaiters,&(d)->lock->event_awaiters_tail)
1351ab64890Smrg
1361ab64890Smrg#if defined(XTHREADS_WARN) || defined(XTHREADS_FILE_LINE)
1371ab64890Smrg#define ConditionWait(d,c) if ((d)->lock) \
1381ab64890Smrg	(*(d)->lock->condition_wait)(c, (d)->lock->mutex,__FILE__,__LINE__)
1391ab64890Smrg#define ConditionSignal(d,c) if ((d)->lock) \
1401ab64890Smrg	(*(d)->lock->condition_signal)(c,__FILE__,__LINE__)
1411ab64890Smrg#define ConditionBroadcast(d,c) if ((d)->lock) \
1421ab64890Smrg	(*(d)->lock->condition_broadcast)(c,__FILE__,__LINE__)
1431ab64890Smrg#else
1441ab64890Smrg#define ConditionWait(d,c) if ((d)->lock) \
1451ab64890Smrg	(*(d)->lock->condition_wait)(c, (d)->lock->mutex)
1461ab64890Smrg#define ConditionSignal(d,c) if ((d)->lock) \
1471ab64890Smrg	(*(d)->lock->condition_signal)(c)
1481ab64890Smrg#define ConditionBroadcast(d,c) if ((d)->lock) \
1491ab64890Smrg	(*(d)->lock->condition_broadcast)(c)
1501ab64890Smrg#endif
1511ab64890Smrg
1521ab64890Smrgtypedef struct _LockInfoRec {
1531ab64890Smrg	xmutex_t	lock;
1541ab64890Smrg} LockInfoRec;
1551ab64890Smrg
156d4a3aaf4Smrg/* A list of threads currently invoking error handlers on this display
157d4a3aaf4Smrg * LockDisplay operates differently for these threads, avoiding
158d4a3aaf4Smrg * generating any requests or reading any events as that can cause
159d4a3aaf4Smrg * recursion into the error handling code, which will deadlock the
160d4a3aaf4Smrg * thread.
161d4a3aaf4Smrg */
162d4a3aaf4Smrgstruct _XErrorThreadInfo
163d4a3aaf4Smrg{
164d4a3aaf4Smrg    struct _XErrorThreadInfo *next;
165d4a3aaf4Smrg    xthread_t error_thread;
166d4a3aaf4Smrg};
167d4a3aaf4Smrg
1681ab64890Smrg/* XOpenDis.c */
1691ab64890Smrgextern int (*_XInitDisplayLock_fn)(Display *dpy);
1701ab64890Smrgextern void (*_XFreeDisplayLock_fn)(Display *dpy);
1711ab64890Smrg
1721ab64890Smrg#endif /* _X_locking_H_ */
173