Globals.c 3.31 KB
Newer Older
1 2 3 4 5 6 7 8 9
/* -----------------------------------------------------------------------------
 *
 * (c) The GHC Team, 1995-2009
 *
 * The RTS stores some "global" values on behalf of libraries, so that
 * some libraries can ensure that certain top-level things are shared
 * even when multiple versions of the library are loaded.  e.g. see
 * Data.Typeable and GHC.Conc.
 *
10 11 12 13 14 15 16
 * How are multiple versions of a library loaded? Examples:
 *
 *   base - a statically-linked ghci has its own copy, so might libraries it
 *          dynamically loads
 *
 *   libHSghc - a statically-linked ghc has its own copy and so will Core
 *              plugins it dynamically loads (cf CoreMonad.reinitializeGlobals)
17
 *
18 19
 * ---------------------------------------------------------------------------*/

Simon Marlow's avatar
Simon Marlow committed
20
#include "PosixSource.h"
21
#include "Rts.h"
Simon Marlow's avatar
Simon Marlow committed
22 23 24

#include "Globals.h"
#include "Stable.h"
25

26
typedef enum {
27 28 29 30 31 32
    GHCConcSignalSignalHandlerStore,
    GHCConcWindowsPendingDelaysStore,
    GHCConcWindowsIOManagerThreadStore,
    GHCConcWindowsProddingStore,
    SystemEventThreadEventManagerStore,
    SystemEventThreadIOManagerThreadStore,
33 34
    SystemTimerThreadEventManagerStore,
    SystemTimerThreadIOManagerThreadStore,
35
    LibHSghcFastStringTable,
36 37
    MaxStoreKey
} StoreKey;
38 39 40 41 42

#ifdef THREADED_RTS
Mutex globalStoreLock;
#endif

43
static StgStablePtr store[MaxStoreKey];
44

45 46 47
void
initGlobalStore(void)
{
48
    uint32_t i;
49 50 51
    for (i=0; i < MaxStoreKey; i++) {
        store[i] = 0;
    }
52 53 54 55 56 57 58 59
#ifdef THREADED_RTS
    initMutex(&globalStoreLock);
#endif
}

void
exitGlobalStore(void)
{
60
    uint32_t i;
61 62 63
#ifdef THREADED_RTS
    closeMutex(&globalStoreLock);
#endif
64 65 66 67 68
    for (i=0; i < MaxStoreKey; i++) {
        if (store[i] != 0) {
            freeStablePtr(store[i]);
            store[i] = 0;
        }
69 70 71
    }
}

72
static StgStablePtr getOrSetKey(StoreKey key, StgStablePtr ptr)
73
{
74
    StgStablePtr ret = store[key];
75 76 77
    if(ret==0) {
#ifdef THREADED_RTS
        ACQUIRE_LOCK(&globalStoreLock);
78
        ret = store[key];
79 80
        if(ret==0) {
#endif
81
            store[key] = ret = ptr;
82 83 84 85 86 87
#ifdef THREADED_RTS
        }
        RELEASE_LOCK(&globalStoreLock);
#endif
    }
    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 118 119 120 121 122 123
    return getOrSetKey(SystemEventThreadEventManagerStore,ptr);
}

StgStablePtr
getOrSetSystemEventThreadIOManagerThreadStore(StgStablePtr ptr)
{
    return getOrSetKey(SystemEventThreadIOManagerThreadStore,ptr);
124
}
125 126 127 128 129 130 131 132 133 134 135 136

StgStablePtr
getOrSetSystemTimerThreadEventManagerStore(StgStablePtr ptr)
{
    return getOrSetKey(SystemTimerThreadEventManagerStore,ptr);
}

StgStablePtr
getOrSetSystemTimerThreadIOManagerThreadStore(StgStablePtr ptr)
{
    return getOrSetKey(SystemTimerThreadIOManagerThreadStore,ptr);
}
137 138 139 140 141 142

StgStablePtr
getOrSetLibHSghcFastStringTable(StgStablePtr ptr)
{
    return getOrSetKey(LibHSghcFastStringTable,ptr);
}