1/*
2 * Copyright (C) 2008 Apple, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 *
22 * Except as contained in this notice, the name(s) of the above copyright
23 * holders shall not be used in advertising or otherwise to promote the sale,
24 * use or other dealings in this Software without prior written authorization.
25 */
26
27#ifdef HAVE_DIX_CONFIG_H
28#include <dix-config.h>
29#endif
30
31#include "threadSafety.h"
32#include "os.h"
33
34pthread_t APPKIT_THREAD_ID;
35pthread_t SERVER_THREAD_ID;
36
37#include <AvailabilityMacros.h>
38
39#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
40#include <execinfo.h>
41
42void spewCallStack(void) {
43    void* callstack[128];
44    int i, frames = backtrace(callstack, 128);
45    char** strs = backtrace_symbols(callstack, frames);
46
47    for (i = 0; i < frames; ++i) {
48        ErrorF("%s\n", strs[i]);
49    }
50
51    free(strs);
52}
53#else
54void spewCallStack(void) {
55	return;
56}
57#endif
58
59void _threadSafetyAssert(pthread_t tid, const char *file, const char *fun, int line) {
60    if(pthread_equal(pthread_self(), tid))
61        return;
62
63    /* NOOOO! */
64    ErrorF("Thread Assertion Failed: self=%s, expected=%s\n%s:%s:%d\n",
65           threadSafetyID(pthread_self()), threadSafetyID(tid),
66           file, fun, line);
67    spewCallStack();
68}
69
70const char *threadSafetyID(pthread_t tid) {
71    if(pthread_equal(tid, APPKIT_THREAD_ID)) {
72        return "Appkit Thread";
73    } else if(pthread_equal(tid, SERVER_THREAD_ID)) {
74        return "Xserver Thread";
75    } else {
76        return "Unknown Thread";
77    }
78}
79