Fold integer-simple.git into ghc.git (re #8545)
[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 #include "Trace.h"
17
18 #include <stdlib.h>
19 #include <string.h>
20
21 #if defined(DEBUG)
22
23 static HashTable * threadLabels = NULL;
24
25 void
26 initThreadLabelTable(void)
27 {
28 if (threadLabels == NULL) {
29 threadLabels = allocHashTable();
30 }
31 }
32
33 void
34 freeThreadLabelTable(void)
35 {
36 if (threadLabels != NULL) {
37 freeHashTable(threadLabels, stgFree);
38 threadLabels = NULL;
39 }
40 }
41
42 static void
43 updateThreadLabel(StgWord key, void *data)
44 {
45 removeThreadLabel(key);
46 insertHashTable(threadLabels,key,data);
47 }
48
49 void *
50 lookupThreadLabel(StgWord key)
51 {
52 return lookupHashTable(threadLabels,key);
53 }
54
55 void
56 removeThreadLabel(StgWord key)
57 {
58 void * old = NULL;
59 if ((old = lookupHashTable(threadLabels,key))) {
60 removeHashTable(threadLabels,key,old);
61 stgFree(old);
62 }
63 }
64
65 #endif /* DEBUG */
66
67 void
68 labelThread(Capability *cap STG_UNUSED,
69 StgTSO *tso STG_UNUSED,
70 char *label STG_UNUSED)
71 {
72 #if defined(DEBUG)
73 int len;
74 void *buf;
75
76 /* Caveat: Once set, you can only set the thread name to "" */
77 len = strlen(label)+1;
78 buf = stgMallocBytes(len * sizeof(char), "Schedule.c:labelThread()");
79 strncpy(buf,label,len);
80 /* Update will free the old memory for us */
81 updateThreadLabel(tso->id,buf);
82 #endif
83 traceThreadLabel(cap, tso, label);
84 }
85