Tizen 2.1 base
[framework/uifw/xorg/lib/libx11.git] / src / locking.h
1 /*
2
3 Copyright 1992, 1998  The Open Group
4
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
9 documentation.
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21 Except as contained in this notice, the name of The Open Group shall not be
22 used in advertising or otherwise to promote the sale, use or other dealings
23 in 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/Xthreads.h>
40
41 struct _XCVList {
42     xcondition_t cv;
43     xReply *buf;
44     struct _XCVList *next;
45 };
46
47 extern xthread_t (*_Xthread_self_fn)( /* in XlibInt.c */
48     void
49 );
50
51 /* Display->lock is a pointer to one of these */
52
53 struct _XLockInfo {
54         xmutex_t mutex;         /* mutex for critical sections */
55         int reply_bytes_left;   /* nbytes of the reply still to read */
56         Bool reply_was_read;    /* _XReadEvents read a reply for _XReply */
57         struct _XCVList *reply_awaiters; /* list of CVs for _XReply */
58         struct _XCVList **reply_awaiters_tail;
59         struct _XCVList *event_awaiters; /* list of CVs for _XReadEvents */
60         struct _XCVList **event_awaiters_tail;
61         Bool reply_first;       /* who may read, reply queue or event queue */
62         /* for XLockDisplay */
63         int locking_level;      /* how many times into XLockDisplay we are */
64         xthread_t locking_thread; /* thread that did XLockDisplay */
65         xcondition_t cv;        /* wait if another thread has XLockDisplay */
66         xthread_t reading_thread; /* cache */
67         xthread_t conni_thread; /* thread in XProcessInternalConnection */
68         xcondition_t writers;   /* wait for writable */
69         int num_free_cvls;
70         struct _XCVList *free_cvls;
71         /* used only in XlibInt.c */
72         void (*pop_reader)(
73                            Display* /* dpy */,
74                            struct _XCVList** /* list */,
75                            struct _XCVList*** /* tail */
76                            );
77         struct _XCVList *(*push_reader)(
78                                         Display *          /* dpy */,
79                                         struct _XCVList*** /* tail */
80                                         );
81         void (*condition_wait)(
82                                xcondition_t /* cv */,
83                                xmutex_t /* mutex */
84 #if defined(XTHREADS_WARN) || defined(XTHREADS_FILE_LINE)
85                                , char* /* file */,
86                                int /* line */
87 #endif
88                                );
89         void (*internal_lock_display)(
90                                       Display* /* dpy */,
91                                       Bool /* wskip */
92 #if defined(XTHREADS_WARN) || defined(XTHREADS_FILE_LINE)
93                                       , char* /* file */,
94                                       int /* line */
95 #endif
96                                       );
97         /* used in XlibInt.c and locking.c */
98         void (*condition_signal)(
99                                  xcondition_t /* cv */
100 #if defined(XTHREADS_WARN) || defined(XTHREADS_FILE_LINE)
101                                  , char* /* file */,
102                                  int /* line */
103 #endif
104                                  );
105         void (*condition_broadcast)(
106                                  xcondition_t /* cv */
107 #if defined(XTHREADS_WARN) || defined(XTHREADS_FILE_LINE)
108                                  , char* /* file */,
109                                  int /* line */
110 #endif
111                                     );
112         /* used in XlibInt.c and XLockDis.c */
113         void (*lock_wait)(
114                           Display* /* dpy */
115                           );
116         void (*user_lock_display)(
117                                   Display* /* dpy */
118                                   );
119         void (*user_unlock_display)(
120                                     Display* /* dpy */
121                                     );
122         struct _XCVList *(*create_cvl)(
123                                        Display * /* dpy */
124                                        );
125 };
126
127 #define UnlockNextEventReader(d) if ((d)->lock) \
128     (*(d)->lock->pop_reader)((d),&(d)->lock->event_awaiters,&(d)->lock->event_awaiters_tail)
129
130 #if defined(XTHREADS_WARN) || defined(XTHREADS_FILE_LINE)
131 #define ConditionWait(d,c) if ((d)->lock) \
132         (*(d)->lock->condition_wait)(c, (d)->lock->mutex,__FILE__,__LINE__)
133 #define ConditionSignal(d,c) if ((d)->lock) \
134         (*(d)->lock->condition_signal)(c,__FILE__,__LINE__)
135 #define ConditionBroadcast(d,c) if ((d)->lock) \
136         (*(d)->lock->condition_broadcast)(c,__FILE__,__LINE__)
137 #else
138 #define ConditionWait(d,c) if ((d)->lock) \
139         (*(d)->lock->condition_wait)(c, (d)->lock->mutex)
140 #define ConditionSignal(d,c) if ((d)->lock) \
141         (*(d)->lock->condition_signal)(c)
142 #define ConditionBroadcast(d,c) if ((d)->lock) \
143         (*(d)->lock->condition_broadcast)(c)
144 #endif
145
146 typedef struct _LockInfoRec {
147         xmutex_t        lock;
148 } LockInfoRec;
149
150 /* XOpenDis.c */
151 extern int (*_XInitDisplayLock_fn)(Display *dpy);
152 extern void (*_XFreeDisplayLock_fn)(Display *dpy);
153
154 #endif /* _X_locking_H_ */