summaryrefslogtreecommitdiff
path: root/src/vm/eventstore.cpp
blob: 13382c0a0c64a0f47645fe36c0fbe401d7fdd374 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.


#include "common.h"
#include "eventstore.hpp"
#include "synch.h"

// A class to maintain a pool of available events.

const int EventStoreLength = 8;
class EventStore
{
public: 
    // Note: No constructors/destructors - global instance

    void Init()
    {
        WRAPPER_NO_CONTRACT;

        m_EventStoreCrst.Init(CrstEventStore, CRST_UNSAFE_ANYMODE);
        m_Store = NULL;
    }

    void Destroy()
    {
        WRAPPER_NO_CONTRACT;
        
        _ASSERTE (g_fEEShutDown);

        m_EventStoreCrst.Destroy();

        EventStoreElem *walk;
        EventStoreElem *next;

        walk = m_Store;
        while (walk) {
            next = walk->next;
            delete (walk);
            walk = next;
        }
    }

    void StoreHandleForEvent (CLREvent* handle)
    {
        CONTRACTL {
            THROWS;
            GC_NOTRIGGER;
        } CONTRACTL_END;

        _ASSERTE (handle);
        CrstHolder ch(&m_EventStoreCrst);
        if (m_Store == NULL) {
            m_Store = new EventStoreElem ();
        }
        EventStoreElem *walk;
#ifdef _DEBUG
        // See if we have some leakage.
        LONG count = 0;
        walk = m_Store; 
        while (walk) {
            count += walk->AvailableEventCount();
            walk = walk->next;
        }
        // The number of events stored in the pool should be small.
        _ASSERTE (count <= ThreadStore::s_pThreadStore->ThreadCountInEE() * 2 + 10);
#endif
        walk = m_Store;        
        while (walk) {
            if (walk->StoreHandleForEvent (handle) )
                return;
            if (walk->next == NULL) {
                break;
            }
            walk = walk->next;
        }
        if (walk != NULL)
        {
            walk->next = new EventStoreElem ();
            walk->next->hArray[0] = handle;
        }
    }

    CLREvent* GetHandleForEvent ()
    {
        CONTRACTL {
            THROWS;
            GC_NOTRIGGER;
        } CONTRACTL_END;

        CLREvent* handle;
        CrstHolder ch(&m_EventStoreCrst);
        EventStoreElem *walk = m_Store;
        while (walk) {
            handle = walk->GetHandleForEvent();
            if (handle != NULL) {
                return handle;
            }
            walk = walk->next;
        }
        handle = new CLREvent;
        _ASSERTE (handle != NULL);
        handle->CreateManualEvent(TRUE);
        return handle;
    }

private:
    struct EventStoreElem
    {
        CLREvent *hArray[EventStoreLength];
        EventStoreElem *next;
        
        EventStoreElem ()
        {
            LIMITED_METHOD_CONTRACT;

            next = NULL;
            for (int i = 0; i < EventStoreLength; i ++) {
                hArray[i] = NULL;
            }
        }

        ~EventStoreElem ()
        {
            LIMITED_METHOD_CONTRACT;

            for (int i = 0; i < EventStoreLength; i++) {
                if (hArray[i]) {
                    delete hArray[i];
                    hArray[i] = NULL;
                }
            }
        }

        // Store a handle in the current EventStoreElem.  Return TRUE if succeessful.
        // Return FALSE if failed due to no free slot.
        BOOL StoreHandleForEvent (CLREvent* handle)
        {
            LIMITED_METHOD_CONTRACT;

            int i;
            for (i = 0; i < EventStoreLength; i++) {
                if (hArray[i] == NULL) {
                    hArray[i] = handle;
                    return TRUE;
                }
            }
            return FALSE;
        }

        // Get a handle from the current EventStoreElem.
        CLREvent* GetHandleForEvent ()
        {
            LIMITED_METHOD_CONTRACT;

            int i;
            for (i = 0; i < EventStoreLength; i++) {
                if (hArray[i] != NULL) {
                    CLREvent* handle = hArray[i];
                    hArray[i] = NULL;
                    return handle;
                }
            }

            return NULL;
        }

#ifdef _DEBUG
        LONG AvailableEventCount ()
        {
            LIMITED_METHOD_CONTRACT;

            LONG count = 0;
            for (int i = 0; i < EventStoreLength; i++) {
                if (hArray[i] != NULL) {
                    count ++;
                }
            }
            return count;
        }
#endif
    };

    EventStoreElem  *m_Store;
    
    // Critical section for adding and removing event used for Object::Wait
    CrstStatic      m_EventStoreCrst;
};

static EventStore s_EventStore;
 
CLREvent* GetEventFromEventStore()
{
    WRAPPER_NO_CONTRACT;

    return s_EventStore.GetHandleForEvent();
}

void StoreEventToEventStore(CLREvent* hEvent)
{
    WRAPPER_NO_CONTRACT;

    s_EventStore.StoreHandleForEvent(hEvent);
}

void InitEventStore()
{
    WRAPPER_NO_CONTRACT;

    s_EventStore.Init();
}

void TerminateEventStore()
{
    WRAPPER_NO_CONTRACT;

    s_EventStore.Destroy();
}