You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.1 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using IPA.Logging;
  8. namespace IPA.Utilities
  9. {
  10. /// <summary>
  11. /// Provides utilities for managing various critical sections.
  12. /// </summary>
  13. public static class CriticalSection
  14. {
  15. internal static void Configure()
  16. {
  17. Logger.log.Debug("Configuring exit handlers");
  18. ResetExitHandlers();
  19. }
  20. #region Execute section
  21. private static readonly EventHandler registeredHandler = HandleExit;
  22. internal static void ResetExitHandlers()
  23. {
  24. SetConsoleCtrlHandler(registeredHandler, false);
  25. SetConsoleCtrlHandler(registeredHandler, true);
  26. }
  27. [DllImport("Kernel32")]
  28. private static extern bool SetConsoleCtrlHandler(EventHandler handler, bool add);
  29. private delegate bool EventHandler(CtrlType sig);
  30. private static EventHandler _handler = null;
  31. private static bool HandleExit(CtrlType type)
  32. {
  33. if (_handler != null)
  34. return _handler(type);
  35. return false;
  36. }
  37. enum CtrlType
  38. {
  39. CTRL_C_EVENT = 0,
  40. CTRL_BREAK_EVENT = 1,
  41. CTRL_CLOSE_EVENT = 2,
  42. CTRL_LOGOFF_EVENT = 5,
  43. CTRL_SHUTDOWN_EVENT = 6
  44. }
  45. private static volatile bool exitRecieved = false;
  46. /// <summary>
  47. /// Enters a critical execution section. Does not nest.
  48. /// </summary>
  49. /// <note>
  50. /// During a critical execution section, the program must execute until the end of the section before
  51. /// exiting. If an exit signal is recieved during the section, it will be canceled, and the process
  52. /// will terminate at the end of the section.
  53. /// </note>
  54. public static void EnterExecuteSection()
  55. {
  56. ResetExitHandlers();
  57. exitRecieved = false;
  58. _handler = sig => exitRecieved = true;
  59. }
  60. /// <summary>
  61. /// Exits a critical execution section. Does not nest.
  62. /// </summary>
  63. /// <note>
  64. /// During a critical execution section, the program must execute until the end of the section before
  65. /// exiting. If an exit signal is recieved during the section, it will be canceled, and the process
  66. /// will terminate at the end of the section.
  67. /// </note>
  68. public static void ExitExecuteSection()
  69. {
  70. _handler = null;
  71. if (exitRecieved)
  72. Environment.Exit(1);
  73. }
  74. #endregion
  75. #region GC section
  76. // i wish i could reference GC_enable and GC_disable directly
  77. [DllImport("mono-2.0-bdwgc")]
  78. private static extern void mono_unity_gc_enable();
  79. [DllImport("mono-2.0-bdwgc")]
  80. private static extern void mono_unity_gc_disable();
  81. /// <summary>
  82. /// Enters a GC critical section. Each call to this must be paired with a call to <see cref="ExitGCSection"/>.
  83. /// </summary>
  84. /// <note>
  85. /// During a GC critical section, no GCs will occur.
  86. ///
  87. /// This may throw an <see cref="EntryPointNotFoundException"/> if the build of Mono the game is running on does
  88. /// not have `mono_unity_gc_disable` exported. Use with caution.
  89. /// </note>
  90. public static void EnterGCSection()
  91. {
  92. mono_unity_gc_disable();
  93. }
  94. /// <summary>
  95. /// Exits a GC critical section. Each call to this must have a preceding call to <see cref="EnterGCSection"/>.
  96. /// </summary>
  97. /// <note>
  98. /// During a GC critical section, no GCs will occur.
  99. ///
  100. /// This may throw an <see cref="EntryPointNotFoundException"/> if the build of Mono the game is running on does
  101. /// not have `mono_unity_gc_enable` exported. Use with caution.
  102. /// </note>
  103. public static void ExitGCSection()
  104. {
  105. mono_unity_gc_enable();
  106. }
  107. #endregion
  108. }
  109. }