2 This file is part of the NoBug debugging library.
5 2007, 2008, 2009, 2010, Christian Thaeter <ct@pipapo.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, contact Christian Thaeter <ct@pipapo.org>.
20 #define NOBUG_LIBNOBUG_C
24 pthread_mutex_t nobug_logging_mutex
= PTHREAD_MUTEX_INITIALIZER
;
30 pthread_key_t nobug_tls_key
;
31 static unsigned nobug_thread_cnt
= 0;
33 struct nobug_tls_data
*
34 nobug_thread_set (const char* name
)
36 struct nobug_tls_data
* tls
= pthread_getspecific (nobug_tls_key
);
39 tls
= malloc (sizeof *tls
);
41 tls
->thread_num
= ++nobug_thread_cnt
;
44 tls
->coverage_disable_cnt
= 0;
45 pthread_setspecific (nobug_tls_key
, tls
);
49 if (!llist_is_empty(&tls
->res_stack
))
51 nobug_log (&nobug_flag_NOBUG_ON
, LOG_EMERG
, "NOBUG",
52 (const struct nobug_context
){"-", 0, "nobug_thread_set"},
53 " changing thread_id while resources are in use");
56 free ((char*)tls
->thread_id
);
61 snprintf (buf
, 256, "%s_%d", name
, tls
->thread_num
);
62 tls
->thread_id
= strdup(buf
);
65 nobug_log (&nobug_flag_NOBUG_ON
, LOG_EMERG
, "NOBUG",
66 (const struct nobug_context
){"-", 0, "nobug_thread_set"},
67 " failed thread id allocation");
71 llist_init (&tls
->res_stack
);
77 struct nobug_tls_data
*
78 nobug_thread_get (void)
80 struct nobug_tls_data
* tls
= pthread_getspecific (nobug_tls_key
);
82 return nobug_thread_set ("thread");
89 nobug_thread_id_set (const char* name
)
91 return nobug_thread_set (name
) -> thread_id
;
96 nobug_thread_id_get (void)
98 return nobug_thread_get () -> thread_id
;
103 nobug_thread_data (void)
105 return &nobug_thread_get () -> data
;