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
6#ifndef _FINALIZER_THREAD_H_
7#define _FINALIZER_THREAD_H_
8
9class FinalizerThread
10{
11 static BOOL fRunFinalizersOnUnload;
12 static BOOL fQuitFinalizer;
13
14#if defined(__linux__) && defined(FEATURE_EVENT_TRACE)
15 static ULONGLONG LastHeapDumpTime;
16#endif
17
18 static CLREvent *hEventFinalizer;
19 static CLREvent *hEventFinalizerDone;
20 static CLREvent *hEventShutDownToFinalizer;
21 static CLREvent *hEventFinalizerToShutDown;
22
23 // Note: This enum makes it easier to read much of the code that deals with the
24 // array of events that the finalizer thread waits on. However, the ordering
25 // is important.
26 // See code:SVR::WaitForFinalizerEvent#MHandleTypeValues for more info
27 enum MHandleType
28 {
29 kLowMemoryNotification = 0,
30 kFinalizer = 1,
31
32#ifdef FEATURE_PROFAPI_ATTACH_DETACH
33 kProfilingAPIAttach = 2,
34#endif // FEATURE_PROFAPI_ATTACH_DETACH
35
36 kHandleCount,
37 };
38
39 static HANDLE MHandles[kHandleCount];
40
41 static void WaitForFinalizerEvent (CLREvent *event);
42
43 static BOOL FinalizerThreadWatchDogHelper();
44
45#ifdef FEATURE_PROFAPI_ATTACH_DETACH
46 static void ProcessProfilerAttachIfNecessary(ULONGLONG * pui64TimestampLastCheckedEventMs);
47#endif // FEATURE_PROFAPI_ATTACH_DETACH
48
49 static Object * DoOneFinalization(Object* fobj, Thread* pThread, int bitToCheck, bool *pbTerminate);
50
51 static void FinalizeAllObjects_Wrapper(void *ptr);
52 static Object * FinalizeAllObjects(Object* fobj, int bitToCheck);
53
54public:
55 static Thread* GetFinalizerThread()
56 {
57 LIMITED_METHOD_CONTRACT;
58 _ASSERTE(g_pFinalizerThread != 0);
59 return g_pFinalizerThread;
60 }
61
62 static BOOL IsCurrentThreadFinalizer();
63
64 static void EnableFinalization();
65
66 static BOOL HaveExtraWorkForFinalizer();
67
68 static void FinalizerThreadWait(DWORD timeout = INFINITE);
69
70 // We wake up a wait for finaliation for two reasons:
71 // if fFinalizer=TRUE, we have finished finalization.
72 // if fFinalizer=FALSE, the timeout for finalization is changed, and AD unload helper thread is notified.
73 static void SignalFinalizationDone(BOOL fFinalizer);
74
75 static VOID FinalizerThreadWorker(void *args);
76 static void FinalizeObjectsOnShutdown(LPVOID args);
77 static DWORD WINAPI FinalizerThreadStart(void *args);
78
79 static void FinalizerThreadCreate();
80 static BOOL FinalizerThreadWatchDog();
81};
82
83#endif // _FINALIZER_THREAD_H_
84