Globals.c 2.6 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
    MaxStoreKey
} StoreKey;
30 31 32 33 34

#ifdef THREADED_RTS
Mutex globalStoreLock;
#endif

35
static StgStablePtr store[MaxStoreKey];
36

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

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

64
static StgStablePtr getOrSetKey(StoreKey key, StgStablePtr ptr)
65
{
66
    StgStablePtr ret = store[key];
67 68 69
    if(ret==0) {
#ifdef THREADED_RTS
        ACQUIRE_LOCK(&globalStoreLock);
70
        ret = store[key];
71 72
        if(ret==0) {
#endif
73
            store[key] = ret = ptr;
74 75 76 77 78 79
#ifdef THREADED_RTS
        }
        RELEASE_LOCK(&globalStoreLock);
#endif
    }
    return ret;
80 81
}    

82
StgStablePtr
83
getOrSetGHCConcSignalSignalHandlerStore(StgStablePtr ptr)
84
{
85
    return getOrSetKey(GHCConcSignalSignalHandlerStore,ptr);
86 87 88
}

StgStablePtr
89
getOrSetGHCConcWindowsPendingDelaysStore(StgStablePtr ptr)
90
{
91
    return getOrSetKey(GHCConcWindowsPendingDelaysStore,ptr);
92 93 94
}

StgStablePtr
95
getOrSetGHCConcWindowsIOManagerThreadStore(StgStablePtr ptr)
96
{
97
    return getOrSetKey(GHCConcWindowsIOManagerThreadStore,ptr);
98 99 100
}

StgStablePtr
101
getOrSetGHCConcWindowsProddingStore(StgStablePtr ptr)
102
{
103
    return getOrSetKey(GHCConcWindowsProddingStore,ptr);
104 105 106
}

StgStablePtr
107
getOrSetSystemEventThreadEventManagerStore(StgStablePtr ptr)
108
{
109 110 111 112 113 114 115
    return getOrSetKey(SystemEventThreadEventManagerStore,ptr);
}

StgStablePtr
getOrSetSystemEventThreadIOManagerThreadStore(StgStablePtr ptr)
{
    return getOrSetKey(SystemEventThreadIOManagerThreadStore,ptr);
116
}