ConcurrencyAbstractionLayer.cs 3.4 KB

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