Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Monotonic clock for #846 #876

Merged
merged 2 commits into from
Apr 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -249,6 +249,7 @@
<Compile Include="Util\Internal\Collections\ImmutableTreeSet.cs" />
<Compile Include="Util\Internal\StringBuilderExtensions.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; }
}
}
}