Massive patch for the first months work adding System FC to GHC #35
[ghc-hetmet.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 #include "ThreadLabels.h"
13 #include "RtsUtils.h"
14 #include "Hash.h"
15
16 #include <stdlib.h>
17 #include <string.h>
18
19 #if defined(DEBUG)
20 /* to the end */
21 static HashTable * threadLabels = NULL;
22
23 void
24 initThreadLabelTable(void)
25 {
26   if (threadLabels == NULL) {
27     threadLabels = allocHashTable();
28   }
29 }
30
31 void
32 updateThreadLabel(StgWord key, void *data)
33 {
34   removeThreadLabel(key);
35   insertHashTable(threadLabels,key,data);
36 }
37
38 void *
39 lookupThreadLabel(StgWord key)
40 {
41   return lookupHashTable(threadLabels,key);
42 }
43
44 void
45 removeThreadLabel(StgWord key)
46 {
47   void * old = NULL;
48   if ((old = lookupHashTable(threadLabels,key))) {
49     removeHashTable(threadLabels,key,old);
50     stgFree(old);
51   }  
52 }
53
54 void
55 labelThread(StgPtr tso, char *label)
56 {
57   int len;
58   void *buf;
59
60   /* Caveat: Once set, you can only set the thread name to "" */
61   len = strlen(label)+1;
62   buf = stgMallocBytes(len * sizeof(char), "Schedule.c:labelThread()");
63   strncpy(buf,label,len);
64   /* Update will free the old memory for us */
65   updateThreadLabel(((StgTSO *)tso)->id,buf);
66 }
67
68 #endif /* DEBUG */