Ryujinx/src/Ryujinx.Common/Memory/PartialUnmaps/ThreadLocalMap.cs
TSRBerry fc20d9b925
[Ryujinx.Common] Address dotnet-format issues (#5358)
* dotnet format style --severity info

Some changes were manually reverted.

* dotnet format analyzers --serverity info

Some changes have been minimally adapted.

* Restore a few unused methods and variables

* Silence dotnet format IDE0060 warnings

* Silence dotnet format IDE0059 warnings

* Address or silence dotnet format IDE1006 warnings

* Address dotnet format CA1816 warnings

* Address or silence dotnet format CA2211 warnings

* Silence CA1806 and CA1834 issues

* Fix formatting for switch expressions

* Address most dotnet format whitespace warnings

* Apply dotnet format whitespace formatting

A few of them have been manually reverted and the corresponding warning was silenced

* Revert formatting changes for while and for-loops

* Format if-blocks correctly

* Run dotnet format whitespace after rebase

* Run dotnet format style after rebase

* Run dotnet format analyzers after rebase

* Run dotnet format after rebase and remove unused usings

- analyzers
- style
- whitespace

* Add comments to disabled warnings

* Remove a few unused parameters

* Replace MmeShadowScratch with Array256<uint>

* Simplify properties and array initialization, Use const when possible, Remove trailing commas

* Run dotnet format after rebase

* Address IDE0251 warnings

* Revert "Simplify properties and array initialization, Use const when possible, Remove trailing commas"

This reverts commit 9462e4136c0a2100dc28b20cf9542e06790aa67e.

* dotnet format whitespace after rebase

* First dotnet format pass

* Second dotnet format pass

* Fix build issues

* Fix StructArrayHelpers.cs

* Apply suggestions from code review

Co-authored-by: Ac_K <Acoustik666@gmail.com>

* Fix return statements

* Fix naming rule violations

* Update src/Ryujinx.Common/Utilities/StreamUtils.cs

Co-authored-by: Ac_K <Acoustik666@gmail.com>

* Add trailing commas

* Address review feedback

* Address review feedback

* Rename remaining type parameters to TKey and TValue

* Fix manual formatting for logging levels

* Fix spacing before comments

---------

Co-authored-by: Ac_K <Acoustik666@gmail.com>
2023-06-28 18:41:38 +02:00

91 lines
3 KiB
C#

using System.Runtime.InteropServices;
using System.Threading;
using static Ryujinx.Common.Memory.PartialUnmaps.PartialUnmapHelpers;
namespace Ryujinx.Common.Memory.PartialUnmaps
{
/// <summary>
/// A simple fixed size thread safe map that can be used from native code.
/// Integer thread IDs map to corresponding structs.
/// </summary>
/// <typeparam name="T">The value type for the map</typeparam>
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct ThreadLocalMap<T> where T : unmanaged
{
public const int MapSize = 20;
public Array20<int> ThreadIds;
public Array20<T> Structs;
public static readonly int ThreadIdsOffset;
public static readonly int StructsOffset;
/// <summary>
/// Populates the field offsets for use when emitting native code.
/// </summary>
static ThreadLocalMap()
{
ThreadLocalMap<T> instance = new();
ThreadIdsOffset = OffsetOf(ref instance, ref instance.ThreadIds);
StructsOffset = OffsetOf(ref instance, ref instance.Structs);
}
/// <summary>
/// Gets the index of a given thread ID in the map, or reserves one.
/// When reserving a struct, its value is set to the given initial value.
/// Returns -1 when there is no space to reserve a new entry.
/// </summary>
/// <param name="threadId">Thread ID to use as a key</param>
/// <param name="initial">Initial value of the associated struct.</param>
/// <returns>The index of the entry, or -1 if none</returns>
public int GetOrReserve(int threadId, T initial)
{
// Try get a match first.
for (int i = 0; i < MapSize; i++)
{
int compare = Interlocked.CompareExchange(ref ThreadIds[i], threadId, threadId);
if (compare == threadId)
{
return i;
}
}
// Try get a free entry. Since the id is assumed to be unique to this thread, we know it doesn't exist yet.
for (int i = 0; i < MapSize; i++)
{
int compare = Interlocked.CompareExchange(ref ThreadIds[i], threadId, 0);
if (compare == 0)
{
Structs[i] = initial;
return i;
}
}
return -1;
}
/// <summary>
/// Gets the struct value for a given map entry.
/// </summary>
/// <param name="index">Index of the entry</param>
/// <returns>A reference to the struct value</returns>
public ref T GetValue(int index)
{
return ref Structs[index];
}
/// <summary>
/// Releases an entry from the map.
/// </summary>
/// <param name="index">Index of the entry to release</param>
public void Release(int index)
{
Interlocked.Exchange(ref ThreadIds[index], 0);
}
}
}