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
23 pthread_mutex_t nobug_logging_mutex
= PTHREAD_MUTEX_INITIALIZER
;
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
)
37 snprintf (buf
, 256, "%s_%d", name
, ++nobug_thread_cnt
);
38 struct nobug_tls_data
* tls
= pthread_getspecific (nobug_tls_key
);
41 tls
= malloc (sizeof *tls
);
43 tls
->thread_id
= strdup(buf
);
45 if (!tls
->thread_id
) abort();
46 llist_init (&tls
->res_stack
);
47 pthread_setspecific (nobug_tls_key
, tls
);
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");
56 free ((char*)tls
->thread_id
);
57 tls
->thread_id
= strdup(buf
);
58 if (!tls
->thread_id
) abort();
59 llist_init (&tls
->res_stack
);
66 struct nobug_tls_data
*
67 nobug_thread_get (void)
69 struct nobug_tls_data
* tls
= pthread_getspecific (nobug_tls_key
);
71 return nobug_thread_set ("thread");
78 nobug_thread_id_set (const char* name
)
80 return nobug_thread_set (name
) -> thread_id
;
85 nobug_thread_id_get (void)
87 return nobug_thread_get () -> thread_id
;
92 nobug_thread_data (void)
94 return &nobug_thread_get () -> data
;