Merge branch 'master' of /Users/benl/devel/ghc/ghc-head
[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 * 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 GHCConcSignalSignalHandlerStore,
24 GHCConcWindowsPendingDelaysStore,
25 GHCConcWindowsIOManagerThreadStore,
26 GHCConcWindowsProddingStore,
27 SystemEventThreadEventManagerStore,
28 SystemEventThreadIOManagerThreadStore,
29 MaxStoreKey
30 } StoreKey;
31
32 #ifdef THREADED_RTS
33 Mutex globalStoreLock;
34 #endif
35
36 static StgStablePtr store[MaxStoreKey];
37
38 void
39 initGlobalStore(void)
40 {
41 nat i;
42 for (i=0; i < MaxStoreKey; i++) {
43 store[i] = 0;
44 }
45 #ifdef THREADED_RTS
46 initMutex(&globalStoreLock);
47 #endif
48 }
49
50 void
51 exitGlobalStore(void)
52 {
53 nat i;
54 #ifdef THREADED_RTS
55 closeMutex(&globalStoreLock);
56 #endif
57 for (i=0; i < MaxStoreKey; i++) {
58 if (store[i] != 0) {
59 freeStablePtr(store[i]);
60 store[i] = 0;
61 }
62 }
63 }
64
65 static StgStablePtr getOrSetKey(StoreKey key, StgStablePtr ptr)
66 {
67 StgStablePtr ret = store[key];
68 if(ret==0) {
69 #ifdef THREADED_RTS
70 ACQUIRE_LOCK(&globalStoreLock);
71 ret = store[key];
72 if(ret==0) {
73 #endif
74 store[key] = ret = ptr;
75 #ifdef THREADED_RTS
76 }
77 RELEASE_LOCK(&globalStoreLock);
78 #endif
79 }
80 return ret;
81 }
82
83
84 StgStablePtr
85 getOrSetTypeableStore(StgStablePtr ptr)
86 {
87 return getOrSetKey(TypeableStore,ptr);
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 }