ConcurrencyAbstractionLayer.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.ComponentModel;
  5. using System.Reactive.PlatformServices;
  6. namespace System.Reactive.Concurrency
  7. {
  8. /// <summary>
  9. /// (Infrastructure) Concurrency abstraction layer.
  10. /// </summary>
  11. internal static class ConcurrencyAbstractionLayer
  12. {
  13. /// <summary>
  14. /// Gets the current CAL. If no CAL has been set yet, it will be initialized to the default.
  15. /// </summary>
  16. public static IConcurrencyAbstractionLayer Current { get; } = Initialize();
  17. private static IConcurrencyAbstractionLayer Initialize()
  18. {
  19. #pragma warning disable CS0618 // Type or member is obsolete
  20. return PlatformEnlightenmentProvider.Current.GetService<IConcurrencyAbstractionLayer>() ?? new DefaultConcurrencyAbstractionLayer();
  21. #pragma warning restore CS0618 // Type or member is obsolete
  22. }
  23. }
  24. /// <summary>
  25. /// (Infrastructure) Concurrency abstraction layer interface.
  26. /// </summary>
  27. /// <remarks>
  28. /// This type is used by the Rx infrastructure and not meant for public consumption or implementation.
  29. /// No guarantees are made about forward compatibility of the type's functionality and its usage.
  30. /// </remarks>
  31. [EditorBrowsable(EditorBrowsableState.Never)]
  32. public interface IConcurrencyAbstractionLayer
  33. {
  34. /// <summary>
  35. /// Queues a method for execution at the specified relative time.
  36. /// </summary>
  37. /// <param name="action">Method to execute.</param>
  38. /// <param name="state">State to pass to the method.</param>
  39. /// <param name="dueTime">Time to execute the method on.</param>
  40. /// <returns>Disposable object that can be used to stop the timer.</returns>
  41. IDisposable StartTimer(Action<object> action, object state, TimeSpan dueTime);
  42. /// <summary>
  43. /// Queues a method for periodic execution based on the specified period.
  44. /// </summary>
  45. /// <param name="action">Method to execute; should be safe for reentrancy.</param>
  46. /// <param name="period">Period for running the method periodically.</param>
  47. /// <returns>Disposable object that can be used to stop the timer.</returns>
  48. IDisposable StartPeriodicTimer(Action action, TimeSpan period);
  49. /// <summary>
  50. /// Queues a method for execution.
  51. /// </summary>
  52. /// <param name="action">Method to execute.</param>
  53. /// <param name="state">State to pass to the method.</param>
  54. /// <returns>Disposable object that can be used to cancel the queued method.</returns>
  55. IDisposable QueueUserWorkItem(Action<object> action, object state);
  56. /// <summary>
  57. /// Blocking sleep operation.
  58. /// </summary>
  59. /// <param name="timeout">Time to sleep.</param>
  60. void Sleep(TimeSpan timeout);
  61. /// <summary>
  62. /// Starts a new stopwatch object.
  63. /// </summary>
  64. /// <returns>New stopwatch object; started at the time of the request.</returns>
  65. IStopwatch StartStopwatch();
  66. /// <summary>
  67. /// Gets whether long-running scheduling is supported.
  68. /// </summary>
  69. bool SupportsLongRunning
  70. {
  71. get;
  72. }
  73. /// <summary>
  74. /// Starts a new long-running thread.
  75. /// </summary>
  76. /// <param name="action">Method to execute.</param>
  77. /// <param name="state">State to pass to the method.</param>
  78. void StartThread(Action<object> action, object state);
  79. }
  80. }