Skip to content
This repository has been archived by the owner on Apr 30, 2021. It is now read-only.

Commit

Permalink
Code changes from code review.
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorg committed Jul 30, 2015
1 parent 0b5013e commit 9f805f4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/mindtouch.dream/Tasking/TaskTimerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,12 @@ public class TaskTimerStatistics {
}
}

// BUGBUGBUG (arnec): we don't actually do anything with timeout, but let every timer take
// an indefinite time.
// check if any timers were gathered for immediate execution
if(timers != null) {
foreach(var entry in timers) {
entry.Key.ExecuteNow(entry.Value);
entry.Key.Execute(entry.Value);
}
}
_running = false;
Expand Down
28 changes: 17 additions & 11 deletions src/mindtouch.dream/system/GlobalClock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static class GlobalClock {
}

//--- Class Properties ---
public static DateTime UtcNow { get { return (((DateTime?)_suspendedTime) ?? DateTime.UtcNow) + TimeSpan.FromMilliseconds(_timeOffset); } }
public static DateTime UtcNow { get { return ((DateTime?)_suspendedTime) ?? (DateTime.UtcNow + TimeSpan.FromMilliseconds(_timeOffset)); } }

//--- Class Methods ---

Expand Down Expand Up @@ -127,22 +127,25 @@ public static class GlobalClock {
/// <param name="cancelFastForward">Optional callback to prematurely cancel the fast-fwoard operation.</param>
/// <remarks>DO NOT USE FOR PRODUCTION CODE!!!</remarks>
public static DateTime FastForward(TimeSpan time, Func<bool> cancelFastForward = null) {
if(time < TimeSpan.Zero) {
throw new ArgumentException("time cannot be negative");

// TODO (2015-07-30, steveb): add flag to detect if this code is running in production and throw an exception if it is!

if(time <= TimeSpan.Zero) {
throw new ArgumentException("time must be positive");
}
lock(_syncRoot) {
var timeMilliseconds = (int)time.TotalMilliseconds;
var intervalMilliseconds = _intervalMilliseconds / 2;
while(timeMilliseconds >= intervalMilliseconds) {
Interlocked.Add(ref _timeOffset, intervalMilliseconds);
var intervalMilliseconds = _intervalMilliseconds;
do {
var elapsed = Math.Min(timeMilliseconds, intervalMilliseconds);
Interlocked.Add(ref _timeOffset, elapsed);
var now = UtcNow;
MasterTick(now, TimeSpan.FromMilliseconds(intervalMilliseconds), true);
MasterTick(now, TimeSpan.FromMilliseconds(elapsed), true);
if((cancelFastForward != null) && cancelFastForward()) {
return now;
}
timeMilliseconds -= intervalMilliseconds;
}
Interlocked.Add(ref _timeOffset, timeMilliseconds);
timeMilliseconds -= elapsed;
} while(timeMilliseconds > 0);
return UtcNow;
}
}
Expand All @@ -153,8 +156,11 @@ public static class GlobalClock {
/// <returns>Object that when disposed resumes the global clock.</returns>
/// <remarks>DO NOT USE FOR PRODUCTION CODE!!!</remarks>
public static IDisposable Suspend() {

// TODO (2015-07-30, steveb): add flag to detect if this code is running in production and throw an exception if it is!

Monitor.Enter(_syncRoot);
var suspendedTime = Interlocked.Exchange(ref _suspendedTime, UtcNow);
var suspendedTime = Interlocked.Exchange(ref _suspendedTime, (DateTime?)UtcNow);
return new DisposeCallback(() => {
Interlocked.Exchange(ref _suspendedTime, suspendedTime);
Monitor.Exit(_syncRoot);
Expand Down

0 comments on commit 9f805f4

Please sign in to comment.