a0d0788335c14f4e75fe656de9ba075c62197a6f
[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  * ---------------------------------------------------------------------------*/
11
12 #include "Rts.h"
13 #include "RtsGlobals.h"
14
15 static StgStablePtr typeableStore      = 0;
16 static StgStablePtr signalHandlerStore = 0;
17
18 #ifdef THREADED_RTS
19 Mutex globalStoreLock;
20 #endif
21
22 void
23 initGlobalStore(void)
24 {
25     typeableStore      = 0;
26     signalHandlerStore = 0;
27 #ifdef THREADED_RTS
28     initMutex(&globalStoreLock);
29 #endif
30 }
31
32 void
33 exitGlobalStore(void)
34 {
35 #ifdef THREADED_RTS
36     closeMutex(&globalStoreLock);
37 #endif
38     if(typeableStore!=0) {
39         freeStablePtr((StgStablePtr)typeableStore);
40         typeableStore=0;
41     }
42     if(signalHandlerStore!=0) {
43         freeStablePtr((StgStablePtr)signalHandlerStore);
44         signalHandlerStore=0;
45     }
46 }
47
48 StgStablePtr
49 getOrSetTypeableStore(StgStablePtr ptr)
50 {
51     StgStablePtr ret = typeableStore;
52     if(ret==0) {
53 #ifdef THREADED_RTS
54         ACQUIRE_LOCK(&globalStoreLock);
55         ret=typeableStore;
56         if(ret==0) {
57 #endif
58             typeableStore = ret = ptr;
59 #ifdef THREADED_RTS
60         }
61         RELEASE_LOCK(&globalStoreLock);
62 #endif
63     }
64     return ret;
65 }
66
67 StgStablePtr
68 getOrSetSignalHandlerStore(StgStablePtr ptr)
69 {
70     StgStablePtr ret = signalHandlerStore;
71     if(ret==0) {
72 #ifdef THREADED_RTS
73         ACQUIRE_LOCK(&globalStoreLock);
74         ret=signalHandlerStore;
75         if(ret==0) {
76 #endif
77             signalHandlerStore = ret = ptr;
78 #ifdef THREADED_RTS
79         }
80         RELEASE_LOCK(&globalStoreLock);
81 #endif
82     }
83     return ret;
84 }