xcb_request_check: Hold the I/O lock while deciding to sync.
[free-sw/xcb/libxcb] / src / xcbint.h
1 /*
2  * Copyright (C) 2001-2004 Bart Massey and Jamey Sharp.
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  * 
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  * 
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  * 
22  * Except as contained in this notice, the names of the authors or their
23  * institutions shall not be used in advertising or otherwise to promote the
24  * sale, use or other dealings in this Software without prior written
25  * authorization from the authors.
26  */
27
28 #ifndef __XCBINT_H
29 #define __XCBINT_H
30
31 #include "bigreq.h"
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 #ifdef GCC_HAS_VISIBILITY
38 #pragma GCC visibility push(hidden)
39 #endif
40
41 enum workarounds {
42     WORKAROUND_NONE,
43     WORKAROUND_GLX_GET_FB_CONFIGS_BUG,
44     WORKAROUND_EXTERNAL_SOCKET_OWNER
45 };
46
47 enum lazy_reply_tag
48 {
49     LAZY_NONE = 0,
50     LAZY_COOKIE,
51     LAZY_FORCED
52 };
53
54 #define XCB_PAD(i) (-(i) & 3)
55
56 #define XCB_SEQUENCE_COMPARE(a,op,b)    ((int64_t) ((a) - (b)) op 0)
57 #define XCB_SEQUENCE_COMPARE_32(a,op,b) (((int) (a) - (int) (b)) op 0)
58
59 #ifndef offsetof
60 #define offsetof(type,member) ((size_t) &((type *)0)->member)
61 #endif
62
63 #ifndef MIN
64 #define MIN(x,y) ((x) < (y) ? (x) : (y))
65 #endif
66
67 #define container_of(pointer,type,member) ((type *)(((char *)(pointer)) - offsetof(type, member)))
68
69 /* xcb_list.c */
70
71 typedef void (*xcb_list_free_func_t)(void *);
72
73 typedef struct _xcb_map _xcb_map;
74
75 _xcb_map *_xcb_map_new(void);
76 void _xcb_map_delete(_xcb_map *q, xcb_list_free_func_t do_free);
77 int _xcb_map_put(_xcb_map *q, unsigned int key, void *data);
78 void *_xcb_map_remove(_xcb_map *q, unsigned int key);
79
80
81 /* xcb_out.c */
82
83 typedef struct _xcb_out {
84     pthread_cond_t cond;
85     int writing;
86
87     pthread_cond_t socket_cond;
88     void (*return_socket)(void *closure);
89     void *socket_closure;
90     int socket_moving;
91
92     char queue[XCB_QUEUE_BUFFER_SIZE];
93     int queue_len;
94
95     uint64_t request;
96     uint64_t request_written;
97
98     pthread_mutex_t reqlenlock;
99     enum lazy_reply_tag maximum_request_length_tag;
100     union {
101         xcb_big_requests_enable_cookie_t cookie;
102         uint32_t value;
103     } maximum_request_length;
104 } _xcb_out;
105
106 int _xcb_out_init(_xcb_out *out);
107 void _xcb_out_destroy(_xcb_out *out);
108
109 int _xcb_out_send(xcb_connection_t *c, struct iovec *vector, int count);
110 void _xcb_out_send_sync(xcb_connection_t *c);
111 int _xcb_out_flush_to(xcb_connection_t *c, uint64_t request);
112
113
114 /* xcb_in.c */
115
116 typedef struct _xcb_in {
117     pthread_cond_t event_cond;
118     int reading;
119
120     char queue[4096];
121     int queue_len;
122
123     uint64_t request_expected;
124     uint64_t request_read;
125     uint64_t request_completed;
126     struct reply_list *current_reply;
127     struct reply_list **current_reply_tail;
128
129     _xcb_map *replies;
130     struct event_list *events;
131     struct event_list **events_tail;
132     struct reader_list *readers;
133
134     struct pending_reply *pending_replies;
135     struct pending_reply **pending_replies_tail;
136 } _xcb_in;
137
138 int _xcb_in_init(_xcb_in *in);
139 void _xcb_in_destroy(_xcb_in *in);
140
141 void _xcb_in_wake_up_next_reader(xcb_connection_t *c);
142
143 int _xcb_in_expect_reply(xcb_connection_t *c, uint64_t request, enum workarounds workaround, int flags);
144 void _xcb_in_replies_done(xcb_connection_t *c);
145
146 int _xcb_in_read(xcb_connection_t *c);
147 int _xcb_in_read_block(xcb_connection_t *c, void *buf, int nread);
148
149
150 /* xcb_xid.c */
151
152 typedef struct _xcb_xid {
153     pthread_mutex_t lock;
154     uint32_t last;
155     uint32_t base;
156     uint32_t max;
157     uint32_t inc;
158 } _xcb_xid;
159
160 int _xcb_xid_init(xcb_connection_t *c);
161 void _xcb_xid_destroy(xcb_connection_t *c);
162
163
164 /* xcb_ext.c */
165
166 typedef struct _xcb_ext {
167     pthread_mutex_t lock;
168     struct lazyreply *extensions;
169     int extensions_size;
170 } _xcb_ext;
171
172 int _xcb_ext_init(xcb_connection_t *c);
173 void _xcb_ext_destroy(xcb_connection_t *c);
174
175
176 /* xcb_conn.c */
177
178 extern const int error_connection;
179
180 struct xcb_connection_t {
181     int has_error;
182
183     /* constant data */
184     xcb_setup_t *setup;
185     int fd;
186
187     /* I/O data */
188     pthread_mutex_t iolock;
189     _xcb_in in;
190     _xcb_out out;
191
192     /* misc data */
193     _xcb_ext ext;
194     _xcb_xid xid;
195 };
196
197 void _xcb_conn_shutdown(xcb_connection_t *c);
198 int _xcb_conn_wait(xcb_connection_t *c, pthread_cond_t *cond, struct iovec **vector, int *count);
199
200
201 /* xcb_auth.c */
202
203 int _xcb_get_auth_info(int fd, xcb_auth_info_t *info, int display);
204
205 #ifdef GCC_HAS_VISIBILITY
206 #pragma GCC visibility pop
207 #endif
208
209 #endif