Skip to content

Commit

Permalink
Merge pull request #876 from kekekeks/monotonic-clock
Browse files Browse the repository at this point in the history
Monotonic clock for #846
  • Loading branch information
Aaronontheweb committed Apr 27, 2015
2 parents 9210ebb + ab0c91f commit b505140
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/core/Akka/Actor/Scheduler/DateTimeNowTimeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public class DateTimeOffsetNowTimeProvider : ITimeProvider, IDateTimeOffsetNowTi
private DateTimeOffsetNowTimeProvider() { }
public DateTimeOffset Now { get { return DateTimeOffset.UtcNow; } }

public TimeSpan MonotonicClock {get { return Util.MonotonicClock.Elapsed; }}

public TimeSpan HighResMonotonicClock{get { return Util.MonotonicClock.ElapsedHighRes; }}

public static DateTimeOffsetNowTimeProvider Instance { get { return _instance; } }
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/core/Akka/Actor/Scheduler/ITimeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public interface ITimeProvider
/// Gets the scheduler's notion of current time.
/// </summary>
DateTimeOffset Now { get; }
TimeSpan MonotonicClock { get; }
TimeSpan HighResMonotonicClock { get; }
}
}

3 changes: 3 additions & 0 deletions src/core/Akka/Actor/Scheduler/SchedulerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ void IActionScheduler.ScheduleRepeatedly(TimeSpan initialDelay, TimeSpan interva

DateTimeOffset ITimeProvider.Now { get { return TimeNow; } }


protected abstract DateTimeOffset TimeNow { get; }
public abstract TimeSpan MonotonicClock { get; }
public abstract TimeSpan HighResMonotonicClock { get; }

protected abstract void InternalScheduleTellOnce(TimeSpan delay, ICanTell receiver, object message, IActorRef sender, ICancelable cancelable);

Expand Down
2 changes: 2 additions & 0 deletions src/core/Akka/Actor/Scheduler/TaskBasedScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class TaskBasedScheduler : SchedulerBase, IDateTimeOffsetNowTimeProvider
{

protected override DateTimeOffset TimeNow { get { return DateTimeOffset.Now; } }
public override TimeSpan MonotonicClock { get { return Util.MonotonicClock.Elapsed; } }
public override TimeSpan HighResMonotonicClock { get { return Util.MonotonicClock.ElapsedHighRes; } }

protected override void InternalScheduleTellOnce(TimeSpan delay, ICanTell receiver, object message, IActorRef sender, ICancelable cancelable)
{
Expand Down
1 change: 1 addition & 0 deletions src/core/Akka/Akka.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@
<Compile Include="Util\Internal\StringBuilderExtensions.cs" />
<Compile Include="Util\Internal\TaskExtensions.cs" />
<Compile Include="Util\ISurrogate.cs" />
<Compile Include="Util\MonotonicClock.cs" />
<Compile Include="Util\StandardOutWriter.cs" />
<Compile Include="Event\Subscription.cs" />
<Compile Include="Event\TraceLogger.cs" />
Expand Down
31 changes: 31 additions & 0 deletions src/core/Akka/Util/MonotonicClock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace Akka.Util
{
internal static class MonotonicClock
{
private static readonly Stopwatch Stopwatch = Stopwatch.StartNew();
private static readonly bool IsMono = Type.GetType("Mono.Runtime") != null;
[DllImport("kernel32")]
private static extern ulong GetTickCount64();

private const int TicksInMillisecond = 10000;

public static TimeSpan Elapsed
{
get
{
return IsMono
? Stopwatch.Elapsed
: new TimeSpan((long) GetTickCount64()*TicksInMillisecond);
}
}

public static TimeSpan ElapsedHighRes
{
get { return Stopwatch.Elapsed; }
}
}
}

0 comments on commit b505140

Please sign in to comment.