free the entries in the thread label table on exit
[ghc.git] / rts / ThreadLabels.c
1 /* -----------------------------------------------------------------------------
2 * ThreadLabels.c
3 *
4 * (c) The GHC Team 2002-2003
5 *
6 * Table of thread labels.
7 *
8 * ---------------------------------------------------------------------------*/
9
10 #include "PosixSource.h"
11 #include "Rts.h"
12
13 #include "ThreadLabels.h"
14 #include "RtsUtils.h"
15 #include "Hash.h"
16
17 #include <stdlib.h>
18 #include <string.h>
19
20 #if defined(DEBUG)
21 /* to the end */
22 static HashTable * threadLabels = NULL;
23
24 void
25 initThreadLabelTable(void)
26 {
27 if (threadLabels == NULL) {
28 threadLabels = allocHashTable();
29 }
30 }
31
32 void
33 freeThreadLabelTable(void)
34 {
35 if (threadLabels != NULL) {
36 freeHashTable(threadLabels, stgFree);
37 threadLabels = NULL;
38 }
39 }
40
41 static void
42 updateThreadLabel(StgWord key, void *data)
43 {
44 removeThreadLabel(key);
45 insertHashTable(threadLabels,key,data);
46 }
47
48 void *
49 lookupThreadLabel(StgWord key)
50 {
51 return lookupHashTable(threadLabels,key);
52 }
53
54 void
55 removeThreadLabel(StgWord key)
56 {
57 void * old = NULL;
58 if ((old = lookupHashTable(threadLabels,key))) {
59 removeHashTable(threadLabels,key,old);
60 stgFree(old);
61 }
62 }
63
64 void
65 labelThread(StgPtr tso, char *label)
66 {
67 int len;
68 void *buf;
69
70 /* Caveat: Once set, you can only set the thread name to "" */
71 len = strlen(label)+1;
72 buf = stgMallocBytes(len * sizeof(char), "Schedule.c:labelThread()");
73 strncpy(buf,label,len);
74 /* Update will free the old memory for us */
75 updateThreadLabel(((StgTSO *)tso)->id,buf);
76 }
77
78 #endif /* DEBUG */