pipadoc uses some bashisms
[nobug.git] / src / nobug_thread.c
blobd8ba2a37214fa7cf74f09f4bc6ecad642a8415cf
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 //TODO assert that the thread holds no resources when the id gets reset
52 free ((char*)tls->thread_id);
53 tls->thread_id = strdup(buf);
54 if (!tls->thread_id) abort();
55 llist_init (&tls->res_stack);
58 return tls;
62 struct nobug_tls_data*
63 nobug_thread_get (void)
65 struct nobug_tls_data* tls = pthread_getspecific (nobug_tls_key);
66 if (!tls)
67 return nobug_thread_set ("thread");
69 return tls;
73 const char*
74 nobug_thread_id_set (const char* name)
76 return nobug_thread_set (name) -> thread_id;
80 const char*
81 nobug_thread_id_get (void)
83 return nobug_thread_get () -> thread_id;
87 void**
88 nobug_thread_data (void)
90 return &nobug_thread_get () -> data;