summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/System/Threading/Timer.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/mscorlib/src/System/Threading/Timer.cs')
-rw-r--r--src/mscorlib/src/System/Threading/Timer.cs147
1 files changed, 12 insertions, 135 deletions
diff --git a/src/mscorlib/src/System/Threading/Timer.cs b/src/mscorlib/src/System/Threading/Timer.cs
index 5bfefccad2..93d2922799 100644
--- a/src/mscorlib/src/System/Threading/Timer.cs
+++ b/src/mscorlib/src/System/Threading/Timer.cs
@@ -8,7 +8,6 @@ namespace System.Threading
{
using System;
using System.Security;
- using System.Security.Permissions;
using Microsoft.Win32;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@@ -21,7 +20,6 @@ namespace System.Threading
- [System.Runtime.InteropServices.ComVisible(true)]
public delegate void TimerCallback(Object state);
//
@@ -109,7 +107,6 @@ namespace System.Threading
{
}
- [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
protected override bool ReleaseHandle()
{
return DeleteAppDomainTimer(handle);
@@ -206,7 +203,6 @@ namespace System.Threading
[DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
[SuppressUnmanagedCodeSecurity]
- [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
static extern bool DeleteAppDomainTimer(IntPtr handle);
#endregion
@@ -221,83 +217,6 @@ namespace System.Threading
volatile int m_pauseTicks = 0; // Time when Pause was called
- internal void Pause()
- {
- lock(this)
- {
- // Delete the native timer so that no timers are fired in the Pause zone
- if(m_appDomainTimer != null && !m_appDomainTimer.IsInvalid)
- {
- m_appDomainTimer.Dispose();
- m_appDomainTimer = null;
- m_isAppDomainTimerScheduled = false;
- m_pauseTicks = TickCount;
- }
- }
- }
-
- internal void Resume()
- {
- //
- // Update timers to adjust their due-time to accomodate Pause/Resume
- //
- lock (this)
- {
- // prevent ThreadAbort while updating state
- try { }
- finally
- {
- int pauseTicks = m_pauseTicks;
- m_pauseTicks = 0; // Set this to 0 so that now timers can be scheduled
-
- int resumedTicks = TickCount;
- int pauseDuration = resumedTicks - pauseTicks;
-
- bool haveTimerToSchedule = false;
- uint nextAppDomainTimerDuration = uint.MaxValue;
-
- TimerQueueTimer timer = m_timers;
- while (timer != null)
- {
- Debug.Assert(timer.m_dueTime != Timeout.UnsignedInfinite);
- Debug.Assert(resumedTicks >= timer.m_startTicks);
-
- uint elapsed; // How much of the timer dueTime has already elapsed
-
- // Timers started before the paused event has to be sufficiently delayed to accomodate
- // for the Pause time. However, timers started after the Paused event shouldnt be adjusted.
- // E.g. ones created by the app in its Activated event should fire when it was designated.
- // The Resumed event which is where this routine is executing is after this Activated and hence
- // shouldn't delay this timer
-
- if(timer.m_startTicks <= pauseTicks)
- elapsed = (uint)(pauseTicks - timer.m_startTicks);
- else
- elapsed = (uint)(resumedTicks - timer.m_startTicks);
-
- // Handling the corner cases where a Timer was already due by the time Resume is happening,
- // We shouldn't delay those timers.
- // Example is a timer started in App's Activated event with a very small duration
- timer.m_dueTime = (timer.m_dueTime > elapsed) ? timer.m_dueTime - elapsed : 0;;
- timer.m_startTicks = resumedTicks; // re-baseline
-
- if (timer.m_dueTime < nextAppDomainTimerDuration)
- {
- haveTimerToSchedule = true;
- nextAppDomainTimerDuration = timer.m_dueTime;
- }
-
- timer = timer.m_next;
- }
-
- if (haveTimerToSchedule)
- {
- EnsureAppDomainTimerFiresBy(nextAppDomainTimerDuration);
- }
- }
- }
- }
-
//
// Fire any timers that have expired, and update the native timer to schedule the rest of them.
@@ -512,19 +431,13 @@ namespace System.Threading
volatile WaitHandle m_notifyWhenNoCallbacksRunning;
- internal TimerQueueTimer(TimerCallback timerCallback, object state, uint dueTime, uint period, ref StackCrawlMark stackMark)
+ internal TimerQueueTimer(TimerCallback timerCallback, object state, uint dueTime, uint period)
{
m_timerCallback = timerCallback;
m_state = state;
m_dueTime = Timeout.UnsignedInfinite;
m_period = Timeout.UnsignedInfinite;
-
- if (!ExecutionContext.IsFlowSuppressed())
- {
- m_executionContext = ExecutionContext.Capture(
- ref stackMark,
- ExecutionContext.CaptureOptions.IgnoreSyncCtx | ExecutionContext.CaptureOptions.OptimizeDefaultCase);
- }
+ m_executionContext = ExecutionContext.Capture();
//
// After the following statement, the timer may fire. No more manipulation of timer state outside of
@@ -678,29 +591,15 @@ namespace System.Threading
}
else
{
- using (ExecutionContext executionContext =
- m_executionContext.IsPreAllocatedDefault ? m_executionContext : m_executionContext.CreateCopy())
- {
- ContextCallback callback = s_callCallbackInContext;
- if (callback == null)
- s_callCallbackInContext = callback = new ContextCallback(CallCallbackInContext);
-
- ExecutionContext.Run(
- executionContext,
- callback,
- this, // state
- true); // ignoreSyncCtx
- }
+ ExecutionContext.Run(m_executionContext, s_callCallbackInContext, this);
}
}
- private static ContextCallback s_callCallbackInContext;
-
- private static void CallCallbackInContext(object state)
+ private static readonly ContextCallback s_callCallbackInContext = state =>
{
TimerQueueTimer t = (TimerQueueTimer)state;
t.m_timerCallback(t.m_state);
- }
+ };
}
//
@@ -756,14 +655,12 @@ namespace System.Threading
}
- [System.Runtime.InteropServices.ComVisible(true)]
public sealed class Timer : MarshalByRefObject, IDisposable
{
private const UInt32 MAX_SUPPORTED_TIMEOUT = (uint)0xfffffffe;
private TimerHolder m_timer;
- [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
public Timer(TimerCallback callback,
Object state,
int dueTime,
@@ -774,12 +671,10 @@ namespace System.Threading
if (period < -1 )
throw new ArgumentOutOfRangeException(nameof(period), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegOrNegative1"));
Contract.EndContractBlock();
- StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
- TimerSetup(callback,state,(UInt32)dueTime,(UInt32)period,ref stackMark);
+ TimerSetup(callback,state,(UInt32)dueTime,(UInt32)period);
}
- [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
public Timer(TimerCallback callback,
Object state,
TimeSpan dueTime,
@@ -797,22 +692,18 @@ namespace System.Threading
if (periodTm > MAX_SUPPORTED_TIMEOUT)
throw new ArgumentOutOfRangeException(nameof(periodTm),Environment.GetResourceString("ArgumentOutOfRange_PeriodTooLarge"));
- StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
- TimerSetup(callback,state,(UInt32)dueTm,(UInt32)periodTm,ref stackMark);
+ TimerSetup(callback,state,(UInt32)dueTm,(UInt32)periodTm);
}
[CLSCompliant(false)]
- [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
public Timer(TimerCallback callback,
Object state,
UInt32 dueTime,
UInt32 period)
{
- StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
- TimerSetup(callback,state,dueTime,period,ref stackMark);
+ TimerSetup(callback,state,dueTime,period);
}
- [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
public Timer(TimerCallback callback,
Object state,
long dueTime,
@@ -827,11 +718,9 @@ namespace System.Threading
if (period > MAX_SUPPORTED_TIMEOUT)
throw new ArgumentOutOfRangeException(nameof(period),Environment.GetResourceString("ArgumentOutOfRange_PeriodTooLarge"));
Contract.EndContractBlock();
- StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
- TimerSetup(callback,state,(UInt32) dueTime, (UInt32) period,ref stackMark);
+ TimerSetup(callback,state,(UInt32) dueTime, (UInt32) period);
}
- [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
public Timer(TimerCallback callback)
{
int dueTime = -1; // we want timer to be registered, but not activated. Requires caller to call
@@ -839,31 +728,19 @@ namespace System.Threading
// for a timer to be fired before the returned value is assigned to the variable,
// potentially causing the callback to reference a bogus value (if passing the timer to the callback).
- StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
- TimerSetup(callback, this, (UInt32)dueTime, (UInt32)period, ref stackMark);
+ TimerSetup(callback, this, (UInt32)dueTime, (UInt32)period);
}
private void TimerSetup(TimerCallback callback,
Object state,
UInt32 dueTime,
- UInt32 period,
- ref StackCrawlMark stackMark)
+ UInt32 period)
{
if (callback == null)
throw new ArgumentNullException(nameof(TimerCallback));
Contract.EndContractBlock();
- m_timer = new TimerHolder(new TimerQueueTimer(callback, state, dueTime, period, ref stackMark));
- }
-
- internal static void Pause()
- {
- TimerQueue.Instance.Pause();
- }
-
- internal static void Resume()
- {
- TimerQueue.Instance.Resume();
+ m_timer = new TimerHolder(new TimerQueueTimer(callback, state, dueTime, period));
}
public bool Change(int dueTime, int period)