[fix]Config创建默认配置文件的开关Runtime.CreateConfigOnMissing,仅需对自动创建生效,而不应该阻止用户主动Save
智能大石头 编写于 2024-08-09 00:30:41 石头 提交于 2024-08-10 14:22:24
X
namespace System.Threading;

public struct SpinWait
{
	private const int step = 10;

	private const int maxTime = 200;

	private static readonly bool isSingleCpu = Environment.ProcessorCount == 1;

	private int ntime;

	public bool NextSpinWillYield
	{
		get
		{
			if (!isSingleCpu)
			{
				return ntime % 10 == 0;
			}
			return true;
		}
	}

	public int Count => ntime;

	public void SpinOnce()
	{
		ntime++;
		if (isSingleCpu)
		{
			Thread.Sleep(0);
		}
		else if (ntime % 10 == 0)
		{
			Thread.Sleep(0);
		}
		else
		{
			Thread.SpinWait(Math.Min(ntime, 200) << 1);
		}
	}

	public static void SpinUntil(Func<bool> condition)
	{
		SpinWait spinWait = default(SpinWait);
		while (!condition())
		{
			spinWait.SpinOnce();
		}
	}

	public static bool SpinUntil(Func<bool> condition, TimeSpan timeout)
	{
		return SpinUntil(condition, (int)timeout.TotalMilliseconds);
	}

	public static bool SpinUntil(Func<bool> condition, int millisecondsTimeout)
	{
		SpinWait spinWait = default(SpinWait);
		Watch watch = Watch.StartNew();
		while (!condition())
		{
			if (watch.ElapsedMilliseconds > millisecondsTimeout)
			{
				return false;
			}
			spinWait.SpinOnce();
		}
		return true;
	}

	public void Reset()
	{
		ntime = 0;
	}
}