//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// UserRTX.NET satellite library | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using System;
using System.Threading;
using System.Globalization;

#if !WASM
namespace user_rtx { using u = user_rtx;
#else
namespace user_rtx.wasm { using u = user_rtx.wasm;
#endif

static partial class thread {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public static Thread Create( ThreadStart start ) =>
   Create( start, null );

public static Thread Create( ThreadStart start, ThreadPriority? priority ) =>
   Create( start, priority, true );

//--------------------------------------------------------------------------------------------------

public static Thread Create( ThreadStart start, bool inheritCultures ) =>
   Create( start, inheritCultures, inheritCultures );

public static Thread Create( ThreadStart start, bool inheritCulture, bool inheritUICulture ) =>
   Create( start, null, inheritCulture, inheritUICulture );

public static Thread Create( ThreadStart start,
                             ThreadPriority? priority,
                             [u.def_true] bool inheritCultures ) =>
   Create( start, priority, inheritCultures, inheritCultures );

public static Thread Create( ThreadStart start,
                             ThreadPriority? priority,
                             [u.def_true] bool inheritCulture,
                             [u.def_true] bool inheritUICulture ) =>
   CreateX( start, priority, inheritCulture, inheritUICulture );

//==================================================================================================

public static Thread Create( ParameterizedThreadStart start ) =>
   Create( start, null );

public static Thread Create( ParameterizedThreadStart start, ThreadPriority? priority ) =>
   Create( start, priority, true );

//--------------------------------------------------------------------------------------------------

public static Thread Create( ParameterizedThreadStart start, bool inheritCultures ) =>
   Create( start, inheritCultures, inheritCultures );

public static Thread Create( ParameterizedThreadStart start,
                             [u.def_true] bool inheritCulture,
                             [u.def_true] bool inheritUICulture ) =>
   Create( start, null, inheritCulture, inheritUICulture );

public static Thread Create( ParameterizedThreadStart start,
                             ThreadPriority? priority,
                             [u.def_true] bool inheritCultures ) =>
   Create( start, priority, inheritCultures, inheritCultures );

public static Thread Create( ParameterizedThreadStart start,
                             ThreadPriority? priority,
                             [u.def_true] bool inheritCulture,
                             [u.def_true] bool inheritUICulture ) =>
   CreateX( start, priority, inheritCulture, inheritUICulture );

//==================================================================================================

static Thread CreateX( Delegate start,
                       ThreadPriority? priority,
                       bool inheritCulture,
                       bool inheritUICulture )
{
   CultureInfo?
      culture = inheritCulture ? Thread.CurrentThread.CurrentCulture : null,
      uiCulture = inheritUICulture ? Thread.CurrentThread.CurrentUICulture : null;
   Action assingCulture = delegate {
      if ( culture != null )
         Thread.CurrentThread.CurrentCulture = culture;
      if ( uiCulture != null )
         Thread.CurrentThread.CurrentUICulture = uiCulture;
   };
   Thread thread;
   if ( start is ThreadStart )
      thread = new Thread( () => {
         using ( new u.MemoryBarrier() ) {
            assingCulture();
            ((ThreadStart)start)();
         }
      });
   else
   if ( start is ParameterizedThreadStart )
      thread = new Thread( (object? parameter) => {
         using ( new u.MemoryBarrier() ) {
            assingCulture();
            ((ParameterizedThreadStart)start)( parameter );
         }
      });
   else
      throw u.assertion_fail.DEAD_BRANCH();
   thread.Priority = priority ?? ThreadPriority.Normal;
   return thread;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // thread

} // user_rtx (.wasm)
