aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Graphics.GAL/Multithreading/ThreadedHelpers.cs
blob: 7ddb19e623ec21db584917eec12f5ce4a0407d13 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System;
using System.Threading;

namespace Ryujinx.Graphics.GAL.Multithreading
{
    static class ThreadedHelpers
    {
        public static void SpinUntilNonNull<T>(ref T obj) where T : class
        {
            Span<SpinWait> spinWait = stackalloc SpinWait[1];

            while (obj == null)
            {
                spinWait[0].SpinOnce(-1);
            }
        }

        public static void SpinUntilExchange(ref int target, int value, int comparand)
        {
            Span<SpinWait> spinWait = stackalloc SpinWait[1];

            while (Interlocked.CompareExchange(ref target, value, comparand) != comparand)
            {
                spinWait[0].SpinOnce(-1);
            }
        }
    }
}