Don't abort() on locking assertions if LIBXCB_ALLOW_SLOPPY_LOCK is set.
[free-sw/xcb/libxcb] / src / xcb_xlib.c
1 /* Copyright (C) 2005 Bart Massey and Jamey Sharp.
2  *
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:
9  * 
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  * 
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.
19  * 
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.
24  */
25
26 #include "xcbxlib.h"
27 #include "xcbint.h"
28
29 #include <assert.h>
30
31 #ifdef HAVE_BACKTRACE
32 #include <execinfo.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #endif
36
37 static void xcb_xlib_printbt(void)
38 {
39 #ifdef HAVE_BACKTRACE
40         void *array[20];
41         int size;
42         char **strings;
43         int i;
44
45         size = backtrace(array, 20);
46         strings = backtrace_symbols(array, size);
47
48         fprintf(stderr, "Locking assertion failure.  Backtrace:\n");
49
50         for (i = 0; i < size; ++i)
51                 fprintf(stderr, "#%i %s\n", i, strings[i]);
52
53         free(strings);
54 #endif
55 }
56
57 #ifndef NDEBUG
58 #define xcb_assert(c,x) do { if (!(x)) { xcb_xlib_printbt(); if (!(c)->xlib.sloppy_lock) assert(x); } } while(0)
59 #else
60 #define xcb_assert(c,x)
61 #endif
62
63 unsigned int xcb_get_request_sent(xcb_connection_t *c)
64 {
65     if(c->has_error)
66         return 0;
67     return c->out.request;
68 }
69
70 void xcb_xlib_lock(xcb_connection_t *c)
71 {
72     _xcb_lock_io(c);
73     xcb_assert(c, !c->xlib.lock);
74     c->xlib.lock = 1;
75     c->xlib.thread = pthread_self();
76     _xcb_unlock_io(c);
77 }
78
79 void xcb_xlib_unlock(xcb_connection_t *c)
80 {
81     _xcb_lock_io(c);
82     xcb_assert(c, c->xlib.lock);
83     xcb_assert(c, pthread_equal(c->xlib.thread, pthread_self()));
84     c->xlib.lock = 0;
85     pthread_cond_broadcast(&c->xlib.cond);
86     _xcb_unlock_io(c);
87 }