summaryrefslogtreecommitdiff
path: root/src/vm/threads.inl
blob: 26682ec09bb85ad4f40f34959ac8031623969c17 (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// 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.
//


// 
// 
/*============================================================
**
** Header:  Threads.inl
**
** Purpose: Implements Thread inline functions
**
**
===========================================================*/
#ifndef _THREADS_INL
#define _THREADS_INL

#include "threads.h"
#include "appdomain.hpp"
#include "frames.h"

#ifndef DACCESS_COMPILE
#ifdef FEATURE_IMPLICIT_TLS

#ifndef __llvm__
EXTERN_C __declspec(thread) ThreadLocalInfo gCurrentThreadInfo;
#else // !__llvm__
EXTERN_C __thread ThreadLocalInfo gCurrentThreadInfo;
#endif // !__llvm__

EXTERN_C inline Thread* STDCALL GetThread()
{
    return gCurrentThreadInfo.m_pThread;
}

EXTERN_C inline AppDomain* STDCALL GetAppDomain()
{
    return gCurrentThreadInfo.m_pAppDomain;
}

#endif // FEATURE_IMPLICIT_TLS
#endif // !DACCESS_COMPILE

#ifdef ENABLE_GET_THREAD_GENERIC_FULL_CHECK
// See code:GetThreadGenericFullCheck
inline /* static */ BOOL Thread::ShouldEnforceEEThreadNotRequiredContracts()
{
    LIMITED_METHOD_CONTRACT;
    return s_fEnforceEEThreadNotRequiredContracts;
}
#endif // ENABLE_GET_THREAD_GENERIC_FULL_CHECK

inline void Thread::IncLockCount()
{
    LIMITED_METHOD_CONTRACT;
    _ASSERTE(GetThread() == this);
    m_dwLockCount++;
    _ASSERTE(m_dwLockCount != 0 || HasThreadStateNC(TSNC_UnbalancedLocks) || GetDomain()->OkToIgnoreOrphanedLocks());
}

inline void Thread::DecLockCount()
{
    LIMITED_METHOD_CONTRACT;
    _ASSERTE(GetThread() == this);
    _ASSERTE(m_dwLockCount > 0 || HasThreadStateNC(TSNC_UnbalancedLocks) || GetDomain()->OkToIgnoreOrphanedLocks());
    m_dwLockCount--;
}

inline
Frame* Thread::FindFrame(SIZE_T StackPointer)
{
    Frame* pFrame = GetFrame();

    while ((SIZE_T)pFrame < StackPointer)
    {
        pFrame = pFrame->Next();
    }

    return pFrame;
}

inline void Thread::SetThrowable(OBJECTREF pThrowable DEBUG_ARG(ThreadExceptionState::SetThrowableErrorChecking stecFlags))
{
    WRAPPER_NO_CONTRACT;
    
    m_ExceptionState.SetThrowable(pThrowable DEBUG_ARG(stecFlags));
}

inline void Thread::SetKickOffDomainId(ADID ad)
{
    CONTRACTL {
        NOTHROW;
        GC_NOTRIGGER;
        SO_TOLERANT;
    }
    CONTRACTL_END;

    m_pKickOffDomainId = ad;
}


inline ADID Thread::GetKickOffDomainId()
{
    CONTRACTL {
        NOTHROW;
        GC_NOTRIGGER;
        SO_TOLERANT;
    }
    CONTRACTL_END;

    _ASSERTE(m_pKickOffDomainId.m_dwId != 0);
    return m_pKickOffDomainId;
}

// get the current notification (if any) from this thread
inline OBJECTHANDLE Thread::GetThreadCurrNotification()
{
    CONTRACTL
    {
        SO_NOT_MAINLINE;
        NOTHROW;
        GC_NOTRIGGER;
        SUPPORTS_DAC;
    }
    CONTRACTL_END;

    return m_hCurrNotification;
}

// set the current notification (if any) from this thread
inline void Thread::SetThreadCurrNotification(OBJECTHANDLE handle)
{
    CONTRACTL
    {
        SO_NOT_MAINLINE;
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    m_hCurrNotification = handle;
}

// clear the current notification (if any) from this thread
inline void Thread::ClearThreadCurrNotification()
{
    CONTRACTL
    {
        SO_NOT_MAINLINE;
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    m_hCurrNotification = NULL;
}


inline OBJECTREF Thread::GetExposedObjectRaw()
{
    CONTRACTL {
        NOTHROW;
        GC_NOTRIGGER;
        SO_TOLERANT;
        SUPPORTS_DAC;
    }
    CONTRACTL_END;

    return ObjectFromHandle(m_ExposedObject);
}

inline void Thread::FinishSOWork()
{
    WRAPPER_NO_CONTRACT;
#ifdef FEATURE_STACK_PROBE
    if (HasThreadStateNC(TSNC_SOWorkNeeded))
    {
        ResetThreadStateNC(TSNC_SOWorkNeeded);
        // Wake up AD unload thread to finish SO work that is delayed due to limit stack
        AppDomain::EnableADUnloadWorkerForThreadAbort();
    }
#else
    _ASSERTE(!HasThreadStateNC(TSNC_SOWorkNeeded));
#endif
}

inline DWORD Thread::IncrementOverridesCount()
{
    WRAPPER_NO_CONTRACT;
    return m_ADStack.IncrementOverridesCount();
}

inline DWORD Thread::DecrementOverridesCount()
{
    WRAPPER_NO_CONTRACT;
    return m_ADStack.DecrementOverridesCount();
}

inline DWORD Thread::GetOverridesCount()
{
    WRAPPER_NO_CONTRACT;
    return m_ADStack.GetOverridesCount();
}

inline DWORD Thread::IncrementAssertCount()
{
    WRAPPER_NO_CONTRACT;
    return m_ADStack.IncrementAssertCount();
}

inline DWORD Thread::DecrementAssertCount()
{
    WRAPPER_NO_CONTRACT;
    return m_ADStack.DecrementAssertCount();
}

inline DWORD Thread::GetAssertCount()
{
    LIMITED_METHOD_CONTRACT;
    return m_ADStack.GetAssertCount();
}

#ifndef DACCESS_COMPILE
inline void Thread::PushDomain(ADID pDomain)
{
    WRAPPER_NO_CONTRACT;
    m_ADStack.PushDomain(pDomain);
}

inline ADID Thread::PopDomain()
{
    WRAPPER_NO_CONTRACT;
    return m_ADStack.PopDomain();
}
#endif // DACCESS_COMPILE

inline DWORD Thread::GetNumAppDomainsOnThread()
{
    WRAPPER_NO_CONTRACT;
    return m_ADStack.GetNumDomains();
}

inline BOOL Thread::CheckThreadWideSpecialFlag(DWORD flags)
{
    WRAPPER_NO_CONTRACT;
    return m_ADStack.GetThreadWideSpecialFlag() & flags;
}

inline void Thread::InitDomainIteration(DWORD *pIndex)
{
    WRAPPER_NO_CONTRACT;
    m_ADStack.InitDomainIteration(pIndex);
}

inline ADID Thread::GetNextDomainOnStack(DWORD *pIndex, DWORD *pOverrides, DWORD *pAsserts)
{
    WRAPPER_NO_CONTRACT;
    return m_ADStack.GetNextDomainOnStack(pIndex, pOverrides, pAsserts);
}

inline void Thread::UpdateDomainOnStack(DWORD pIndex, DWORD asserts, DWORD overrides)
{
    WRAPPER_NO_CONTRACT;
    return m_ADStack.UpdateDomainOnStack(pIndex, asserts, overrides);
}

#ifdef FEATURE_COMINTEROP
inline void Thread::RevokeApartmentSpy()
{
    LIMITED_METHOD_CONTRACT;

    if (m_fInitializeSpyRegistered)
    {
        VERIFY(SUCCEEDED(CoRevokeInitializeSpy(m_uliInitializeSpyCookie)));
        m_fInitializeSpyRegistered = false;
    }
}

inline LPVOID Thread::GetLastSTACtxCookie(BOOL *pfNAContext)
{
    LIMITED_METHOD_CONTRACT;
    *pfNAContext = ((UINT_PTR)m_pLastSTACtxCookie & 1);
    return (LPVOID)((UINT_PTR)m_pLastSTACtxCookie & ~1);
}

inline void Thread::SetLastSTACtxCookie(LPVOID pCtxCookie, BOOL fNAContext)
{
    LIMITED_METHOD_CONTRACT;
    if (fNAContext)
    {
        // The ctx cookie is an interface pointer so we can steal the lowest bit
        // to mark whether the context is known to be Neutral Apartment or not.
        m_pLastSTACtxCookie = (LPVOID)((UINT_PTR)pCtxCookie | 1);
    }
    else
    {
        m_pLastSTACtxCookie = pCtxCookie;
    }
}
#endif // FEATURE_COMINTEROP

#include "appdomainstack.inl"

inline bool Thread::IsGCSpecial()
{
    LIMITED_METHOD_CONTRACT;
    return m_fGCSpecial;
}

inline void Thread::SetGCSpecial(bool fGCSpecial)
{
    LIMITED_METHOD_CONTRACT;
    m_fGCSpecial = fGCSpecial;
}

#endif