ConcurrencyAbstractionLayer.cs 3.6 KB

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