Globals.c 2.96 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
 * If/when we switch to a dynamically-linked GHCi, this can all go
 * away, because there would be just one copy of each library.
 *
13 14
 * ---------------------------------------------------------------------------*/

Simon Marlow's avatar
Simon Marlow committed
15
#include "PosixSource.h"
16
#include "Rts.h"
Simon Marlow's avatar
Simon Marlow committed
17 18 19

#include "Globals.h"
#include "Stable.h"
20

21
typedef enum {
22 23 24 25 26 27
    GHCConcSignalSignalHandlerStore,
    GHCConcWindowsPendingDelaysStore,
    GHCConcWindowsIOManagerThreadStore,
    GHCConcWindowsProddingStore,
    SystemEventThreadEventManagerStore,
    SystemEventThreadIOManagerThreadStore,
28 29
    SystemTimerThreadEventManagerStore,
    SystemTimerThreadIOManagerThreadStore,
30 31
    MaxStoreKey
} StoreKey;
32 33 34 35 36

#ifdef THREADED_RTS
Mutex globalStoreLock;
#endif

37
static StgStablePtr store[MaxStoreKey];
38

39 40 41
void
initGlobalStore(void)
{
42 43 44 45
    nat i;
    for (i=0; i < MaxStoreKey; i++) {
        store[i] = 0;
    }
46 47 48 49 50 51 52 53
#ifdef THREADED_RTS
    initMutex(&globalStoreLock);
#endif
}

void
exitGlobalStore(void)
{
54
    nat i;
55 56 57
#ifdef THREADED_RTS
    closeMutex(&globalStoreLock);
#endif
58 59 60 61 62
    for (i=0; i < MaxStoreKey; i++) {
        if (store[i] != 0) {
            freeStablePtr(store[i]);
            store[i] = 0;
        }
63 64 65
    }
}

66
static StgStablePtr getOrSetKey(StoreKey key, StgStablePtr ptr)
67
{
68
    StgStablePtr ret = store[key];
69 70 71
    if(ret==0) {
#ifdef THREADED_RTS
        ACQUIRE_LOCK(&globalStoreLock);
72
        ret = store[key];
73 74
        if(ret==0) {
#endif
75
            store[key] = ret = ptr;
76 77 78 79 80 81
#ifdef THREADED_RTS
        }
        RELEASE_LOCK(&globalStoreLock);
#endif
    }
    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 112 113 114 115 116 117
    return getOrSetKey(SystemEventThreadEventManagerStore,ptr);
}

StgStablePtr
getOrSetSystemEventThreadIOManagerThreadStore(StgStablePtr ptr)
{
    return getOrSetKey(SystemEventThreadIOManagerThreadStore,ptr);
118
}
119 120 121 122 123 124 125 126 127 128 129 130

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

StgStablePtr
getOrSetSystemTimerThreadIOManagerThreadStore(StgStablePtr ptr)
{
    return getOrSetKey(SystemTimerThreadIOManagerThreadStore,ptr);
}