Globals.c 2.72 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 22
typedef enum {
    TypeableStore,
23 24 25 26 27 28
    GHCConcSignalSignalHandlerStore,
    GHCConcWindowsPendingDelaysStore,
    GHCConcWindowsIOManagerThreadStore,
    GHCConcWindowsProddingStore,
    SystemEventThreadEventManagerStore,
    SystemEventThreadIOManagerThreadStore,
29 30
    MaxStoreKey
} StoreKey;
31 32 33 34 35

#ifdef THREADED_RTS
Mutex globalStoreLock;
#endif

36
static StgStablePtr store[MaxStoreKey];
37

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

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

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


StgStablePtr
getOrSetTypeableStore(StgStablePtr ptr)
{
    return getOrSetKey(TypeableStore,ptr);
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
}