Replace pending_replies generic queue with a hand-implemented typesafe version.
[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 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 enum workarounds {
36     WORKAROUND_NONE,
37     WORKAROUND_GLX_GET_FB_CONFIGS_BUG
38 };
39
40 #define XCB_PAD(i) (-(i) & 3)
41
42 /* xcb_list.c */
43
44 typedef struct _xcb_list _xcb_list;
45 typedef void (*XCBListFreeFunc)(void *);
46
47 _xcb_list *_xcb_list_new(void);
48 void _xcb_list_delete(_xcb_list *list, XCBListFreeFunc do_free);
49 int _xcb_list_insert(_xcb_list *list, void *data);
50 int _xcb_list_append(_xcb_list *list, void *data);
51 void *_xcb_list_peek_head(_xcb_list *list);
52 void *_xcb_list_remove_head(_xcb_list *list);
53 void *_xcb_list_remove(_xcb_list *list, int (*cmp)(const void *, const void *), const void *data);
54 void *_xcb_list_find(_xcb_list *list, int (*cmp)(const void *, const void *), const void *data);
55
56 typedef _xcb_list _xcb_queue;
57
58 _xcb_queue *_xcb_queue_new(void);
59 void _xcb_queue_delete(_xcb_queue *q, XCBListFreeFunc do_free);
60 int _xcb_queue_enqueue(_xcb_queue *q, void *data);
61 void *_xcb_queue_dequeue(_xcb_queue *q);
62 int _xcb_queue_is_empty(_xcb_queue *q);
63
64 typedef _xcb_list _xcb_map;
65
66 _xcb_map *_xcb_map_new(void);
67 void _xcb_map_delete(_xcb_map *q, XCBListFreeFunc do_free);
68 int _xcb_map_put(_xcb_map *q, unsigned int key, void *data);
69 void *_xcb_map_get(_xcb_map *q, unsigned int key);
70 void *_xcb_map_remove(_xcb_map *q, unsigned int key);
71
72
73 /* xcb_out.c */
74
75 typedef struct _xcb_out {
76     pthread_cond_t cond;
77     int writing;
78
79     char queue[4096];
80     int queue_len;
81     struct iovec *vec;
82     int vec_len;
83
84     unsigned int request;
85     unsigned int request_written;
86
87     pthread_mutex_t reqlenlock;
88     CARD32 maximum_request_length;
89 } _xcb_out;
90
91 int _xcb_out_init(_xcb_out *out);
92 void _xcb_out_destroy(_xcb_out *out);
93
94 int _xcb_out_write(XCBConnection *c);
95 int _xcb_out_write_block(XCBConnection *c, struct iovec *vector, size_t count);
96 int _xcb_out_flush(XCBConnection *c);
97
98
99 /* xcb_in.c */
100
101 typedef struct _xcb_in {
102     pthread_cond_t event_cond;
103     int reading;
104
105     char queue[4096];
106     int queue_len;
107
108     unsigned int request_read;
109     _xcb_queue *current_reply;
110
111     _xcb_map *replies;
112     _xcb_queue *events;
113     _xcb_list *readers;
114
115     struct pending_reply *pending_replies;
116     struct pending_reply **pending_replies_tail;
117 } _xcb_in;
118
119 int _xcb_in_init(_xcb_in *in);
120 void _xcb_in_destroy(_xcb_in *in);
121
122 int _xcb_in_expect_reply(XCBConnection *c, unsigned int request, enum workarounds workaround);
123
124 int _xcb_in_read(XCBConnection *c);
125 int _xcb_in_read_block(XCBConnection *c, void *buf, int nread);
126
127
128 /* xcb_xid.c */
129
130 typedef struct _xcb_xid {
131     pthread_mutex_t lock;
132     CARD32 last;
133     CARD32 base;
134     CARD32 max;
135     CARD32 inc;
136 } _xcb_xid;
137
138 int _xcb_xid_init(XCBConnection *c);
139 void _xcb_xid_destroy(XCBConnection *c);
140
141
142 /* xcb_ext.c */
143
144 typedef struct _xcb_ext {
145     pthread_mutex_t lock;
146     struct lazyreply *extensions;
147     int extensions_size;
148 } _xcb_ext;
149
150 int _xcb_ext_init(XCBConnection *c);
151 void _xcb_ext_destroy(XCBConnection *c);
152
153
154 /* xcb_conn.c */
155
156 struct XCBConnection {
157     /* constant data */
158     XCBConnSetupSuccessRep *setup;
159     int fd;
160
161     /* I/O data */
162     pthread_mutex_t iolock;
163     _xcb_in in;
164     _xcb_out out;
165
166     /* misc data */
167     _xcb_ext ext;
168     _xcb_xid xid;
169 };
170
171 int _xcb_conn_wait(XCBConnection *c, const int should_write, pthread_cond_t *cond);
172 #endif