Search code examples
c#.netasp.net-coreportable-class-library.net-core

Spinwait for a number of spins in Portable Class Library


I'm looking to replace the SpinWait method in a PCL targeting ASPNET Core, .NET 4.5.2, and Android+iOS via Xamarin but can't find a direct equivalent. At the moment I'm looking at replacing

System.Threading.Thread.SpinWait(10);

with

SpinWait spinWait = new SpinWait();
SpinWait.SpinUntil(() => spinWait.Count >= 10);

but I have no idea if I'm opening a can of worms by doing. or if maybe

for(int i=0; i<10; i++) SpinOnce();

is better. I had avoided this because SpinOnce() yields after every spin so seems likely to be less efficient.

Context:

I'm currently porting this high-precision timer to a PCL (ASPNET Core, .NET 4.5.2, and Android+iOS via Xamarin) and coming up against an issue with the SpinWait method removal. http://www.codeproject.com/Articles/98346/Microsecond-and-Millisecond-NET-Timer

In the .NET Framework proper there is a method SpinWait() which accepts an int which dictates how many spins to wait for (https://msdn.microsoft.com/en-us/library/system.threading.thread.spinwait(v=vs.110).aspx) but the System.Threading.Thread namespace isn't available when targeting this combination of frameworks but does include a SpinWait struct (https://msdn.microsoft.com/en-us/library/system.threading.spinwait(v=vs.110).aspx) which gives me access to SpinOnce() and a SpinUntil() method which accepts a boolean-returning function.


Solution

  • Turns out my intuition about avoiding yielding was not entirely correct and have discovered that in the source of SpinUntil (http://referencesource.microsoft.com/#mscorlib/system/threading/SpinWait.cs) there is actually a repeated call to SpinOnce().

    Without the error checking logic it looks like this:

    //Error Checking Logic
    SpinWait spinner = new SpinWait();
            while (!condition())
            {
                //Timeout checks
                spinner.SpinOnce();
                //Timeout and Yielding checks
                }
            }
    //Return Logic
    

    I am therefore taking this as an acceptable example of implementation and will be using (unless a better solution presents itself) something similar to the below to recreate the desired behaviour.

    SpinWait spinner = new SpinWait();
        while (spinner.Count<10)
        {
            spinner.SpinOnce();
            //Timeout and Yielding checks
            }
        }