Address #11471 by putting RuntimeRep in kinds.
[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 #if defined(THREADED_RTS)
24 static Mutex threadLabels_mutex;
25 #endif /* THREADED_RTS */
26
27 static HashTable * threadLabels = NULL;
28
29 void
30 initThreadLabelTable(void)
31 {
32 #if defined(THREADED_RTS)
33 initMutex(&threadLabels_mutex);
34 #endif /* THREADED_RTS */
35
36 if (threadLabels == NULL) {
37 threadLabels = allocHashTable();
38 }
39 }
40
41 void
42 freeThreadLabelTable(void)
43 {
44 ACQUIRE_LOCK(&threadLabels_mutex);
45
46 if (threadLabels != NULL) {
47 freeHashTable(threadLabels, stgFree);
48 threadLabels = NULL;
49 }
50
51 RELEASE_LOCK(&threadLabels_mutex);
52 }
53
54 static void
55 updateThreadLabel(StgWord key, void *data)
56 {
57 removeThreadLabel(key);
58
59 ACQUIRE_LOCK(&threadLabels_mutex);
60
61 insertHashTable(threadLabels,key,data);
62
63 RELEASE_LOCK(&threadLabels_mutex);
64 }
65
66 void *
67 lookupThreadLabel(StgWord key)
68 {
69 void * result;
70 ACQUIRE_LOCK(&threadLabels_mutex);
71
72 result = lookupHashTable(threadLabels,key);
73
74 RELEASE_LOCK(&threadLabels_mutex);
75
76 return result;
77 }
78
79 void
80 removeThreadLabel(StgWord key)
81 {
82 ACQUIRE_LOCK(&threadLabels_mutex);
83
84 void * old = NULL;
85 if ((old = lookupHashTable(threadLabels,key))) {
86 removeHashTable(threadLabels,key,old);
87 stgFree(old);
88 }
89
90 RELEASE_LOCK(&threadLabels_mutex);
91 }
92
93 #endif /* DEBUG */
94
95 void
96 labelThread(Capability *cap STG_UNUSED,
97 StgTSO *tso STG_UNUSED,
98 char *label STG_UNUSED)
99 {
100 #if defined(DEBUG)
101 int len;
102 void *buf;
103
104 /* Caveat: Once set, you can only set the thread name to "" */
105 len = strlen(label)+1;
106 buf = stgMallocBytes(len * sizeof(char), "Schedule.c:labelThread()");
107 strncpy(buf,label,len);
108 /* Update will free the old memory for us */
109 updateThreadLabel(tso->id,buf);
110 #endif
111 traceThreadLabel(cap, tso, label);
112 }