aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Common/Memory/PartialUnmaps/NativeReaderWriterLock.cs
blob: 17106ae5f6cf7b09bcb2c8f075fc83334b2da248 (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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System.Runtime.InteropServices;
using System.Threading;
using static Ryujinx.Common.Memory.PartialUnmaps.PartialUnmapHelpers;

namespace Ryujinx.Common.Memory.PartialUnmaps
{
    /// <summary>
    /// A simple implementation of a ReaderWriterLock which can be used from native code.
    /// </summary>
    [StructLayout(LayoutKind.Sequential, Pack = 4)]
    public struct NativeReaderWriterLock
    {
        public int WriteLock;
        public int ReaderCount;

        public static readonly int WriteLockOffset;
        public static readonly int ReaderCountOffset;

        /// <summary>
        /// Populates the field offsets for use when emitting native code.
        /// </summary>
        static NativeReaderWriterLock()
        {
            NativeReaderWriterLock instance = new();

            WriteLockOffset = OffsetOf(ref instance, ref instance.WriteLock);
            ReaderCountOffset = OffsetOf(ref instance, ref instance.ReaderCount);
        }

        /// <summary>
        /// Acquires the reader lock.
        /// </summary>
        public void AcquireReaderLock()
        {
            // Must take write lock for a very short time to become a reader.

            while (Interlocked.CompareExchange(ref WriteLock, 1, 0) != 0)
            {
            }

            Interlocked.Increment(ref ReaderCount);

            Interlocked.Exchange(ref WriteLock, 0);
        }

        /// <summary>
        /// Releases the reader lock.
        /// </summary>
        public void ReleaseReaderLock()
        {
            Interlocked.Decrement(ref ReaderCount);
        }

        /// <summary>
        /// Upgrades to a writer lock. The reader lock is temporarily released while obtaining the writer lock.
        /// </summary>
        public void UpgradeToWriterLock()
        {
            // Prevent any more threads from entering reader.
            // If the write lock is already taken, wait for it to not be taken.

            Interlocked.Decrement(ref ReaderCount);

            while (Interlocked.CompareExchange(ref WriteLock, 1, 0) != 0)
            {
            }

            // Wait for reader count to drop to 0, then take the lock again as the only reader.

            while (Interlocked.CompareExchange(ref ReaderCount, 1, 0) != 0)
            {
            }
        }

        /// <summary>
        /// Downgrades from a writer lock, back to a reader one.
        /// </summary>
        public void DowngradeFromWriterLock()
        {
            // Release the WriteLock.

            Interlocked.Exchange(ref WriteLock, 0);
        }
    }
}