Merge branch 'master' of http://darcs.haskell.org/ghc
[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 GHCConcSignalSignalHandlerStore,
23 GHCConcWindowsPendingDelaysStore,
24 GHCConcWindowsIOManagerThreadStore,
25 GHCConcWindowsProddingStore,
26 SystemEventThreadEventManagerStore,
27 SystemEventThreadIOManagerThreadStore,
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 StgStablePtr
83 getOrSetGHCConcSignalSignalHandlerStore(StgStablePtr ptr)
84 {
85 return getOrSetKey(GHCConcSignalSignalHandlerStore,ptr);
86 }
87
88 StgStablePtr
89 getOrSetGHCConcWindowsPendingDelaysStore(StgStablePtr ptr)
90 {
91 return getOrSetKey(GHCConcWindowsPendingDelaysStore,ptr);
92 }
93
94 StgStablePtr
95 getOrSetGHCConcWindowsIOManagerThreadStore(StgStablePtr ptr)
96 {
97 return getOrSetKey(GHCConcWindowsIOManagerThreadStore,ptr);
98 }
99
100 StgStablePtr
101 getOrSetGHCConcWindowsProddingStore(StgStablePtr ptr)
102 {
103 return getOrSetKey(GHCConcWindowsProddingStore,ptr);
104 }
105
106 StgStablePtr
107 getOrSetSystemEventThreadEventManagerStore(StgStablePtr ptr)
108 {
109 return getOrSetKey(SystemEventThreadEventManagerStore,ptr);
110 }
111
112 StgStablePtr
113 getOrSetSystemEventThreadIOManagerThreadStore(StgStablePtr ptr)
114 {
115 return getOrSetKey(SystemEventThreadIOManagerThreadStore,ptr);
116 }