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

Removing checks for WeakReference<T> #362

Merged
merged 1 commit into from
Apr 13, 2017
Merged
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
46 changes: 0 additions & 46 deletions Rx.NET/Source/src/System.Reactive/Internal/SystemClock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// 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.

using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reactive.Concurrency;
Expand All @@ -22,11 +21,7 @@ public static class SystemClock
{
private static Lazy<ISystemClock> s_serviceSystemClock = new Lazy<ISystemClock>(InitializeSystemClock);
private static Lazy<INotifySystemClockChanged> s_serviceSystemClockChanged = new Lazy<INotifySystemClockChanged>(InitializeSystemClockChanged);
#if NO_WEAKREFOFT
private static readonly HashSet<WeakReference> s_systemClockChanged = new HashSet<WeakReference>();
#else
private static readonly HashSet<WeakReference<LocalScheduler>> s_systemClockChanged = new HashSet<WeakReference<LocalScheduler>>();
#endif
private static IDisposable s_systemClockChangedHandlerCollector;

private static int _refCount;
Expand Down Expand Up @@ -69,13 +64,8 @@ private static void OnSystemClockChanged(object sender, SystemClockChangedEventA
{
foreach (var entry in s_systemClockChanged)
{
#if NO_WEAKREFOFT
var scheduler = entry.Target as LocalScheduler;
if (scheduler != null)
#else
var scheduler = default(LocalScheduler);
if (entry.TryGetTarget(out scheduler))
#endif
{
scheduler.SystemClockChanged(sender, e);
}
Expand Down Expand Up @@ -109,11 +99,7 @@ internal static void Register(LocalScheduler scheduler)
//
lock (s_systemClockChanged)
{
#if NO_WEAKREFOFT
s_systemClockChanged.Add(new WeakReference(scheduler, false));
#else
s_systemClockChanged.Add(new WeakReference<LocalScheduler>(scheduler));
#endif

if (s_systemClockChanged.Count == 1)
{
Expand All @@ -137,29 +123,16 @@ private static void CollectHandlers()
//
lock (s_systemClockChanged)
{
#if NO_WEAKREFOFT
var remove = default(HashSet<WeakReference>);
#else
var remove = default(HashSet<WeakReference<LocalScheduler>>);
#endif

foreach (var handler in s_systemClockChanged)
{
#if NO_WEAKREFOFT
var scheduler = handler.Target as LocalScheduler;
if (scheduler == null)
#else
var scheduler = default(LocalScheduler);
if (!handler.TryGetTarget(out scheduler))
#endif
{
if (remove == null)
{
#if NO_WEAKREFOFT
remove = new HashSet<WeakReference>();
#else
remove = new HashSet<WeakReference<LocalScheduler>>();
#endif
}

remove.Add(handler);
Expand Down Expand Up @@ -254,23 +227,4 @@ public SystemClockChangedEventArgs(DateTimeOffset oldTime, DateTimeOffset newTim
/// </summary>
public DateTimeOffset NewTime { get; private set; }
}

#if NO_WEAKREFOFT
class WeakReference<T>
where T : class
{
private readonly WeakReference _weakReference;

public WeakReference(T value)
{
_weakReference = new WeakReference(value);
}

public bool TryGetTarget(out T value)
{
value = (T)_weakReference.Target;
return value != null;
}
}
#endif
}