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

#if !WASM
namespace CommonHelpers {
#else
namespace CommonHelpers.WASM {
#endif

//**************************************************************************************************
/// <summary>
/// </summary>
partial class HThread {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public static Thread Create(ThreadStart start)
{
   return Create( start, null );
}

public static Thread Create(ThreadStart start,[HOptional] ThreadPriority? priority)
{
   return Create( start, priority, true );
}

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

public static Thread Create(ThreadStart start,bool inheritCultures)
{
   return Create( start, inheritCultures, inheritCultures );
}

public static Thread Create(ThreadStart start,bool inheritCulture,bool inheritUICulture)
{
   return Create( start, null, inheritCulture, inheritUICulture );
}

public static Thread Create( ThreadStart start,
                             [HOptional] ThreadPriority? priority,
                             bool inheritCultures )
{
   return Create( start, priority, inheritCultures, inheritCultures );
}

public static Thread Create( ThreadStart start,
                             [HOptional] ThreadPriority? priority,
                             bool inheritCulture,
                             bool inheritUICulture )
{
   return CreateEx( start, priority, inheritCulture, inheritUICulture );
}

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

public static Thread Create(ParameterizedThreadStart start)
{
   return Create( start, null );
}

public static Thread Create(ParameterizedThreadStart start,[HOptional] ThreadPriority? priority)
{
   return Create( start, priority, true );
}

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

public static Thread Create(ParameterizedThreadStart start,bool inheritCultures)
{
   return Create( start, inheritCultures, inheritCultures );
}

public static Thread Create( ParameterizedThreadStart start,
                             bool inheritCulture,
                             bool inheritUICulture )
{
   return Create( start, null, inheritCulture, inheritUICulture );
}

public static Thread Create( ParameterizedThreadStart start,
                             [HOptional] ThreadPriority? priority,
                             bool inheritCultures )
{
   return Create( start, priority, inheritCultures, inheritCultures );
}

public static Thread Create( ParameterizedThreadStart start,
                             [HOptional] ThreadPriority? priority,
                             bool inheritCulture,
                             bool inheritUICulture )
{
   return CreateEx( start, priority, inheritCulture, inheritUICulture );
}

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

static Thread CreateEx
(
   Delegate start,
   ThreadPriority? priority,
   bool inheritCulture,
   bool inheritUICulture
)
{
   HArgChecking.VerifyNotNull( start );
   Thread thread;
   CultureInfo
      culture = inheritCulture ? Thread.CurrentThread.CurrentCulture : null,
      uiCulture = inheritUICulture ? Thread.CurrentThread.CurrentUICulture : null;
   if ( start is ThreadStart )
      thread = new Thread( delegate (){
         using (new HMemoryBarrier())
         {
            if (culture!=null) Thread.CurrentThread.CurrentCulture = culture;
            if (uiCulture!=null) Thread.CurrentThread.CurrentUICulture = uiCulture;
            ((ThreadStart)start)();
         }
      });
   else
   if ( start is ParameterizedThreadStart )
      thread = new Thread( delegate (object parameter){
         using (new HMemoryBarrier())
         {
            if (culture!=null) Thread.CurrentThread.CurrentCulture = culture;
            if (uiCulture!=null) Thread.CurrentThread.CurrentUICulture = uiCulture;
            ((ParameterizedThreadStart)start)( parameter );
         }
      });
   else
      throw new HArgumentTypeException();
   thread.Priority = priority ?? ThreadPriority.Normal;
   return thread;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // HThread
//**************************************************************************************************

} // CommonHelpers (.WASM)
