Release preparation, bump library version, makefile update for README
[nobug.git] / src / nobug_thread.c
blob3ff63c6c6710f2972b907114622b49329b0b7bcb
1 /*
2 This file is part of the NoBug debugging library.
4 Copyright (C)
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
21 #include "nobug.h"
22 #include "llist.h"
24 pthread_mutex_t nobug_logging_mutex = PTHREAD_MUTEX_INITIALIZER;
28 thread id handling
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);
37 if (!tls)
39 tls = malloc (sizeof *tls);
40 if (!tls) abort();
41 tls->thread_num = ++nobug_thread_cnt;
42 tls->thread_gen = 0;
43 tls->data = NULL;
44 tls->coverage_disable_cnt = 0;
45 pthread_setspecific (nobug_tls_key, tls);
47 else
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");
54 abort();
56 free ((char*)tls->thread_id);
57 ++tls->thread_gen;
60 char buf[256];
61 snprintf (buf, 256, "%s_%d", name, tls->thread_num);
62 tls->thread_id = strdup(buf);
63 if (!tls->thread_id)
65 nobug_log (&nobug_flag_NOBUG_ON, LOG_EMERG, "NOBUG",
66 (const struct nobug_context){"-", 0, "nobug_thread_set"},
67 " failed thread id allocation");
68 abort();
71 llist_init (&tls->res_stack);
73 return tls;
77 struct nobug_tls_data*
78 nobug_thread_get (void)
80 struct nobug_tls_data* tls = pthread_getspecific (nobug_tls_key);
81 if (!tls)
82 return nobug_thread_set ("thread");
84 return tls;
88 const char*
89 nobug_thread_id_set (const char* name)
91 return nobug_thread_set (name) -> thread_id;
95 const char*
96 nobug_thread_id_get (void)
98 return nobug_thread_get () -> thread_id;
102 void**
103 nobug_thread_data (void)
105 return &nobug_thread_get () -> data;