Thanks to testing by Jeroen van de Nieuwenhof, MacOS X is now supported
[nobug.git] / src / nobug_thread.c
blobcb658e5e078f4b45ea14c51d156b235e97990991
1 /*
2 This file is part of the NoBug debugging library.
4 Copyright (C) 2007, 2008, Christian Thaeter <chth@gmx.net>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, contact Christian Thaeter <ct@pipapo.org>.
19 #define NOBUG_LIBNOBUG_C
20 #include "nobug.h"
21 #include "llist.h"
23 pthread_mutex_t nobug_logging_mutex = PTHREAD_MUTEX_INITIALIZER;
27 thread id handling
29 pthread_key_t nobug_tls_key;
30 static unsigned nobug_thread_cnt = 0;
33 struct nobug_tls_data*
34 nobug_thread_set (const char* name)
36 char buf[256];
37 snprintf (buf, 256, "%s_%d", name, ++nobug_thread_cnt);
38 struct nobug_tls_data * tls = pthread_getspecific (nobug_tls_key);
39 if (!tls)
41 tls = malloc (sizeof *tls);
42 if (!tls) abort();
43 tls->thread_id = strdup(buf);
44 tls->data = NULL;
45 if (!tls->thread_id) abort();
46 llist_init (&tls->res_stack);
47 pthread_setspecific (nobug_tls_key, tls);
49 else
51 if (!llist_is_empty(&tls->res_stack))
53 nobug_log (&nobug_flag_NOBUG_ON, LOG_EMERG, "NOBUG: changing thread_id while resources are in use");
54 abort();
56 free ((char*)tls->thread_id);
57 tls->thread_id = strdup(buf);
58 if (!tls->thread_id) abort();
59 llist_init (&tls->res_stack);
62 return tls;
66 struct nobug_tls_data*
67 nobug_thread_get (void)
69 struct nobug_tls_data* tls = pthread_getspecific (nobug_tls_key);
70 if (!tls)
71 return nobug_thread_set ("thread");
73 return tls;
77 const char*
78 nobug_thread_id_set (const char* name)
80 return nobug_thread_set (name) -> thread_id;
84 const char*
85 nobug_thread_id_get (void)
87 return nobug_thread_get () -> thread_id;
91 void**
92 nobug_thread_data (void)
94 return &nobug_thread_get () -> data;