Skip to content

Commit 698e098

Browse files
Convert Thread.Initialize()
1 parent 35af68d commit 698e098

File tree

6 files changed

+27
-19
lines changed

6 files changed

+27
-19
lines changed

src/coreclr/System.Private.CoreLib/src/System/Threading/Thread.CoreCLR.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,14 @@ static Thread GetCurrentThreadWorker()
171171
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "ThreadNative_GetCurrentThreadSlow")]
172172
private static partial void GetCurrentThreadSlow(ObjectHandleOnStack thread);
173173

174-
[MethodImpl(MethodImplOptions.InternalCall)]
175-
private extern void Initialize();
174+
private void Initialize()
175+
{
176+
Thread _this = this;
177+
Initialize(ObjectHandleOnStack.Create(ref _this));
178+
}
179+
180+
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "ThreadNative_Initialize")]
181+
private static partial void Initialize(ObjectHandleOnStack thread);
176182

177183
/// <summary>Clean up the thread when it goes away.</summary>
178184
~Thread() => InternalFinalize(); // Delegate to the unmanaged portion.

src/coreclr/vm/comsynchronizable.cpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -426,33 +426,36 @@ extern "C" UINT64 QCALLTYPE ThreadNative_GetCurrentOSThreadId()
426426
return threadId;
427427
}
428428

429-
FCIMPL1(void, ThreadNative::Initialize, ThreadBaseObject* pThisUNSAFE)
429+
extern "C" void QCALLTYPE ThreadNative_Initialize(QCall::ObjectHandleOnStack t)
430430
{
431-
FCALL_CONTRACT;
431+
QCALL_CONTRACT;
432432

433-
THREADBASEREF pThis = (THREADBASEREF) pThisUNSAFE;
433+
BEGIN_QCALL;
434434

435-
HELPER_METHOD_FRAME_BEGIN_1(pThis);
435+
GCX_COOP();
436436

437-
_ASSERTE(pThis != NULL);
438-
_ASSERTE(pThis->m_InternalThread == NULL);
437+
THREADBASEREF threadRef = NULL;
438+
GCPROTECT_BEGIN(threadRef)
439+
threadRef = (THREADBASEREF)t.Get();
440+
441+
_ASSERTE(threadRef != NULL);
442+
_ASSERTE(threadRef->GetInternal() == NULL);
439443

440444
// if we don't have an internal Thread object associated with this exposed object,
441445
// now is our first opportunity to create one.
442-
Thread *unstarted = SetupUnstartedThread();
443-
446+
Thread* unstarted = SetupUnstartedThread();
444447
PREFIX_ASSUME(unstarted != NULL);
445448

446-
pThis->SetInternal(unstarted);
447-
pThis->SetManagedThreadId(unstarted->GetThreadId());
448-
unstarted->SetExposedObject(pThis);
449+
threadRef->SetInternal(unstarted);
450+
threadRef->SetManagedThreadId(unstarted->GetThreadId());
451+
unstarted->SetExposedObject(threadRef);
449452

450453
// Initialize the thread priority to normal.
451-
pThis->SetPriority(ThreadNative::PRIORITY_NORMAL);
454+
threadRef->SetPriority(ThreadNative::PRIORITY_NORMAL);
452455

453-
HELPER_METHOD_FRAME_END();
456+
GCPROTECT_END();
457+
END_QCALL;
454458
}
455-
FCIMPLEND
456459

457460
// Return whether or not this is a background thread.
458461
FCIMPL1(FC_BOOL_RET, ThreadNative::GetIsBackground, ThreadBaseObject* pThisUNSAFE)

src/coreclr/vm/comsynchronizable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ friend class ThreadBaseObject;
5252
ThreadAbortRequested = 128,
5353
};
5454

55-
static FCDECL1(void, Initialize, ThreadBaseObject* pThisUNSAFE);
5655
static FCDECL1(FC_BOOL_RET, GetIsBackground, ThreadBaseObject* pThisUNSAFE);
5756
static FCDECL1(INT32, GetThreadState, ThreadBaseObject* pThisUNSAFE);
5857

@@ -84,6 +83,7 @@ extern "C" void QCALLTYPE ThreadNative_SetIsBackground(QCall::ThreadHandle threa
8483
extern "C" void QCALLTYPE ThreadNative_InformThreadNameChange(QCall::ThreadHandle thread, LPCWSTR name, INT32 len);
8584
extern "C" BOOL QCALLTYPE ThreadNative_YieldThread();
8685
extern "C" UINT64 QCALLTYPE ThreadNative_GetCurrentOSThreadId();
86+
extern "C" void QCALLTYPE ThreadNative_Initialize(QCall::ObjectHandleOnStack t);
8787
extern "C" INT32 QCALLTYPE ThreadNative_GetThreadState(QCall::ObjectHandleOnStack t);
8888

8989
#ifdef FEATURE_COMINTEROP_APARTMENT_SUPPORT

src/coreclr/vm/ecalllist.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,6 @@ FCFuncStart(gMathFFuncs)
298298
FCFuncEnd()
299299

300300
FCFuncStart(gThreadFuncs)
301-
FCFuncElement("Initialize", ThreadNative::Initialize)
302301
FCFuncElement("GetCurrentThread", ThreadNative::GetCurrentThread)
303302
FCFuncElement("InternalFinalize", ThreadNative::Finalize)
304303
FCFuncElement("GetIsBackground", ThreadNative::GetIsBackground)

src/coreclr/vm/object.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1302,7 +1302,6 @@ typedef DPTR(class ThreadBaseObject) PTR_ThreadBaseObject;
13021302
class ThreadBaseObject : public Object
13031303
{
13041304
friend class ClrDataAccess;
1305-
friend class ThreadNative;
13061305
friend class CoreLibBinder;
13071306
friend class Object;
13081307

src/coreclr/vm/qcallentrypoints.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ static const Entry s_QCall[] =
241241
DllImportEntry(ThreadNative_InformThreadNameChange)
242242
DllImportEntry(ThreadNative_YieldThread)
243243
DllImportEntry(ThreadNative_GetCurrentOSThreadId)
244+
DllImportEntry(ThreadNative_Initialize)
244245
DllImportEntry(ThreadNative_GetThreadState)
245246
#ifdef FEATURE_COMINTEROP_APARTMENT_SUPPORT
246247
DllImportEntry(ThreadNative_GetApartmentState)

0 commit comments

Comments
 (0)