Skip to content

Commit bb816b8

Browse files
committed
Use Thread.CurrentThread
1 parent 35f1e73 commit bb816b8

File tree

1 file changed

+11
-37
lines changed
  • src/libraries/System.Private.CoreLib/src/System/Threading

1 file changed

+11
-37
lines changed

src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs

Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,33 +24,17 @@ public sealed partial class Thread : CriticalFinalizerObject
2424
// State associated with starting new thread
2525
private sealed class StartHelper
2626
{
27-
#if TARGET_OSX
28-
// On other platforms, when the underlying native thread is created,
29-
// the thread name is set to the name of the managed thread by another thread.
30-
// However, on OS X, only the thread itself can set its name.
31-
// The thread reference here allows the native thread to set its name before the actual work starts.
32-
// See https://github.com/dotnet/runtime/issues/106464.
33-
internal Thread _thread;
34-
#endif
3527
internal int _maxStackSize;
3628
internal Delegate _start;
3729
internal object? _startArg;
3830
internal CultureInfo? _culture;
3931
internal CultureInfo? _uiCulture;
4032
internal ExecutionContext? _executionContext;
4133

42-
#if !TARGET_OSX
4334
internal StartHelper(Delegate start)
4435
{
4536
_start = start;
4637
}
47-
#else
48-
internal StartHelper(Thread thread, Delegate start)
49-
{
50-
_thread = thread;
51-
_start = start;
52-
}
53-
#endif
5438

5539
internal static readonly ContextCallback s_threadStartContextCallback = new ContextCallback(Callback);
5640

@@ -88,11 +72,17 @@ private void RunWorker()
8872

8973
try
9074
{
91-
#if TARGET_OSX
92-
if (!string.IsNullOrEmpty(_thread.Name))
75+
#if TARGET_OSX || NATIVEAOT
76+
// On other platforms, when the underlying native thread is created,
77+
// the thread name is set to the name of the managed thread by another thread.
78+
// However, on OS X and NativeAOT (across all OSes), only the thread itself can set its name.
79+
// Therefore, by this point the native thread is still unnamed as it has not started yet.
80+
// See https://github.com/dotnet/runtime/issues/106464.
81+
Thread thread = Thread.CurrentThread;
82+
if (!string.IsNullOrEmpty(thread.Name))
9383
{
9484
// Name the underlying native thread to match the managed thread name.
95-
_thread.ThreadNameChanged(_thread.Name);
85+
thread.ThreadNameChanged(thread.Name);
9686
}
9787
#endif
9888
if (start is ThreadStart threadStart)
@@ -143,11 +133,7 @@ public Thread(ThreadStart start)
143133
{
144134
ArgumentNullException.ThrowIfNull(start);
145135

146-
#if !TARGET_OSX
147136
_startHelper = new StartHelper(start);
148-
#else
149-
_startHelper = new StartHelper(this, start);
150-
#endif
151137

152138
Initialize();
153139
}
@@ -158,11 +144,7 @@ public Thread(ThreadStart start, int maxStackSize)
158144

159145
ArgumentOutOfRangeException.ThrowIfNegative(maxStackSize);
160146

161-
#if !TARGET_OSX
162-
_startHelper = new StartHelper(start);
163-
#else
164-
_startHelper = new StartHelper(this, start);
165-
#endif
147+
_startHelper = new StartHelper(start) { _maxStackSize = maxStackSize };
166148

167149
Initialize();
168150
}
@@ -171,11 +153,7 @@ public Thread(ParameterizedThreadStart start)
171153
{
172154
ArgumentNullException.ThrowIfNull(start);
173155

174-
#if !TARGET_OSX
175156
_startHelper = new StartHelper(start);
176-
#else
177-
_startHelper = new StartHelper(this, start);
178-
#endif
179157

180158
Initialize();
181159
}
@@ -186,11 +164,7 @@ public Thread(ParameterizedThreadStart start, int maxStackSize)
186164

187165
ArgumentOutOfRangeException.ThrowIfNegative(maxStackSize);
188166

189-
#if !TARGET_OSX
190-
_startHelper = new StartHelper(start);
191-
#else
192-
_startHelper = new StartHelper(this, start);
193-
#endif
167+
_startHelper = new StartHelper(start) { _maxStackSize = maxStackSize };
194168

195169
Initialize();
196170
}

0 commit comments

Comments
 (0)