A game framework written with osu! in mind.
at master 3.5 kB view raw
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. 2// See the LICENCE file in the repository root for full licence text. 3 4using System; 5using System.Diagnostics; 6using osu.Framework.Platform; 7 8namespace osu.Framework.Development 9{ 10 /// <summary> 11 /// Utilities to ensure correct thread usage throughout a game. 12 /// </summary> 13 public static class ThreadSafety 14 { 15 /// <summary> 16 /// Whether the current code is executing on the input thread. 17 /// </summary> 18 [field: ThreadStatic] 19 public static bool IsInputThread { get; internal set; } 20 21 /// <summary> 22 /// Whether the current code is executing on the update thread. 23 /// </summary> 24 [field: ThreadStatic] 25 public static bool IsUpdateThread { get; internal set; } 26 27 /// <summary> 28 /// Whether the current code is executing on the draw thread. 29 /// </summary> 30 [field: ThreadStatic] 31 public static bool IsDrawThread { get; internal set; } 32 33 /// <summary> 34 /// Whether the current code is executing on the audio thread. 35 /// </summary> 36 [field: ThreadStatic] 37 public static bool IsAudioThread { get; internal set; } 38 39 /// <summary> 40 /// Asserts that the current code is executing on the input thread. 41 /// </summary> 42 /// <remarks> 43 /// Only asserts in debug builds due to performance concerns. 44 /// </remarks> 45 [Conditional("DEBUG")] 46 internal static void EnsureInputThread() => Debug.Assert(IsInputThread); 47 48 /// <summary> 49 /// Asserts that the current code is executing on the update thread. 50 /// </summary> 51 /// <remarks> 52 /// Only asserts in debug builds due to performance concerns. 53 /// </remarks> 54 [Conditional("DEBUG")] 55 internal static void EnsureUpdateThread() => Debug.Assert(IsUpdateThread); 56 57 /// <summary> 58 /// Asserts that the current code is not executing on the update thread. 59 /// </summary> 60 /// <remarks> 61 /// Only asserts in debug builds due to performance concerns. 62 /// </remarks> 63 [Conditional("DEBUG")] 64 internal static void EnsureNotUpdateThread() => Debug.Assert(!IsUpdateThread); 65 66 /// <summary> 67 /// Asserts that the current code is executing on the draw thread. 68 /// </summary> 69 /// <remarks> 70 /// Only asserts in debug builds due to performance concerns. 71 /// </remarks> 72 [Conditional("DEBUG")] 73 internal static void EnsureDrawThread() => Debug.Assert(IsDrawThread); 74 75 /// <summary> 76 /// Asserts that the current code is executing on the audio thread. 77 /// </summary> 78 /// <remarks> 79 /// Only asserts in debug builds due to performance concerns. 80 /// </remarks> 81 [Conditional("DEBUG")] 82 internal static void EnsureAudioThread() => Debug.Assert(IsAudioThread); 83 84 /// <summary> 85 /// The current execution mode. 86 /// </summary> 87 internal static ExecutionMode ExecutionMode; 88 89 /// <summary> 90 /// Resets all statics for the current thread. 91 /// </summary> 92 internal static void ResetAllForCurrentThread() 93 { 94 IsInputThread = false; 95 IsUpdateThread = false; 96 IsDrawThread = false; 97 IsAudioThread = false; 98 } 99 } 100}