@@ -71,9 +71,7 @@ public partial class Thread
71
71
#pragma warning restore 169 , 414 , 649
72
72
73
73
private string ? _name ;
74
- private Delegate ? m_start ;
75
- private object ? m_start_arg ;
76
- private CultureInfo ? culture , ui_culture ;
74
+ private StartHelper ? _startHelper ;
77
75
internal ExecutionContext ? _executionContext ;
78
76
internal SynchronizationContext ? _synchronizationContext ;
79
77
@@ -175,90 +173,6 @@ public ThreadPriority Priority
175
173
176
174
public ThreadState ThreadState => GetState ( this ) ;
177
175
178
- public Thread ( ThreadStart start )
179
- : this ( )
180
- {
181
- if ( start == null )
182
- {
183
- throw new ArgumentNullException ( nameof ( start ) ) ;
184
- }
185
-
186
- Create ( start ) ;
187
- }
188
-
189
- public Thread ( ThreadStart start , int maxStackSize )
190
- : this ( )
191
- {
192
- if ( start == null )
193
- {
194
- throw new ArgumentNullException ( nameof ( start ) ) ;
195
- }
196
- if ( maxStackSize < 0 )
197
- {
198
- throw new ArgumentOutOfRangeException ( nameof ( maxStackSize ) , SR . ArgumentOutOfRange_NeedNonNegNum ) ;
199
- }
200
-
201
- Create ( start , maxStackSize ) ;
202
- }
203
-
204
- public Thread ( ParameterizedThreadStart start )
205
- : this ( )
206
- {
207
- if ( start == null )
208
- {
209
- throw new ArgumentNullException ( nameof ( start ) ) ;
210
- }
211
-
212
- Create ( start ) ;
213
- }
214
-
215
- public Thread ( ParameterizedThreadStart start , int maxStackSize )
216
- : this ( )
217
- {
218
- if ( start == null )
219
- {
220
- throw new ArgumentNullException ( nameof ( start ) ) ;
221
- }
222
- if ( maxStackSize < 0 )
223
- {
224
- throw new ArgumentOutOfRangeException ( nameof ( maxStackSize ) , SR . ArgumentOutOfRange_NeedNonNegNum ) ;
225
- }
226
-
227
- Create ( start , maxStackSize ) ;
228
- }
229
-
230
- private void RequireCurrentThread ( )
231
- {
232
- if ( this != CurrentThread )
233
- {
234
- throw new InvalidOperationException ( SR . Thread_Operation_RequiresCurrentThread ) ;
235
- }
236
- }
237
-
238
- private void SetCultureOnUnstartedThread ( CultureInfo value , bool uiCulture )
239
- {
240
- if ( value == null )
241
- {
242
- throw new ArgumentNullException ( nameof ( value ) ) ;
243
- }
244
- if ( ( ThreadState & ThreadState . Unstarted ) == 0 )
245
- {
246
- throw new InvalidOperationException ( SR . Thread_Operation_RequiresCurrentThread ) ;
247
- }
248
- if ( uiCulture )
249
- ui_culture = value ;
250
- else
251
- culture = value ;
252
- }
253
-
254
- private void Create ( ThreadStart start ) => SetStartHelper ( ( Delegate ) start , 0 ) ; // 0 will setup Thread with default stackSize
255
-
256
- private void Create ( ThreadStart start , int maxStackSize ) => SetStartHelper ( ( Delegate ) start , maxStackSize ) ;
257
-
258
- private void Create ( ParameterizedThreadStart start ) => SetStartHelper ( ( Delegate ) start , 0 ) ;
259
-
260
- private void Create ( ParameterizedThreadStart start , int maxStackSize ) => SetStartHelper ( ( Delegate ) start , maxStackSize ) ;
261
-
262
176
public ApartmentState GetApartmentState ( ) => ApartmentState . Unknown ;
263
177
264
178
public void DisableComObjectEagerCleanup ( )
@@ -291,10 +205,12 @@ public bool Join(int millisecondsTimeout)
291
205
return JoinInternal ( this , millisecondsTimeout ) ;
292
206
}
293
207
294
- private void SetStartHelper ( Delegate start , int maxStackSize )
208
+ private void Initialize ( )
295
209
{
296
- m_start = start ;
297
- stack_size = maxStackSize ;
210
+ InitInternal ( this ) ;
211
+
212
+ // TODO: This can go away once the mono/mono mirror is disabled
213
+ stack_size = _startHelper ! . _maxStackSize ;
298
214
}
299
215
300
216
public static void SpinWait ( int iterations )
@@ -316,103 +232,28 @@ public static void Sleep(int millisecondsTimeout)
316
232
317
233
internal static void UninterruptibleSleep0 ( ) => SleepInternal ( 0 , false ) ;
318
234
319
- #if ! TARGET_BROWSER
320
- internal const bool IsThreadStartSupported = true ;
321
-
322
- [ UnsupportedOSPlatform ( "browser" ) ]
323
- public void Start ( )
324
- {
325
- _executionContext = ExecutionContext . Capture ( ) ;
326
- StartInternal ( this ) ;
327
- }
328
-
329
- [ UnsupportedOSPlatform ( "browser" ) ]
330
- public void Start ( object parameter )
331
- {
332
- if ( m_start is ThreadStart )
333
- throw new InvalidOperationException ( SR . InvalidOperation_ThreadWrongThreadStart ) ;
334
-
335
- m_start_arg = parameter ;
336
- Start ( ) ;
337
- }
338
-
339
- [ UnsupportedOSPlatform ( "browser" ) ]
340
- internal void UnsafeStart ( )
341
- {
342
- StartInternal ( this ) ;
343
- }
344
-
345
- [ UnsupportedOSPlatform ( "browser" ) ]
346
- internal void UnsafeStart ( object parameter )
347
- {
348
- Debug . Assert ( m_start is ThreadStart ) ;
349
-
350
- m_start_arg = parameter ;
351
- UnsafeStart ( ) ;
352
- }
353
-
354
235
// Called from the runtime
355
236
internal void StartCallback ( )
356
237
{
357
- ExecutionContext ? context = _executionContext ;
358
- _executionContext = null ;
359
- if ( context != null && ! context . IsDefault )
360
- {
361
- ExecutionContext . RunInternal ( context , s_threadStartContextCallback , this ) ;
362
- }
363
- else
364
- {
365
- StartCallbackWorker ( ) ;
366
- }
367
- }
368
-
369
- private static readonly ContextCallback s_threadStartContextCallback = new ContextCallback ( StartCallback_Context ) ;
370
-
371
- private static void StartCallback_Context ( object ? state )
372
- {
373
- Debug . Assert ( state is Thread ) ;
374
- ( ( Thread ) state ) . StartCallbackWorker ( ) ;
375
- }
376
-
377
- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ] // otherwise an unnecessary long-lived stack frame in many threads
378
- private void StartCallbackWorker ( )
379
- {
380
- if ( culture != null )
381
- {
382
- CultureInfo . CurrentCulture = culture ;
383
- culture = null ;
384
- }
238
+ StartHelper ? startHelper = _startHelper ;
239
+ Debug . Assert ( startHelper != null ) ;
240
+ _startHelper = null ;
385
241
386
- if ( ui_culture != null )
387
- {
388
- CultureInfo . CurrentUICulture = ui_culture ;
389
- ui_culture = null ;
390
- }
391
-
392
- if ( m_start is ThreadStart del )
393
- {
394
- m_start = null ;
395
- del ( ) ;
396
- }
397
- else
398
- {
399
- Debug . Assert ( m_start is ParameterizedThreadStart ) ;
400
- var pdel = ( ParameterizedThreadStart ) m_start ! ;
401
- object ? arg = m_start_arg ;
402
- m_start = null ;
403
- m_start_arg = null ;
404
- pdel ( arg ) ;
405
- }
242
+ startHelper . Run ( ) ;
406
243
}
407
244
408
245
// Called from the runtime
409
246
internal static void ThrowThreadStartException ( Exception ex ) => throw new ThreadStartException ( ex ) ;
410
247
248
+ private void StartCore ( )
249
+ {
250
+ StartInternal ( this ) ;
251
+ }
252
+
411
253
[ DynamicDependency ( nameof ( StartCallback ) ) ]
412
254
[ DynamicDependency ( nameof ( ThrowThreadStartException ) ) ]
413
255
[ MethodImplAttribute ( MethodImplOptions . InternalCall ) ]
414
256
private static extern void StartInternal ( Thread runtime_thread ) ;
415
- #endif
416
257
417
258
partial void ThreadNameChanged ( string ? value )
418
259
{
0 commit comments