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