diff --git a/Assets/Pacore/Runtime/Threading/ThreadEnforcer.cs b/Assets/Pacore/Runtime/Threading/ThreadEnforcer.cs new file mode 100644 index 0000000..bd30802 --- /dev/null +++ b/Assets/Pacore/Runtime/Threading/ThreadEnforcer.cs @@ -0,0 +1,43 @@ +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"); + } + } + } +} \ No newline at end of file diff --git a/Assets/Pacore/Runtime/Threading/ThreadEnforcer.cs.meta b/Assets/Pacore/Runtime/Threading/ThreadEnforcer.cs.meta new file mode 100644 index 0000000..36dd31c --- /dev/null +++ b/Assets/Pacore/Runtime/Threading/ThreadEnforcer.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: a4aad0735c2147beb2b087872fe4c364 +timeCreated: 1769347995 \ No newline at end of file diff --git a/Assets/Pacore/Runtime/Threading/ThreadSafeTry.cs b/Assets/Pacore/Runtime/Threading/ThreadSafeTry.cs index 3f08bff..4b676ce 100644 --- a/Assets/Pacore/Runtime/Threading/ThreadSafeTry.cs +++ b/Assets/Pacore/Runtime/Threading/ThreadSafeTry.cs @@ -5,7 +5,7 @@ using System; namespace PashaBibko.Pacore.Threading { - public static class ThreadSafe + public static partial class ThreadSafe { [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Try([NotNull] Action action, Action final = null) diff --git a/Assets/TestMonoBehaviour.cs b/Assets/TestMonoBehaviour.cs index afe07f1..1d9167b 100644 --- a/Assets/TestMonoBehaviour.cs +++ b/Assets/TestMonoBehaviour.cs @@ -1,6 +1,8 @@ +using System; using System.Threading; using PashaBibko.Pacore.Attributes; using PashaBibko.Pacore.DevTools; +using PashaBibko.Pacore.Threading; using UnityEngine; [CreateInstanceOnStart] public class TestMonoBehaviour : MonoBehaviour @@ -22,11 +24,13 @@ using UnityEngine; [InspectorCallable("Test button")] public void LogTestValue() { - Debug.Log($"Test value [{Test}]"); - } - - [InspectorCallable("Other Test button")] public void DontLogTestValue() - { - Debug.Log("Test"); + try + { + ThreadSafe.EnforceBackgroundThread(); + } + catch (Exception err) + { + Debug.LogError(err.Message); + } } }