ConcurrencyAbstractionLayer.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. return PlatformEnlightenmentProvider.Current.GetService<IConcurrencyAbstractionLayer>() ?? new DefaultConcurrencyAbstractionLayer();
  20. }
  21. }
  22. /// <summary>
  23. /// (Infrastructure) Concurrency abstraction layer interface.
  24. /// </summary>
  25. /// <remarks>
  26. /// This type is used by the Rx infrastructure and not meant for public consumption or implementation.
  27. /// No guarantees are made about forward compatibility of the type's functionality and its usage.
  28. /// </remarks>
  29. [EditorBrowsable(EditorBrowsableState.Never)]
  30. public interface IConcurrencyAbstractionLayer
  31. {
  32. /// <summary>
  33. /// Queues a method for execution at the specified relative time.
  34. /// </summary>
  35. /// <param name="action">Method to execute.</param>
  36. /// <param name="state">State to pass to the method.</param>
  37. /// <param name="dueTime">Time to execute the method on.</param>
  38. /// <returns>Disposable object that can be used to stop the timer.</returns>
  39. IDisposable StartTimer(Action<object> action, object state, TimeSpan dueTime);
  40. /// <summary>
  41. /// Queues a method for periodic execution based on the specified period.
  42. /// </summary>
  43. /// <param name="action">Method to execute; should be safe for reentrancy.</param>
  44. /// <param name="period">Period for running the method periodically.</param>
  45. /// <returns>Disposable object that can be used to stop the timer.</returns>
  46. IDisposable StartPeriodicTimer(Action action, TimeSpan period);
  47. /// <summary>
  48. /// Queues a method for execution.
  49. /// </summary>
  50. /// <param name="action">Method to execute.</param>
  51. /// <param name="state">State to pass to the method.</param>
  52. /// <returns>Disposable object that can be used to cancel the queued method.</returns>
  53. IDisposable QueueUserWorkItem(Action<object> action, object state);
  54. /// <summary>
  55. /// Blocking sleep operation.
  56. /// </summary>
  57. /// <param name="timeout">Time to sleep.</param>
  58. void Sleep(TimeSpan timeout);
  59. /// <summary>
  60. /// Starts a new stopwatch object.
  61. /// </summary>
  62. /// <returns>New stopwatch object; started at the time of the request.</returns>
  63. IStopwatch StartStopwatch();
  64. /// <summary>
  65. /// Gets whether long-running scheduling is supported.
  66. /// </summary>
  67. bool SupportsLongRunning
  68. {
  69. get;
  70. }
  71. /// <summary>
  72. /// Starts a new long-running thread.
  73. /// </summary>
  74. /// <param name="action">Method to execute.</param>
  75. /// <param name="state">State to pass to the method.</param>
  76. void StartThread(Action<object> action, object state);
  77. }
  78. }