// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using System.Diagnostics; using osu.Framework.Platform; namespace osu.Framework.Development { /// /// Utilities to ensure correct thread usage throughout a game. /// public static class ThreadSafety { /// /// Whether the current code is executing on the input thread. /// [field: ThreadStatic] public static bool IsInputThread { get; internal set; } /// /// Whether the current code is executing on the update thread. /// [field: ThreadStatic] public static bool IsUpdateThread { get; internal set; } /// /// Whether the current code is executing on the draw thread. /// [field: ThreadStatic] public static bool IsDrawThread { get; internal set; } /// /// Whether the current code is executing on the audio thread. /// [field: ThreadStatic] public static bool IsAudioThread { get; internal set; } /// /// Asserts that the current code is executing on the input thread. /// /// /// Only asserts in debug builds due to performance concerns. /// [Conditional("DEBUG")] internal static void EnsureInputThread() => Debug.Assert(IsInputThread); /// /// Asserts that the current code is executing on the update thread. /// /// /// Only asserts in debug builds due to performance concerns. /// [Conditional("DEBUG")] internal static void EnsureUpdateThread() => Debug.Assert(IsUpdateThread); /// /// Asserts that the current code is not executing on the update thread. /// /// /// Only asserts in debug builds due to performance concerns. /// [Conditional("DEBUG")] internal static void EnsureNotUpdateThread() => Debug.Assert(!IsUpdateThread); /// /// Asserts that the current code is executing on the draw thread. /// /// /// Only asserts in debug builds due to performance concerns. /// [Conditional("DEBUG")] internal static void EnsureDrawThread() => Debug.Assert(IsDrawThread); /// /// Asserts that the current code is executing on the audio thread. /// /// /// Only asserts in debug builds due to performance concerns. /// [Conditional("DEBUG")] internal static void EnsureAudioThread() => Debug.Assert(IsAudioThread); /// /// The current execution mode. /// internal static ExecutionMode ExecutionMode; /// /// Resets all statics for the current thread. /// internal static void ResetAllForCurrentThread() { IsInputThread = false; IsUpdateThread = false; IsDrawThread = false; IsAudioThread = false; } } }