@@ -24,6 +24,14 @@ 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
27
35
internal int _maxStackSize ;
28
36
internal Delegate _start ;
29
37
internal object ? _startArg ;
@@ -72,6 +80,13 @@ private void RunWorker()
72
80
73
81
try
74
82
{
83
+ #if TARGET_OSX
84
+ if ( _thread != null && ! string . IsNullOrEmpty ( _thread . Name ) )
85
+ {
86
+ // Name the underlying native thread to match the managed thread name.
87
+ _thread . ThreadNameChanged ( _thread . Name ) ;
88
+ }
89
+ #endif
75
90
if ( start is ThreadStart threadStart )
76
91
{
77
92
threadStart ( ) ;
@@ -121,6 +136,9 @@ public Thread(ThreadStart start)
121
136
ArgumentNullException . ThrowIfNull ( start ) ;
122
137
123
138
_startHelper = new StartHelper ( start ) ;
139
+ #if TARGET_OSX
140
+ _startHelper . _thread = this ;
141
+ #endif
124
142
125
143
Initialize ( ) ;
126
144
}
@@ -132,6 +150,9 @@ public Thread(ThreadStart start, int maxStackSize)
132
150
ArgumentOutOfRangeException . ThrowIfNegative ( maxStackSize ) ;
133
151
134
152
_startHelper = new StartHelper ( start ) { _maxStackSize = maxStackSize } ;
153
+ #if TARGET_OSX
154
+ _startHelper . _thread = this ;
155
+ #endif
135
156
136
157
Initialize ( ) ;
137
158
}
@@ -141,6 +162,9 @@ public Thread(ParameterizedThreadStart start)
141
162
ArgumentNullException . ThrowIfNull ( start ) ;
142
163
143
164
_startHelper = new StartHelper ( start ) ;
165
+ #if TARGET_OSX
166
+ _startHelper . _thread = this ;
167
+ #endif
144
168
145
169
Initialize ( ) ;
146
170
}
@@ -152,6 +176,9 @@ public Thread(ParameterizedThreadStart start, int maxStackSize)
152
176
ArgumentOutOfRangeException . ThrowIfNegative ( maxStackSize ) ;
153
177
154
178
_startHelper = new StartHelper ( start ) { _maxStackSize = maxStackSize } ;
179
+ #if TARGET_OSX
180
+ _startHelper . _thread = this ;
181
+ #endif
155
182
156
183
Initialize ( ) ;
157
184
}
0 commit comments