Globals.c 2.97 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
 * 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
Moritz Angermann's avatar
Moritz Angermann committed
16
 *              plugins it dynamically loads.
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,
Moritz Angermann's avatar
Moritz Angermann committed
36 37 38 39 40
    LibHSghcPersistentLinkerState,
    LibHSghcInitLinkerDone,
    LibHSghcGlobalDynFlags,
    LibHSghcStaticOptions,
    LibHSghcStaticOptionsReady,
41 42
    MaxStoreKey
} StoreKey;
43 44 45 46 47

#ifdef THREADED_RTS
Mutex globalStoreLock;
#endif

48
static StgStablePtr store[MaxStoreKey];
49

50 51 52
void
initGlobalStore(void)
{
53
    uint32_t i;
54 55 56
    for (i=0; i < MaxStoreKey; i++) {
        store[i] = 0;
    }
57 58 59 60 61 62 63 64
#ifdef THREADED_RTS
    initMutex(&globalStoreLock);
#endif
}

void
exitGlobalStore(void)
{
65
    uint32_t i;
66 67 68
#ifdef THREADED_RTS
    closeMutex(&globalStoreLock);
#endif
69 70 71 72 73
    for (i=0; i < MaxStoreKey; i++) {
        if (store[i] != 0) {
            freeStablePtr(store[i]);
            store[i] = 0;
        }
74 75 76
    }
}

77
static StgStablePtr getOrSetKey(StoreKey key, StgStablePtr ptr)
78
{
79
    StgStablePtr ret = store[key];
80 81 82
    if(ret==0) {
#ifdef THREADED_RTS
        ACQUIRE_LOCK(&globalStoreLock);
83
        ret = store[key];
84 85
        if(ret==0) {
#endif
86
            store[key] = ret = ptr;
87 88 89 90 91 92
#ifdef THREADED_RTS
        }
        RELEASE_LOCK(&globalStoreLock);
#endif
    }
    return ret;
93 94
}

Moritz Angermann's avatar
Moritz Angermann committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
#define mkStoreAccessor(name) \
    StgStablePtr \
    getOrSet##name(StgStablePtr ptr) \
    { return getOrSetKey(name, ptr); }

mkStoreAccessor(GHCConcSignalSignalHandlerStore)
mkStoreAccessor(GHCConcWindowsPendingDelaysStore)
mkStoreAccessor(GHCConcWindowsIOManagerThreadStore)
mkStoreAccessor(GHCConcWindowsProddingStore)
mkStoreAccessor(SystemEventThreadEventManagerStore)
mkStoreAccessor(SystemEventThreadIOManagerThreadStore)
mkStoreAccessor(SystemTimerThreadEventManagerStore)
mkStoreAccessor(SystemTimerThreadIOManagerThreadStore)
mkStoreAccessor(LibHSghcFastStringTable)
mkStoreAccessor(LibHSghcPersistentLinkerState)
mkStoreAccessor(LibHSghcInitLinkerDone)
mkStoreAccessor(LibHSghcGlobalDynFlags)
mkStoreAccessor(LibHSghcStaticOptions)
mkStoreAccessor(LibHSghcStaticOptionsReady)