1 /* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp.
3 * Permission is hereby granted, free of charge, to any person obtaining a
4 * copy of this software and associated documentation files (the "Software"),
5 * to deal in the Software without restriction, including without limitation
6 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 * and/or sell copies of the Software, and to permit persons to whom the
8 * Software is furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
17 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 * Except as contained in this notice, the names of the authors or their
21 * institutions shall not be used in advertising or otherwise to promote the
22 * sale, use or other dealings in this Software without prior written
23 * authorization from the authors.
26 /* A cache for QueryExtension results. */
36 enum { LAZY_COOKIE, LAZY_FORCED } tag;
38 XCBQueryExtensionCookie cookie;
39 XCBQueryExtensionRep *reply;
43 static lazyreply *get_lazyreply(XCBConnection *c, XCBExtension *ext)
45 static pthread_mutex_t global_lock = PTHREAD_MUTEX_INITIALIZER;
46 static int next_global_id;
50 pthread_mutex_lock(&global_lock);
52 ext->global_id = ++next_global_id;
53 pthread_mutex_unlock(&global_lock);
55 data = _xcb_map_get(c->ext.extensions, ext->global_id);
58 /* cache miss: query the server */
59 data = malloc(sizeof(lazyreply));
62 data->tag = LAZY_COOKIE;
63 data->value.cookie = XCBQueryExtension(c, strlen(ext->name), ext->name);
64 _xcb_map_put(c->ext.extensions, ext->global_id, data);
69 static void free_lazyreply(void *p)
72 if(data->tag == LAZY_FORCED)
73 free(data->value.reply);
77 /* Public interface */
79 /* Do not free the returned XCBQueryExtensionRep - on return, it's aliased
81 const XCBQueryExtensionRep *XCBGetExtensionData(XCBConnection *c, XCBExtension *ext)
85 pthread_mutex_lock(&c->ext.lock);
86 data = get_lazyreply(c, ext);
87 if(data && data->tag == LAZY_COOKIE)
89 data->tag = LAZY_FORCED;
90 data->value.reply = XCBQueryExtensionReply(c, data->value.cookie, 0);
92 pthread_mutex_unlock(&c->ext.lock);
94 return data ? data->value.reply : 0;
97 void XCBPrefetchExtensionData(XCBConnection *c, XCBExtension *ext)
99 pthread_mutex_lock(&c->ext.lock);
100 get_lazyreply(c, ext);
101 pthread_mutex_unlock(&c->ext.lock);
104 /* Private interface */
106 int _xcb_ext_init(XCBConnection *c)
108 if(pthread_mutex_init(&c->ext.lock, 0))
111 c->ext.extensions = _xcb_map_new();
112 if(!c->ext.extensions)
118 void _xcb_ext_destroy(XCBConnection *c)
120 pthread_mutex_destroy(&c->ext.lock);
121 _xcb_map_delete(c->ext.extensions, free_lazyreply);