Remove the Unicode alternative for ".." (#3894)
[ghc-hetmet.git] / rts / Globals.c
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 1995-2009
4  *
5  * The RTS stores some "global" values on behalf of libraries, so that
6  * some libraries can ensure that certain top-level things are shared
7  * even when multiple versions of the library are loaded.  e.g. see
8  * Data.Typeable and GHC.Conc.
9  *
10  * If/when we switch to a dynamically-linked GHCi, this can all go
11  * away, because there would be just one copy of each library.
12  *
13  * ---------------------------------------------------------------------------*/
14
15 #include "PosixSource.h"
16 #include "Rts.h"
17
18 #include "Globals.h"
19 #include "Stable.h"
20
21 typedef enum {
22     TypeableStore,
23     GHCConcSignalHandlerStore,
24     GHCConcPendingEventsStore,
25     GHCConcPendingDelaysStore,
26     GHCConcIOManagerThreadStore,
27     GHCConcProddingStore,
28     MaxStoreKey
29 } StoreKey;
30
31 #ifdef THREADED_RTS
32 Mutex globalStoreLock;
33 #endif
34
35 static StgStablePtr store[MaxStoreKey];
36
37 void
38 initGlobalStore(void)
39 {
40     nat i;
41     for (i=0; i < MaxStoreKey; i++) {
42         store[i] = 0;
43     }
44 #ifdef THREADED_RTS
45     initMutex(&globalStoreLock);
46 #endif
47 }
48
49 void
50 exitGlobalStore(void)
51 {
52     nat i;
53 #ifdef THREADED_RTS
54     closeMutex(&globalStoreLock);
55 #endif
56     for (i=0; i < MaxStoreKey; i++) {
57         if (store[i] != 0) {
58             freeStablePtr(store[i]);
59             store[i] = 0;
60         }
61     }
62 }
63
64 static StgStablePtr getOrSetKey(StoreKey key, StgStablePtr ptr)
65 {
66     StgStablePtr ret = store[key];
67     if(ret==0) {
68 #ifdef THREADED_RTS
69         ACQUIRE_LOCK(&globalStoreLock);
70         ret = store[key];
71         if(ret==0) {
72 #endif
73             store[key] = ret = ptr;
74 #ifdef THREADED_RTS
75         }
76         RELEASE_LOCK(&globalStoreLock);
77 #endif
78     }
79     return ret;
80 }    
81
82
83 StgStablePtr
84 getOrSetTypeableStore(StgStablePtr ptr)
85 {
86     return getOrSetKey(TypeableStore,ptr);
87 }
88
89 StgStablePtr
90 getOrSetGHCConcSignalHandlerStore(StgStablePtr ptr)
91 {
92     return getOrSetKey(GHCConcSignalHandlerStore,ptr);
93 }
94
95 StgStablePtr
96 getOrSetGHCConcPendingEventsStore(StgStablePtr ptr)
97 {
98     return getOrSetKey(GHCConcPendingEventsStore,ptr);
99 }
100
101 StgStablePtr
102 getOrSetGHCConcPendingDelaysStore(StgStablePtr ptr)
103 {
104     return getOrSetKey(GHCConcPendingDelaysStore,ptr);
105 }
106
107 StgStablePtr
108 getOrSetGHCConcIOManagerThreadStore(StgStablePtr ptr)
109 {
110     return getOrSetKey(GHCConcIOManagerThreadStore,ptr);
111 }
112
113 StgStablePtr
114 getOrSetGHCConcProddingStore(StgStablePtr ptr)
115 {
116     return getOrSetKey(GHCConcProddingStore,ptr);
117 }