Added Pacore
This commit is contained in:
44
Assets/Pacore/Runtime/Threading/ThreadEnforcer.cs
Normal file
44
Assets/Pacore/Runtime/Threading/ThreadEnforcer.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading;
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
namespace PashaBibko.Pacore.Threading
|
||||
{
|
||||
public static partial class ThreadSafe
|
||||
{
|
||||
private static SynchronizationContext MainThreadContext { get; set; }
|
||||
|
||||
public class IncorrectThreadException : Exception
|
||||
{
|
||||
public IncorrectThreadException(string message)
|
||||
: base(message)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
|
||||
private static void CaptureMainThreadContext()
|
||||
{
|
||||
MainThreadContext = SynchronizationContext.Current;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void EnforceMainThread()
|
||||
{
|
||||
if (SynchronizationContext.Current != MainThreadContext)
|
||||
{
|
||||
throw new IncorrectThreadException("Main thread function was called on a background thread");
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void EnforceBackgroundThread()
|
||||
{
|
||||
if (SynchronizationContext.Current == MainThreadContext)
|
||||
{
|
||||
throw new IncorrectThreadException("Background thread function was called on the main thread");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user