summaryrefslogtreecommitdiff
path: root/rts/Globals.c
blob: b7ed8147525e3d089e410932a5c0681ad506c862 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* -----------------------------------------------------------------------------
 *
 * (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.
 *
 * 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.
 *
 * ---------------------------------------------------------------------------*/

#include "rts/PosixSource.h"
#include "Rts.h"

#include "Globals.h"
#include "StablePtr.h"

typedef enum {
    GHCConcSignalSignalHandlerStore,
    GHCConcWindowsPendingDelaysStore,
    GHCConcWindowsIOManagerThreadStore,
    GHCConcWindowsProddingStore,
    SystemEventThreadEventManagerStore,
    SystemEventThreadIOManagerThreadStore,
    SystemTimerThreadEventManagerStore,
    SystemTimerThreadIOManagerThreadStore,
    LibHSghcFastStringTable,
    LibHSghcGlobalHasPprDebug,
    LibHSghcGlobalHasNoDebugOutput,
    LibHSghcGlobalHasNoStateHack,
    MaxStoreKey
} StoreKey;

#if defined(THREADED_RTS)
Mutex globalStoreLock;
#endif

static StgStablePtr store[MaxStoreKey];

void
initGlobalStore(void)
{
    uint32_t i;
    for (i=0; i < MaxStoreKey; i++) {
        store[i] = 0;
    }
#if defined(THREADED_RTS)
    initMutex(&globalStoreLock);
#endif
}

void
exitGlobalStore(void)
{
    uint32_t i;
#if defined(THREADED_RTS)
    closeMutex(&globalStoreLock);
#endif
    for (i=0; i < MaxStoreKey; i++) {
        if (store[i] != 0) {
            freeStablePtr(store[i]);
            store[i] = 0;
        }
    }
}

static StgStablePtr getOrSetKey(StoreKey key, StgStablePtr ptr)
{
    StgStablePtr ret = store[key];
    if(ret==0) {
#if defined(THREADED_RTS)
        ACQUIRE_LOCK(&globalStoreLock);
        ret = store[key];
        if(ret==0) {
#endif
            store[key] = ret = ptr;
#if defined(THREADED_RTS)
        }
        RELEASE_LOCK(&globalStoreLock);
#endif
    }
    return ret;
}

#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(LibHSghcGlobalHasPprDebug)
mkStoreAccessor(LibHSghcGlobalHasNoDebugOutput)
mkStoreAccessor(LibHSghcGlobalHasNoStateHack)

HsInt ghc_unique_counter = 0;
HsInt ghc_unique_inc     = 1;