Add GetLoaderAllocatorObjectForGC to IGCToCLR (#17443)
[platform/upstream/coreclr.git] / src / gc / sample / gcenv.ee.cpp
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4
5 #include "common.h"
6
7 #include "windows.h"
8
9 #include "gcenv.h"
10 #include "gc.h"
11
12 MethodTable * g_pFreeObjectMethodTable;
13
14 EEConfig * g_pConfig;
15
16 gc_alloc_context g_global_alloc_context;
17
18 bool CLREventStatic::CreateManualEventNoThrow(bool bInitialState)
19 {
20     m_hEvent = CreateEventW(NULL, TRUE, bInitialState, NULL);
21     m_fInitialized = true;
22
23     return IsValid();
24 }
25
26 bool CLREventStatic::CreateAutoEventNoThrow(bool bInitialState)
27 {
28     m_hEvent = CreateEventW(NULL, FALSE, bInitialState, NULL);
29     m_fInitialized = true;
30
31     return IsValid();
32 }
33
34 bool CLREventStatic::CreateOSManualEventNoThrow(bool bInitialState)
35 {
36     m_hEvent = CreateEventW(NULL, TRUE, bInitialState, NULL);
37     m_fInitialized = true;
38
39     return IsValid();
40 }
41
42 bool CLREventStatic::CreateOSAutoEventNoThrow(bool bInitialState)
43 {
44     m_hEvent = CreateEventW(NULL, FALSE, bInitialState, NULL);
45     m_fInitialized = true;
46
47     return IsValid();
48 }
49
50 void CLREventStatic::CloseEvent()
51 {
52     if (m_fInitialized && m_hEvent != INVALID_HANDLE_VALUE)
53     {
54         CloseHandle(m_hEvent);
55         m_hEvent = INVALID_HANDLE_VALUE;
56     }
57 }
58
59 bool CLREventStatic::IsValid() const
60 {
61     return m_fInitialized && m_hEvent != INVALID_HANDLE_VALUE;
62 }
63
64 bool CLREventStatic::Set()
65 {
66     if (!m_fInitialized)
67         return false;
68     return !!SetEvent(m_hEvent);
69 }
70
71 bool CLREventStatic::Reset()
72 {
73     if (!m_fInitialized)
74         return false;
75     return !!ResetEvent(m_hEvent);
76 }
77
78 uint32_t CLREventStatic::Wait(uint32_t dwMilliseconds, bool bAlertable)
79 {
80     DWORD result = WAIT_FAILED;
81
82     if (m_fInitialized)
83     {
84         bool        disablePreemptive = false;
85         Thread *    pCurThread = GetThread();
86
87         if (NULL != pCurThread)
88         {
89             disablePreemptive = GCToEEInterface::EnablePreemptiveGC();
90         }
91
92         result = WaitForSingleObjectEx(m_hEvent, dwMilliseconds, bAlertable);
93
94         if (disablePreemptive)
95         {
96             GCToEEInterface::DisablePreemptiveGC();
97         }
98     }
99
100     return result;
101 }
102
103 __declspec(thread) Thread * pCurrentThread;
104
105 Thread * GetThread()
106 {
107     return pCurrentThread;
108 }
109
110 Thread * g_pThreadList = NULL;
111
112 Thread * ThreadStore::GetThreadList(Thread * pThread)
113 {
114     if (pThread == NULL)
115         return g_pThreadList;
116
117     return pThread->m_pNext;
118 }
119
120 void ThreadStore::AttachCurrentThread()
121 {
122     // TODO: Locks
123
124     Thread * pThread = new Thread();
125     pThread->GetAllocContext()->init();
126     pCurrentThread = pThread;
127
128     pThread->m_pNext = g_pThreadList;
129     g_pThreadList = pThread;
130 }
131
132 void GCToEEInterface::SuspendEE(SUSPEND_REASON reason)
133 {
134     g_theGCHeap->SetGCInProgress(true);
135
136     // TODO: Implement
137 }
138
139 void GCToEEInterface::RestartEE(bool bFinishedGC)
140 {
141     // TODO: Implement
142
143     g_theGCHeap->SetGCInProgress(false);
144 }
145
146 void GCToEEInterface::GcScanRoots(promote_func* fn,  int condemned, int max_gen, ScanContext* sc)
147 {
148     // TODO: Implement - Scan stack roots on given thread
149 }
150
151 void GCToEEInterface::GcStartWork(int condemned, int max_gen)
152 {
153 }
154
155 void GCToEEInterface::AfterGcScanRoots(int condemned, int max_gen, ScanContext* sc)
156 {
157 }
158
159 void GCToEEInterface::GcBeforeBGCSweepWork()
160 {
161 }
162
163 void GCToEEInterface::GcDone(int condemned)
164 {
165 }
166
167 bool GCToEEInterface::RefCountedHandleCallbacks(Object * pObject)
168 {
169     return false;
170 }
171
172 bool GCToEEInterface::IsPreemptiveGCDisabled()
173 {
174     Thread* pThread = ::GetThread();
175     return pThread->PreemptiveGCDisabled();
176 }
177
178 bool GCToEEInterface::EnablePreemptiveGC()
179 {
180     bool bToggleGC = false;
181     Thread* pThread = ::GetThread();
182
183     if (pThread)
184     {
185         bToggleGC = !!pThread->PreemptiveGCDisabled();
186         if (bToggleGC)
187         {
188             pThread->EnablePreemptiveGC();
189         }
190     }
191
192     return bToggleGC;
193 }
194
195 void GCToEEInterface::DisablePreemptiveGC()
196 {
197     Thread* pThread = ::GetThread();
198     pThread->DisablePreemptiveGC();
199 }
200
201 Thread* GCToEEInterface::GetThread()
202 {
203     return ::GetThread();
204 }
205
206 gc_alloc_context * GCToEEInterface::GetAllocContext()
207 {
208     Thread* pThread = ::GetThread();
209     return pThread->GetAllocContext();
210 }
211
212 void GCToEEInterface::GcEnumAllocContexts (enum_alloc_context_func* fn, void* param)
213 {
214     Thread * pThread = NULL;
215     while ((pThread = ThreadStore::GetThreadList(pThread)) != NULL)
216     {
217         fn(pThread->GetAllocContext(), param);
218     }
219 }
220
221 uint8_t* GCToEEInterface::GetLoaderAllocatorObjectForGC(Object* pObject)
222 {
223     return NULL;
224 }
225
226 void GCToEEInterface::SyncBlockCacheWeakPtrScan(HANDLESCANPROC /*scanProc*/, uintptr_t /*lp1*/, uintptr_t /*lp2*/)
227 {
228 }
229
230 void GCToEEInterface::SyncBlockCacheDemote(int /*max_gen*/)
231 {
232 }
233
234 void GCToEEInterface::SyncBlockCachePromotionsGranted(int /*max_gen*/)
235 {
236 }
237
238 void GCToEEInterface::DiagGCStart(int gen, bool isInduced)
239 {
240 }
241
242 void GCToEEInterface::DiagUpdateGenerationBounds()
243 {
244 }
245
246 void GCToEEInterface::DiagGCEnd(size_t index, int gen, int reason, bool fConcurrent)
247 {
248 }
249
250 void GCToEEInterface::DiagWalkFReachableObjects(void* gcContext)
251 {
252 }
253
254 void GCToEEInterface::DiagWalkSurvivors(void* gcContext)
255 {
256 }
257
258 void GCToEEInterface::DiagWalkLOHSurvivors(void* gcContext)
259 {
260 }
261
262 void GCToEEInterface::DiagWalkBGCSurvivors(void* gcContext)
263 {
264 }
265
266 void GCToEEInterface::StompWriteBarrier(WriteBarrierParameters* args)
267 {
268 }
269
270 void GCToEEInterface::EnableFinalization(bool foundFinalizers)
271 {
272     // Signal to finalizer thread that there are objects to finalize
273     // TODO: Implement for finalization
274 }
275
276 void GCToEEInterface::HandleFatalError(unsigned int exitCode)
277 {
278     abort();
279 }
280
281 bool GCToEEInterface::ShouldFinalizeObjectForUnload(AppDomain* pDomain, Object* obj)
282 {
283     return true;
284 }
285
286 bool GCToEEInterface::ForceFullGCToBeBlocking()
287 {
288     return false;
289 }
290
291 bool GCToEEInterface::EagerFinalized(Object* obj)
292 {
293     // The sample does not finalize anything eagerly.
294     return false;
295 }
296
297 bool GCToEEInterface::GetBooleanConfigValue(const char* key, bool* value)
298 {
299     return false;
300 }
301
302 bool GCToEEInterface::GetIntConfigValue(const char* key, int64_t* value)
303 {
304     return false;
305 }
306
307 bool GCToEEInterface::GetStringConfigValue(const char* key, const char** value)
308 {
309     return false;
310 }
311
312 void GCToEEInterface::FreeStringConfigValue(const char *value)
313 {
314
315 }
316
317 bool GCToEEInterface::IsGCThread()
318 {
319     return false;
320 }
321
322 bool GCToEEInterface::WasCurrentThreadCreatedByGC()
323 {
324     return false;
325 }
326
327 MethodTable* GCToEEInterface::GetFreeObjectMethodTable()
328 {
329     return g_pFreeObjectMethodTable;
330 }
331
332 bool GCToEEInterface::CreateThread(void (*threadStart)(void*), void* arg, bool is_suspendable, const char* name)
333 {
334     return false;
335 }
336
337 void GCToEEInterface::WalkAsyncPinnedForPromotion(Object* object, ScanContext* sc, promote_func* callback)
338 {
339 }
340
341 void GCToEEInterface::WalkAsyncPinned(Object* object, void* context, void (*callback)(Object*, Object*, void*))
342 {
343 }