Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Atomic Burstable ID Provider #258

Merged
merged 3 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions Scripts/Runtime/Data/IDProviderAtomicBurstable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using Anvil.CSharp.Data;
using System.Threading;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine;

namespace Anvil.Unity.DOTS.Data
{
/// <summary>
/// An <see cref="IDProvider"/> that is compatible with Burst and will get the next ID atomically so it is
/// safe to use in threaded context.
/// </summary>
[BurstCompatible]
public readonly unsafe struct IDProviderAtomicBurstable
mbaker3 marked this conversation as resolved.
Show resolved Hide resolved
mbaker3 marked this conversation as resolved.
Show resolved Hide resolved
{
public const uint DEFAULT_SUPPLY_WARNING_THRESHOLD = uint.MaxValue - 1_000_000;

/// <summary>
/// The threshold to signify the ID supply is near exhaustion
/// </summary>
public readonly uint SupplyWarningThreshold;

[NativeDisableUnsafePtrRestriction] private readonly uint* m_IDPointer;

/// <summary>
/// Creates a new instance of <see cref="IDProviderAtomicBurstable"/>.
/// </summary>
/// <remarks>
/// NOTE: Because this is a struct, it could be created by using the default constructor which will fail.
/// Debug.Asserts will catch this at runtime when used.
/// It is expected to create this struct and pass in an explicit supply warning threshold instead, the
/// <see cref="DEFAULT_SUPPLY_WARNING_THRESHOLD"/> is provided to make this easy to do so.
///
/// ALSO NOTE: Because this must be compatible with Burst, there is no event to signify when the
mbaker3 marked this conversation as resolved.
Show resolved Hide resolved
/// <see cref="SupplyWarningThreshold"/> has been met like in <see cref="IDProvider"/>. Instead, you must
/// poll this struct periodically and call <see cref="HasIDExceededSupplyWarningThreshold"/>.
/// </remarks>
/// <param name="supplyWarningThreshold">The threshold to signify the ID supply is near exhaustion</param>
public IDProviderAtomicBurstable(uint supplyWarningThreshold)
mbaker3 marked this conversation as resolved.
Show resolved Hide resolved
{
m_IDPointer = (uint*)UnsafeUtility.Malloc(
UnsafeUtility.SizeOf<uint>(),
UnsafeUtility.AlignOf<uint>(),
Allocator.Persistent);
UnsafeUtility.MemClear(m_IDPointer, UnsafeUtility.SizeOf<uint>());

SupplyWarningThreshold = supplyWarningThreshold;
}

/// <summary>
/// Provides the next ID atomically
/// </summary>
/// <returns>The next ID to use</returns>
public uint GetNextID()
{
//This will catch if anyone accidentally used the default constructor
Debug.Assert(SupplyWarningThreshold > 0);

return (uint)Interlocked.Add(ref UnsafeUtility.AsRef<int>(m_IDPointer), 1);
}

/// <summary>
/// Returns whether the IDs have passed the warning threshold for supply running out.
/// </summary>
/// <returns>true if IDs have passed the threshold, false if not</returns>
public bool HasIDExceededSupplyWarningThreshold()
{
//This will catch if anyone accidentally used the default constructor
Debug.Assert(SupplyWarningThreshold > 0);

long address = (long)m_IDPointer;
uint currentID = (uint)Interlocked.Read(ref address);
return currentID > SupplyWarningThreshold;
}
}
}
3 changes: 3 additions & 0 deletions Scripts/Runtime/Data/IDProviderAtomicBurstable.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.