@@ -24,33 +24,17 @@ public sealed partial class Thread : CriticalFinalizerObject
24
24
// State associated with starting new thread
25
25
private sealed class StartHelper
26
26
{
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
35
27
internal int _maxStackSize ;
36
28
internal Delegate _start ;
37
29
internal object ? _startArg ;
38
30
internal CultureInfo ? _culture ;
39
31
internal CultureInfo ? _uiCulture ;
40
32
internal ExecutionContext ? _executionContext ;
41
33
42
- #if ! TARGET_OSX
43
34
internal StartHelper ( Delegate start )
44
35
{
45
36
_start = start ;
46
37
}
47
- #else
48
- internal StartHelper ( Thread thread , Delegate start )
49
- {
50
- _thread = thread ;
51
- _start = start ;
52
- }
53
- #endif
54
38
55
39
internal static readonly ContextCallback s_threadStartContextCallback = new ContextCallback ( Callback ) ;
56
40
@@ -88,11 +72,17 @@ private void RunWorker()
88
72
89
73
try
90
74
{
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 ) )
93
83
{
94
84
// Name the underlying native thread to match the managed thread name.
95
- _thread . ThreadNameChanged ( _thread . Name ) ;
85
+ thread . ThreadNameChanged ( thread . Name ) ;
96
86
}
97
87
#endif
98
88
if ( start is ThreadStart threadStart )
@@ -143,11 +133,7 @@ public Thread(ThreadStart start)
143
133
{
144
134
ArgumentNullException . ThrowIfNull ( start ) ;
145
135
146
- #if ! TARGET_OSX
147
136
_startHelper = new StartHelper ( start ) ;
148
- #else
149
- _startHelper = new StartHelper ( this , start ) ;
150
- #endif
151
137
152
138
Initialize ( ) ;
153
139
}
@@ -158,11 +144,7 @@ public Thread(ThreadStart start, int maxStackSize)
158
144
159
145
ArgumentOutOfRangeException . ThrowIfNegative ( maxStackSize ) ;
160
146
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 } ;
166
148
167
149
Initialize ( ) ;
168
150
}
@@ -171,11 +153,7 @@ public Thread(ParameterizedThreadStart start)
171
153
{
172
154
ArgumentNullException . ThrowIfNull ( start ) ;
173
155
174
- #if ! TARGET_OSX
175
156
_startHelper = new StartHelper ( start ) ;
176
- #else
177
- _startHelper = new StartHelper ( this , start ) ;
178
- #endif
179
157
180
158
Initialize ( ) ;
181
159
}
@@ -186,11 +164,7 @@ public Thread(ParameterizedThreadStart start, int maxStackSize)
186
164
187
165
ArgumentOutOfRangeException . ThrowIfNegative ( maxStackSize ) ;
188
166
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 } ;
194
168
195
169
Initialize ( ) ;
196
170
}
0 commit comments