[Tizen] Unify dnetmemoryenumlib terms to match the codebase (#291)
[platform/upstream/coreclr.git] / src / vm / threads.inl
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
7 // 
8 // 
9 /*============================================================
10 **
11 ** Header:  Threads.inl
12 **
13 ** Purpose: Implements Thread inline functions
14 **
15 **
16 ===========================================================*/
17 #ifndef _THREADS_INL
18 #define _THREADS_INL
19
20 #include "threads.h"
21 #include "appdomain.hpp"
22 #include "frames.h"
23
24 #ifndef DACCESS_COMPILE
25
26 #ifndef __GNUC__
27 EXTERN_C __declspec(thread) ThreadLocalInfo gCurrentThreadInfo;
28 #else // !__GNUC__
29 EXTERN_C __thread ThreadLocalInfo gCurrentThreadInfo;
30 #endif // !__GNUC__
31
32 EXTERN_C inline Thread* STDCALL GetThread()
33 {
34     return gCurrentThreadInfo.m_pThread;
35 }
36
37 EXTERN_C inline AppDomain* STDCALL GetAppDomain()
38 {
39     return AppDomain::GetCurrentDomain();
40 }
41
42 #endif // !DACCESS_COMPILE
43
44 inline void Thread::IncLockCount()
45 {
46     LIMITED_METHOD_CONTRACT;
47     _ASSERTE(GetThread() == this);
48     m_dwLockCount++;
49     _ASSERTE(m_dwLockCount != 0 || HasThreadStateNC(TSNC_UnbalancedLocks));
50 }
51
52 inline void Thread::DecLockCount()
53 {
54     LIMITED_METHOD_CONTRACT;
55     _ASSERTE(GetThread() == this);
56     _ASSERTE(m_dwLockCount > 0 || HasThreadStateNC(TSNC_UnbalancedLocks));
57     m_dwLockCount--;
58 }
59
60 inline
61 Frame* Thread::FindFrame(SIZE_T StackPointer)
62 {
63     Frame* pFrame = GetFrame();
64
65     while ((SIZE_T)pFrame < StackPointer)
66     {
67         pFrame = pFrame->Next();
68     }
69
70     return pFrame;
71 }
72
73 inline void Thread::SetThrowable(OBJECTREF pThrowable DEBUG_ARG(ThreadExceptionState::SetThrowableErrorChecking stecFlags))
74 {
75     WRAPPER_NO_CONTRACT;
76     
77     m_ExceptionState.SetThrowable(pThrowable DEBUG_ARG(stecFlags));
78 }
79
80 // get the current notification (if any) from this thread
81 inline OBJECTHANDLE Thread::GetThreadCurrNotification()
82 {
83     CONTRACTL
84     {
85         NOTHROW;
86         GC_NOTRIGGER;
87         SUPPORTS_DAC;
88     }
89     CONTRACTL_END;
90
91     return m_hCurrNotification;
92 }
93
94 // set the current notification (if any) from this thread
95 inline void Thread::SetThreadCurrNotification(OBJECTHANDLE handle)
96 {
97     CONTRACTL
98     {
99         NOTHROW;
100         GC_NOTRIGGER;
101     }
102     CONTRACTL_END;
103
104     m_hCurrNotification = handle;
105 }
106
107 // clear the current notification (if any) from this thread
108 inline void Thread::ClearThreadCurrNotification()
109 {
110     CONTRACTL
111     {
112         NOTHROW;
113         GC_NOTRIGGER;
114     }
115     CONTRACTL_END;
116
117     m_hCurrNotification = NULL;
118 }
119
120
121 inline OBJECTREF Thread::GetExposedObjectRaw()
122 {
123     CONTRACTL {
124         NOTHROW;
125         GC_NOTRIGGER;
126         SUPPORTS_DAC;
127     }
128     CONTRACTL_END;
129
130     return ObjectFromHandle(m_ExposedObject);
131 }
132
133 inline void Thread::FinishSOWork()
134 {
135     WRAPPER_NO_CONTRACT;
136     _ASSERTE(!HasThreadStateNC(TSNC_SOWorkNeeded));
137 }
138
139 #ifdef FEATURE_COMINTEROP
140 inline void Thread::RevokeApartmentSpy()
141 {
142     LIMITED_METHOD_CONTRACT;
143
144     if (m_fInitializeSpyRegistered)
145     {
146         VERIFY(SUCCEEDED(CoRevokeInitializeSpy(m_uliInitializeSpyCookie)));
147         m_fInitializeSpyRegistered = false;
148     }
149 }
150
151 inline LPVOID Thread::GetLastSTACtxCookie(BOOL *pfNAContext)
152 {
153     LIMITED_METHOD_CONTRACT;
154     *pfNAContext = ((UINT_PTR)m_pLastSTACtxCookie & 1);
155     return (LPVOID)((UINT_PTR)m_pLastSTACtxCookie & ~1);
156 }
157
158 inline void Thread::SetLastSTACtxCookie(LPVOID pCtxCookie, BOOL fNAContext)
159 {
160     LIMITED_METHOD_CONTRACT;
161     if (fNAContext)
162     {
163         // The ctx cookie is an interface pointer so we can steal the lowest bit
164         // to mark whether the context is known to be Neutral Apartment or not.
165         m_pLastSTACtxCookie = (LPVOID)((UINT_PTR)pCtxCookie | 1);
166     }
167     else
168     {
169         m_pLastSTACtxCookie = pCtxCookie;
170     }
171 }
172 #endif // FEATURE_COMINTEROP
173
174 inline bool Thread::IsGCSpecial()
175 {
176     LIMITED_METHOD_CONTRACT;
177     return m_fGCSpecial;
178 }
179
180 inline void Thread::SetGCSpecial(bool fGCSpecial)
181 {
182     LIMITED_METHOD_CONTRACT;
183     m_fGCSpecial = fGCSpecial;
184 }
185
186 #if !defined(DACCESS_COMPILE) && !defined(CROSSGEN_COMPILE)
187
188 inline Thread::CurrentPrepareCodeConfigHolder::CurrentPrepareCodeConfigHolder(Thread *thread, PrepareCodeConfig *config)
189     : m_thread(thread)
190 #ifdef _DEBUG
191     , m_config(config)
192 #endif
193 {
194     LIMITED_METHOD_CONTRACT;
195     _ASSERTE(thread != nullptr);
196     _ASSERTE(thread == GetThread());
197     _ASSERTE(config != nullptr);
198
199     PrepareCodeConfig *previousConfig = thread->m_currentPrepareCodeConfig;
200     if (previousConfig != nullptr)
201     {
202         config->SetNextInSameThread(previousConfig);
203     }
204     thread->m_currentPrepareCodeConfig = config;
205 }
206
207 inline Thread::CurrentPrepareCodeConfigHolder::~CurrentPrepareCodeConfigHolder()
208 {
209     LIMITED_METHOD_CONTRACT;
210
211     PrepareCodeConfig *config = m_thread->m_currentPrepareCodeConfig;
212     _ASSERTE(config == m_config);
213     m_thread->m_currentPrepareCodeConfig = config->GetNextInSameThread();
214     config->SetNextInSameThread(nullptr);
215 }
216
217 #endif // !DACCESS_COMPILE && !CROSSGEN_COMPILE
218
219 #endif