1 /* Copyright (C) 2001-2008 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.
39 /* Public interface */
41 uint32_t xcb_generate_id(xcb_connection_t *c)
46 pthread_mutex_lock(&c->xid.lock);
47 if(c->xid.last >= c->xid.max - c->xid.inc + 1)
49 xcb_xc_misc_get_xid_range_reply_t *range;
50 assert(c->xid.last == c->xid.max);
51 if (c->xid.last == 0) {
52 /* finish setting up initial range */
53 c->xid.max = c->setup->resource_id_mask;
55 /* check for extension */
56 const xcb_query_extension_reply_t *xc_misc_reply =
57 xcb_get_extension_data(c, &xcb_xc_misc_id);
59 pthread_mutex_unlock(&c->xid.lock);
63 range = xcb_xc_misc_get_xid_range_reply(c,
64 xcb_xc_misc_get_xid_range(c), 0);
65 /* XXX The latter disjunct is what the server returns
66 when it is out of XIDs. Sweet. */
67 if(!range || (range->start_id == 0 && range->count == 1))
69 pthread_mutex_unlock(&c->xid.lock);
72 assert(range->count > 0 && range->start_id > 0);
73 c->xid.last = range->start_id;
74 c->xid.max = range->start_id + (range->count - 1) * c->xid.inc;
78 c->xid.last += c->xid.inc;
80 ret = c->xid.last | c->xid.base;
81 pthread_mutex_unlock(&c->xid.lock);
85 /* Private interface */
87 int _xcb_xid_init(xcb_connection_t *c)
89 if(pthread_mutex_init(&c->xid.lock, 0))
93 c->xid.base = c->setup->resource_id_base;
94 c->xid.inc = c->setup->resource_id_mask & -(c->setup->resource_id_mask);
98 void _xcb_xid_destroy(xcb_connection_t *c)
100 pthread_mutex_destroy(&c->xid.lock);