aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Common/Memory/PartialUnmaps/NativeReaderWriterLock.cs
diff options
context:
space:
mode:
authorTSR Berry <20988865+TSRBerry@users.noreply.github.com>2023-04-08 01:22:00 +0200
committerMary <thog@protonmail.com>2023-04-27 23:51:14 +0200
commitcee712105850ac3385cd0091a923438167433f9f (patch)
tree4a5274b21d8b7f938c0d0ce18736d3f2993b11b1 /Ryujinx.Common/Memory/PartialUnmaps/NativeReaderWriterLock.cs
parentcd124bda587ef09668a971fa1cac1c3f0cfc9f21 (diff)
Move solution and projects to src
Diffstat (limited to 'Ryujinx.Common/Memory/PartialUnmaps/NativeReaderWriterLock.cs')
-rw-r--r--Ryujinx.Common/Memory/PartialUnmaps/NativeReaderWriterLock.cs80
1 files changed, 0 insertions, 80 deletions
diff --git a/Ryujinx.Common/Memory/PartialUnmaps/NativeReaderWriterLock.cs b/Ryujinx.Common/Memory/PartialUnmaps/NativeReaderWriterLock.cs
deleted file mode 100644
index 78eeb16f..00000000
--- a/Ryujinx.Common/Memory/PartialUnmaps/NativeReaderWriterLock.cs
+++ /dev/null
@@ -1,80 +0,0 @@
-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 int WriteLockOffset;
- public static int ReaderCountOffset;
-
- /// <summary>
- /// Populates the field offsets for use when emitting native code.
- /// </summary>
- static NativeReaderWriterLock()
- {
- NativeReaderWriterLock instance = new NativeReaderWriterLock();
-
- 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);
- }
- }
-}