Added RTS hooks for the timer manager.
[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 SystemTimerThreadEventManagerStore,
29 SystemTimerThreadIOManagerThreadStore,
30 MaxStoreKey
31 } StoreKey;
32
33 #ifdef THREADED_RTS
34 Mutex globalStoreLock;
35 #endif
36
37 static StgStablePtr store[MaxStoreKey];
38
39 void
40 initGlobalStore(void)
41 {
42 nat i;
43 for (i=0; i < MaxStoreKey; i++) {
44 store[i] = 0;
45 }
46 #ifdef THREADED_RTS
47 initMutex(&globalStoreLock);
48 #endif
49 }
50
51 void
52 exitGlobalStore(void)
53 {
54 nat i;
55 #ifdef THREADED_RTS
56 closeMutex(&globalStoreLock);
57 #endif
58 for (i=0; i < MaxStoreKey; i++) {
59 if (store[i] != 0) {
60 freeStablePtr(store[i]);
61 store[i] = 0;
62 }
63 }
64 }
65
66 static StgStablePtr getOrSetKey(StoreKey key, StgStablePtr ptr)
67 {
68 StgStablePtr ret = store[key];
69 if(ret==0) {
70 #ifdef THREADED_RTS
71 ACQUIRE_LOCK(&globalStoreLock);
72 ret = store[key];
73 if(ret==0) {
74 #endif
75 store[key] = ret = ptr;
76 #ifdef THREADED_RTS
77 }
78 RELEASE_LOCK(&globalStoreLock);
79 #endif
80 }
81 return ret;
82 }
83
84 StgStablePtr
85 getOrSetGHCConcSignalSignalHandlerStore(StgStablePtr ptr)
86 {
87 return getOrSetKey(GHCConcSignalSignalHandlerStore,ptr);
88 }
89
90 StgStablePtr
91 getOrSetGHCConcWindowsPendingDelaysStore(StgStablePtr ptr)
92 {
93 return getOrSetKey(GHCConcWindowsPendingDelaysStore,ptr);
94 }
95
96 StgStablePtr
97 getOrSetGHCConcWindowsIOManagerThreadStore(StgStablePtr ptr)
98 {
99 return getOrSetKey(GHCConcWindowsIOManagerThreadStore,ptr);
100 }
101
102 StgStablePtr
103 getOrSetGHCConcWindowsProddingStore(StgStablePtr ptr)
104 {
105 return getOrSetKey(GHCConcWindowsProddingStore,ptr);
106 }
107
108 StgStablePtr
109 getOrSetSystemEventThreadEventManagerStore(StgStablePtr ptr)
110 {
111 return getOrSetKey(SystemEventThreadEventManagerStore,ptr);
112 }
113
114 StgStablePtr
115 getOrSetSystemEventThreadIOManagerThreadStore(StgStablePtr ptr)
116 {
117 return getOrSetKey(SystemEventThreadIOManagerThreadStore,ptr);
118 }
119
120 StgStablePtr
121 getOrSetSystemTimerThreadEventManagerStore(StgStablePtr ptr)
122 {
123 return getOrSetKey(SystemTimerThreadEventManagerStore,ptr);
124 }
125
126 StgStablePtr
127 getOrSetSystemTimerThreadIOManagerThreadStore(StgStablePtr ptr)
128 {
129 return getOrSetKey(SystemTimerThreadIOManagerThreadStore,ptr);
130 }