s/Invisible/Inferred/g s/Visible/Required/g
[ghc.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 * How are multiple versions of a library loaded? Examples:
11 *
12 * base - a statically-linked ghci has its own copy, so might libraries it
13 * dynamically loads
14 *
15 * libHSghc - a statically-linked ghc has its own copy and so will Core
16 * plugins it dynamically loads (cf CoreMonad.reinitializeGlobals)
17 *
18 * ---------------------------------------------------------------------------*/
19
20 #include "PosixSource.h"
21 #include "Rts.h"
22
23 #include "Globals.h"
24 #include "Stable.h"
25
26 typedef enum {
27 GHCConcSignalSignalHandlerStore,
28 GHCConcWindowsPendingDelaysStore,
29 GHCConcWindowsIOManagerThreadStore,
30 GHCConcWindowsProddingStore,
31 SystemEventThreadEventManagerStore,
32 SystemEventThreadIOManagerThreadStore,
33 SystemTimerThreadEventManagerStore,
34 SystemTimerThreadIOManagerThreadStore,
35 LibHSghcFastStringTable,
36 MaxStoreKey
37 } StoreKey;
38
39 #ifdef THREADED_RTS
40 Mutex globalStoreLock;
41 #endif
42
43 static StgStablePtr store[MaxStoreKey];
44
45 void
46 initGlobalStore(void)
47 {
48 uint32_t i;
49 for (i=0; i < MaxStoreKey; i++) {
50 store[i] = 0;
51 }
52 #ifdef THREADED_RTS
53 initMutex(&globalStoreLock);
54 #endif
55 }
56
57 void
58 exitGlobalStore(void)
59 {
60 uint32_t i;
61 #ifdef THREADED_RTS
62 closeMutex(&globalStoreLock);
63 #endif
64 for (i=0; i < MaxStoreKey; i++) {
65 if (store[i] != 0) {
66 freeStablePtr(store[i]);
67 store[i] = 0;
68 }
69 }
70 }
71
72 static StgStablePtr getOrSetKey(StoreKey key, StgStablePtr ptr)
73 {
74 StgStablePtr ret = store[key];
75 if(ret==0) {
76 #ifdef THREADED_RTS
77 ACQUIRE_LOCK(&globalStoreLock);
78 ret = store[key];
79 if(ret==0) {
80 #endif
81 store[key] = ret = ptr;
82 #ifdef THREADED_RTS
83 }
84 RELEASE_LOCK(&globalStoreLock);
85 #endif
86 }
87 return ret;
88 }
89
90 StgStablePtr
91 getOrSetGHCConcSignalSignalHandlerStore(StgStablePtr ptr)
92 {
93 return getOrSetKey(GHCConcSignalSignalHandlerStore,ptr);
94 }
95
96 StgStablePtr
97 getOrSetGHCConcWindowsPendingDelaysStore(StgStablePtr ptr)
98 {
99 return getOrSetKey(GHCConcWindowsPendingDelaysStore,ptr);
100 }
101
102 StgStablePtr
103 getOrSetGHCConcWindowsIOManagerThreadStore(StgStablePtr ptr)
104 {
105 return getOrSetKey(GHCConcWindowsIOManagerThreadStore,ptr);
106 }
107
108 StgStablePtr
109 getOrSetGHCConcWindowsProddingStore(StgStablePtr ptr)
110 {
111 return getOrSetKey(GHCConcWindowsProddingStore,ptr);
112 }
113
114 StgStablePtr
115 getOrSetSystemEventThreadEventManagerStore(StgStablePtr ptr)
116 {
117 return getOrSetKey(SystemEventThreadEventManagerStore,ptr);
118 }
119
120 StgStablePtr
121 getOrSetSystemEventThreadIOManagerThreadStore(StgStablePtr ptr)
122 {
123 return getOrSetKey(SystemEventThreadIOManagerThreadStore,ptr);
124 }
125
126 StgStablePtr
127 getOrSetSystemTimerThreadEventManagerStore(StgStablePtr ptr)
128 {
129 return getOrSetKey(SystemTimerThreadEventManagerStore,ptr);
130 }
131
132 StgStablePtr
133 getOrSetSystemTimerThreadIOManagerThreadStore(StgStablePtr ptr)
134 {
135 return getOrSetKey(SystemTimerThreadIOManagerThreadStore,ptr);
136 }
137
138 StgStablePtr
139 getOrSetLibHSghcFastStringTable(StgStablePtr ptr)
140 {
141 return getOrSetKey(LibHSghcFastStringTable,ptr);
142 }