Skip to content

Commit

Permalink
Save allocations of closures and allow delegate caching in Windows co…
Browse files Browse the repository at this point in the history
…de that schedules async work. (#664)

* Save allocations of closures and allow delegate caching in Windows code that schedules async work.

* Cleanup some unused references.
  • Loading branch information
danielcweber authored Jun 27, 2018
1 parent 80a85ef commit d981f09
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public IDisposable StartTimer(Action<object> action, object state, TimeSpan dueT
Normalize(dueTime)
);

return Disposable.Create(res.Cancel);
return res.AsDisposable();
}

public IDisposable StartPeriodicTimer(Action action, TimeSpan period)
Expand All @@ -42,7 +42,7 @@ public IDisposable StartPeriodicTimer(Action action, TimeSpan period)
period
);

return Disposable.Create(res.Cancel);
return res.AsDisposable();
}

public IDisposable QueueUserWorkItem(Action<object> action, object state)
Expand All @@ -52,7 +52,7 @@ public IDisposable QueueUserWorkItem(Action<object> action, object state)
action(state);
});

return Disposable.Create(res.Cancel);
return res.AsDisposable();
}

public void Sleep(TimeSpan timeout)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// See the LICENSE file in the project root for more information.

#if WINDOWS
using System.Reactive.Disposables;
using Windows.System.Threading;

namespace System.Reactive.Concurrency
Expand Down Expand Up @@ -75,12 +74,12 @@ public override IDisposable Schedule<TState>(TState state, Func<IScheduler, TSta

var userWorkItem = new UserWorkItem<TState>(this, state, action);

var res = global::Windows.System.Threading.ThreadPool.RunAsync(
var res = ThreadPool.RunAsync(
iaa => userWorkItem.Run(),
Priority,
Options);

userWorkItem.CancelQueueDisposable = Disposable.Create(res.Cancel);
userWorkItem.CancelQueueDisposable = res.AsDisposable();

return userWorkItem;
}
Expand Down Expand Up @@ -113,11 +112,11 @@ private IDisposable ScheduleSlow<TState>(TState state, TimeSpan dueTime, Func<IS
{
var userWorkItem = new UserWorkItem<TState>(this, state, action);

var res = global::Windows.System.Threading.ThreadPoolTimer.CreateTimer(
var res = ThreadPoolTimer.CreateTimer(
tpt => userWorkItem.Run(),
dueTime);

userWorkItem.CancelQueueDisposable = Disposable.Create(res.Cancel);
userWorkItem.CancelQueueDisposable = res.AsDisposable();

return userWorkItem;
}
Expand Down Expand Up @@ -161,7 +160,7 @@ public PeriodicallyScheduledWorkItem(TState state, TimeSpan period, Func<TState,
_state = state;
_action = action;

_timer = global::Windows.System.Threading.ThreadPoolTimer.CreatePeriodicTimer(
_timer = ThreadPoolTimer.CreatePeriodicTimer(
Tick,
period);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public override IDisposable Schedule<TState>(TState state, Func<IScheduler, TSta

return new CompositeDisposable(
d,
Disposable.Create(res.Cancel)
res.AsDisposable()
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.

#if WINDOWS
using System.Reactive.Disposables;
using Windows.System.Threading;
using Windows.Foundation;

namespace System.Reactive.Concurrency
{
internal static class ThreadPoolTimerExtensions
{
public static IDisposable AsDisposable(this ThreadPoolTimer threadPoolTimer)
{
return Disposable.Create(threadPoolTimer, _ => _.Cancel());
}

public static IDisposable AsDisposable(this IAsyncInfo asyncInfo)
{
return Disposable.Create(asyncInfo, _ => _.Cancel());
}
}
}
#endif

0 comments on commit d981f09

Please sign in to comment.