| 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.Threading; |
| 5 |
using System.Reactive.Disposables; |
| 6 |
|
| 7 |
namespace System.Reactive.Concurrency |
| 8 |
{ |
| 9 |
/// <summary> |
| 10 |
/// Represents an object that schedules units of work on the current thread. |
| 11 |
/// </summary> |
| 12 |
/// <seealso cref="Scheduler.CurrentThread">Singleton instance of this type exposed through this static property.</seealso> |
| 13 |
public sealed class CurrentThreadScheduler : LocalScheduler |
| 14 |
{ |
| 15 |
private static readonly CurrentThreadScheduler s_instance = new CurrentThreadScheduler(); |
| 16 |
|
| 17 |
CurrentThreadScheduler() |
| 18 |
{ |
| 19 |
} |
| 20 |
|
| 21 |
/// <summary> |
| 22 |
/// Gets the singleton instance of the current thread scheduler. |
| 23 |
/// </summary> |
| 24 |
public static CurrentThreadScheduler Instance |
| 25 |
{ |
| 26 |
get { return s_instance; } |
| 27 |
} |
| 28 |
|
| 29 |
#if !NO_TLS |
| 30 |
[ThreadStatic] |
| 31 |
static SchedulerQueue<TimeSpan> s_threadLocalQueue; |
| 32 |
|
| 33 |
[ThreadStatic] |
| 34 |
static IStopwatch s_clock; |
| 35 |
|
| 36 |
private static SchedulerQueue<TimeSpan> GetQueue() |
| 37 |
{ |
| 38 |
return s_threadLocalQueue; |
| 39 |
} |
| 40 |
|
| 41 |
private static void SetQueue(SchedulerQueue<TimeSpan> newQueue) |
| 42 |
{ |
| 43 |
s_threadLocalQueue = newQueue; |
| 44 |
} |
| 45 |
|
| 46 |
private static TimeSpan Time |
| 47 |
{ |
| 48 |
get |
| 49 |
{ |
| 50 |
if (s_clock == null) |
| 51 |
s_clock = ConcurrencyAbstractionLayer.Current.StartStopwatch(); |
| 52 |
|
| 53 |
return s_clock.Elapsed; |
| 54 |
} |
| 55 |
} |
| 56 |
#else |
| 57 |
private static readonly System.Collections.Generic.Dictionary<int, SchedulerQueue<TimeSpan>> s_queues = new System.Collections.Generic.Dictionary<int, SchedulerQueue<TimeSpan>>(); |
| 58 |
|
| 59 |
private static readonly System.Collections.Generic.Dictionary<int, IStopwatch> s_clocks = new System.Collections.Generic.Dictionary<int, IStopwatch>(); |
| 60 |
|
| 61 |
private static SchedulerQueue<TimeSpan> GetQueue() |
| 62 |
{ |
| 63 |
lock (s_queues) |
| 64 |
{ |
| 65 |
var item = default(SchedulerQueue<TimeSpan>); |
| 66 |
if (s_queues.TryGetValue(Thread.CurrentThread.ManagedThreadId, out item)) |
| 67 |
return item; |
| 68 |
|
| 69 |
return null; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
private static void SetQueue(SchedulerQueue<TimeSpan> newQueue) |
| 74 |
{ |
| 75 |
lock (s_queues) |
| 76 |
{ |
| 77 |
if (newQueue == null) |
| 78 |
s_queues.Remove(Thread.CurrentThread.ManagedThreadId); |
| 79 |
else |
| 80 |
s_queues[Thread.CurrentThread.ManagedThreadId] = newQueue; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
private static TimeSpan Time |
| 85 |
{ |
| 86 |
get |
| 87 |
{ |
| 88 |
lock (s_clocks) |
| 89 |
{ |
| 90 |
var clock = default(IStopwatch); |
| 91 |
if (!s_clocks.TryGetValue(Thread.CurrentThread.ManagedThreadId, out clock)) |
| 92 |
s_clocks[Thread.CurrentThread.ManagedThreadId] = clock = ConcurrencyAbstractionLayer.Current.StartStopwatch(); |
| 93 |
|
| 94 |
return clock.Elapsed; |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
#endif |
| 99 |
|
| 100 |
/// <summary> |
| 101 |
/// Gets a value that indicates whether the caller must call a Schedule method. |
| 102 |
/// </summary> |
| 103 |
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "Now marked as obsolete.")] |
| 104 |
[EditorBrowsable(EditorBrowsableState.Never)] |
| 105 |
[Obsolete(Constants_Core.OBSOLETE_SCHEDULEREQUIRED)] // Preferring static method call over instance method call. |
| 106 |
public bool ScheduleRequired |
| 107 |
{ |
| 108 |
get |
| 109 |
{ |
| 110 |
return IsScheduleRequired; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
/// <summary> |
| 115 |
/// Gets a value that indicates whether the caller must call a Schedule method. |
| 116 |
/// </summary> |
| 117 |
[EditorBrowsable(EditorBrowsableState.Advanced)] |
| 118 |
public static bool IsScheduleRequired |
| 119 |
{ |
| 120 |
get |
| 121 |
{ |
| 122 |
return GetQueue() == null; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
/// <summary> |
| 127 |
/// Schedules an action to be executed after dueTime. |
| 128 |
/// </summary> |
| 129 |
/// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam> |
| 130 |
/// <param name="state">State passed to the action to be executed.</param> |
| 131 |
/// <param name="action">Action to be executed.</param> |
| 132 |
/// <param name="dueTime">Relative time after which to execute the action.</param> |
| 133 |
/// <returns>The disposable object used to cancel the scheduled action (best effort).</returns> |
| 134 |
/// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception> |
| 135 |
public override IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action) |
| 136 |
{ |
| 137 |
if (action == null) |
| 138 |
throw new ArgumentNullException("action"); |
| 139 |
|
| 140 |
var dt = Time + Scheduler.Normalize(dueTime); |
| 141 |
|
| 142 |
var si = new ScheduledItem<TimeSpan, TState>(this, state, action, dt); |
| 143 |
|
| 144 |
var queue = GetQueue(); |
| 145 |
|
| 146 |
if (queue == null) |
| 147 |
{ |
| 148 |
queue = new SchedulerQueue<TimeSpan>(4); |
| 149 |
queue.Enqueue(si); |
| 150 |
|
| 151 |
CurrentThreadScheduler.SetQueue(queue); |
| 152 |
try |
| 153 |
{ |
| 154 |
Trampoline.Run(queue); |
| 155 |
} |
| 156 |
finally |
| 157 |
{ |
| 158 |
CurrentThreadScheduler.SetQueue(null); |
| 159 |
} |
| 160 |
} |
| 161 |
else |
| 162 |
{ |
| 163 |
queue.Enqueue(si); |
| 164 |
} |
| 165 |
|
| 166 |
return Disposable.Create(si.Cancel); |
| 167 |
} |
| 168 |
|
| 169 |
static class Trampoline |
| 170 |
{ |
| 171 |
public static void Run(SchedulerQueue<TimeSpan> queue) |
| 172 |
{ |
| 173 |
while (queue.Count > 0) |
| 174 |
{ |
| 175 |
var item = queue.Dequeue(); |
| 176 |
if (!item.IsCanceled) |
| 177 |
{ |
| 178 |
var wait = item.DueTime - CurrentThreadScheduler.Time; |
| 179 |
if (wait.Ticks > 0) |
| 180 |
{ |
| 181 |
ConcurrencyAbstractionLayer.Current.Sleep(wait); |
| 182 |
} |
| 183 |
|
| 184 |
if (!item.IsCanceled) |
| 185 |
item.Invoke(); |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
} |