Gen2GcCallback.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Diagnostics;
  5. using System.Runtime.ConstrainedExecution;
  6. using System.Runtime.InteropServices;
  7. namespace System
  8. {
  9. /// <summary>
  10. /// Schedules a callback roughly every gen 2 GC (you may see a Gen 0 an Gen 1 but only once)
  11. /// (We can fix this by capturing the Gen 2 count at startup and testing, but I mostly don't care)
  12. /// </summary>
  13. internal sealed class Gen2GcCallback : CriticalFinalizerObject
  14. {
  15. private readonly Func<bool>? _callback0;
  16. private readonly Func<object, bool>? _callback1;
  17. private readonly GCHandle _weakTargetObj;
  18. private Gen2GcCallback(Func<bool> callback)
  19. {
  20. _callback0 = callback;
  21. }
  22. private Gen2GcCallback(Func<object, bool> callback, object targetObj)
  23. {
  24. _callback1 = callback;
  25. _weakTargetObj = GCHandle.Alloc(targetObj, GCHandleType.Weak);
  26. }
  27. /// <summary>
  28. /// Schedule 'callback' to be called in the next GC. If the callback returns true it is
  29. /// rescheduled for the next Gen 2 GC. Otherwise the callbacks stop.
  30. /// </summary>
  31. public static void Register(Func<bool> callback)
  32. {
  33. // Create a unreachable object that remembers the callback function and target object.
  34. new Gen2GcCallback(callback);
  35. }
  36. /// <summary>
  37. /// Schedule 'callback' to be called in the next GC. If the callback returns true it is
  38. /// rescheduled for the next Gen 2 GC. Otherwise the callbacks stop.
  39. ///
  40. /// NOTE: This callback will be kept alive until either the callback function returns false,
  41. /// or the target object dies.
  42. /// </summary>
  43. public static void Register(Func<object, bool> callback, object targetObj)
  44. {
  45. // Create a unreachable object that remembers the callback function and target object.
  46. new Gen2GcCallback(callback, targetObj);
  47. }
  48. ~Gen2GcCallback()
  49. {
  50. if (_weakTargetObj.IsAllocated)
  51. {
  52. // Check to see if the target object is still alive.
  53. object? targetObj = _weakTargetObj.Target;
  54. if (targetObj == null)
  55. {
  56. // The target object is dead, so this callback object is no longer needed.
  57. _weakTargetObj.Free();
  58. return;
  59. }
  60. // Execute the callback method.
  61. try
  62. {
  63. Debug.Assert(_callback1 != null);
  64. if (!_callback1(targetObj))
  65. {
  66. // If the callback returns false, this callback object is no longer needed.
  67. return;
  68. }
  69. }
  70. catch
  71. {
  72. // Ensure that we still get a chance to resurrect this object, even if the callback throws an exception.
  73. #if DEBUG
  74. // Except in DEBUG, as we really shouldn't be hitting any exceptions here.
  75. throw;
  76. #endif
  77. }
  78. }
  79. else
  80. {
  81. // Execute the callback method.
  82. try
  83. {
  84. Debug.Assert(_callback0 != null);
  85. if (!_callback0())
  86. {
  87. // If the callback returns false, this callback object is no longer needed.
  88. return;
  89. }
  90. }
  91. catch
  92. {
  93. // Ensure that we still get a chance to resurrect this object, even if the callback throws an exception.
  94. #if DEBUG
  95. // Except in DEBUG, as we really shouldn't be hitting any exceptions here.
  96. throw;
  97. #endif
  98. }
  99. }
  100. // Resurrect ourselves by re-registering for finalization.
  101. GC.ReRegisterForFinalize(this);
  102. }
  103. }
  104. }