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