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